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
What it reports
Section titled “What it reports”A credential written as a string literal without interpolation (adjacent literals are joined):
- 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. - A secret-like value bound to a credential name, such as
password,token,apiKeyorsecretKey. - The value of an
Authorizationheader:Bearer <t>orBasic <t>, with<t>secret-like. - Key material: the text of package:encrypt’s
Key.fromUtf8, a key of zeros, or the key of package:crypto’sHmacwritten 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…apiKeyof a Firebase app infirebase_options.dart, Stripe’s publishablepk_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.
Example
Section titled “Example”const sendGridApiKey = '…'; // a real keyfinal key = Key.fromUtf8('my 32 length key................');const sendGridApiKey = String.fromEnvironment('SENDGRID_API_KEY');// flutter build apk --dart-define-from-file=secrets.jsonRule parameters
Section titled “Rule parameters”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. |
Suppressing it
Section titled “Suppressing it”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_credentialconst 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.
Details
Section titled “Details”All cases
What it reports in full
Section titled “What it reports in full”- 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
AuthorizationorProxy-Authorization, in any case. - Key material is:
- the text of package:encrypt’s
Key.fromUtf8,Key.fromBase64andKey.fromBase16; - a key of zeros,
Key.allZerosOfLength(n).Key.fromLength(n)is random since encrypt 5.0.2, asKey.fromSecureRandom(n)is, and is not reported. - the key of package:crypto’s
Hmacgiven as a list of literal numbers,utf8.encode('<literal>')or'<literal>'.codeUnits.
- the text of package:encrypt’s
The credential shapes
Section titled “The credential shapes”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).
The credential names
Section titled “The credential names”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)orapikey; - last two words are
api key,private key,secret key,access key,signing key,encryption key,master keyorsession id.
These are not credential names:
- a
tokenafterfcm,push,device,apns,registration,messaging,notification,page,next page,continuation,sync,cancel,mixpanelormixpanel project; - a
pinaftercertificate,cert,ssl,tls,spkiorpublic key: it pins the server a connection may reach; - an
api keyaftersearchorsearch only: search services make their search-only keys for client code.
When a value is secret-like
Section titled “When a value is secret-like”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 asuserAccessTokenKey); - 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.
Keys that are public by design
Section titled “Keys that are public by design”Not reported:
- Google’s
AIza…of 39 characters or more of its alphabet: theapiKeyof a Firebase app infirebase_options.dart, also one that an example runs on past the 39; - Stripe’s publishable
pk_live_…andpk_test_…; - RevenueCat’s public SDK keys
appl_…,goog_…,amzn_…andstrp_…; - Mapbox’s public
pk.…tokens; - PostHog’s project tokens
phc_…(“It is ok for your project token (starts withphc_) to be public”, posthog.com/docs/privacy). Its personal keysphx_…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.
Security class
Section titled “Security class”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.