-
Notifications
You must be signed in to change notification settings - Fork 103
feat: Add security headers in response #779
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cfa9617
feat: Add security headers in response
arcoraven bec192e
change order
arcoraven 5d3fc76
update cross-spawn package
arcoraven 88c2284
fix import
arcoraven 1e8710e
wip
arcoraven c52dad9
fix cors, remove basic auth
arcoraven dabd202
use const
arcoraven 8042881
small updates
arcoraven d445c9a
Merge remote-tracking branch 'origin/main' into ph/securityHeaderMidd…
arcoraven File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,95 @@ | ||
| import type { FastifyInstance } from "fastify"; | ||
| import type { ParsedConfig } from "../../schema/config"; | ||
| import { ADMIN_QUEUES_BASEPATH } from "./adminRoutes"; | ||
|
|
||
| const STANDARD_METHODS = "GET,POST,DELETE,PUT,PATCH,HEAD,PUT,PATCH,POST,DELETE"; | ||
| const DEFAULT_ALLOWED_HEADERS = [ | ||
| "Authorization", | ||
| "Content-Type", | ||
| "ngrok-skip-browser-warning", | ||
| ]; | ||
|
|
||
| export function withCors(server: FastifyInstance, config: ParsedConfig) { | ||
| server.addHook("onRequest", async (request, reply) => { | ||
| const origin = request.headers.origin; | ||
|
|
||
| // Allow backend calls (no origin header). | ||
| if (!origin) { | ||
| return; | ||
| } | ||
|
|
||
| // Allow admin routes to be accessed from the same host. | ||
| if (request.url.startsWith(ADMIN_QUEUES_BASEPATH)) { | ||
| const host = request.headers.host; | ||
| const originHost = new URL(origin).host; | ||
| if (originHost !== host) { | ||
| reply.code(403).send({ error: "Invalid origin" }); | ||
| return; | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| const allowedOrigins = config.accessControlAllowOrigin | ||
| .split(",") | ||
| .map(sanitizeOrigin); | ||
|
|
||
| // Always set `Vary: Origin` to prevent caching issues even on invalid origins. | ||
| reply.header("Vary", "Origin"); | ||
|
|
||
| if (isAllowedOrigin(origin, allowedOrigins)) { | ||
| // Set CORS headers if valid origin. | ||
| reply.header("Access-Control-Allow-Origin", origin); | ||
| reply.header("Access-Control-Allow-Methods", STANDARD_METHODS); | ||
|
|
||
| // Handle preflight requests | ||
| if (request.method === "OPTIONS") { | ||
| const requestedHeaders = | ||
| request.headers["access-control-request-headers"]; | ||
| reply.header( | ||
| "Access-Control-Allow-Headers", | ||
| requestedHeaders ?? DEFAULT_ALLOWED_HEADERS.join(","), | ||
| ); | ||
|
|
||
| reply.header("Cache-Control", "public, max-age=3600"); | ||
| reply.header("Access-Control-Max-Age", "3600"); | ||
| reply.code(204).send(); | ||
| return; | ||
| } | ||
| } else { | ||
| reply.code(403).send({ error: "Invalid origin" }); | ||
| return; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| function isAllowedOrigin(origin: string, allowedOrigins: string[]) { | ||
| return ( | ||
| allowedOrigins | ||
| // Check if the origin matches any allowed origins. | ||
| .some((allowed) => { | ||
| if (allowed === "https://thirdweb-preview.com") { | ||
| return /^https?:\/\/.*\.thirdweb-preview\.com$/.test(origin); | ||
| } | ||
| if (allowed === "https://thirdweb-dev.com") { | ||
| return /^https?:\/\/.*\.thirdweb-dev\.com$/.test(origin); | ||
| } | ||
|
|
||
| // Allow wildcards in the origin. For example "foo.example.com" matches "*.example.com" | ||
| if (allowed.includes("*")) { | ||
| const wildcardPattern = allowed.replace(/\*/g, ".*"); | ||
| const regex = new RegExp(`^${wildcardPattern}$`); | ||
| return regex.test(origin); | ||
| } | ||
|
|
||
| // Otherwise check for an exact match. | ||
| return origin === allowed; | ||
| }) | ||
| ); | ||
| } | ||
|
|
||
| function sanitizeOrigin(origin: string) { | ||
| if (origin.endsWith("/")) { | ||
| return origin.slice(0, -1); | ||
| } | ||
| return origin; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updating all fastify "callback style" or "async/await" style. This will be strictly enforced in a future fastify version.