Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unfortunate crossover of HTTP_HOST and SERVER_PORT variables #223

Closed
zeleznypa opened this issue Mar 20, 2023 · 3 comments
Closed

Unfortunate crossover of HTTP_HOST and SERVER_PORT variables #223

zeleznypa opened this issue Mar 20, 2023 · 3 comments

Comments

@zeleznypa
Copy link

zeleznypa commented Mar 20, 2023

An unfortunate combination of parameters leads to an unexpected result when generating a URL.

if (
(isset($_SERVER[$tmp = 'HTTP_HOST']) || isset($_SERVER[$tmp = 'SERVER_NAME']))
&& preg_match('#^([a-z0-9_.-]+|\[[a-f0-9:]+\])(:\d+)?$#Di', $_SERVER[$tmp], $pair)
) {
$url->setHost(rtrim(strtolower($pair[1]), '.'));
if (isset($pair[2])) {
$url->setPort((int) substr($pair[2], 1));
} elseif (isset($_SERVER['SERVER_PORT'])) {
$url->setPort((int) $_SERVER['SERVER_PORT']);
}
}
}

When a user accesses the address http://example.local, NGINX passes the value example.local to the variable $_SERVER['HTTP_HOST']. Nette will use this as the basis for future URL generation. However, it does not find a value for the port in the content and so reaches for the $_SERVER['SERVER_PORT'] variable, which may lead to an undesirable result.

If there is an extra nginx proxy in front of the application that listens for example on port 80 and then redirects the communication to the application that listens by default on port 8080, the value of this variable will be just 8080. But the user has entered and is expecting port 80.

If the variable $_SERVER['HTTP_HOST'] is populated but does not contain the port part, it means that the default is used, i.e. 80 for HTTP and 443 for HTTPS.

Using $_SERVER['SERVER_PORT'] only makes sense in conjunction with $_SERVER['SERVER_NAME'].

@dg
Copy link
Member

dg commented Mar 20, 2023

Can $_SERVER['SERVER_NAME'] even contain a port, e.g. foo.cz:8080?

@zeleznypa
Copy link
Author

@dg Default configuration of NGINX have just foo.cz in the $_SERVER['SERVER_NAME'].
But in theory you can setup anything into this variable :)

@dg dg closed this as completed in 75c6bb9 Mar 20, 2023
dg added a commit that referenced this issue Mar 20, 2023
@zeleznypa
Copy link
Author

❤️🙇‍♂️👍

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

No branches or pull requests

2 participants