SodiumNonceLengthMismatch
warning native on by default quick-fix PHP ≥ 7.2Group: Probable bugs
Secretbox requires its defined nonce byte length. Construct the nonce using the extension's required-length constant.
Example
php
<?php
$c=sodium_crypto_secretbox($message,random_bytes(12),$key);php
<?php
$c=sodium_crypto_secretbox($message,random_bytes(\SODIUM_CRYPTO_SECRETBOX_NONCEBYTES),$key);Reported:
- line 2: Use the required secretbox nonce length.
Detection
- Resolved sodium_crypto_secretbox second argument has known byte length other than 24. Recognize literal strings, str_repeat of literal strings and literal counts, and random_bytes of a positive literal count. Highlight nonce.
- Resolve builtin functions, classes, constants and methods; user-defined lookalikes are excluded. Follow only bounded local proof in one lexical scope.
- Emit one finding per violating operation, not per supporting evidence statement. When the proof is inconclusive, nothing is reported.
Exceptions (no report)
- Do not report unresolved or shadowed builtin names, unknown values, or incomplete receiver/type provenance. Unknown calls, aliases escaping, and intervening writes invalidate local proof. Similar code outside the stated detection contract is not a finding.
- Both standard suppression forms suppress this native ID.
Fix
F1. When the nonce is a direct resolved random_bytes call with a positive literal length, replace only that length node with \SODIUM_CRYPTO_SECRETBOX_NONCEBYTES. Preserve comments and argument formatting outside the literal span. Do not fix stored random-byte results, dynamic or variable counts, string literals, or other nonce constructors.
PHP versions
Requires PHP 7.2 or later.
Configure
In custos.json:
json
{
"rules": {
"SodiumNonceLengthMismatch": {
"enabled": false
}
}
}Suppress
Before the statement or declaration (or the first statement of the file):
php
// @custos-ignore SodiumNonceLengthMismatch