Skip to content

Pyjoyment.Plugin.SecurityHeaders: the headers that protect the pages - #636

Merged
dex4er merged 1 commit into
headers-security-shortcutsfrom
security-headers-plugin
Nov 18, 2026
Merged

dex4er merged 1 commit into
headers-security-shortcutsfrom
security-headers-plugin

Conversation

@dex4er

@dex4er dex4er commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator

Part of #634, its second step, stacked on #635.

Changes

  • New plugin Pyjoyment.Plugin.SecurityHeaders, with no counterpart in Mojolicious, which adds the headers in a hook after_dispatch, so every response gets them: the actions, the static files, and the pages of not found and of exceptions.
    • Its options use Python values: hsts={'max_age': ..., 'include_subdomains': ..., 'preload': ...}, csp and csp_report_only as dicts of directives with lists of sources, and permissions as 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, and None or False leaves the header out.
    • Defaults, which restrict nothing that a page usually needs:
      • Strict-Transport-Security: max-age=31536000
      • Content-Security-Policy: base-uri 'self'; frame-ancestors 'self'; object-src 'none'
      • Permissions-Policy: camera=(), geolocation=(), microphone=()
      • X-Content-Type-Options: nosniff
      • Referrer-Policy: strict-origin-when-cross-origin
      • Cross-Origin-Opener-Policy: same-origin
    • embedder_policy, resource_policy and csp_report_only are off by default.
    • csp_report_only is 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-Security is sent only for a secure request, including one that a trusted reverse proxy received over HTTPS, and never for localhost and 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.
    • A header that the action has set is kept. The stash value 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.
  • The plugin is listed in Pyjoyment.Plugins and 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 for localhost, LocalHost:3000 or app.localhost; options that leave headers out; the serialization of Permissions-Policy and of both CSP headers; the options of a route; an unknown option of a response is logged, one of the plugin raises
  • t/docs/Pyjoyment_Plugin_SecurityHeaders.py: the examples of the documentation
  • pyright strict: 0 errors
  • PYTHONDEVMODE=1 python -m Pyjo.Prove -l -j 4 -r t on asyncio and on Pyjo.Reactor.Poll: all tests pass
  • sphinx-build -W passes
  • trunk check: no issues
  • CI

🤖 Generated with Claude Code

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>
@dex4er dex4er added this to the 0.4.1 milestone Nov 18, 2026
@dex4er
dex4er added this pull request to stack #637 November 18, 2026 16:45
@read-the-docs-community

Copy link
Copy Markdown

Documentation build overview

📚 Pyjoyment | 🛠️ Build #35990185 | 📁 Comparing e45ad06 against latest (4deb3f0)

  🔍 Preview build  

6 files changed · + 1 added · ± 5 modified

+ Added

± Modified

@github-actions

Copy link
Copy Markdown
Contributor

Docker Smoke Test

Job Tests Passed ✅ Skipped ⚠️ Failed ❌ Log
smoke (app) 11 11 0 0 ✅ 134965626584
smoke (fastcgi) 5 5 0 0 ✅ 134965627471
smoke (kubernetes) 8 8 0 0 ✅ 134965627450
smoke (lambda) 2 2 0 0 ✅ 134965627150
smoke (lambda-image) 5 5 0 0 ✅ 134965627354
smoke (lambda-stream) 2 2 0 0 ✅ 134965627470
smoke (lambda-zip) 9 9 0 0 ✅ 134965627112
smoke (lite) 4 4 0 0 ✅ 134965626961
smoke (lite-pep723) 4 4 0 0 ✅ 134965627028
All 50 50 0 0 ✅

The run of the workflow

@github-actions

Copy link
Copy Markdown
Contributor

Tests

Job Tests Passed ✅ Skipped ⚠️ Failed ❌ Log
prove (python 3.12, Asyncio) 7492 7463 29 0 ✅ 134965627667
prove (python 3.12, Poll) 7464 7433 31 0 ✅ 134965627305
prove (python 3.13, Asyncio) 7492 7463 29 0 ✅ 134965627641
prove (python 3.13, Poll) 7464 7433 31 0 ✅ 134965627474
prove (python 3.14, Asyncio) 7523 7499 24 0 ✅ 134965628031
prove (python 3.14, Poll) 7495 7469 26 0 ✅ 134965627662
prove (python 3.15, Asyncio) 7523 7499 24 0 ✅ 134965627446
prove (python 3.15, Poll) 7495 7469 26 0 ✅ 134965627567
prove (python 3.15t, Asyncio) 7523 7499 24 0 ✅ 134965627196
prove (python 3.15t, Poll) 7495 7469 26 0 ✅ 134965627699
prove (python mise, Asyncio) 7602 7567 35 0 ✅ 134965627034
prove (python mise, Asyncio, Windows) 7323 7257 66 0 ✅ 134965627674
prove (python mise, Poll) 7574 7537 37 0 ✅ 134965627645
prove (python mise, Poll, Windows) 7295 7227 68 0 ✅ 134965626626
prove (python pypy3.12, Asyncio) 7492 7463 29 0 ✅ 134965627562
prove (python pypy3.12, Poll) 7464 7433 31 0 ✅ 134965627580
pytest (python 3.12, ubuntu-26.04-arm) 444 418 26 0 ✅ 134965627225
pytest (python 3.15, ubuntu-26.04-arm) 444 423 21 0 ✅ 134965627214
pytest (python 3.15, windows-2025) 444 395 49 0 ✅ 134965629080
servers (python 3.12) 54 54 0 0 ✅ 134965627338
servers (python 3.15) 53 53 0 0 ✅ 134965627528
All 121155 120523 632 0 ✅

The run of the workflow

@dex4er dex4er modified the milestones: 0.4.1, 0.5.0 Nov 18, 2026
@dex4er
dex4er merged commit 411cabd into main Nov 18, 2026
36 checks passed
@dex4er
dex4er deleted the security-headers-plugin branch November 18, 2026 21:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant