#8779: fix(security): use constant-time comparison for token validation
gateway
stale
Fixes #6021
## Summary
Replaces insecure `===` / `!==` string comparisons with constant-time comparison `timingSafeEqual` (via a helper) for sensitive token validation.
## Changes
- Export `safeEqual` from `src/gateway/auth.ts`.
- Use `safeEqual` in `src/gateway/server-http.ts` (hook tokens).
- Use `safeEqual` (local copy) in `src/infra/node-pairing.ts` (node tokens).
- Use `safeEqual` (local copy) in `src/infra/device-pairing.ts` (device tokens).
## Security
Prevents timing side-channel attacks that could allow attackers to guess tokens character-by-character.
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR introduces a `safeEqual` helper based on `crypto.timingSafeEqual` and replaces `===`/`!==` comparisons for gateway/password tokens and hook tokens, plus similar changes for node/device pairing token verification.
The overall direction is good (mitigates character-by-character timing attacks on secret comparisons), and the change is localized to the existing auth and pairing verification paths (`src/gateway/*` and `src/infra/*`).
<h3>Confidence Score: 4/5</h3>
- This PR is largely safe to merge and improves secret comparison security, with a small edge-case risk around input length/encoding handling.
- Changes are narrowly scoped to token comparisons and rely on a standard primitive (`timingSafeEqual`). The main concern is the helper’s early-return on string length and UTF-16 vs UTF-8 length mismatch potentially causing a throw for non-ASCII inputs; if tokens are guaranteed ASCII/UUIDs this is likely fine.
- src/gateway/auth.ts, src/infra/node-pairing.ts, src/infra/device-pairing.ts
<!-- greptile_other_comments_section -->
<sub>(3/5) Reply to the agent's comments like "Can you suggest a fix for this @greptileai?" or ask follow-up questions!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#14197: fix(security): harden browser API auth, token comparisons, and hook...
by leecarollyn-gif · 2026-02-11
84.9%
#15722: fix: prefer explicit token over stored device token for remote gate...
by 0xPotatoofdoom · 2026-02-13
77.0%
#22227: fix(security): harden gateway auth — audit logging, pairing, mode v...
by novalis133 · 2026-02-20
76.3%
#16310: fix(ws-connection): skip device pairing when client authenticates w...
by nawinsharma · 2026-02-14
76.3%
#21651: fix(gateway): token fallback + operator.admin scope superset in pai...
by lan17 · 2026-02-20
75.6%
#22766: fix(security): enable gateway auth rate limiting by default (CWE-307)
by brandonwise · 2026-02-21
75.5%
#7654: feat(security): zero-trust localhost auth with DNS rebinding protec...
by joncode · 2026-02-03
75.4%
#10745: feat: Security improvements and Windows compatibility fixes
by lluviaoscuradeldoce-design · 2026-02-06
75.0%
#19885: test(gateway,browser): isolate tests from ambient OPENCLAW_GATEWAY_...
by NewdlDewdl · 2026-02-18
74.8%
#6112: refactor: add early return for invalid Authorization headers
by T1mn · 2026-02-01
74.8%