-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Python: Model extra_response_headers
#22734
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
Open
tausbn
wants to merge
1
commit into
main
Choose a base branch
from
tausbn/python315-model-extra-response-headers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+152
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
python/ql/lib/change-notes/2026-09-22-python315-http-headers.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| --- | ||
| category: minorAnalysis | ||
| --- | ||
| * Added modeling for the `extra_response_headers` constructor argument and instance attribute of `http.server.SimpleHTTPRequestHandler`, introduced in Python 3.15. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
python/ql/test/library-tests/frameworks/stdlib/test_extra_response_headers.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| from http.server import BaseHTTPRequestHandler, SimpleHTTPRequestHandler | ||
| import http.server | ||
|
|
||
|
|
||
| def constructors(request, address, server, headers): | ||
| SimpleHTTPRequestHandler(request, address, server, extra_response_headers=headers) # $ headerWriteBulk=headers headerWriteBulkUnsanitized=name,value | ||
| http.server.SimpleHTTPRequestHandler(request, address, server, extra_response_headers=headers) # $ headerWriteBulk=headers headerWriteBulkUnsanitized=name,value | ||
| handler = SimpleHTTPRequestHandler | ||
| handler(request, address, server, extra_response_headers=headers) # $ headerWriteBulk=headers headerWriteBulkUnsanitized=name,value | ||
|
|
||
| SimpleHTTPRequestHandler(request, address, server, extra_response_headers=[("X-Example", "value")]) # $ headerWriteBulk=List headerWriteBulkUnsanitized=name,value headerWriteNameUnsanitized="X-Example" headerWriteValueUnsanitized="value" | ||
| SimpleHTTPRequestHandler(request, address, server) | ||
|
|
||
|
|
||
| class CustomHandler(SimpleHTTPRequestHandler): | ||
| def change_headers(self, headers): | ||
| self.extra_response_headers = headers # $ headerWriteBulk=headers headerWriteBulkUnsanitized=name,value | ||
| alias = self | ||
| alias.extra_response_headers = [("X-Alias", "value")] # $ headerWriteBulk=List headerWriteBulkUnsanitized=name,value headerWriteNameUnsanitized="X-Alias" headerWriteValueUnsanitized="value" | ||
| ensure_tainted(alias.headers) # $ tainted | ||
| ensure_not_tainted(alias.extra_response_headers) | ||
|
|
||
| def delegate(self, headers): | ||
| write_simple_headers(self, headers) | ||
|
|
||
|
|
||
| class DerivedHandler(CustomHandler): | ||
| def change_again(self, headers): | ||
| self.extra_response_headers = headers # $ headerWriteBulk=headers headerWriteBulkUnsanitized=name,value | ||
|
|
||
|
|
||
| def constructed_instance(request, address, server, headers): | ||
| handler = SimpleHTTPRequestHandler(request, address, server) | ||
| handler.extra_response_headers = headers # $ headerWriteBulk=headers headerWriteBulkUnsanitized=name,value | ||
| write_simple_headers(handler, headers) | ||
|
|
||
|
|
||
| def write_simple_headers(handler, headers): | ||
| alias = handler | ||
| alias.extra_response_headers = headers # $ headerWriteBulk=headers headerWriteBulkUnsanitized=name,value | ||
|
|
||
|
|
||
| def annotated_instance(handler: SimpleHTTPRequestHandler, headers): | ||
| handler.extra_response_headers = headers # $ headerWriteBulk=headers headerWriteBulkUnsanitized=name,value | ||
|
|
||
|
|
||
| class InheritedInitHandler(SimpleHTTPRequestHandler): | ||
| pass | ||
|
|
||
|
|
||
| def inherited_constructor(request, address, server, headers): | ||
| InheritedInitHandler(request, address, server, extra_response_headers=headers) # $ headerWriteBulk=headers headerWriteBulkUnsanitized=name,value | ||
|
|
||
|
|
||
| class BaseOnlyHandler(BaseHTTPRequestHandler): | ||
| def change_headers(self, headers): | ||
| # BaseHTTPRequestHandler does not consume this attribute. | ||
| self.extra_response_headers = headers | ||
| write_base_headers(self, headers) | ||
|
|
||
|
|
||
| def write_base_headers(handler, headers): | ||
| alias = handler | ||
| alias.extra_response_headers = headers | ||
|
|
||
|
|
||
| class Unrelated: | ||
| def __init__(self, *, extra_response_headers): | ||
| self.extra_response_headers = extra_response_headers | ||
|
|
||
|
|
||
| def unrelated(headers): | ||
| Unrelated(extra_response_headers=headers) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
python/ql/test/query-tests/Security/CWE-113-HeaderInjection/Tests1/extra_response_headers.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| from http.server import BaseHTTPRequestHandler, SimpleHTTPRequestHandler | ||
|
|
||
|
|
||
| class ExtraHeadersHandler(SimpleHTTPRequestHandler): | ||
| def do_GET(self): | ||
| name = self.headers["X-Header-Name"] | ||
| value = self.headers["X-Header-Value"] | ||
|
|
||
| self.extra_response_headers = [("X-Value", value)] # BAD | ||
| alias = self | ||
| alias.extra_response_headers = [(name, "value")] # BAD | ||
|
|
||
| clean_name = name.replace("\r", "").replace("\n", "") | ||
| clean_value = value.replace("\r", "").replace("\n", "") | ||
| self.extra_response_headers = [(clean_name, clean_value)] # GOOD | ||
| self.extra_response_headers = [("X-Static", "value")] # GOOD | ||
| super().do_GET() | ||
|
|
||
|
|
||
| class DispatchHandler(BaseHTTPRequestHandler): | ||
| def do_GET(self): | ||
| name = self.headers["X-Header-Name"] | ||
| value = self.headers["X-Header-Value"] | ||
|
|
||
| SimpleHTTPRequestHandler( | ||
| self.request, self.client_address, self.server, | ||
| extra_response_headers=[("X-Value", value)], # BAD | ||
| ) | ||
| # BaseHTTPRequestHandler does not consume this attribute. | ||
| self.extra_response_headers = [(name, value)] # GOOD |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.