1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-04-24 01:18:01 +02:00

fix: optimize cores headers (#5629)

This commit enhances two aspects of CORS:

- Always support CORS preflight requests.
- Do not add additional secuity headers for prefligh calls.
This commit is contained in:
Ivar Conradi Østhus 2023-12-13 16:12:17 +01:00 committed by GitHub
parent adb9ba5c09
commit 4618a52014
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 10 additions and 10 deletions

View File

@ -102,10 +102,7 @@ export default async function getApp(
// so this must be handled before the API token middleware. // so this must be handled before the API token middleware.
app.options( app.options(
`${baseUriPath}/api/frontend*`, `${baseUriPath}/api/frontend*`,
conditionalMiddleware( corsOriginMiddleware(services, config),
() => config.flagResolver.isEnabled('embedProxy'),
corsOriginMiddleware(services, config),
),
); );
app.use(baseUriPath, patMiddleware(config, services)); app.use(baseUriPath, patMiddleware(config, services));

View File

@ -7,7 +7,6 @@ export const conditionalMiddleware = (
const router = Router(); const router = Router();
router.use((req, res, next) => { router.use((req, res, next) => {
res.setHeader('Vary', 'Origin');
if (condition()) { if (condition()) {
middleware(req, res, next); middleware(req, res, next);
} else { } else {

View File

@ -19,7 +19,7 @@ export const corsOriginMiddleware = (
{ proxyService }: Pick<IUnleashServices, 'proxyService'>, { proxyService }: Pick<IUnleashServices, 'proxyService'>,
config: IUnleashConfig, config: IUnleashConfig,
): RequestHandler => { ): RequestHandler => {
return cors(async (req, callback) => { const corsFunc = cors(async (req, callback) => {
try { try {
const { frontendApiOrigins = [] } = const { frontendApiOrigins = [] } =
await proxyService.getFrontendSettings(); await proxyService.getFrontendSettings();
@ -33,4 +33,8 @@ export const corsOriginMiddleware = (
callback(error); callback(error);
} }
}); });
return (req, res, next) => {
res.setHeader('Vary', 'Origin');
corsFunc(req, res, next);
};
}; };

View File

@ -116,11 +116,11 @@ const secureHeaders: (config: IUnleashConfig) => RequestHandler = (config) => {
}); });
return (req, res, next) => { return (req, res, next) => {
const stripHeadersOnAPI = if (req.method === 'OPTIONS') {
config.flagResolver.isEnabled('stripHeadersOnAPI'); return next();
if ( } else if (
req.path.startsWith(`${config.server.baseUriPath}/api/`) && req.path.startsWith(`${config.server.baseUriPath}/api/`) &&
stripHeadersOnAPI config.flagResolver.isEnabled('stripHeadersOnAPI')
) { ) {
apiHelmet(req, res, next); apiHelmet(req, res, next);
} else { } else {