From 63f00e0c8a3041064378934ab1882f41f5f67537 Mon Sep 17 00:00:00 2001 From: Piotr Roszatycki Date: Wed, 18 Nov 2026 17:17:03 +0100 Subject: [PATCH] Pyjo.Headers: shortcuts of the headers of security and of CORS Permissions-Policy, Content-Security-Policy-Report-Only, Reporting-Endpoints, X-Content-Type-Options, Referrer-Policy, the Cross-Origin-*-Policy headers and the headers of CORS other than Access-Control-Allow-Origin, with their names in NAMES, so that they keep their normal case. Co-Authored-By: Claude Opus 5.5 --- Pyjo/Headers.py | 69 ++++++++++++++++++++++++++++++++++++++++++++++- t/Pyjo/Headers.py | 27 +++++++++++++++++++ 2 files changed, 95 insertions(+), 1 deletion(-) diff --git a/Pyjo/Headers.py b/Pyjo/Headers.py index e27c3806..43d5d33e 100644 --- a/Pyjo/Headers.py +++ b/Pyjo/Headers.py @@ -87,7 +87,13 @@ 'If-Modified-Since If-None-Match Last-Modified Link Location Origin Proxy-Authenticate Proxy-Authorization ' 'Range Sec-WebSocket-Accept Sec-WebSocket-Extensions Sec-WebSocket-Key Sec-WebSocket-Protocol ' 'Sec-WebSocket-Version Server Server-Timing Set-Cookie Status Strict-Transport-Security TE Trailer ' - 'Transfer-Encoding Upgrade User-Agent Vary WWW-Authenticate' + 'Transfer-Encoding Upgrade User-Agent Vary WWW-Authenticate ' + # Not in Mojolicious: the headers of security and of 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 Content-Security-Policy-Report-Only Cross-Origin-Embedder-Policy ' + 'Cross-Origin-Opener-Policy Cross-Origin-Resource-Policy Permissions-Policy Referrer-Policy ' + 'Reporting-Endpoints X-Content-Type-Options' ).split() } @@ -107,6 +113,10 @@ ] +# Not in Mojolicious: the specification of the shortcuts of the headers of CORS +_CORS = ' from the `CORS protocol `_ of Fetch' + + class _Shortcut: # A property for a common header, as Mojo makes them with monkey_patch def __init__(self, name: str, example: str, about: str = '') -> None: @@ -288,11 +298,20 @@ def __str__(self) -> str: accept_encoding = _Shortcut('Accept-Encoding', "'gzip'") accept_language = _Shortcut('Accept-Language', "'de, en'") accept_ranges = _Shortcut('Accept-Ranges', "'bytes'") + # Not in Mojolicious: the headers of CORS other than Access-Control-Allow-Origin + access_control_allow_credentials = _Shortcut('Access-Control-Allow-Credentials', "'true'", _CORS) + access_control_allow_headers = _Shortcut('Access-Control-Allow-Headers', "'Content-Type, Authorization'", _CORS) + access_control_allow_methods = _Shortcut('Access-Control-Allow-Methods', "'GET, POST'", _CORS) access_control_allow_origin = _Shortcut( 'Access-Control-Allow-Origin', "'*'", ' from `Cross-Origin Resource Sharing `_', ) + # Not in Mojolicious: the headers of CORS other than Access-Control-Allow-Origin + access_control_expose_headers = _Shortcut('Access-Control-Expose-Headers', "'Server-Timing'", _CORS) + access_control_max_age = _Shortcut('Access-Control-Max-Age', '86400', _CORS) + access_control_request_headers = _Shortcut('Access-Control-Request-Headers', "'content-type'", _CORS) + access_control_request_method = _Shortcut('Access-Control-Request-Method', "'PUT'", _CORS) def add(self, name: str, *values: Anything) -> Self: """:: @@ -369,8 +388,31 @@ def clone(self) -> Self: "'default-src https:'", ' from `Content Security Policy 1.0 `_', ) + # Not in Mojolicious: the policy that the browser reports without enforcing it + content_security_policy_report_only = _Shortcut( + 'Content-Security-Policy-Report-Only', + '"default-src \'self\'; report-to csp"', + ' from `Content Security Policy Level 3 `_', + ) content_type = _Shortcut('Content-Type', "'text/plain'") cookie = _Shortcut('Cookie', "'f=b'", ' from :rfc:`6265`') + # Not in Mojolicious: the policies of the isolation of a document from the ones of other origins + cross_origin_embedder_policy = _Shortcut( + 'Cross-Origin-Embedder-Policy', + "'require-corp'", + ' from the `HTML Standard `_', + ) + cross_origin_opener_policy = _Shortcut( + 'Cross-Origin-Opener-Policy', + "'same-origin'", + ' from the `HTML Standard' + ' `_', + ) + cross_origin_resource_policy = _Shortcut( + 'Cross-Origin-Resource-Policy', + "'same-origin'", + ' from `Fetch `_', + ) date = _Shortcut('Date', "'Sun, 17 Aug 2008 16:27:35 GMT'") def dehop(self) -> Self: @@ -608,6 +650,12 @@ def parse(self, chunk: str | bytes | bytearray) -> Self: return self + # Not in Mojolicious: the features of the browser that a document may use + permissions_policy = _Shortcut( + 'Permissions-Policy', + "'camera=(), geolocation=(), microphone=()'", + ' from `Permissions Policy `_', + ) proxy_authenticate = _Shortcut('Proxy-Authenticate', '\'Basic "realm"\'') proxy_authorization = _Shortcut('Proxy-Authorization', "'Basic Zm9vOmJhcg=='") range = _Shortcut('Range', "'bytes=2-8'") @@ -644,6 +692,13 @@ def referrer(self) -> str | None: def referrer(self, value: Anything) -> None: self.header('Referer', value) + # Not in Mojolicious: the referrer that the browser sends with the requests of a document + referrer_policy = _Shortcut( + 'Referrer-Policy', + "'strict-origin-when-cross-origin'", + ' from `Referrer Policy `_', + ) + def remove(self, name: str) -> Self: """:: @@ -654,6 +709,12 @@ def remove(self, name: str) -> Self: self._headers.pop(name.lower(), None) return self + # Not in Mojolicious: the endpoints of the reports of the browser, as the ones of CSP + reporting_endpoints = _Shortcut( + 'Reporting-Endpoints', + '\'csp="/csp-reports"\'', + ' from `Reporting API `_', + ) sec_websocket_accept = _Shortcut( 'Sec-WebSocket-Accept', "'s3pPLMBiTxaQ9kYGzzhZRbK+xOo='", @@ -753,6 +814,12 @@ def values(self) -> list[str]: vary = _Shortcut('Vary', "'*'") www_authenticate = _Shortcut('WWW-Authenticate', '\'Basic realm="realm"\'') + # Not in Mojolicious: the type of the content that the browser does not sniff + x_content_type_options = _Shortcut( + 'X-Content-Type-Options', + "'nosniff'", + ' from `Fetch `_', + ) new = Pyjo_Headers.new diff --git a/t/Pyjo/Headers.py b/t/Pyjo/Headers.py index 96cf0743..0dedd52b 100644 --- a/t/Pyjo/Headers.py +++ b/t/Pyjo/Headers.py @@ -97,6 +97,33 @@ is_ok(headers.set(vary='foo').vary, 'foo', 'right value') is_ok(headers.set(www_authenticate='foo').www_authenticate, 'foo', 'right value') + # Not in Mojolicious: the headers of security and of CORS + for attr in ( + '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', + 'content_security_policy_report_only', + 'cross_origin_embedder_policy', + 'cross_origin_opener_policy', + 'cross_origin_resource_policy', + 'permissions_policy', + 'referrer_policy', + 'reporting_endpoints', + 'x_content_type_options', + ): + is_ok(getattr(headers.set(**{attr: 'foo'}), attr), 'foo', f'right value of {attr}') + names = Pyjo.Headers.new().add('permissions-policy', 'foo').add('x-content-type-options', 'nosniff').names() + is_deeply_ok(names, ['Permissions-Policy', 'X-Content-Type-Options'], 'normal case') + is_ok( + Pyjo.Headers.new().set(content_security_policy_report_only='foo').to_str(), + 'Content-Security-Policy-Report-Only: foo', + 'right header', + ) + is_ok(headers.set(referrer='foo').referrer, 'foo', 'right value') is_ok(headers.referer, 'foo', 'right value') is_ok(headers.set(referer='bar').referer, 'bar', 'right value')