Potential fix for code scanning alert no. 13: Insecure configuration of Helmet security middleware#206
Potential fix for code scanning alert no. 13: Insecure configuration of Helmet security middleware#206perinst wants to merge 1 commit intofeature/mergefrom
Conversation
…of Helmet security middleware Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
WalkthroughUpdated Helmet middleware configuration: in non-production, contentSecurityPolicy is now an explicit directive object instead of disabled; production behavior remains unchanged. Other Helmet settings are unchanged. No exported API signatures changed. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant App as Application
participant HC as helmetConfig()
participant HM as Helmet Middleware
App->>HC: Build Helmet options
alt Production
HC-->>App: options { contentSecurityPolicy: undefined, ... }
else Non-Production
Note over HC: Changed: explicit CSP object
HC-->>App: options { contentSecurityPolicy: { directives... }, ... }
end
App->>HM: Initialize middleware with options
App->>HM: Handle request
HM-->>App: Set security headers (CSP varies by env)
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
✨ Finishing Touches
🧪 Generate unit tests
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 2
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (1)
src/config/middlewares/helmet.config.ts(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src/config/middlewares/helmet.config.ts (1)
src/config/env.config.ts (1)
config(54-74)
| contentSecurityPolicy: config.isProduction | ||
| ? undefined | ||
| : { | ||
| directives: { | ||
| "default-src": ["'self'"], | ||
| "script-src": ["'self'", "'unsafe-inline'"], | ||
| "style-src": ["'self'", "'unsafe-inline'"], | ||
| "img-src": ["'self'", "data:"], | ||
| }, | ||
| }, |
There was a problem hiding this comment.
Production CSP is unintentionally disabled (contentSecurityPolicy: undefined).
This sets CSP to undefined in production, which disables the CSP header. That contradicts the PR objective and keeps the app vulnerable in prod.
Apply this diff to enforce a stricter CSP in production and a relaxed CSP in non-production:
- contentSecurityPolicy: config.isProduction
- ? undefined
- : {
- directives: {
- "default-src": ["'self'"],
- "script-src": ["'self'", "'unsafe-inline'"],
- "style-src": ["'self'", "'unsafe-inline'"],
- "img-src": ["'self'", "data:"],
- },
- },
+ contentSecurityPolicy: config.isProduction
+ ? {
+ useDefaults: true,
+ directives: {
+ "default-src": ["'self'"],
+ "base-uri": ["'self'"],
+ "frame-ancestors": ["'none'"],
+ "object-src": ["'none'"],
+ "script-src": ["'self'"],
+ "style-src": ["'self'"],
+ "img-src": ["'self'", "data:"],
+ },
+ }
+ : {
+ useDefaults: true,
+ directives: {
+ "default-src": ["'self'"],
+ "script-src": ["'self'", "'unsafe-inline'"],
+ "style-src": ["'self'", "'unsafe-inline'"],
+ "img-src": ["'self'", "data:"],
+ },
+ // Optionally enable in dev to observe violations without blocking:
+ // reportOnly: true,
+ },📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| contentSecurityPolicy: config.isProduction | |
| ? undefined | |
| : { | |
| directives: { | |
| "default-src": ["'self'"], | |
| "script-src": ["'self'", "'unsafe-inline'"], | |
| "style-src": ["'self'", "'unsafe-inline'"], | |
| "img-src": ["'self'", "data:"], | |
| }, | |
| }, | |
| contentSecurityPolicy: config.isProduction | |
| ? { | |
| useDefaults: true, | |
| directives: { | |
| "default-src": ["'self'"], | |
| "base-uri": ["'self'"], | |
| "frame-ancestors": ["'none'"], | |
| "object-src": ["'none'"], | |
| "script-src": ["'self'"], | |
| "style-src": ["'self'"], | |
| "img-src": ["'self'", "data:"], | |
| }, | |
| } | |
| : { | |
| useDefaults: true, | |
| directives: { | |
| "default-src": ["'self'"], | |
| "script-src": ["'self'", "'unsafe-inline'"], | |
| "style-src": ["'self'", "'unsafe-inline'"], | |
| "img-src": ["'self'", "data:"], | |
| }, | |
| // Optionally enable in dev to observe violations without blocking: | |
| // reportOnly: true, | |
| }, |
🤖 Prompt for AI Agents
In src/config/middlewares/helmet.config.ts around lines 10 to 19, the current
code sets contentSecurityPolicy to undefined in production which disables the
CSP header; instead, configure a strict CSP when config.isProduction is true and
a relaxed CSP when false: change the ternary so that when production you provide
a restrictive directives object (e.g., default-src 'self'; script-src 'self'
with no 'unsafe-inline'; style-src 'self' possibly with nonce or removing
'unsafe-inline'; img-src 'self' data:; connect-src as needed), and when not
production keep the current relaxed directives (allowing 'unsafe-inline' for
scripts/styles). Ensure you return a valid directives object in both branches so
Helmet emits the header in production.
💡 Verification agent
🧩 Analysis chain
Quick sanity check: ensure CSP isn’t disabled elsewhere and header appears in both envs.
Run these read-only checks.
🏁 Script executed:
#!/bin/bash
set -euo pipefail
echo "Search for any CSP disabled configurations:"
rg -nP --glob '!**/node_modules/**' '\bcontentSecurityPolicy\s*:\s*(false|undefined)\b' || true
echo
echo "Verify helmet middleware is applied:"
rg -nP -C3 '\bhelmetConfig\s*\(' src || true
echo
echo "Manual check (after starting the app):"
echo "DEV: curl -sSI http://localhost:3333 | rg -i '^content-security-policy'"
echo "PROD: NODE_ENV=production curl -sSI http://localhost:3333 | rg -i '^content-security-policy'"Length of output: 807
🏁 Script executed:
#!/bin/bash
set -euo pipefail
echo "Searching for any helmet() calls in src files:"
rg -nP --glob '!**/node_modules/**' '\bhelmet\s*\(' src || true
echo
echo "Searching for @fastify/helmet or fastifyHelmet registrations:"
rg -nP --glob '!**/node_modules/**' '(?:@fastify/helmet|fastifyHelmet)' -n src || trueLength of output: 504
Ensure CSP middleware is properly configured and applied
- In src/index.ts:37 replace
app.use(helmet())withapp.use(helmetConfig())so your custom CSP directives actually take effect. - The ternary in helmet.config.ts (
contentSecurityPolicy: config.isProduction ? undefined : { … }) currently disables CSP in production—invert or adjust this condition if you intend to enable CSP in the production environment.
🤖 Prompt for AI Agents
In src/config/middlewares/helmet.config.ts around lines 10 to 19 and
src/index.ts at line 37, the CSP is currently applied only in non-production
(contentSecurityPolicy uses config.isProduction ? undefined : {...}) and the app
entry uses app.use(helmet()) so your custom helmet config never gets applied;
invert or adjust the ternary to enable the desired CSP in production (e.g., use
config.isProduction ? { directives: ... } : undefined) and update src/index.ts
to call app.use(helmetConfig()) instead of app.use(helmet()) so the custom CSP
directives are actually applied.
|



Potential fix for https://github.com/perinst/dozu-api-service/security/code-scanning/13
To fix the issue, we will replace the
contentSecurityPolicyconfiguration to ensure that CSP is not disabled entirely. Instead of setting it tofalsein non-production environments, we will provide a relaxed CSP configuration for development while keeping the default or a stricter CSP configuration for production. This ensures that CSP is always enabled, reducing the risk of injection attacks.The changes will involve:
contentSecurityPolicyconfiguration to use a relaxed policy in non-production environments.Suggested fixes powered by Copilot Autofix. Review carefully before merging.
Summary by CodeRabbit