Repository navigation
Description
What would you like to be added?
Support for the standard security headers of a web application, in the framework and in what teaches it: Strict-Transport-Security, Content-Security-Policy (with its report-only mode and the reports), Permissions-Policy, the small baseline headers that go with them, and CORS beyond Access-Control-Allow-Origin.
What there is now:
Pyjo.Headershas the shortcuts of Mojo only:content_security_policy,strict_transport_securityandaccess_control_allow_origin. There is none forPermissions-Policy,Content-Security-Policy-Report-Only,Reporting-Endpoints,X-Content-Type-Options,Referrer-Policy, theCross-Origin-*-Policyheaders or the other headers of CORS.- No plugin, helper or recipe sets these headers. The skill
pyjoyment:apisetsAccess-Control-Allow-Origin: *in a hookafter_dispatch, and the cookbook (JSONP) sets it in an action. Nothing handles a preflightOPTIONSrequest or addsVary: Origin. Pyjoyment.Plugin.AllowedHostsalready checksHostandOrigin, andPyjo.Transactiontakes the scheme ofX-Forwarded-Protofromtrusted_proxies, which HSTS needs behind a reverse proxy.- The bundled templates have inline code:
<style>inexception.html.epandnot_found.html.ep, and<script>,<style>andstyle=indebug.html.ep, so a policy without'unsafe-inline'breaks them, and a report-only one reports them on every page of an error.
Why is this needed?
These headers are the baseline that a review of an application checks first, and an agent that writes an application with the skills of the plugin pyjoyment should set them without being asked. Setting them by hand is easy to get wrong: HSTS over plain HTTP or with preload too early, a CSP that is enforced before its reports are read, a report endpoint without a size limit, CORS that reflects any Origin with credentials. A nonce per request is what lets a strict CSP coexist with the inline scripts and styles of templates, which pyjoyment.dev needs for its deployments.
Plan
Each step is a pull request of its own, in this order, as the later ones use the earlier ones:
- Header shortcuts in
Pyjo.Headers(# Not in Mojolicious:), with their names inNAMES:permissions_policy,content_security_policy_report_only,reporting_endpoints,x_content_type_options,referrer_policy,cross_origin_opener_policy,cross_origin_resource_policy,cross_origin_embedder_policy, and for CORSaccess_control_allow_credentials,access_control_allow_headers,access_control_allow_methods,access_control_expose_headers,access_control_max_age,access_control_request_headers,access_control_request_method. - Plugin of the security headers (name to be decided, as
SecurityHeaders): the policies as Python values, not strings to concatenate, with defaults that are safe to turn on;Strict-Transport-Securityonly for a request that is secure, also through a trusted proxy;Content-Security-PolicyorContent-Security-Policy-Report-Only;Permissions-Policy;X-Content-Type-Options: nosniff,Referrer-PolicyandCross-Origin-Opener-Policy. A header that the action has set already is kept, and a route can change or turn off a policy for its response. - Nonce per request: a helper that gives the nonce of the response, added to
script-srcandstyle-srcof the policy when they are there; the tag helpersjavascriptandstylesheetwith inline content give their element the nonce; the bundled templates use it, so that a strict policy does not break the pages of errors and of debug. - Reports of CSP: an optional route of the plugin for
report-uri(application/csp-report) andreport-towithReporting-Endpoints(application/reports+json), both sent, as browsers support one or the other; it answers204, limits the size of the body, logs the report withapp.logand emits it for an application that stores them elsewhere. - CORS plugin (separate from the security headers): the allowed origins, by default the ones of
AllowedHostswhere the application has it; the answer to a preflightOPTIONSrequest with the allowed methods and headers andAccess-Control-Max-Age;Access-Control-Allow-Originof the origin of the request withVary: Origin, never*with credentials;Access-Control-Expose-Headers; per route. - Guide: a cookbook recipe Security headers (HSTS rollout, CSP from report-only to enforced, nonce, Permissions-Policy) and the CORS recipe moved to the plugin, with tested examples.
- Skills: a section in
pyjoyment:deploywith the minimal setup and the mistakes (HSTS over HTTP,includeSubDomains/preloadtoo early, an enforced CSP without reports,sessions.secureleft off behind HTTPS); the nonce inpyjoyment:templates; CORS inpyjoyment:apiwith the plugin instead of the hook of*.
Not included
X-Frame-Options:frame-ancestorsof CSP replaces it.X-XSS-Protection,Expect-CT,Feature-PolicyandPublic-Key-Pins: obsolete, and the first one can open holes in old browsers.- Hashes of inline code (
'sha256-...') for the static HTML files, which get no nonce: possible later, if a deployment needs it.
Risks
- A nonce in an HTML page that a shared cache keeps, as CloudFront in front of pyjoyment.dev, is the same for every client until the cache expires. The policy still works, but the nonce is no longer a secret of the response, so such pages need
Cache-Control: privateorno-store, or hashes instead. Strict-Transport-Securitywith a longmax-age,includeSubDomainsorpreloadcannot be withdrawn quickly; the defaults and the docs start short.- A report endpoint is public and unauthenticated: it needs the limit of size, no CSRF, and logs that cannot be flooded.
Validation
- Tests of each shortcut, plugin and helper on both reactors, as usual.
- The bundled pages of errors and of debug render with no violation under a strict policy with the nonce.
- pyjoyment.dev runs with an enforced CSP with a nonce and with the report endpoint, after a release.
Mojolicious counterpart
None. Mojo::Headers has only the shortcuts access_control_allow_origin, content_security_policy and strict_transport_security; the plugins, the nonce and the other shortcuts are not in Mojolicious.
Activity
dex4er commentedon Nov 18, 2026
Done in the stack #635, #636, #638, #639, #640, #641 and #642, released in 0.5.0.
Deviations from the plan:
AllowedHostsby default: that plugin does not expose its configuration, so the origins are given toCORSexplicitly, and the documentation says that they must be among the origins ofAllowedHoststoo (Pyjoyment.Plugin.CORS: Cross-Origin Resource Sharing #640).paths, path prefixes or regular expressions, not an option of a route.csp_report_onlyis a policy of its own, not a switch ofcsp, so a stricter policy is tried out with reports while the current one is enforced (Pyjoyment.Plugin.SecurityHeaders: the headers that protect the pages #636).sessions.secureleft off behind HTTPS is not among the mistakes ofpyjoyment:deploy; it remains for a follow-up of the skill.