Repository navigation
fix(csp): Match the directive name only at the start of a directive in AppendCSPRule - #5172
Conversation
…n AppendCSPRule Signed-off-by: Ryo Kusnadi <ryokusnadi@gmail.com>
|
@RyoKusnadi hey, thank for contribution! it make sense , but could you please don't introduce a specific function for this and keep everything in appendCSPRule with a streightforward logic: find the directive by its first word, append its values, join the policy back together it suppose also to preserve directives order. Aslo it woud be great to add support for this two cases:
|
Signed-off-by: Ryo Kusnadi <ryokusnadi@gmail.com>
|
Thanks @shepilov, good points! I dropped the separate helper. It also covers the two cases you mentioned:
I added test cases for both. The existing cases pass unchanged, and |
Problem
When an allow list holds a host like
https://frame-src.example.com, an app that opens intent services outside the cozy domain gets the new source added todefault-srcinstead offrame-src. The external service frame is then still blocked, anddefault-srcis wider than intended.How it happens
csp_allowlist.default: https://frame-src.example.comis put indefault-src, which comes beforeframe-srcin the policy. Opening an app then callsAppendCSPRule(c, "frame-src", ...)fromallowExternalIntentServicesFrame(web/apps/serve.go). A bareframe-srctoken in the list gives the same result.Cause
appendCSPRule(web/middlewares/secure.go) usedstrings.Index(currentRules, ruleType), which finds the first occurrence of the text anywhere, including inside a source.Fix
appendCSPRulenow splits the policy on;, finds the directive whose first word is the name, appends the values, and joins the policy back together. Directives keep their order and their spacing. This also:frame-srcfrom matchingframe-src-elem;FRAME-SRCisframe-src), as CSP does, so no duplicate directive is added that browsers would ignore;;. Before, updatingframe-src 'self'returned an empty policy. The normal builder always adds the;, so this was not hit in practice.Scope
appendCSPRuleis the only place that searches the policy by name. Output for policies the old code handled correctly is unchanged (the existing cases still pass as they are).Testing
Added cases to the
AppendCSPRulesubtest inweb/middlewares/secure_test.go: the two from the issue, a bare token,frame-src-elem, no trailing;, and a mixed-case name. Before the fix the first three fail, for example:expected: "default-src 'self' https://frame-src.example.com/;frame-src 'self' https://svc.example.com;"actual : "default-src 'self' https://frame-src.example.com/ https://svc.example.com;frame-src 'self';"After the fix,
go test ./web/middlewares/passes;go vetandgofmt -lare clean. Not run: the rest of the repo's tests (many need CouchDB).Fixes #5161