Build a Policy From Your Traffic
Writing a Content-Security-Policy by hand means guessing every script, style, image, font, frame and connection your pages load. Discovery replaces the guess with your own traffic. For a week, your visitors’ browsers report everything your site loads, on every page, including the ones behind a login. HeaderHawk then proposes a strict policy, one directive at a time, with the evidence behind each source.
Discovery is on every plan. Owners, admins and members can start it and decide on sources; viewers can read the draft.
1. Start discovery
Section titled “1. Start discovery”Open Policy › Draft in the dashboard, choose the site and press Start discovery. The page shows two headers to add where you set headers today:
- a
Reporting-Endpointsline naming a second endpoint,headerhawk-discovery, beside your site’s own - a
Content-Security-Policy-Report-Onlypolicy that allows nothing:
default-src 'none'; script-src 'report-sample'; style-src 'report-sample';object-src 'none'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'It is report-only, so it blocks nothing. Browsers apply and report every
policy they are sent, so it sits beside any policy you already serve without
changing what that one reports. 'report-sample' asks the browser for the
first 40 characters of each inline script, style and eval, which is how the
draft tells one inline block from another.
Discovery reports go to the site’s endpoint with /discovery appended, and
HeaderHawk keeps them apart from your ordinary reports. They never appear in
Issues or the Explorer, and they never raise an alert, including CSP policy
changed. While discovery is off, that endpoint accepts reports and discards
them.
2. Let it run for a week
Section titled “2. Let it run for a week”A policy that allows nothing makes every page view report every resource on it. To keep that affordable:
- Each browser’s first report we receive of each source is kept. A source
is a directive and the origin it loaded from, such as
https://cdn.example.netunderscript-src. Chrome, Firefox, Safari and Edge are kept apart, so a source two of them load is seen by both, however quiet the page. - Repeats are sampled at 5%. A repeat only adds to how widely a source is loaded, which a sample shows as well as every report would.
- Only kept reports count toward your monthly allowance. A sampled-out repeat costs nothing.
- A large burst of reports can be refused, and nothing else is. Discovery
reports are received on capacity of their own, kept apart from your
violations and every other report type, so a burst never costs those
anything. Past that capacity, which every site running discovery shares, a
report is answered
503and never reaches us. A source is only marked as seen once it is stored, so the next page view reports it again; only a page that sends more than that capacity on every load can lose the same reports each time. Check a proposal against the pages you know before you enforce it.
The page counts the days the run has had. Before seven it recommends a full week, so that a weekly job, a monthly report page or a checkout most visitors never reach has time to be seen. A run lasts 14 days, a week of traffic and a week to review it, and then stops by itself; the page says on which day. There is no button to stop it sooner, and no reason to: the reports cost little, and a source that turns up while you review is added to the draft as one to decide. The week is guidance, not a gate. Above the draft, Start reviewing now moves the site on to reviewing whenever you have seen enough (Your Next Step).
3. Review the draft
Section titled “3. Review the draft”The draft is one card per directive, its sources ranked by how many reports named them. Each source carries its evidence: the browsers that loaded it, how many reports and pages, up to five example pages, and when it was first and last seen.
How a report becomes a source:
- A URL on the page’s own origin becomes
'self'. - Any other URL becomes its scheme and host, such as
https://cdn.example.net. data:is proposed for images, fonts and media when it was seen, andblob:for images, media, workers and frames. Neither is ever proposed forscript-src.- Browser-extension schemes never become sources. A source whose reports look like extension noise is listed but left out.
- Several subdomains of one site, such as
a.cdn.example.netandb.cdn.example.net, are offered as one*.cdn.example.netsuggestion, left out until you keep it.
A source is in the policy by default once two browser families have loaded it, or once it has about twenty reports. That is the same bar a violation clears before it alerts. Below it, the source is listed with its switch off, because one browser on one visit might be an extension or a one-off.
Turn a switch on to keep a source and off to drop it. The policy and its grade change as you do, and your decisions are saved, so the week’s review can happen across several sessions or by several people. A decision outlasts the run: start discovery again later and the sources you kept or dropped keep their answer.
Above the draft, the page counts the sources still to decide. Keep the rest as proposed decides each of them as the draft shows it now: in the policy, or left out. Review is done once nothing is left to decide.
Who each source is, and the ones that look wrong
Section titled “Who each source is, and the ones that look wrong”Each third-party source is named from a catalog of known hosts, built from the open third-party-web dataset: the company, the product when it has several, and what it is for, such as analytics, payments or fonts. A host on your site’s own domain is marked as yours and never looked up. A host on a shared platform, such as a CloudFront distribution or an S3 bucket, is not named after the platform, because anyone can serve files there.
A third-party host the catalog does not know can get a name suggested by an AI model, Claude from Anthropic, marked Suggested by AI with a sentence on what it does. The model is sent the host name and the directives it was reported under, and nothing about your pages, your visitors or your site. While it is being asked, the source shows Looking up… and the Draft screen checks again every few seconds for about a minute. It answers “unknown” rather than guess, and a name it gives for a company the catalog knows, on a domain the catalog does not list for that company, is dropped, since that is what a look-alike domain gets. A suggestion is only a label: it never changes whether a source is in the policy or flagged. Each host is asked about once and the answer kept for 30 days, for every account, and an account asks about at most 200 new hosts a day.
A source that looks wrong is flagged with the reason, and left out until you keep it or drop it, whatever its evidence:
| Flag | When |
|---|---|
| Arrived late | First seen in the last three days of a week-long run (six of a two-week run), after the rest of the draft had stopped growing. Runs shorter than a week are not checked. |
| Only on payment pages | Every page it was seen on is one of your payment pages. Known payment providers are not flagged, since their scripts load only on the checkout by design. |
| Looks like | A letter or two away from a known host, from another source in the draft, or from your own domain, including look-alike characters from other alphabets. |
| Unknown and rare | Not a known host, and seen on only one page or by only one browser family, though often enough to be in by default. |
A flagged source still counts as one to decide on the Overview, so the review step is not done until someone has kept or dropped it. Leave out drops it and records that you looked. A flag never adds a source to the policy and never removes one from the list. Payment pages are on the Professional and Business plans; without one marked, no source can be flagged for appearing only on them.
Directives the draft sets itself
Section titled “Directives the draft sets itself”Some directives are pinned rather than taken from traffic:
| Directive | Value |
|---|---|
default-src |
'none', so anything not listed is refused |
object-src |
'none', the only safe value |
base-uri |
'none', or 'self' if a page set a <base> element |
frame-ancestors |
'none', or 'self' if a page was framed |
form-action |
'none' if no form was submitted; otherwise its targets, kept or dropped like any source |
A fetch directive nothing was reported under is left out and falls back to
default-src 'none'.
Some of what the run saw cannot be allowed safely by naming a source. Each is a flag above the cards, with its samples, the pages it came from and a How to fix this link:
- Inline scripts and styles. The draft never contains
'unsafe-inline'. Fixing an inline script or style takes one from the flag to a changed header. evaland WebAssembly. The draft never contains'unsafe-eval'. See Fixing code built from strings.- Framing. No browser names the page that framed yours, so
frame-ancestorsis'self'. See Allowing the sites that frame yours. <base>. No browser names the origin a<base>element pointed at, so add it tobase-uriby hand.- Plugins.
object-srcstays'none'. - A scheme where one is not proposed, such as
data:underscript-src. Schemes says what each one allows.
Fixing an inline script or style
Section titled “Fixing an inline script or style”An inline script is code written into the page itself, between
<script> tags or in an attribute, rather than loaded from a file. The draft
leaves 'unsafe-inline' out because it would let any script injected into
the page run as well. These steps change the draft’s header so that your own
inline code is allowed and nothing else is.
1. Find it. The flag shows the first characters each browser reported and up to five pages it ran on. Open one of those pages, view its source, and search for the sample. Where the sample sits decides what to do:
- Inside a
<script>or<style>element, it is an inline block. Carry on with step 2. - Inside an attribute, such as
onclick="…"orstyle="…", it is an inline event handler or style attribute. A nonce or a hash on a<script>does not cover it. Move the handler into a script withaddEventListener, or the style into a stylesheet. Where you cannot, a hash of the attribute’s value together with'unsafe-hashes'allows that one attribute, asscript-src-attrandstyle-src-attrexplain.
2. Choose a hash, a nonce or a file.
- A hash when the block’s bytes never change: a static site, or a snippet pasted from a vendor.
- A nonce when a server renders each page and can put a fresh value in both the header and the tag.
- A file when the site is static and the block changes, for example
because a build step writes a date into it. Move the code to a
.jsor.cssfile on your own origin, and check that the directive has'self'.
3. With a hash, compute the SHA-256 of the block’s exact contents, every byte between the opening and closing tags:
echo -n 'window.APP_CONFIG = { region: "eu-west-1" };' \ | openssl dgst -sha256 -binary \ | openssl enc -base64Whitespace counts. A space, a line break or a change of indentation is a
different hash, so copy the block from the page’s source rather than retyping
it. Add the result, quoted and prefixed as 'sha256-…', to script-src for a
script or style-src for a <style> element. Edit the header you copied from
the Draft, or press Open in the header builder and add it there.
Hashes has the details.
4. With a nonce, your server generates a random value for every response,
puts it in the header as 'nonce-…' and on each <script> or <style> tag
as nonce="…". The integration guides show it for
Next.js,
Node.js,
Cloudflare and
Vercel. A host that only serves fixed
files cannot send a nonce, because the value has to change on every response.
Use a hash or a file there. Nonces has the
rules that make one worth having.
5. With a script that loads the rest, such as a tag manager or a bundle’s
entry point, add 'strict-dynamic' to script-src. A nonce on that one
script, or a hash if it is an inline block, is then enough: whatever it loads is trusted, and the hosts listed
in script-src are ignored by browsers that understand it.
'strict-dynamic' explains how
older browsers read the same policy.
Deploy the changed header report-only. If the inline code still reports, its bytes differ from what you hashed, or its tag is missing the nonce.
Allowing the sites that frame yours
Section titled “Allowing the sites that frame yours”The framing flag means a browser reported one of your pages inside a frame.
No browser says which page framed it, so the draft sets frame-ancestors to
'self', which allows your own pages and refuses every other site once the
policy is enforced. If the frames are your own, 'self' is already right.
If another site embeds yours, such as a partner or a site builder’s preview,
add its origin after 'self':
frame-ancestors 'self' https://partner.exampleframe-ancestors explains the directive.
Fixing code built from strings
Section titled “Fixing code built from strings”Code built from strings is flagged when a page passes a string to eval,
new Function, setTimeout or setInterval. The samples are the first
characters of that string. 'unsafe-eval' would allow it, along with any
string an attacker manages to get evaluated, so the draft leaves it out.
- Your own code: pass a function instead of a string, such as
setTimeout(() => refresh(), 1000)in place ofsetTimeout("refresh()", 1000), and parse data withJSON.parserather thaneval. - A library or a vendor’s script: search your bundle for the sample to
find which one. Many libraries that compile templates at run time have a
build-time option or a version that does not, so check its documentation
before adding
'unsafe-eval'. - WebAssembly: compiling a WebAssembly module is flagged on its own. Add
'wasm-unsafe-eval'toscript-src, which allows WebAssembly without allowingeval.
The grade
Section titled “The grade”The grade beside the policy is the one the free scanner would give the policy enforced, with its findings. The draft is served report-only, which the rubric caps, so the grade says what the policy is worth once you enforce it.
4. Deploy the draft report-only
Section titled “4. Deploy the draft report-only”Once nothing is left to decide, the top of the page becomes Deploy your
policy in report-only, with the block of response headers to serve: the
draft as a Content-Security-Policy-Report-Only header that reports to the
site’s ordinary endpoint, not the discovery one, beside the other headers
your site has switched on. It is the block the site’s setup panel shows.
Open in the header builder, beside the policy, opens it in the free
report header builder,
where you can edit it and get the lines for nginx, Apache, Vercel, Netlify or
Node.js.
Once it is deployed, press Check my page, under the block: HeaderHawk fetches one of the site’s pages, and once it finds the draft served, the report-only week starts, even if the policy is so close a fit that no browser ever reports against it. A report quoting the draft counts the same way, and so does the daily scan of a monitored page on the Professional and Business plans. If your pages serve a policy other than the draft, say one you edited in the header builder, the panel says so and lists how it differs from the draft: serve the draft, or change its decisions to match yours, to start its week (A different policy served).
Its violations arrive in Policy › Issues like any other policy’s. A quiet Issues view on the pages discovery saw is the evidence that the draft fits; anything it lists is a source the week did not see, or an inline block that still needs a nonce or a hash.
The draft’s policy replaces the discovery line: serve one or the other,
not both. Once your pages serve the draft, no more discovery reports arrive,
and the headerhawk-discovery endpoint in Reporting-Endpoints can go too.
The draft stays, read from the run’s reports.
5. Enforce
Section titled “5. Enforce”The run stops by itself on its end date. Once it has, Start a new run, in the help beside the run’s heading, begins a new one from that moment, for a site that has changed enough to need it. The new run’s sources replace this one’s in the draft, and your keep and drop decisions stay.
When Issues has been quiet for long enough to trust, rename the header from
Content-Security-Policy-Report-Only to Content-Security-Policy. Keep a
report-only copy beside it if you want to try the next change first.