knownprintDocs

Install

Content Security Policy

The Content Security Policy directives a page needs to run the knownprint agent, for the script tag loader and for the ES module build.

If your site sends a Content-Security-Policy header, it must allow the knownprint agent. The agent needs three directives and nothing else: no inline code, no eval, no styles, images, fonts or frames of its own.

The directives

A page that loads the agent with the script tag /kp/agent.js (see Add the browser agent) needs these directives. The examples on this page write the knownprint host your account names as https://na1.knownprint.com, the host accounts name today; use the one your account names:

Text
script-src 'self';
connect-src 'self' https://na1.knownprint.com;
worker-src blob:;
DirectiveValueWhat needs it
script-src'self'The loader /kp/agent.js, and the agent file it adds to the page from /kp/a/ on your origin.
connect-src'self'The agent's requests to your knownprint path, /kp/ on your origin.
connect-srcThe knownprint host your account names (today https://na1.knownprint.com)Required; without it the integration is not supported.
worker-srcblob:Required; without it the integration is not supported.

These go into your existing policy beside your own sources. A complete policy for a site that serves everything from its own origin could read:

Text
default-src 'self'; script-src 'self'; connect-src 'self' https://na1.knownprint.com; worker-src blob:; object-src 'none'; base-uri 'none'; frame-ancestors 'none'

The agent works under a policy as strict as default-src 'none' plus the three directives above. Nothing it does needs 'unsafe-inline', 'unsafe-eval', style-src, img-src, font-src or frame-src.

When you use another endpoint

'self' covers any path on your origin, so a proxy rule on another path (for example /fraud-check/ with endpoint: '/fraud-check') needs no change. If your policy lists paths rather than 'self', allow the endpoint's path in both script-src and connect-src, for example https://www.example.com/fraud-check/.

If worker-src is missing

A policy without worker-src falls back to child-src, then script-src, then default-src for workers. If none of them allows blob:, the browser blocks the agent's blob: worker, and the integration is not supported. Add worker-src blob: rather than adding blob: to script-src: it allows workers from blob: URLs and nothing else.

Your call to load()

The call to knownprint.load() is your own code, and your policy treats it like any other script:

  • In an external file served from your origin (as in the examples on Add the browser agent), script-src 'self' already allows it.
  • In an inline <script>, it needs a nonce or a hash under any policy without 'unsafe-inline'.
Inline call with a nonce
<script src="/kp/agent.js" nonce="r4nd0mN0nc3" defer></script>
<script nonce="r4nd0mN0nc3">
  document.addEventListener('DOMContentLoaded', () => {
    window.knownprint?.load({ pk: 'pk_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' }).catch(() => {});
  });
</script>

Your server draws a new random nonce for every response and sends the same value in the header, for example script-src 'nonce-r4nd0mN0nc3' 'self'.

Nonce and strict-dynamic policies

Many sites use a strict policy built on nonces. The loader adds the agent file to the page itself, without a nonce, so the policy must allow that file in one of two ways:

Policy stylescript-srcNotes
Nonce with 'strict-dynamic''nonce-{random}' 'strict-dynamic'Put the nonce on the /kp/agent.js tag. 'strict-dynamic' lets a script with the nonce add the agent file. Browsers that support 'strict-dynamic' ignore 'self' and host sources.
Nonce without 'strict-dynamic''nonce-{random}' 'self''self' allows the agent file, which comes from your origin.

In both cases connect-src and worker-src stay as in The directives.

A hash-only policy cannot pin the agent: its files change with each agent release. Allow your knownprint path by 'self' or by path instead.

Text
script-src 'nonce-r4nd0mN0nc3' 'strict-dynamic';
connect-src 'self' https://na1.knownprint.com;
worker-src blob:;
object-src 'none';
base-uri 'none'

The ES module build

With the ES module build the agent is part of your own JavaScript bundle and there is no /kp/agent.js:

DirectiveValueWhat needs it
script-srcwhatever already serves your bundleThe agent is inside your bundle.
connect-src'self'The agent's requests to your knownprint path.
connect-srcThe knownprint host your account names (today https://na1.knownprint.com)Required; without it the integration is not supported.
worker-srcblob:Required; without it the integration is not supported.

What happens when a directive is missing

The agent never breaks your page. A missing directive shows up as a policy violation in the browser's console and in your violation reports, and as one of these:

MissingResult
script-src for /kp/agent.jswindow.knownprint is not defined.
script-src for the agent file under /kp/a/load() rejects with load_failed.
connect-src 'self' (or your endpoint's path)identify() rejects with network.
connect-src for the knownprint host your account namesThe integration is not supported.
worker-src blob:The integration is not supported.

In the first three cases you get no request id, and your backend treats the action as unknown (No request id means unknown). The last two are required configuration, so check your whole policy before you go live.

Other policies that affect the agent

  • sandbox. A page served with the sandbox directive and without allow-same-origin has an opaque origin. load() without an endpoint then rejects with invalid_options, and knownprint refuses requests from an opaque origin. Serve pages that run the agent without sandbox, or with allow-scripts allow-same-origin.
  • Trusted Types. The agent does not create a Trusted Types policy. A page that enforces require-trusted-types-for 'script' needs a default policy that lets through the agent file's URL under your knownprint path and the agent's blob: worker URLs.
  • Allowed origins. Not a browser policy, but often confused with one: knownprint accepts identifications only from the origins listed on your public key. A page on an origin that is not listed gets origin_not_allowed. See API keys.

Roll out safely

Before you enforce a changed policy, send it as Content-Security-Policy-Report-Only for a while and watch the reports. A violation report names the directive and the blocked URL.

nginx
add_header Content-Security-Policy-Report-Only "default-src 'self'; script-src 'self'; connect-src 'self' https://na1.knownprint.com; worker-src blob:; object-src 'none'; base-uri 'none'; report-uri /csp-reports" always;
Caddy
header Content-Security-Policy-Report-Only "default-src 'self'; script-src 'self'; connect-src 'self' https://na1.knownprint.com; worker-src blob:; object-src 'none'; base-uri 'none'; report-uri /csp-reports"

To see violations while you test, open the browser's developer tools on a page that runs the agent, call identify(), and read the console. Or log them from the page:

JavaScript
document.addEventListener('securitypolicyviolation', (event) => {
  console.warn('CSP', event.effectiveDirective, event.blockedURI);
});

A violation inside a Web Worker appears in the console but not as this event in the page. A clean run shows no violation from the page or the worker, and identify() resolves a request id.

Then add the go-live checklist to your release.