🛡️ Sentinel: [HIGH] Add Content-Security-Policy and Permissions-Policy headers#148
🛡️ Sentinel: [HIGH] Add Content-Security-Policy and Permissions-Policy headers#148Dexploarer wants to merge 1 commit intodevelopfrom
Conversation
- Adds Content-Security-Policy header to `applyCors` middleware - Adds Permissions-Policy header to `applyCors` middleware - Exports `applyCors` for testing purposes - Adds `test/verify-csp.test.ts` (deleted after verification) to ensure headers are present
|
👋 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 API server's security posture by introducing 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
|
There was a problem hiding this comment.
Code Review
This PR introduces important security headers, Content-Security-Policy and Permissions-Policy, to enhance the application's security. While the addition of these headers is a positive step, the current Content-Security-Policy is configured with 'unsafe-inline' and 'unsafe-eval' for scripts, which undermines its effectiveness against XSS attacks. I have provided a critical review comment with a suggested fix to strengthen this policy. The Permissions-Policy appears to be well-configured.
| 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", |
There was a problem hiding this comment.
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:
- Remove
'unsafe-inline'and'unsafe-eval'fromscript-src. This is the most critical change. - Refactor any inline scripts (
<script>...</script>oronclick="...") into separate.jsfiles served from your origin ('self'). - 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-srcalso contains'unsafe-inline', which should ideally be removed.img-srcandconnect-srcallow loading resources from anyhttps:source, which is very broad. Consider restricting this to specific, trusted domains.
| "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", |
This PR adds important security headers (
Content-Security-PolicyandPermissions-Policy) to the API server responses.Security Enhancements
Verification
test/verify-csp.test.tswas created and run to confirm the headers are present in the response from/api/status.Changes
src/api/server.ts: UpdatedapplyCorsto set the new headers and exported it..jules/sentinel.md: Recorded the security learning.PR created automatically by Jules for task 5928814524265511167 started by @Dexploarer