Skip to content

Commit 21f42c6

Browse files
committed
🐛 fix(serve): Very permisive helmet config to allow http cross orign request
1 parent 079df4e commit 21f42c6

File tree

1 file changed

+36
-28
lines changed

1 file changed

+36
-28
lines changed

server/src/app.ts

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,44 +18,32 @@ import cors from 'cors';
1818

1919
const app = express();
2020

21-
// Security middleware - Helmet configuration optimized for HTTP-only deployment
21+
// Security middleware - VERY permissive Helmet configuration for HTTP-only deployment
2222
app.use(
2323
helmet({
24-
// Content Security Policy - Permissive for HTTP-only cross-origin requests
25-
contentSecurityPolicy: {
26-
useDefaults: false, // Disable Helmet defaults that include upgrade-insecure-requests
27-
directives: {
28-
defaultSrc: ['\'self\''],
29-
scriptSrc: ['\'self\'', '\'unsafe-inline\'', '\'unsafe-eval\'', 'blob:'], // Add blob: for Swagger UI
30-
styleSrc: ['\'self\'', '\'unsafe-inline\'', 'fonts.googleapis.com'], // Allow Google Fonts
31-
imgSrc: ['\'self\'', 'data:', 'https:', 'http:', 'blob:'], // Allow both HTTP and HTTPS images
32-
connectSrc: ['\'self\'', 'http:', 'https:', 'ws:', 'wss:'], // Allow all HTTP/HTTPS connections for API calls
33-
fontSrc: ['\'self\'', 'fonts.gstatic.com'], // Allow Google Fonts
34-
objectSrc: ['\'none\''],
35-
mediaSrc: ['\'self\''],
36-
frameSrc: ['\'self\''], // Allow frames for Swagger UI
37-
workerSrc: ['\'self\'', 'blob:'], // Add worker-src for Swagger UI
38-
baseUri: ['\'self\''],
39-
formAction: ['\'self\''],
40-
frameAncestors: ['\'self\''],
41-
scriptSrcAttr: ['\'none\''],
42-
// Explicitly DO NOT include upgrade-insecure-requests for HTTP-only serving
43-
},
44-
},
24+
// Completely disable Content Security Policy for HTTP-only cross-origin compatibility
25+
contentSecurityPolicy: false, // Disabled to allow all cross-origin requests
4526
// Cross-Origin Embedder Policy
4627
crossOriginEmbedderPolicy: false, // Disabled for cross-origin compatibility
4728
// Cross-Origin Opener Policy - Allow for cross-origin compatibility
4829
crossOriginOpenerPolicy: false,
30+
// Cross-Origin Resource Policy - CRITICAL: Allow cross-origin requests
31+
crossOriginResourcePolicy: false, // Disabled to allow cross-origin API calls
4932
// HTTP Strict Transport Security - Disabled for HTTP-only serving
5033
hsts: false, // Disabled since we're serving over HTTP
5134
// Prevent MIME type sniffing
5235
noSniff: true,
53-
// X-Frame-Options - Allow cross-origin frames
54-
frameguard: false, // Disabled to allow cross-origin iframe embedding if needed
36+
// X-Frame-Options - Completely disabled for cross-origin frames
37+
frameguard: false, // Disabled to allow cross-origin iframe embedding
5538
// Hide X-Powered-By header
5639
hidePoweredBy: true,
57-
// Referrer Policy - More permissive for HTTP-only
58-
referrerPolicy: { policy: 'no-referrer-when-downgrade' },
40+
// Referrer Policy - Most permissive for HTTP-only cross-origin requests
41+
referrerPolicy: false, // Disabled to allow all referrer information
42+
// Disable all other restrictive policies
43+
permittedCrossDomainPolicies: false,
44+
dnsPrefetchControl: false,
45+
// Origin Agent Cluster - Disable for cross-origin compatibility
46+
originAgentCluster: false,
5947
})
6048
);
6149

@@ -71,13 +59,33 @@ const allowedOrigins =
7159
]
7260
: '*';
7361

62+
// VERY permissive CORS configuration for HTTP-only cross-origin requests
7463
app.use(
7564
cors({
7665
origin: allowedOrigins,
77-
methods: ['GET', 'POST', 'PUT', 'DELETE'],
78-
allowedHeaders: ['Content-Type', 'Authorization'],
66+
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
67+
allowedHeaders: ['Content-Type', 'Authorization', 'Accept', 'Origin', 'X-Requested-With'],
68+
credentials: false, // Disable credentials for HTTP-only cross-origin requests
69+
optionsSuccessStatus: 200, // Support legacy browsers
7970
})
8071
);
72+
73+
// Add explicit headers to override any browser restrictions
74+
app.use((req, res, next) => {
75+
// Allow cross-origin requests explicitly
76+
res.header('Access-Control-Allow-Origin', allowedOrigins === '*' ? '*' : req.headers.origin);
77+
res.header('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE,OPTIONS');
78+
res.header('Access-Control-Allow-Headers', 'Content-Type,Authorization,Accept,Origin,X-Requested-With');
79+
res.header('Access-Control-Max-Age', '86400'); // Cache preflight for 24 hours
80+
81+
// Remove any restrictive headers that might block cross-origin requests
82+
res.removeHeader('Cross-Origin-Resource-Policy');
83+
res.removeHeader('Cross-Origin-Embedder-Policy');
84+
res.removeHeader('Cross-Origin-Opener-Policy');
85+
86+
next();
87+
});
88+
8189
app.use(json());
8290
app.use(loggerMiddleware);
8391

0 commit comments

Comments
 (0)