Skip to content

fix(security): add Content-Security-Policy header to web gateway - #966

Merged
zmanian merged 2 commits into
nearai:stagingfrom
zmanian:security/pr514-csp-rebased
Mar 12, 2026
Merged

zmanian merged 2 commits into
nearai:stagingfrom
zmanian:security/pr514-csp-rebased

Conversation

@zmanian

@zmanian zmanian commented Mar 11, 2026

Copy link
Copy Markdown
Collaborator

Supersedes #514.

Why this replacement exists:

  • the original PR branch lives on difflabai/ironclaw
  • this credential could not push the required rebase/conflict fix back to that fork
  • this branch carries the same CSP hardening rebased onto current staging

Additional adjustment included after #510:

Please review/merge this replacement instead of the original blocked PR.

gabehamilton and others added 2 commits March 11, 2026 15:57
The web gateway set X-Frame-Options and X-Content-Type-Options but had
no Content-Security-Policy header. Without CSP, there is no browser-
enforced mitigation against XSS attacks. This adds a tailored CSP that
matches the resources the frontend actually loads.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Remove cdnjs.cloudflare.com from script-src (not used in codebase)
- Add explicit object-src 'none' per security best practice
- Add regression test asserting CSP header presence and directives

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@github-actions github-actions Bot added the scope: channel/web Web gateway channel label Mar 11, 2026
@github-actions github-actions Bot added the size: M 50-199 changed lines label Mar 11, 2026
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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 web gateway by introducing a Content-Security-Policy header. This measure helps protect users from malicious content injection and ensures that only trusted sources are allowed to execute scripts or load resources, building upon previous security efforts and incorporating necessary adjustments for external dependencies.

Highlights

  • Security Enhancement: Implemented a Content-Security-Policy (CSP) header for the web gateway to mitigate various types of attacks, such as Cross-Site Scripting (XSS) and data injection.
  • CSP Configuration: Configured the CSP to allow self for most directives, with specific allowances for script-src (including https://cdn.jsdelivr.net and https://cdnjs.cloudflare.com) and style-src (including https://fonts.googleapis.com and unsafe-inline).
  • Regression Testing: Added a new integration test to verify the presence and correct content of the Content-Security-Policy header on web gateway responses, ensuring the security measure is properly applied.
Changelog
  • src/channels/web/server.rs
    • Added a new SetResponseHeaderLayer to include the Content-Security-Policy header in all web gateway responses.
    • Defined a comprehensive CSP string with directives for default-src, script-src, style-src, font-src, connect-src, img-src, object-src, frame-ancestors, base-uri, and form-action.
    • Introduced a new #[tokio::test] function, test_csp_header_present_on_responses, to assert the presence and specific content of the CSP header in HTTP responses.
Activity
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@github-actions github-actions Bot added risk: medium Business logic, config, or moderate-risk modules contributor: core 20+ merged PRs labels Mar 11, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request is a great security enhancement, adding a Content-Security-Policy header to the web gateway. The implementation is solid and includes a valuable test case to ensure the header is present and correct. My primary feedback is a suggestion to further harden the CSP by removing 'unsafe-inline' from the style-src directive, which would provide stronger protection against XSS attacks.

header::HeaderValue::from_static(
"default-src 'self'; \
script-src 'self' https://cdn.jsdelivr.net https://cdnjs.cloudflare.com; \
style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; \

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

security-medium medium

The style-src directive includes 'unsafe-inline', which allows the use of inline <style> blocks and style attributes. This is a potential security risk as it can be a vector for Cross-Site Scripting (XSS) if an attacker finds a way to inject content into the page.

This is likely needed due to style="display: none;" attributes in src/channels/web/static/index.html. To improve security, I recommend refactoring the frontend code to use a CSS class for controlling visibility instead of inline styles. For example:

CSS:

.hidden {
  display: none;
}

JavaScript:

// To hide
element.classList.add('hidden');

// To show
element.classList.remove('hidden');

This change would allow you to remove 'unsafe-inline' from the CSP, making it more robust.

Suggested change
style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; \
style-src 'self' https://fonts.googleapis.com; \

@zmanian
zmanian enabled auto-merge (squash) March 11, 2026 23:31
@zmanian
zmanian merged commit f48fe95 into nearai:staging Mar 12, 2026
14 checks passed
@ironclaw-ci ironclaw-ci Bot mentioned this pull request Mar 12, 2026
bkutasi pushed a commit to bkutasi/ironclaw that referenced this pull request Mar 28, 2026
…rai#966)

* fix(security): add Content-Security-Policy header to web gateway

The web gateway set X-Frame-Options and X-Content-Type-Options but had
no Content-Security-Policy header. Without CSP, there is no browser-
enforced mitigation against XSS attacks. This adds a tailored CSP that
matches the resources the frontend actually loads.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix(security): address CSP review feedback

- Remove cdnjs.cloudflare.com from script-src (not used in codebase)
- Add explicit object-src 'none' per security best practice
- Add regression test asserting CSP header presence and directives

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Gabe Hamilton <gabe@near.ai>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
drchirag1991 pushed a commit to drchirag1991/ironclaw that referenced this pull request Apr 8, 2026
…rai#966)

* fix(security): add Content-Security-Policy header to web gateway

The web gateway set X-Frame-Options and X-Content-Type-Options but had
no Content-Security-Policy header. Without CSP, there is no browser-
enforced mitigation against XSS attacks. This adds a tailored CSP that
matches the resources the frontend actually loads.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix(security): address CSP review feedback

- Remove cdnjs.cloudflare.com from script-src (not used in codebase)
- Add explicit object-src 'none' per security best practice
- Add regression test asserting CSP header presence and directives

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Gabe Hamilton <gabe@near.ai>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contributor: core 20+ merged PRs risk: medium Business logic, config, or moderate-risk modules scope: channel/web Web gateway channel size: M 50-199 changed lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants