Skip to content

hard_coded_credential

Reports a credential written into the source as a string literal. Keep the secret on your backend, or pass it in at build time.

Tier
blocking
Scope
Dart
Lowest preset
recommended
Fix
none
Security class
CWE-798, CWE-321 · mobile risk M1 (improper credential usage) · severity 7.8

A credential written as a string literal without interpolation (adjacent literals are joined):

  1. A text of a credential shape, wherever it is: a format that is secret by itself, such as a PEM private key, a GitHub token or a Stripe sk_live_… key.
  2. A secret-like value bound to a credential name, such as password, token, apiKey or secretKey.
  3. The value of an Authorization header: Bearer <t> or Basic <t>, with <t> secret-like.
  4. Key material: the text of package:encrypt’s Key.fromUtf8, a key of zeros, or the key of package:crypto’s Hmac written as literals.

The message names the kind of secret and the name it is bound to, never its value.

It does not report:

  • keys that are public by design, such as the AIza… apiKey of a Firebase app in firebase_options.dart, Stripe’s publishable pk_live_…, or RevenueCat’s public SDK keys;
  • Stripe’s test keys (sk_test_…) and AWS access-key identifiers;
  • values read from the environment (String.fromEnvironment, --dart-define), which are not literals;
  • debug-only code, and the test side.

Whatever is in the app is in every copy of it. An APK or an IPA unpacks in a minute, and string literals survive any obfuscation. A secret there is no longer a secret, and you cannot rotate it without a release. A hard-coded encryption key makes the encryption decorative.

Violating
const sendGridApiKey = '…'; // a real key
final key = Key.fromUtf8('my 32 length key................');
Correctedkeep the secret on your backend and have the app ask for what it may do. For a key the app needs at build time and that is not a secret of yours, pass it from a file outside version control
const sendGridApiKey = String.fromEnvironment('SENDGRID_API_KEY');
// flutter build apk --dart-define-from-file=secrets.json

Set under the assay: key of the project’s root analysis_options.yaml.

Rule parameter Default Effect
enabled true false switches the rule off for the whole project.
exclude [] Path globs where the rule is silent.

Keep a key that is public by design with a justified suppression. Write why on the line directly above it:

// The public key of the maps SDK, restricted to the app's bundle id.
// ignore: hard_coded_credential
const mapsApiKey = '…';

// ignore_for_file: assay/hard_coded_credential silences it in a whole file, and // ignore: assay/hard_coded_credential with the prefix works too. See Suppressions.

All cases
  • The violation is on the literal, or for a key of zeros on the creation.
  • A value is bound to a credential name as the initializer of a variable or a field, the right side of an assignment, a named argument, a parameter’s default value, or the value of a map entry or of an index under a string key.
  • The header may be Authorization or Proxy-Authorization, in any case.
  • Key material is:
    • the text of package:encrypt’s Key.fromUtf8, Key.fromBase64 and Key.fromBase16;
    • a key of zeros, Key.allZerosOfLength(n). Key.fromLength(n) is random since encrypt 5.0.2, as Key.fromSecureRandom(n) is, and is not reported.
    • the key of package:crypto’s Hmac given as a list of literal numbers, utf8.encode('<literal>') or '<literal>'.codeUnits.

A private key in the PEM format (with its body and its last line), GitHub gh[pousr]_… and github_pat_…, GitLab glpat-…, Slack xox[abposr]-… and its webhook URLs, Discord webhook URLs, Stripe sk_live_… and rk_live_…, Google OAuth GOCSPX-…, the legacy server keys of Firebase Cloud Messaging, OpenAI sk-proj-…, Anthropic sk-ant-…, SendGrid SG.….…, npm npm_…, PyPI tokens, Shopify shp(at|ss|ca|pa)_…, Square sq0csp-…, Telegram bot tokens, Hugging Face hf_…, and a JSON web token whose payload says "role":"service_role" (Supabase’s service key).

Names are split into words at case changes, _, -, . and digits. A credential name is one whose:

  • last word is password, passwd, pwd, passphrase, passcode, pin, otp, secret, token, credential(s) or apikey;
  • last two words are api key, private key, secret key, access key, signing key, encryption key, master key or session id.

These are not credential names:

  • a token after fcm, push, device, apns, registration, messaging, notification, page, next page, continuation, sync, cancel, mixpanel or mixpanel project;
  • a pin after certificate, cert, ssl, tls, spki or public key: it pins the server a connection may reach;
  • an api key after search or search only: search services make their search-only keys for client code.

A value is secret-like when all of these hold:

  • it has 8 to 1000 characters and no white space;
  • it is not a template ($, {{, %s), a URL, a path, an e-mail address, a file name or a dotted identifier;
  • it is not the name of a key (auth_token, refresh-token, AUTH_TOKEN, or camelCase and PascalCase names such as userAccessTokenKey);
  • it is not a one-word label in a natural language, with a letter beyond ASCII, words joined by - or ' included (Passwortbestätigung), with or without a final ?, !, . or :;
  • it is not the name it is bound to;
  • it holds no placeholder word (your, xxx, example, sample, dummy, placeholder, changeme, todo, fixme, insert, replace, redacted, ***), and is not one character repeated;
  • it has a digit, or 16 characters or more with both cases or the punctuation of base64;
  • it is not a key that is public by design.

Not reported:

  • Google’s AIza… of 39 characters or more of its alphabet: the apiKey of a Firebase app in firebase_options.dart, also one that an example runs on past the 39;
  • Stripe’s publishable pk_live_… and pk_test_…;
  • RevenueCat’s public SDK keys appl_…, goog_…, amzn_… and strp_…;
  • Mapbox’s public pk.… tokens;
  • PostHog’s project tokens phc_… (“It is ok for your project token (starts with phc_) to be public”, posthog.com/docs/privacy). Its personal keys phx_… are secret and stay reported under a credential name.
  • the pin of a certificate, sha256/ and 44 characters of base64, under any name;
  • every other JSON web token, such as Supabase’s anonymous key;
  • Mixpanel’s project token under a name of it, mixpanelToken (“A project’s token is not a secret value”, docs.mixpanel.com/reference/project-token);
  • a search-only API key under a name that says so, algoliaSearchApiKey (Algolia’s search-only key “is safe to use in your production frontend code”, algolia.com/doc/guides/security/api-keys).

Nor are obfuscated values, the lists of numbers that a generator writes. Stripe’s test keys and AWS access-key identifiers are not reported because they are not shapes.

A key that is public by design in another shape, such as another SDK’s public key, is reported where it is bound to a credential name. So is Algolia’s search-only key passed as the apiKey: of its clients (SearchClient, HitsSearcher), since its Admin API key has the same form and goes to the same argument, and a Mixpanel project token under a name that does not say mixpanel. Keep such a key under a name that says what it is, such as algoliaSearchApiKey, or with a justified suppression.

In SARIF, the rule’s descriptor carries the tags security, external/cwe/cwe-798, external/cwe/cwe-321 and external/owasp-mobile-2024/m1, and the security-severity 7.8. No other report changes. See Security.