Skip to content
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

feat: enable CSP in all environments (local/preview/prod) #1334

Merged
merged 3 commits into from
Oct 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 38 additions & 13 deletions frontend/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,12 @@ const nextConfig = {
},
// eslint-disable-next-line prefer-arrow/prefer-arrow-functions
async headers() {
if (process.env.NEXT_PUBLIC_ENV === "prod") {
return [
{
source: "/(.*)",
headers: securityHeaders,
},
];
} else {
return [];
}
return [
{
source: "/(.*)",
headers: securityHeaders,
},
];
},
};

Expand All @@ -28,7 +24,11 @@ const ContentSecurityPolicy = {
"https://fonts.googleapis.com",
process.env.NEXT_PUBLIC_SUPABASE_URL,
"https://api.june.so",
"https://www.quivr.app/",
{
prod: "https://www.quivr.app/",
preview: "https://preview.quivr.app/",
local: ["http://localhost:3000", "http://localhost:3001"],
},
],
"connect-src": [
"'self'",
Expand All @@ -50,13 +50,38 @@ const ContentSecurityPolicy = {
"'unsafe-inline'",
"'unsafe-eval'",
"https://va.vercel-scripts.com/",
"https://www.quivr.app/",
{
prod: "https://www.quivr.app/",
preview: "https://preview.quivr.app/",
local: ["http://localhost:3000", "http://localhost:3001"],
},
"https://www.google-analytics.com/",
],
"frame-ancestors": ["'none'"],
"style-src": ["'unsafe-inline'", "https://www.quivr.app/"],
"style-src": [
"'unsafe-inline'",
{
prod: "https://www.quivr.app/",
preview: "https://preview.quivr.app/",
local: ["http://localhost:3000", "http://localhost:3001"],
},
],
};

// Resolve environment-specific CSP values
for (const directive of Object.values(ContentSecurityPolicy)) {
for (const [index, resource] of directive.entries()) {
if (typeof resource === "string") {
continue;
}
directive[index] = resource[process.env.NEXT_PUBLIC_ENV];
if (Array.isArray(directive[index])) {
directive[index] = directive[index].join(" ");
}
}
}

// Build CSP string
const cspString = Object.entries(ContentSecurityPolicy)
.map(([key, values]) => `${key} ${values.join(" ")};`)
.join(" ");
Expand Down
Loading