Advisory

GHSA-38p6-h87p-r4cg

Details, part 2 of 2. The advisory

Details

Impact

verifyNonce() is Grav's documented core primitive for CSRF protection, used directly by core and referenced by the plugin ecosystem, including the Form plugin and Admin plugin, both outside this repository. Because the comparison is not constant time, an attacker in a position to send many requests and measure response timing with enough precision could in principle recover a valid nonce byte by byte rather than needing to guess the full 32 character value at once, weakening the CSRF protection below its intended security margin. The practical difficulty of pulling this off over a real network, given millisecond scale jitter against a nanosecond scale signal, is high, which is why I am reporting this as a hardening issue rather than claiming a demonstrated working exploit against a live site.

Suggested fix

Replace the two === comparisons in verifyNonce() with hash_equals(), matching the pattern already used in Session.php, FileCache.php, Scheduler.php, and JobQueue.php:

public static function verifyNonce($nonce, $action)
{
    if (is_array($nonce)) {
        $nonce = array_shift($nonce);
    }

    if (!is_string($nonce)) {
        return false;
    }

    if (hash_equals(self::getNonce($action), $nonce)) {
        return true;
    }

    return hash_equals(self::getNonce($action, true), $nonce);
}

hash_equals() also correctly requires the first argument to be a string, so the existing implicit array-to-string edge cases are worth double checking when you make this change.

===========================================================
CWE FIELD
===========================================================
CWE-208, Observable Timing Discrepancy

===========================================================
CVSS CALCULATOR SELECTIONS (v3.1)
===========================================================
Attack Vector: Network
Attack Complexity: High
Privileges Required: None
User Interaction: None
Scope: Unchanged
Confidentiality: None
Integrity: Low
Availability: None

Resulting vector: CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:U/C:N/I:L/A:N
Resulting score: 5.3, severity Medium

Note for the maintainer: Attack Complexity is set to High because, as shown above, the measured timing signal at the real nonce length is small, on the order of a few nanoseconds, so reliable remote exploitation would require substantial statistical averaging and a favorable network position. If your own testing shows this is easier to exploit against a real deployment than my local measurement suggests, please rescore Attack Complexity to Low.

===========================================================
SEVERITY FIELD
===========================================================
Moderate

Sources & licences