Skip to content
Draft
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions .jules/sentinel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## 2025-02-18 - [API Security Headers]
**Vulnerability:** Missing Content-Security-Policy (CSP) and Permissions-Policy headers in API responses.
**Learning:** The API server also serves the static UI in production, making CSP crucial for mitigating XSS risks. Also, `bun test` requires explicit mocks for dependencies when `node_modules` are incomplete or path mappings are broken.
**Prevention:** Enforce security headers in the central CORS/headers middleware (`applyCors`) to ensure they apply to all responses.
10 changes: 9 additions & 1 deletion src/api/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4240,7 +4240,7 @@ function resolveCorsOrigin(origin?: string): string | null {
return null;
}

function applyCors(
export function applyCors(
req: http.IncomingMessage,
res: http.ServerResponse,
): boolean {
Expand Down Expand Up @@ -4268,6 +4268,14 @@ function applyCors(
res.setHeader("X-Frame-Options", "DENY");
res.setHeader("X-XSS-Protection", "1; mode=block");
res.setHeader("Referrer-Policy", "strict-origin-when-cross-origin");
res.setHeader(
"Content-Security-Policy",
"default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval' blob:; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; img-src 'self' data: blob: https:; font-src 'self' data: https://fonts.gstatic.com; connect-src 'self' ws: wss: https:; media-src 'self' blob:; frame-ancestors 'none'; upgrade-insecure-requests",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security-critical critical

The script-src directive includes 'unsafe-inline' and 'unsafe-eval', which significantly weakens the security posture against Cross-Site Scripting (XSS) attacks. These keywords allow the execution of inline scripts and dynamic code from strings, which are common XSS vectors. A strong Content Security Policy should avoid these to be effective.

To improve this:

  1. Remove 'unsafe-inline' and 'unsafe-eval' from script-src. This is the most critical change.
  2. Refactor any inline scripts (<script>...</script> or onclick="...") into separate .js files served from your origin ('self').
  3. For any inline scripts that cannot be moved, use a nonce-based or hash-based approach. This involves generating a unique nonce on the server for each request and adding it to the CSP header and the script tags.

Additionally, for even stronger security:

  • style-src also contains 'unsafe-inline', which should ideally be removed.
  • img-src and connect-src allow loading resources from any https: source, which is very broad. Consider restricting this to specific, trusted domains.
Suggested change
"default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval' blob:; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; img-src 'self' data: blob: https:; font-src 'self' data: https://fonts.gstatic.com; connect-src 'self' ws: wss: https:; media-src 'self' blob:; frame-ancestors 'none'; upgrade-insecure-requests",
"default-src 'self'; script-src 'self' blob:; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; img-src 'self' data: blob: https:; font-src 'self' data: https://fonts.gstatic.com; connect-src 'self' ws: wss: https:; media-src 'self' blob:; frame-ancestors 'none'; upgrade-insecure-requests",

);
res.setHeader(
"Permissions-Policy",
"accelerometer=(), camera=(), geolocation=(), gyroscope=(), magnetometer=(), payment=(), usb=()",
);

return true;
}
Expand Down
Loading