🛡️ Sentinel: [HIGH] Add API security headers (CSP, Permissions-Policy)#138
🛡️ Sentinel: [HIGH] Add API security headers (CSP, Permissions-Policy)#138Dexploarer wants to merge 1 commit intomainfrom
Conversation
This change enhances the security of the API server by adding: - `Content-Security-Policy`: Defaulting to `default-src 'none'; ...` to prevent XSS/injection attacks. - `Permissions-Policy`: Disabling sensitive features like camera, microphone, and geolocation by default. Also fixed a build script issue in `packages/plugin-pi-ai` to support Bun builds correctly.
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary of ChangesHello @Dexploarer, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the security posture of the API by implementing crucial HTTP security headers, specifically Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
| sourcemap: true, | ||
| sourcemap: "external", | ||
| minify: false, | ||
| external: ["node:*", "@elizaos/core", "@mariozechner/pi-ai", "zod"], |
There was a problem hiding this comment.
Maintainability Issue:
The external array is hardcoded with specific dependencies. If the project's dependencies change, this list may become outdated, leading to bundling errors or runtime failures. Consider dynamically generating the list of external dependencies based on the project's package.json or using a more maintainable approach.
Example improvement:
external: Object.keys(require('./package.json').dependencies),| sourcemap: "external", | ||
| minify: false, | ||
| external: ["node:*", "@elizaos/core", "@mariozechner/pi-ai", "zod"], | ||
| naming: { |
There was a problem hiding this comment.
Scalability Concern:
The naming pattern [dir]/[name].[ext] is suitable for a single entrypoint, but if multiple entrypoints are added, this could lead to filename collisions or unclear output structure. Consider a more robust naming scheme that includes additional context, such as hashes or subdirectories, to avoid potential conflicts and improve clarity.
| function req( | ||
| port: number, | ||
| method: string, | ||
| p: string, | ||
| ): Promise<{ | ||
| status: number; | ||
| headers: http.IncomingHttpHeaders; | ||
| }> { | ||
| return new Promise((resolve, reject) => { | ||
| const r = http.request( | ||
| { | ||
| hostname: "127.0.0.1", | ||
| port, | ||
| path: p, | ||
| method, | ||
| }, | ||
| (res) => { | ||
| res.resume(); // consume body | ||
| res.on("end", () => { | ||
| resolve({ status: res.statusCode ?? 0, headers: res.headers }); | ||
| }); | ||
| }, | ||
| ); | ||
| r.on("error", reject); | ||
| r.end(); | ||
| }); | ||
| } |
There was a problem hiding this comment.
The req function does not set a timeout for the HTTP request. If the server fails to respond, the test may hang indefinitely, leading to unreliable test runs and potential CI timeouts.
Recommended solution:
Add a timeout to the HTTP request, for example:
const r = http.request({ ... }, (res) => { ... });
r.setTimeout(5000, () => {
r.abort();
reject(new Error('Request timed out'));
});This ensures the test fails fast if the server is unresponsive.
There was a problem hiding this comment.
Code Review
This pull request enhances API security by adding Content-Security-Policy and Permissions-Policy headers, which is a great step towards hardening the application against browser-based attacks. The changes are well-tested with a new end-to-end test.
I've identified a potential improvement in the Content-Security-Policy configuration to make it even more secure. My feedback focuses on strengthening the sandbox directive. Please see the detailed comments.
| res.setHeader( | ||
| "Content-Security-Policy", | ||
| "default-src 'none'; frame-ancestors 'none'; sandbox allow-scripts allow-same-origin;", | ||
| ); |
There was a problem hiding this comment.
The Content-Security-Policy is a great addition for security. However, the sandbox directive is configured with allow-scripts and allow-same-origin, which is a potentially dangerous combination. This configuration can allow a sandboxed document to programmatically remove the sandbox attribute, reducing its effectiveness.
Given that default-src 'none' is already in place (which blocks script loading), allow-scripts is likely ineffective but adds confusion. To strengthen this policy, I recommend making the sandbox directive as strict as possible by removing allow-scripts and allow-same-origin. A stricter policy would be sandbox;. This will prevent any script execution and access to origin storage within the sandboxed context, providing a stronger defense against content injection vulnerabilities.
| res.setHeader( | |
| "Content-Security-Policy", | |
| "default-src 'none'; frame-ancestors 'none'; sandbox allow-scripts allow-same-origin;", | |
| ); | |
| res.setHeader( | |
| "Content-Security-Policy", | |
| "default-src 'none'; frame-ancestors 'none'; sandbox;", | |
| ); |
| expect(headers["content-security-policy"]).toBe( | ||
| "default-src 'none'; frame-ancestors 'none'; sandbox allow-scripts allow-same-origin;", | ||
| ); |
There was a problem hiding this comment.
This test assertion will need to be updated to reflect the recommended change to the Content-Security-Policy header in src/api/server.ts. The expected value should be updated to match the stricter sandbox; directive.
| expect(headers["content-security-policy"]).toBe( | |
| "default-src 'none'; frame-ancestors 'none'; sandbox allow-scripts allow-same-origin;", | |
| ); | |
| expect(headers["content-security-policy"]).toBe( | |
| "default-src 'none'; frame-ancestors 'none'; sandbox;" | |
| ); |
🛡️ Sentinel: [HIGH] Add API security headers (CSP, Permissions-Policy)
🚨 Severity: HIGH
💡 Vulnerability: Missing strict Content-Security-Policy and Permissions-Policy on API responses.
🎯 Impact: Although intended as an API, missing headers could allow for exploitation if endpoints are rendered in browsers or if content injection vulnerabilities are discovered.
🔧 Fix: Added strict default headers to
applyCorsmiddleware insrc/api/server.ts.✅ Verification: Added e2e test
test/api-security-headers.e2e.test.tsverifying header presence. Verified regression withtest/api-server.e2e.test.ts.Also fixed
packages/plugin-pi-ai/build.tsto usesourcemap: "external"for compatibility with Bun build.PR created automatically by Jules for task 13463756395184022857 started by @Dexploarer
Mintlify
0 threads from 0 users in Mintlify