Repository navigation
Pyjoyment.Plugin.SecurityHeaders: the headers that protect the pages - #636
Merged
Merged
Conversation
Strict-Transport-Security for secure requests, but not for localhost, Content-Security-Policy and its report-only policy, Permissions-Policy, X-Content-Type-Options, Referrer-Policy and the Cross-Origin-*-Policy headers on every response, as Python values or strings. A header that the action sets is kept, and the stash security_headers replaces the options for a response. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
Docker Smoke Test
|
Contributor
Tests
|
7 of 8 tasks
7 tasks done
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Part of #634, its second step, stacked on #635.
Changes
Pyjoyment.Plugin.SecurityHeaders, with no counterpart in Mojolicious, which adds the headers in a hookafter_dispatch, so every response gets them: the actions, the static files, and the pages of not found and of exceptions.hsts={'max_age': ..., 'include_subdomains': ..., 'preload': ...},cspandcsp_report_onlyas dicts of directives with lists of sources, andpermissionsas a dict of features with lists of origins, written as the structured fields of RFC 8941 ((self "https://..."),*). A string is used as the header as it is, andNoneorFalseleaves the header out.Strict-Transport-Security: max-age=31536000Content-Security-Policy: base-uri 'self'; frame-ancestors 'self'; object-src 'none'Permissions-Policy: camera=(), geolocation=(), microphone=()X-Content-Type-Options: nosniffReferrer-Policy: strict-origin-when-cross-originCross-Origin-Opener-Policy: same-originembedder_policy,resource_policyandcsp_report_onlyare off by default.csp_report_onlyis a policy of its own, not a switch, so that a stricter policy is tried out with reports while the current one is enforced.Strict-Transport-Securityis sent only for a secure request, including one that a trusted reverse proxy received over HTTPS, and never forlocalhostand its subdomains: a browser would otherwise treat every server on a developer's machine, on any port, as HTTPS-only. Browsers ignore it for IP addresses anyway.security_headers, from the defaults of a route or set by an action, replaces options for one response. An unknown option is logged as an error, because the response has already been rendered, while an unknown option of the plugin itself raises when the plugin is registered.Pyjoyment.Pluginsand in the guide's index, has an API page, and is on pyright's strict list.The nonce, the route of the reports of CSP and CORS come in the next pull requests of the stack.
Validation
t/Pyjoyment/security_headers_lite_app.py: the headers on the response of an action, a static file, a 404 and a 500; the headers of the action are kept; HSTS only over HTTPS, not forlocalhost,LocalHost:3000orapp.localhost; options that leave headers out; the serialization ofPermissions-Policyand of both CSP headers; the options of a route; an unknown option of a response is logged, one of the plugin raisest/docs/Pyjoyment_Plugin_SecurityHeaders.py: the examples of the documentationPYTHONDEVMODE=1 python -m Pyjo.Prove -l -j 4 -r ton asyncio and onPyjo.Reactor.Poll: all tests passsphinx-build -Wpassestrunk check: no issues🤖 Generated with Claude Code