Skip to content

Security headers: HSTS, CSP with nonce and reports, Permissions-Policy, CORS #634

Description

@dex4er

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.Headers has the shortcuts of Mojo only: content_security_policy, strict_transport_security and access_control_allow_origin. There is none for Permissions-Policy, Content-Security-Policy-Report-Only, Reporting-Endpoints, X-Content-Type-Options, Referrer-Policy, the Cross-Origin-*-Policy headers or the other headers of CORS.
  • No plugin, helper or recipe sets these headers. The skill pyjoyment:api sets Access-Control-Allow-Origin: * in a hook after_dispatch, and the cookbook (JSONP) sets it in an action. Nothing handles a preflight OPTIONS request or adds Vary: Origin.
  • Pyjoyment.Plugin.AllowedHosts already checks Host and Origin, and Pyjo.Transaction takes the scheme of X-Forwarded-Proto from trusted_proxies, which HSTS needs behind a reverse proxy.
  • The bundled templates have inline code: <style> in exception.html.ep and not_found.html.ep, and <script>, <style> and style= in debug.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 in NAMES: 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 CORS access_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-Security only for a request that is secure, also through a trusted proxy; Content-Security-Policy or Content-Security-Policy-Report-Only; Permissions-Policy; X-Content-Type-Options: nosniff, Referrer-Policy and Cross-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-src and style-src of the policy when they are there; the tag helpers javascript and stylesheet with 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) and report-to with Reporting-Endpoints (application/reports+json), both sent, as browsers support one or the other; it answers 204, limits the size of the body, logs the report with app.log and emits it for an application that stores them elsewhere.
  • CORS plugin (separate from the security headers): the allowed origins, by default the ones of AllowedHosts where the application has it; the answer to a preflight OPTIONS request with the allowed methods and headers and Access-Control-Max-Age; Access-Control-Allow-Origin of the origin of the request with Vary: 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:deploy with the minimal setup and the mistakes (HSTS over HTTP, includeSubDomains/preload too early, an enforced CSP without reports, sessions.secure left off behind HTTPS); the nonce in pyjoyment:templates; CORS in pyjoyment:api with the plugin instead of the hook of *.

Not included

  • X-Frame-Options: frame-ancestors of CSP replaces it.
  • X-XSS-Protection, Expect-CT, Feature-Policy and Public-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: private or no-store, or hashes instead.
  • Strict-Transport-Security with a long max-age, includeSubDomains or preload cannot 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

  1. added theissue type on Nov 18, 2026
  2. modified the milestones: , 0.5.0 on Nov 18, 2026
  3. dex4er commented on Nov 18, 2026

    @dex4er
    CollaboratorAuthor

    Done in the stack #635, #636, #638, #639, #640, #641 and #642, released in 0.5.0.

    Deviations from the plan:

    • CORS origins are not taken from AllowedHosts by default: that plugin does not expose its configuration, so the origins are given to CORS explicitly, and the documentation says that they must be among the origins of AllowedHosts too (Pyjoyment.Plugin.CORS: Cross-Origin Resource Sharing #640).
    • CORS per route is paths, path prefixes or regular expressions, not an option of a route.
    • csp_report_only is a policy of its own, not a switch of csp, 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.secure left off behind HTTPS is not among the mistakes of pyjoyment:deploy; it remains for a follow-up of the skill.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions