From a494130eea0c8d73c370ea9d8fe7b5a3e3c6eaca Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Thu, 2 Jul 2026 03:31:26 +1200 Subject: [PATCH] refactor(cache): match the NOSCRIPT error code, not a substring MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Redis error replies carry no numeric code (getCode() is 0 on both the raw socket and php-redis); the code is the reply's leading token — "NOSCRIPT No matching script." Match that token via NoScript::matches() instead of a substring anywhere in the message, so echoed key/value text containing the word can't false-trigger the EVAL fallback. Centralizes the literal and the NoScript construction (NoScript::from) across both adapters. Addresses review. Co-Authored-By: Claude Opus 4.8 --- src/Cache/Adapter/Redis.php | 16 +++++---- src/Cache/Adapter/Redis/Multiplexing.php | 6 ++-- src/Cache/Adapter/Redis/NoScript.php | 25 +++++++++++++++ tests/Cache/Unit/Redis/NoScriptTest.php | 41 ++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 10 deletions(-) create mode 100644 tests/Cache/Unit/Redis/NoScriptTest.php diff --git a/src/Cache/Adapter/Redis.php b/src/Cache/Adapter/Redis.php index 1df070895b..e457d55790 100644 --- a/src/Cache/Adapter/Redis.php +++ b/src/Cache/Adapter/Redis.php @@ -200,20 +200,22 @@ class Redis extends Leasable implements Adapter, Retryable try { $result = $this->redis->evalSha($sha, [$key, ...$args], 1); } catch (\RedisException $e) { - if (\str_contains($e->getMessage(), 'NOSCRIPT')) { - throw new NoScript($e->getMessage(), 0, $e); + // php-redis may throw NOSCRIPT rather than returning false. + if (NoScript::matches($e->getMessage())) { + throw NoScript::from($e); } throw $e; } - // php-redis reports a script error as false + getLastError() rather - // than a throw. A lease script always returns an int on success, so - // false is an error; NOSCRIPT means leaseRun() should resend the body. - if ($result === false && \str_contains((string) $this->redis->getLastError(), 'NOSCRIPT')) { + // ...or it reports the same error as false + getLastError(). A lease + // script always returns an int on success, so false is an error to + // inspect; NOSCRIPT means leaseRun() should resend the body. + $error = (string) $this->redis->getLastError(); + if ($result === false && NoScript::matches($error)) { $this->redis->clearLastError(); - throw new NoScript('NOSCRIPT'); + throw NoScript::from($error); } return $result; diff --git a/src/Cache/Adapter/Redis/Multiplexing.php b/src/Cache/Adapter/Redis/Multiplexing.php index 45a9b2a9d6..3f4ac90d2e 100644 --- a/src/Cache/Adapter/Redis/Multiplexing.php +++ b/src/Cache/Adapter/Redis/Multiplexing.php @@ -183,9 +183,9 @@ class Multiplexing extends Leasable implements Adapter, TelemetryFeature } catch (\RedisException $e) { // NOSCRIPT is a server reply, not a connection fault: signal leaseRun() // to resend the body. ConnectionException (a RedisException subclass) - // never carries this message, so reconnect handling is unaffected. - if (\str_contains($e->getMessage(), 'NOSCRIPT')) { - throw new NoScript($e->getMessage(), 0, $e); + // never carries this code, so reconnect handling is unaffected. + if (NoScript::matches($e->getMessage())) { + throw NoScript::from($e); } throw $e; diff --git a/src/Cache/Adapter/Redis/NoScript.php b/src/Cache/Adapter/Redis/NoScript.php index 3c830cc0c2..90d395e561 100644 --- a/src/Cache/Adapter/Redis/NoScript.php +++ b/src/Cache/Adapter/Redis/NoScript.php @@ -10,4 +10,29 @@ namespace Utopia\Cache\Adapter\Redis; */ final class NoScript extends \RuntimeException { + /** + * RESP error code for an EVALSHA cache miss. Redis error replies carry no + * numeric code (getCode() is 0 on both the raw socket and php-redis), so the + * code is the reply's leading token — "NOSCRIPT No matching script." — the + * same across the raw RESP frame and php-redis getLastError(). + */ + private const CODE = 'NOSCRIPT'; + + /** + * True when a Redis error string's code (its leading token) is NOSCRIPT. + * Matching the token, not a substring, avoids false positives on echoed key + * or value text that merely contains the word. + */ + public static function matches(string $error): bool + { + return \explode(' ', $error, 2)[0] === self::CODE; + } + + /** Build the signal from the underlying Redis error (exception or message). */ + public static function from(\Throwable|string $reason): self + { + return $reason instanceof \Throwable + ? new self($reason->getMessage(), 0, $reason) + : new self($reason); + } } diff --git a/tests/Cache/Unit/Redis/NoScriptTest.php b/tests/Cache/Unit/Redis/NoScriptTest.php new file mode 100644 index 0000000000..b0b593fdce --- /dev/null +++ b/tests/Cache/Unit/Redis/NoScriptTest.php @@ -0,0 +1,41 @@ +assertTrue(NoScript::matches('NOSCRIPT No matching script. Please use EVAL.')); + $this->assertTrue(NoScript::matches('NOSCRIPT')); + } + + public function testDoesNotMatchWhenNoScriptIsNotTheCode(): void + { + // A substring check would wrongly trip on echoed key/value text or a + // different error code — the code is only ever the leading token. + $this->assertFalse(NoScript::matches('ERR user set key to "NOSCRIPT"')); + $this->assertFalse(NoScript::matches('WRONGTYPE Operation against a key holding the wrong kind of value')); + $this->assertFalse(NoScript::matches('')); + } + + public function testFromExceptionPreservesMessageAndCause(): void + { + $cause = new \RedisException('NOSCRIPT No matching script. Please use EVAL.'); + $signal = NoScript::from($cause); + + $this->assertSame($cause->getMessage(), $signal->getMessage()); + $this->assertSame($cause, $signal->getPrevious()); + } + + public function testFromStringCarriesMessage(): void + { + $signal = NoScript::from('NOSCRIPT No matching script. Please use EVAL.'); + + $this->assertSame('NOSCRIPT No matching script. Please use EVAL.', $signal->getMessage()); + $this->assertNull($signal->getPrevious()); + } +}