Skip to content

fix(csp): Match the directive name only at the start of a directive in AppendCSPRule - #5172

Merged
shepilov merged 2 commits into
linagora:masterfrom
RyoKusnadi:fix/csp-append-rule-directive-name
Nov 4, 2026
Merged

shepilov merged 2 commits into
linagora:masterfrom
RyoKusnadi:fix/csp-append-rule-directive-name

Conversation

@RyoKusnadi

@RyoKusnadi RyoKusnadi commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

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 to default-src instead of frame-src. The external service frame is then still blocked, and default-src is wider than intended.

How it happens

csp_allowlist.default: https://frame-src.example.com is put in default-src, which comes before frame-src in the policy. Opening an app then calls AppendCSPRule(c, "frame-src", ...) from allowExternalIntentServicesFrame (web/apps/serve.go). A bare frame-src token in the list gives the same result.

Cause

appendCSPRule (web/middlewares/secure.go) used strings.Index(currentRules, ruleType), which finds the first occurrence of the text anywhere, including inside a source.

Fix

appendCSPRule now 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:

  • stops frame-src from matching frame-src-elem;
  • matches names case-insensitively (FRAME-SRC is frame-src), as CSP does, so no duplicate directive is added that browsers would ignore;
  • handles a policy without a trailing ;. Before, updating frame-src 'self' returned an empty policy. The normal builder always adds the ;, so this was not hit in practice.

Scope

appendCSPRule is 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 AppendCSPRule subtest in web/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 vet and gofmt -l are clean. Not run: the rest of the repo's tests (many need CouchDB).

Fixes #5161

…n AppendCSPRule

Signed-off-by: Ryo Kusnadi <ryokusnadi@gmail.com>
@shepilov

shepilov commented Nov 4, 2026

Copy link
Copy Markdown
Member

@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:

  • No trailing semicolon: updating "frame-src 'self'" currently returns an empty policy. This bug predates the PR; the normal builder avoids it by always adding semicolons.
  • Mixed-case names: FRAME-SRC should match frame-src. CSP directive names are case-insensitive, and browsers ignore later duplicate directives

Signed-off-by: Ryo Kusnadi <ryokusnadi@gmail.com>
@RyoKusnadi

Copy link
Copy Markdown
Contributor Author

Thanks @shepilov, good points! I dropped the separate helper. appendCSPRule now splits the policy on ;, finds the directive by its first word, appends the values and joins it back, so the directives keep their order.

It also covers the two cases you mentioned:

  • No trailing semicolon: frame-src 'self' now becomes frame-src 'self' new-rule instead of an empty policy, and a new directive is added after a ;.
  • Mixed case: FRAME-SRC matches frame-src (strings.EqualFold), so no duplicate is added.

I added test cases for both. The existing cases pass unchanged, and go test ./web/middlewares/ passes. I also updated the description to match.

@shepilov
shepilov merged commit a6d505e into linagora:master Nov 4, 2026
@RyoKusnadi
RyoKusnadi deleted the fix/csp-append-rule-directive-name branch November 5, 2026 12:37
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.

CSP: AppendCSPRule adds the sources to an earlier directive when the directive name appears in one of its sources (e.g. https://frame-src.example.com)

2 participants