Skip to content

Conversation

@guspower
Copy link
Contributor

@guspower guspower commented May 4, 2025

A first pass implementation as discussed in issue #909

Let me know what you think - I'll update the docs when you're happy with it.

@guspower
Copy link
Contributor Author

guspower commented May 4, 2025

Ah I was unaware of the playwright tests - looks like they caught a bug in my implementation. Cool, I will sort it out.

Copy link
Collaborator

@lovasoa lovasoa left a comment

Choose a reason for hiding this comment

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

Thank you for taking the time to dive in !

This is a good start, but the logic is a little bit wrong here. Each nonce has to be used only once. We cannot define a random nonce on startup and then use it repeatedly. Otherwise an attacker can easily make a script injection that contains the known nonce.

How I think it should work:

  • in app_config, we store a parsed csp string. It could be something as simple as struct CSPFormat { before_nonce: String, after_nonce: String }
  • add a method to request_context.content_security_policy that takes a CSPFormat and generates a header.
  • in render.rs, replace the csp insertion with request_context.content_security_policy.add_to_response(&mut response, app_state.config.content_security_policy)

@guspower
Copy link
Contributor Author

guspower commented May 4, 2025

Yep makes sense +1.

I'll take another run at this tomorrow and ensure each nonce is per-request.

guspower added 2 commits May 5, 2025 13:03
…ght test to verify subsequent requests return a different nonce.
@guspower
Copy link
Contributor Author

guspower commented May 5, 2025

OK this should now be correct (I added an e2e test to verify). Let me know what you think!

@lovasoa lovasoa force-pushed the extend-csp-configuration branch from af329fc to a88f350 Compare May 5, 2025 22:09
@lovasoa lovasoa merged commit 1582956 into sqlpage:main May 6, 2025
10 checks passed
@lovasoa
Copy link
Collaborator

lovasoa commented May 6, 2025

Done. Thank you @guspower !

@lovasoa lovasoa mentioned this pull request May 6, 2025
@guspower
Copy link
Contributor Author

guspower commented May 6, 2025

Excellent thank you @lovasoa !!!

@guspower guspower deleted the extend-csp-configuration branch May 6, 2025 08:57
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