Skip to content

Conversation

@sgerlach
Copy link
Contributor

Code change to resolve #546
Check for localhost set domain to false if so, else use the HOST_NAME variable

@digininja
Copy link
Owner

Looking at the definition for session_set_cookie_params, the default for the domain is null, wouldn't that be better than false for localhost?

session_set_cookie_params(
    int $lifetime_or_options,
    ?string $path = null,
    ?string $domain = null,
    ?bool $secure = null,
    ?bool $httponly = null
): bool

@sgerlach
Copy link
Contributor Author

I'm unsure about the difference between null and false for this particular function. This example is explicit about false https://www.php.net/manual/en/function.setcookie.php#73107

In testing, setting the value to null does not unset the domain for the cookie again resulting in

Cookie “PHPSESSID” has been rejected for invalid domain.

So it appears that it in fact needs to be false to unset the domain name field. (Or make the default false for the definition) but either way still need the logic check somewhere

@sgerlach
Copy link
Contributor Author

Actually, the logic check needs a bit more tweaking looking for a dot in the hostname and then falsing if not contained. 'localhost:8080' does not pass the HTTP_HOST == 'localhost' and the real problem is the lack of a dot in the hostname - Updated code committed

@digininja
Copy link
Owner

How does this work with 127.0.0.1?

@sgerlach
Copy link
Contributor Author

hmm, more digging is neccesary here - might have to do more with a port number in the hostname if not on a standard port

@digininja
Copy link
Owner

parse_url https://www.php.net/manual/en/function.parse-url.php might help. You can use that to pull out just the host part.

@sgerlach
Copy link
Contributor Author

Yes, the port number was the actual problem. Even though you technically should not set localhost as the domain, browsers do accept it. Browsers are not happy about port numbers in the domain name for the cookie and are generally not set for same origin policy. This update pulls the hostname part from the HTTP_HOST variable to set the session cookie. Really only comes into play when using non-standard ports to serve DVWA, like in docker scenarios or other non-privileged high port work. Happy to squash these commits if need be. Thanks for the pointer to parse_url @digininja

@digininja
Copy link
Owner

digininja commented Mar 21, 2023 via email

@digininja digininja merged commit 97c8d23 into digininja:master Mar 22, 2023
@digininja
Copy link
Owner

Seems to work, thanks.

noe-orga-NTT pushed a commit to noe-orga-NTT/DVWA that referenced this pull request May 30, 2025
Checking for localhost in cookie domain setting
noe-orga-NTT pushed a commit to noe-orga-NTT/DVWA that referenced this pull request May 30, 2025
Checking for localhost in cookie domain setting
noe-orga-NTT pushed a commit to noe-orga-NTT/DVWA that referenced this pull request May 30, 2025
Checking for localhost in cookie domain setting
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Browsers rejecting Session Cookie with localhost domain

2 participants