Skip to content

Commit 1704366

Browse files
authored
Merge pull request #231 from tina4stack/feature/coop-security-header
feat(security): default Cross-Origin-Opener-Policy header (TINA4_COOP)
2 parents aff5dfb + 35a6fa8 commit 1704366

4 files changed

Lines changed: 35 additions & 2 deletions

File tree

‎example/.env.example‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
# TINA4_CSP=default-src 'self' # Content-Security-Policy header
107107
# TINA4_REFERRER_POLICY=strict-origin-when-cross-origin # Referrer-Policy header
108108
# TINA4_PERMISSIONS_POLICY=camera=(), microphone=(), geolocation=() # Permissions-Policy header
109+
# TINA4_COOP=same-origin-allow-popups # Cross-Origin-Opener-Policy (same-origin for full isolation, unsafe-none to opt out)
109110
# TINA4_CSRF=true # CSRF protection (off only when set to false/0/no)
110111
# TINA4_RATE_LIMIT=100 # Requests allowed per window
111112
# TINA4_RATE_WINDOW=60 # Rate-limit window in seconds

‎tests/test_security_headers_contract.py‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"referrer-policy": "strict-origin-when-cross-origin",
6060
"x-xss-protection": "0",
6161
"permissions-policy": "camera=(), microphone=(), geolocation=()",
62+
"cross-origin-opener-policy": "same-origin-allow-popups",
6263
}
6364
HSTS = "31536000"
6465

@@ -92,7 +93,8 @@ def _app(monkeypatch):
9293
Router.clear()
9394
Middleware.reset()
9495
for name in ("TINA4_HSTS", "TINA4_CSP", "TINA4_FRAME_OPTIONS",
95-
"TINA4_REFERRER_POLICY", "TINA4_PERMISSIONS_POLICY", "TINA4_CSRF"):
96+
"TINA4_REFERRER_POLICY", "TINA4_PERMISSIONS_POLICY", "TINA4_CSRF",
97+
"TINA4_COOP"):
9698
monkeypatch.delenv(name, raising=False)
9799

98100
@route_get("/sec-probe")
@@ -160,6 +162,20 @@ def test_each_response_gets_a_fresh_csp_nonce():
160162
assert n1 != n2, f"nonce was reused across responses: {n1}"
161163

162164

165+
def test_coop_defaults_to_same_origin_allow_popups():
166+
"""COOP defaults to same-origin-allow-popups — it isolates the page from
167+
whoever opened it while leaving popups the page opens (OAuth/payment) able to
168+
post back to their opener. Full isolation (same-origin) is opt-in."""
169+
assert _header(_request(), "cross-origin-opener-policy") == "same-origin-allow-popups"
170+
171+
172+
def test_tina4_coop_overrides_the_cross_origin_opener_policy(monkeypatch):
173+
"""TINA4_COOP replaces the default — same-origin for full isolation, or
174+
unsafe-none to opt out."""
175+
monkeypatch.setenv("TINA4_COOP", "same-origin")
176+
assert _header(_request(), "cross-origin-opener-policy") == "same-origin"
177+
178+
163179
# ------------------------------------------------------------ HSTS HTTPS-guarded
164180

165181
def test_hsts_is_present_on_an_https_request(monkeypatch):

‎tests/test_security_headers_every_response_contract.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
from conftest import boot_child_server
3434

3535
CANONICAL = ("x-frame-options", "x-content-type-options", "content-security-policy",
36-
"referrer-policy", "x-xss-protection", "permissions-policy")
36+
"referrer-policy", "x-xss-protection", "permissions-policy",
37+
"cross-origin-opener-policy")
3738
ROUTE_CSP = "default-src 'none'; img-src 'self'"
3839

3940
ROUTES = textwrap.dedent(f'''

‎tina4_python/core/middleware.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,7 @@ class SecurityHeadersMiddleware:
589589
TINA4_CSP — Content-Security-Policy (default: "default-src 'self'")
590590
TINA4_REFERRER_POLICY — Referrer-Policy (default: strict-origin-when-cross-origin)
591591
TINA4_PERMISSIONS_POLICY — Permissions-Policy (default: camera=(), microphone=(), geolocation=())
592+
TINA4_COOP — Cross-Origin-Opener-Policy (default: same-origin-allow-popups)
592593
"""
593594

594595
@staticmethod
@@ -647,6 +648,20 @@ def before_security(request, response):
647648
"camera=(), microphone=(), geolocation=()",
648649
),
649650
)
651+
# Cross-Origin-Opener-Policy severs the window-reference link to a
652+
# cross-origin page, closing reverse-tab-nabbing and cross-window
653+
# XS-Leaks that X-Frame-Options (framing only) does not. The default is
654+
# same-origin-allow-popups, NOT same-origin: it isolates the page from
655+
# whoever opened it while still letting the page talk to popups IT opens,
656+
# so an OAuth / OIDC / payment popup that posts a token back to its opener
657+
# keeps working out of the box. Set TINA4_COOP=same-origin for full
658+
# isolation (the switch, with COEP, for crossOriginIsolated), or
659+
# unsafe-none to opt out. Honoured only in a secure context — so it
660+
# applies on https and on http://localhost, and is inert elsewhere.
661+
response.header(
662+
"Cross-Origin-Opener-Policy",
663+
os.environ.get("TINA4_COOP", "same-origin-allow-popups"),
664+
)
650665

651666
return request, response
652667

0 commit comments

Comments
 (0)