-
-
Notifications
You must be signed in to change notification settings - Fork 16.5k
docs: add warning and best practices for url_for(..., _external=True)… #5722
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
Conversation
Thanks for working on this! I really appreciate how clearly the risks and recommendations were described. This patch aligns well with the concerns I originally raised in issue #5718, glad to see it resolved! |
--------------------------------------- | ||
|
||
When generating external URLs using :func:`url_for` with the ``_external=True`` argument, | ||
Flask constructs the URL using the requeust's ``Host`` header by default. If your application |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It always uses the Host
header, not only by default. That's why the config needs to be set.
**Best Practices:** | ||
|
||
- Always set :data:`SERVER_NAME` in your configuration for production deployments. | ||
- Consider using :data:`trusted_hosts` to restrict which hosts are accepted. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Request.trusted_hosts
attribute on the individual request is not the correct place to start. Instead, use the global TRUSTED_HOSTS
config. The attribute is used to modify that per-request.
**Best Practices:** | ||
|
||
- Always set :data:`SERVER_NAME` in your configuration for production deployments. | ||
- Consider using :data:`trusted_hosts` to restrict which hosts are accepted. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't "consider" using it, do use it, it's the only thing that actually protects from the issue.
critical when generating links for password resets or other sensitive actions that may be | ||
sent to users. | ||
|
||
.. warning:: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the same statement as the previous paragraph, just restated in a warning block and bold now.
|
||
**Best Practices:** | ||
|
||
- Always set :data:`SERVER_NAME` in your configuration for production deployments. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SERVER_NAME
is only used outside of requests, where the injection isn't relevant because there's not request or Host
header anyway.
|
||
When generating external URLs using :func:`url_for` with the ``_external=True`` argument, | ||
Flask constructs the URL using the requeust's ``Host`` header by default. If your application | ||
does not explicitly set the :data:`SERVER_NAME` configuration or use :data:`trusted_hosts`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both of these are wrong, see further on.
Heads up, all this information is wrong in various ways. Do not use AI, especially in regards to information about security. |
Description of the Change
This pull request adds a warning and best practices to the Web Security documentation regarding the use of url_for(..., _external=True) without setting SERVER_NAME or trusted_hosts. The new section explains the risk of host header injection and provides recommendations for safer configuration. This aims to improve developer awareness and help prevent potential security vulnerabilities, as discussed in #5718.
How it Addresses the Issue
Relevant Issue
fixes #5718