fix(cors): scope CORS to /api only and avoid throwing on disallowed origins; prevents static asset 500s on native
Mirror to GitHub / mirror (push) Successful in 37s
Test and Lint / backend-test (push) Successful in 1m28s
Test and Lint / frontend-test (push) Successful in 2m9s
Version and Release / version-bump (push) Successful in 52s
Version and Release / trigger-drone (push) Successful in 3s

This commit is contained in:
2025-09-09 20:23:25 +02:00
parent 2f1a137342
commit 90bb21e38b
+6 -4
View File
@@ -84,7 +84,7 @@ app.use((req, res, next) => {
next(); next();
}); });
// CORS configuration // CORS configuration (apply only to API routes)
const corsOptions = { const corsOptions = {
origin: function (origin, callback) { origin: function (origin, callback) {
const allowedOrigins = [ const allowedOrigins = [
@@ -102,17 +102,19 @@ const corsOptions = {
); );
} }
// Allow requests with no origin (like mobile apps or curl) // Allow requests with no origin (like curl) and allow-listed origins
if (!origin || allowedOrigins.indexOf(origin) !== -1) { if (!origin || allowedOrigins.indexOf(origin) !== -1) {
callback(null, true); callback(null, true);
} else { } else {
callback(new Error('Not allowed by CORS')); // Do not error globally; just omit CORS headers on disallowed origins
callback(null, false);
} }
}, },
credentials: true credentials: true
}; };
app.use(cors(corsOptions)); // Only attach CORS to API endpoints, not static assets
app.use('/api', cors(corsOptions));
// Initialize rate limiters (they will be created dynamically) // Initialize rate limiters (they will be created dynamically)
let generalRateLimiter; let generalRateLimiter;