-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update CSP and move CSP & CORS to middleware
- Loading branch information
1 parent
72f687b
commit 96ba580
Showing
2 changed files
with
39 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,46 @@ | ||
import { NextResponse } from "next/server"; | ||
import * as process from "process"; | ||
|
||
export function configureResponse(response: NextResponse) { | ||
response.headers.set( | ||
"Content-Security-Policy", | ||
` | ||
default-src 'self'; | ||
form-action 'self'; | ||
object-src 'none'; | ||
img-src 'self' https: data:${process.env.NODE_ENV === "development" ? " http:" : ""}; | ||
media-src 'self' https: data:${process.env.NODE_ENV === "development" ? " http:" : ""}; | ||
style-src 'self' 'unsafe-inline'; | ||
font-src 'self' https: data:; | ||
script-src 'self' 'unsafe-inline' https:${process.env.NODE_ENV === "development" ? " 'unsafe-eval'" : ""}; | ||
connect-src 'self' https:${process.env.NODE_ENV === "development" ? " http:" : ""}; | ||
frame-ancestors ${process.env.ADMIN_URL ?? "none"}; | ||
upgrade-insecure-requests; | ||
block-all-mixed-content; | ||
frame-src 'self' https://*.youtube.com https://*.youtube-nocookie.com; | ||
` | ||
.replace(/\s{2,}/g, " ") | ||
.trim(), | ||
); | ||
setContentSecurityPolicy(response); | ||
|
||
if (process.env.ADMIN_URL) { | ||
response.headers.set("Access-Control-Allow-Origin", process.env.ADMIN_URL); | ||
} | ||
return response; | ||
} | ||
|
||
function setContentSecurityPolicy(response: NextResponse) { | ||
type ContentSecurityPolicyDirective = { | ||
directive: string; | ||
values: string[]; | ||
}; | ||
|
||
const siteContentSecurityPolicyDirectives: ContentSecurityPolicyDirective[] = [ | ||
{ directive: "default-src", values: ["'none'"] }, // Restrict any content not explicitly allowed | ||
{ directive: "style-src-elem", values: ["'self'", "'unsafe-inline'"] }, | ||
{ directive: "script-src-elem", values: ["'self'", "'unsafe-inline'"] }, | ||
{ directive: "img-src", values: ["'self'", "data:", process.env.API_URL ?? ""] }, | ||
{ directive: "style-src-attr", values: ["'unsafe-inline'"] }, | ||
{ directive: "font-src", values: ["'self'", "data:"] }, | ||
{ directive: "frame-ancestors", values: [process.env.ADMIN_URL ?? "https:"] }, | ||
]; | ||
|
||
const environmentDependentContentSecurityPolicyDirectives: ContentSecurityPolicyDirective[] = | ||
process.env.NODE_ENV === "development" | ||
? [ | ||
{ directive: "script-src", values: ["'unsafe-eval'"] }, // Needed in local development | ||
{ directive: "connect-src", values: ["ws:"] }, // Used for hot reloading in local development | ||
] | ||
: [{ directive: "upgrade-insecure-requests", values: [] }]; // Upgrade all requests to HTTPS on production | ||
|
||
const contentSecurityPolicyDirectives: ContentSecurityPolicyDirective[] = [ | ||
...siteContentSecurityPolicyDirectives, | ||
...environmentDependentContentSecurityPolicyDirectives, | ||
]; | ||
|
||
const contentSecurityPolicy = contentSecurityPolicyDirectives | ||
.map((directive) => `${directive.directive} ${directive.values.join(" ")}`) | ||
.join("; "); | ||
response.headers.set("Content-Security-Policy", contentSecurityPolicy); | ||
} |