Skip to content
This repository has been archived by the owner on Aug 5, 2024. It is now read-only.

Commit

Permalink
chore(corsConfig.ts): whitelist values can be string or regex (#2352)
Browse files Browse the repository at this point in the history
* chore(corsConfig.ts): whitelist values can be string or regex

* fix: handle the fact that regex is stored as a string in whitelist array
  • Loading branch information
LucasCharrier authored Jan 22, 2024
1 parent eb4ae23 commit 8a53cef
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/utils/corsConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@ var whitelist = config.CORS_ORIGIN;

const corsOptions = {
origin: function (origin, callback) {
if (
whitelist.indexOf(origin) !== -1 ||
process.env.NODE_ENV === 'test' ||
!origin
) {
const isAllowed = whitelist.some((allowedOrigin) => {
// Check if the whitelist entry is a string representation of a regex
if (allowedOrigin.startsWith('/') && allowedOrigin.endsWith('/')) {
const pattern = allowedOrigin.slice(1, -1); // Remove the slashes
const regex = new RegExp(pattern);
return regex.test(origin);
}
// Handle normal string comparison
return origin === allowedOrigin;
});

if (isAllowed || process.env.NODE_ENV === 'test' || !origin) {
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));
Expand Down

0 comments on commit 8a53cef

Please sign in to comment.