Skip to content

weak_random_for_secret

Reports Random() of dart:math making a token, a password, a nonce or another secret. Use Random.secure() instead.

Tier
advisory
Scope
Dart
Lowest preset
recommended
Fix
Random() → Random.secure()
Security class
CWE-338 · mobile risk M10 (insufficient cryptography) · severity 5.9

A dart:math generator made by Random() or Random(seed) whose values make a secret.

A value of it is one that nextInt, nextDouble or nextBool draws from a generator that is:

  • made in place: Random().nextInt(…);
  • or held by a local variable, a final field or a final top-level variable of the file that Random() or Random(seed) makes.

It makes a secret where:

  • the function that draws it is named for one, and the value reaches what it returns. The function’s last word is a credential word (password, pin, otp, secret, token, …, see hard_coded_credential) or nonce, salt, iv, verifier; or its last two words are session id, invite code, reset code or verification code. Examples: generateToken, _randomPassword, createNonce, a getter inviteCode.
  • inside that function, the value reaches a value bound to such a name: a variable’s initializer, the right side of an assignment, a named argument, the value of a map entry under a string key;
  • it initializes a top-level variable or a field whose own name names a secret.

“Reaches” means within the function, through:

  • the SDK calls that turn it into text: picking letters by index, List.generate, String.fromCharCodes, join, toString, padLeft;
  • a local variable the function writes it into, by an assignment (a compound one too, token += …) or an SDK method called on the variable (buffer.write(…), letters.add(…)). A read of that variable then holds the value: return buffer.toString();.

There is one violation per function. It covers the making of the generator (Random()) where the function makes it, and otherwise the first value drawn, as for a generator kept in a field. The message names the kind of secret.

It does not report:

  • Random.secure();
  • values that make no secret: a colour, a delay, a shuffle, a widget key. That includes a retry delay drawn in a function named for a secret that does not return it: Future<String> refreshToken() waiting 100 * attempt + Random().nextInt(100) milliseconds.
  • a name whose last word is no secret: tokenCount, passwordLength;
  • a generator passed in as a parameter, whose making is not seen;
  • a value passed to your own code first, _letterOf(random.nextInt(26)), where the flow ends;
  • debug-only code, such as code under kDebugMode, and tests.

Random() is a fast generator for games and animations, not for secrets. Its next values follow from the ones before, and a generator without a seed is seeded from the clock. Whoever sees a few tokens, or knows roughly when one was made, can compute the others: a session, a reset code, a sign-in nonce.

Random.secure() draws from the operating system’s cryptographic generator, with the same interface.

Violating
String generateToken() {
final random = Random();
return List.generate(32, (_) => _letters[random.nextInt(_letters.length)]).join();
}
Corrected
String generateToken() {
final random = Random.secure();
return List.generate(32, (_) => _letters[random.nextInt(_letters.length)]).join();
}

assay fix and the editor write Random.secure() for a Random() without a seed that the function makes, keeping any import prefix: math.Random.secure(). Nothing can rely on the values of an unseeded generator, and the type stays the same.

There is no fix for a seeded Random(seed), whose sequence code may rely on, or for a generator kept in a field, whose violation is at the first value drawn.

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.

In SARIF, the rule’s descriptor carries the tags security, external/cwe/cwe-338 and external/owasp-mobile-2024/m10, and the security-severity 5.9. No other report changes. See Security.

Write why on the line directly above the suppression:

// A placeholder name for a guest list, shown to the user; no secret.
// ignore: assay/weak_random_for_secret

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