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

Commit

Permalink
fix: handle the fact that regex is stored as a string in whitelist array
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasCharrier committed Jan 22, 2024
1 parent 4c10ed1 commit 9998192
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/utils/corsConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ var whitelist = config.CORS_ORIGIN;

const corsOptions = {
origin: function (origin, callback) {
const isAllowed = whitelist.some((allowedOrigin: RegExp | string) => {
if (typeof allowedOrigin === 'string') {
return origin === allowedOrigin;
} else if (allowedOrigin instanceof RegExp) {
return allowedOrigin.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);
}
return false;
// Handle normal string comparison
return origin === allowedOrigin;
});

if (isAllowed || process.env.NODE_ENV === 'test' || !origin) {
Expand Down

0 comments on commit 9998192

Please sign in to comment.