Skip to content

Enable Content Security Policy#1658

Open
CloCkWeRX wants to merge 1 commit intomasterfrom
enable-csp-330213784843227832
Open

Enable Content Security Policy#1658
CloCkWeRX wants to merge 1 commit intomasterfrom
enable-csp-330213784843227832

Conversation

@CloCkWeRX
Copy link
Copy Markdown
Member

Enabled Content Security Policy (CSP) in Fat Free CRM using nonces for inline scripts and styles. The policy is initially set to report-only mode and includes allowlists for Gravatar, Leaflet, and OpenStreetMap. It also handles environment-specific rules for local development.


PR created automatically by Jules for task 330213784843227832 started by @CloCkWeRX

This commit introduces a robust Content Security Policy (CSP) for the application, following modern security best practices and supporting both standalone and engine deployment models.

Key changes:
- Defined application-wide CSP in `config/initializers/content_security_policy.rb`.
- Implemented a secure, per-request nonce generator (`SecureRandom.base64(16)`).
- Configured allowlists for essential external resources:
  - `unpkg.com` for Leaflet assets.
  - `secure.gravatar.com` for user avatars.
  - `*.openstreetmap.org` for map tiles.
- Enabled `report_only` mode for initial rollout to identify potential issues without breaking functionality.
- Explicitly allowed `http` in the development environment to support non-SSL local setups.
- Converted legacy HAML `:javascript` filters and `%style` tags to Rails `javascript_tag` and `tag.style` helpers with nonces.
- Unified the nonce-based script handling across main and admin layouts, resolving issues with nested script tags and ensuring consistent execution.
- Maintained support for `style` attributes by including `:unsafe_inline` in the `style-src` directive while still providing nonces for style blocks.
- Refactored helper methods like `get_browser_timezone_offset` and `load_select_popups_for` to be CSP-compliant.

Co-authored-by: CloCkWeRX <365751+CloCkWeRX@users.noreply.github.com>
@google-labs-jules
Copy link
Copy Markdown
Contributor

👋 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 @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

end

# Generate session nonces for permitted importmap and inline scripts
config.content_security_policy_nonce_generator = ->(request) { SecureRandom.base64(16) }

Check warning

Code scanning / Rubocop

Checks for unused block arguments. Warning

Lint/UnusedBlockArgument: Unused block argument - request. If it's necessary, use _ or _request as an argument name to indicate that it won't be used. Also consider using a proc without arguments instead of a lambda if you want it to accept any arguments but don't care about them.

# Generate session nonces for permitted importmap and inline scripts
config.content_security_policy_nonce_generator = ->(request) { SecureRandom.base64(16) }
config.content_security_policy_nonce_directives = %w(script-src style-src)

Check notice

Code scanning / Rubocop

Use `%`-literal delimiters consistently. Note

Style/PercentLiteralDelimiters: %w-literals should be delimited by [ and ].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants