-
Notifications
You must be signed in to change notification settings - Fork 2
feat(security): add XSS sanitization and security headers #1455
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
base: main
Are you sure you want to change the base?
Changes from 6 commits
fdc0cc6
0cf03fd
cc43b3b
46815c4
1f9097b
a99adb6
eb9b0f8
50c959d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -27,16 +27,17 @@ import { defaultLocale, locales } from "@/trustlab/payload/utils/locales"; | |||||
|
|
||||||
| const __filename = fileURLToPath(import.meta.url); | ||||||
| const __dirname = path.dirname(__filename); | ||||||
| const defaultAllowedOrigin = site.url.replace(/\/+$/, ""); | ||||||
|
|
||||||
| const cors = | ||||||
| process.env.PAYLOAD_CORS?.split(",") | ||||||
| const normalizeOrigins = (value?: string) => | ||||||
| value | ||||||
| ?.split(",") | ||||||
| .map((d) => d.trim()) | ||||||
| .map((d) => d.replace(/\/+$/, "")) | ||||||
| .filter(Boolean) ?? []; | ||||||
|
|
||||||
| const csrf = | ||||||
| process.env.PAYLOAD_CSRF?.split(",") | ||||||
| .map((d) => d.trim()) | ||||||
| .filter(Boolean) ?? []; | ||||||
| const cors = normalizeOrigins(process.env.PAYLOAD_CORS); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May be:
Suggested change
So that there is no need to check for if the return array is empty later on? |
||||||
| const csrf = normalizeOrigins(process.env.PAYLOAD_CSRF); | ||||||
|
|
||||||
| let nodemailerAdapterArgs: NodemailerAdapterArgs | undefined; | ||||||
| if (process.env.SMTP_HOST && process.env.SMTP_PASS) { | ||||||
|
|
@@ -87,8 +88,8 @@ export default buildConfig({ | |||||
| Users, | ||||||
| Opportunities, | ||||||
| ] as CollectionConfig[], | ||||||
| cors, | ||||||
| csrf, | ||||||
| cors: cors.length ? cors : [defaultAllowedOrigin], | ||||||
| csrf: csrf.length ? csrf : [defaultAllowedOrigin], | ||||||
| db: mongooseAdapter({ | ||||||
| url: process.env.DATABASE_URL ?? false, | ||||||
| }), | ||||||
|
|
@@ -106,8 +107,7 @@ export default buildConfig({ | |||||
| : undefined), | ||||||
| plugins: [...plugins], | ||||||
| secret: process.env.PAYLOAD_SECRET || "", | ||||||
| // Just the origin, without trailing slash. | ||||||
| serverURL: site.url.slice(0, -1), | ||||||
| serverURL: defaultAllowedOrigin, | ||||||
| sharp, | ||||||
| telemetry: process.env.NEXT_TELEMETRY_DISABLED === "0", | ||||||
| typescript: { | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,14 +10,15 @@ import React from "react"; | |
|
|
||
| function HelplineEmbedDialog({ closeLabel, embedCode, onClose, open, title }) { | ||
| const dialogTitleId = React.useId(); | ||
| const hasEmbed = Boolean(embedCode); | ||
|
|
||
| return ( | ||
| <Dialog | ||
| aria-labelledby={dialogTitleId} | ||
| fullWidth | ||
| maxWidth={false} | ||
| onClose={onClose} | ||
| open={Boolean(embedCode && open)} | ||
| open={Boolean(hasEmbed && open)} | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this used inside |
||
| PaperProps={{ | ||
| sx: { | ||
| width: "100%", | ||
|
|
@@ -47,7 +48,7 @@ function HelplineEmbedDialog({ closeLabel, embedCode, onClose, open, title }) { | |
| }, | ||
| }} | ||
| dangerouslySetInnerHTML={ | ||
| embedCode | ||
| hasEmbed | ||
| ? { | ||
| __html: embedCode, | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import { FilterXSS } from "xss"; | ||
|
|
||
| const embedSanitizer = new FilterXSS({ | ||
| whiteList: { | ||
| a: ["href", "target", "rel", "aria-label"], | ||
| br: [], | ||
| div: ["class"], | ||
| iframe: [ | ||
| "allow", | ||
| "allowfullscreen", | ||
| "class", | ||
| "frameborder", | ||
| "height", | ||
| "loading", | ||
| "name", | ||
| "referrerpolicy", | ||
| "sandbox", | ||
| "scrolling", | ||
| "src", | ||
| "title", | ||
| "width", | ||
| ], | ||
| p: ["class"], | ||
| section: ["class"], | ||
| span: ["class"], | ||
| }, | ||
| stripIgnoreTag: true, | ||
| stripIgnoreTagBody: ["script", "style"], | ||
| }); | ||
|
|
||
| export default function sanitizeEmbedHtml(html) { | ||
| if (!html?.trim()) { | ||
| return ""; | ||
| } | ||
|
|
||
| return embedSanitizer.process(html); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| function getContentSecurityPolicy(nodeEnv) { | ||
| const isProduction = nodeEnv === "production"; | ||
| const connectSrc = ["'self'", "https:"]; | ||
| const scriptSrc = ["'self'", "'unsafe-inline'", "https:"]; | ||
|
|
||
| // Next.js dev tooling relies on websocket connections and eval-based refresh | ||
| // helpers. Keep those allowances scoped to non-production environments. | ||
| if (!isProduction) { | ||
| connectSrc.push("http:", "ws:", "wss:"); | ||
| scriptSrc.splice(1, 0, "'unsafe-eval'"); | ||
| } | ||
|
|
||
| return [ | ||
| "default-src 'self'", | ||
| "base-uri 'self'", | ||
| `connect-src ${connectSrc.join(" ")}`, | ||
| "font-src 'self' data: https:", | ||
| "form-action 'self'", | ||
| "frame-ancestors 'self'", | ||
| "frame-src 'self' https:", | ||
| "img-src 'self' data: blob: https:", | ||
| "media-src 'self' blob: https:", | ||
| "object-src 'none'", | ||
| // Inline script/style support is still required by the current app and MUI | ||
| // setup, so this policy tightens sources without breaking rendering. | ||
| `script-src ${scriptSrc.join(" ")}`, | ||
| "style-src 'self' 'unsafe-inline' https:", | ||
|
Comment on lines
+28
to
+29
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aren't |
||
| ].join("; "); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not just use `
...;
...;
`on the whole thing instead of [
'...',
'...',
].join('; ')? |
||
| } | ||
|
|
||
| export function getSecurityHeaders(nodeEnv = process.env.NODE_ENV) { | ||
| const headers = [ | ||
| { | ||
| key: "Content-Security-Policy", | ||
| value: getContentSecurityPolicy(nodeEnv), | ||
| }, | ||
| { | ||
| key: "Permissions-Policy", | ||
| value: "camera=(), geolocation=(), microphone=(), payment=(), usb=()", | ||
| }, | ||
| { | ||
| key: "Referrer-Policy", | ||
| value: "strict-origin-when-cross-origin", | ||
| }, | ||
| { | ||
| key: "X-Content-Type-Options", | ||
| value: "nosniff", | ||
| }, | ||
| { | ||
| key: "X-Frame-Options", | ||
| value: "SAMEORIGIN", | ||
| }, | ||
| ]; | ||
|
|
||
| if (nodeEnv === "production") { | ||
| // HSTS is intentionally production-only so local HTTP development and | ||
| // non-TLS preview setups do not get sticky HTTPS behavior. | ||
| headers.push({ | ||
| key: "Strict-Transport-Security", | ||
|
kelvinkipruto marked this conversation as resolved.
Outdated
|
||
| value: "max-age=31536000; includeSubDomains; preload", | ||
| }); | ||
| } | ||
|
|
||
| return headers; | ||
| } | ||
|
|
||
| export const securityHeaders = getSecurityHeaders(); | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
normalizeOriginto trim trailing/(should it benormalizeornormalise)?