From 6951876c131a3cf72e75cb3de66c1706c32cce4d Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Fri, 7 Aug 2026 11:25:15 +0200 Subject: [PATCH] :bug: Use constant-time comparison for shared key authentication (#11122) Replace standard '=' operator with MessageDigest/isEqual to prevent timing attacks on shared key authentication middleware. Closes #11121 AI-assisted-by: qwen3.7-plus --- backend/src/app/http/middleware.clj | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/backend/src/app/http/middleware.clj b/backend/src/app/http/middleware.clj index fa2faa8a55..31b96927a6 100644 --- a/backend/src/app/http/middleware.clj +++ b/backend/src/app/http/middleware.clj @@ -24,7 +24,8 @@ (:import io.undertow.server.RequestTooBigException java.io.InputStream - java.io.OutputStream)) + java.io.OutputStream + java.security.MessageDigest)) (set! *warn-on-reflection* true) @@ -329,6 +330,11 @@ {:name ::auth :compile (constantly wrap-auth)}) +(defn- constant-time-eq? + "Compare strings in constant time to prevent timing attacks." + [^String a ^String b] + (MessageDigest/isEqual (.getBytes a "UTF-8") (.getBytes b "UTF-8"))) + (defn- wrap-shared-key-auth [handler keys] (if (seq keys) @@ -338,7 +344,7 @@ (let [key-id (-> key-id str/lower keyword)] (if (and (string? key) (contains? keys key-id) - (= key (get keys key-id))) + (constant-time-eq? key (get keys key-id))) (-> request (assoc ::http/auth-key-id key-id) (handler))