Skip to content

A secret key in the page's own scripts

secrets-in-javascriptserious
On this page

Why it matters

  • Everything in a page's HTML reaches every visitor, crawler and archive. A secret key there is published: anyone can copy it and act with your account, charge payments, read data, send email or run up bills.
  • OWASP's Secrets Management cheat sheet says secrets do not belong in code that is shipped. Each provider says the same of its secret keys: they stay on the server.
  • Keys end up in pages by accident: a configuration copied from a server file, a test that went live, a plugin that prints its settings.

How to fix

Revoke the key first: it is public already, and removing it from the page does not take it back. Then:

  • Make a new key and keep it on the server, in an environment variable or a secrets manager.
  • Let the page call your server, and your server call the provider with the key.
  • Where the provider has a publishable key for browsers (Stripe's pk_live_, a Google Maps key restricted to your site), use that one in the page.
html
<script>
  window.checkout = { key: "pk_live_…" }
</script>

How we detect

  1. We read the page's inline scripts that run (no src, a JavaScript type or module); data blocks such as JSON are left out.
  2. We match the formats providers document, each with a prefix no other string has: Stripe secret and restricted keys, GitHub tokens, Slack tokens, OpenAI and Anthropic API keys, SendGrid API keys, an AWS secret access key next to its name, and private keys in PEM.
  3. A finding names the kind of key and shows its prefix and length, never the key.

Script files the page loads are not read here, nor keys a script fetches. Publishable keys, meant for browsers, are not reported.