-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Converted static CORS into middleware, and added to all routers (#161)
* Added CORS stuff * Format + Lint
- Loading branch information
1 parent
9f3b44c
commit faa7bf5
Showing
5 changed files
with
27 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import cors, { CorsOptions } from "cors"; | ||
import Config from "../config.js"; | ||
|
||
// Only allow a certain set of regexes to be allowed via CORS | ||
const allowedOrigins: RegExp[] = [new RegExp(Config.CORS.PROD_REGEX), new RegExp(Config.CORS.DEPLOY_REGEX)]; | ||
|
||
function regexPasses(target: string, patterns: RegExp[]): boolean { | ||
return patterns.some((pattern: RegExp) => { | ||
return pattern.test(target); | ||
}); | ||
} | ||
|
||
// CORS options configuration | ||
const corsOptions: CorsOptions = { | ||
origin: (origin: string | undefined, callback: (error: Error | null, allow?: boolean) => void) => { | ||
if (!origin || regexPasses(origin, allowedOrigins)) { | ||
callback(null, true); | ||
} else { | ||
callback(new Error("Not allowed by CORS")); | ||
} | ||
}, | ||
}; | ||
|
||
export default cors(corsOptions); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters