Skip to content

Commit 345f7af

Browse files
author
Bogdan-Marius-Catanus
committed
fix failing playwright tests
Signed-off-by: Bogdan-Marius-Catanus <bogdan-marius.catanus@ibm.com>
1 parent e895e64 commit 345f7af

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

mcpgateway/middleware/security_headers.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,11 @@ async def dispatch(self, request: Request, call_next) -> Response:
265265
>>> 'Vary' in resp.headers and 'Origin' in resp.headers['Vary']
266266
True
267267
"""
268+
# Generate CSP nonce BEFORE processing request so templates can access it
269+
# This must happen before call_next() so request.state.csp_nonce is available during template rendering
270+
csp_nonce = secrets.token_urlsafe(16)
271+
request.state.csp_nonce = csp_nonce
272+
268273
response = await call_next(request)
269274

270275
# Only apply security headers if enabled
@@ -291,10 +296,7 @@ async def dispatch(self, request: Request, call_next) -> Response:
291296

292297
response.headers["Referrer-Policy"] = "strict-origin-when-cross-origin"
293298

294-
# Content Security Policy with nonce-based approach
295-
# Generate a cryptographically secure nonce for this request
296-
csp_nonce = secrets.token_urlsafe(16)
297-
request.state.csp_nonce = csp_nonce
299+
# Content Security Policy with nonce-based approach (nonce already generated above)
298300

299301
# CSP directives with nonce-based approach for scripts
300302
# Note: style-src uses 'unsafe-inline' without nonce (nonce would disable unsafe-inline)

0 commit comments

Comments
 (0)