Security rules

NGB018: Hardcoded frontend secrets

Reports string literals in TypeScript sources and JSON configuration (environment files, src/assets, public) that match the format of a private credential (Stripe secret keys, AWS access key IDs, GitHub/Slack/npm/SendGrid/OpenAI/Anthropic tokens, PEM private keys, JWTs) or high-entropy values assigned to names such as clientSecret, apiSecret, password or privateKey.

Property Value
Rule ID NGB018
Category Sensitive data & payment security
Default severity high
CWE CWE-798
Built-in presets recommended: error, strict: error, payments: error

Confidence is reported per finding (high, medium, low) and is independent of severity. See Reading findings.

What it checks

Reports string literals in TypeScript sources and JSON configuration (environment files, src/assets, public) that match the format of a private credential (Stripe secret keys, AWS access key IDs, GitHub/Slack/npm/SendGrid/OpenAI/Anthropic tokens, PEM private keys, JWTs) or high-entropy values assigned to names such as clientSecret, apiSecret, password or privateKey. Reported values are always redacted.

Why it matters

Frontend bundles are public. Any secret compiled into them can be extracted with the browser developer tools and used to call the backing service with your privileges, often for a long time because frontend secrets are rarely rotated.

Detection behavior and supported patterns

  • Known private formats: sk_live_ / rk_live_ (critical), sk_test_ (low severity), AKIA…, ghp_ / github_pat_, xox[baprs]-, -----BEGIN … PRIVATE KEY-----, sk-ant-, sk-proj-, SG.…, npm_…, eyJ….eyJ….… (JWT)
  • Generic: literal assigned to a variable/property whose name ends in secret, password, privateKey, clientSecret, apiSecret, accessToken, … with length ≥ 16, no whitespace and Shannon entropy ≥ 3 bits/char (≥ 4 → medium confidence)
  • Not reported: Stripe publishable keys (pk_…), Google/Firebase browser API keys (AIza…), OAuth client IDs, names containing public/publishable/clientId/siteKey, placeholders (changeme, <…>, ${…}, your-…, example, dummy, test…)

Example

TypeScript
export const environment = { paymentsSecretKey: 'sk_live_…' };

Safer alternative

TypeScript
export const environment = { paymentsPublishableKey: 'pk_live_…' }; // secret key stays on the server

Remediation

Remove the secret from the frontend, rotate it immediately, and move the privileged call behind your backend (or a backend-for-frontend). Only publishable/public identifiers belong in Angular environment files.

False-positive considerations

  • Generated identifiers with high entropy assigned to a secret-named property in mocks or demo data.
  • Test-mode keys are reported with low severity because they still grant access to test data.

Known limitations

  • Secrets split across several literals, encoded (base64) or fetched at runtime are not detected.
  • Only JSON files matching the configured globs are inspected; .env files are not read (they are not bundled by the Angular CLI).
  • Whether a key is actually privileged cannot be verified offline; validity is never checked against the provider.

References

Run it

Shell
ngbastion explain NGB018