BTC
ETH
SOL
BNB
GOLD
XRP
DOGE
ADA
Back to home
Security

[MEDIUM] Security Advisory: Hono missing validation of cookie name on write path in setCookie() (hono)

Hono, the lightweight web framework popular on Cloudflare Workers and Node.js, has a flaw in its cookie handling.

Hono, the lightweight web framework popular on Cloudflare Workers and Node.js, has a flaw in its cookie handling. Its setCookie(), serialize(), and serializeSigned() functions skip validation of cookie names. This mismatch with the parsing side—where names are checked—lets invalid characters slip into Set-Cookie headers. In practice, modern runtimes like Node.js and Cloudflare Workers reject these malformed headers outright, crashing the request before any response ships. No header injection or response splitting happens.

The core issue boils down to inconsistency. Hono’s cookie parser enforces RFC 6265 rules on incoming cookies: names must be “token” characters—letters, digits, and a handful of safe symbols like !#$%&’*+-.^_`|~. No spaces, no control characters like carriage returns (\r) or newlines (\n). But when you generate outgoing cookies, nothing stops you from using junk in the name. Feed it user-controlled input, say from a query param or header, and you get garbage like:

Set-Cookie: legit\r\nX-Injected: evil=value

That looks like a classic header injection vector. Attackers dream of splitting responses to smuggle malicious content. But test it: Node.js (v20+) and Cloudflare Workers throw errors on invalid headers during res.writeHead() or equivalent. The response never leaves. Hono maintainers confirmed this—no reproduction of exploits in real environments.

Why Developers Might Hit This

Hono powers fast APIs and edgeside apps. Its cookie module, borrowed from cookie-js patterns, prioritizes speed over pedantic checks. Most devs hardcode names like sessionId or authToken—no problem. Trouble brews if you dynamically generate names from untrusted sources. Rare? Sure. Think A/B testing with user IDs in names, or misconfigured analytics. Or copy-paste bugs where a full cookie object gets user input.

Broader context: Cookie mishaps aren’t new. Express.js had similar laxness years back, fixed after CRIME and BREACH attacks highlighted header risks. Hono, at v4.x as of this advisory (July 2024), targets the same runtimes. Stats from npm: Hono has 1.2M weekly downloads, Workers dominates its usage. A crash on edge cases dents reliability, especially in serverless where restarts cost latency.

Real Impact: Crashes, Not Compromises

Security rating: Low. No confirmed exploits. Runtimes block the bang—Node’s OutgoingMessage validates headers strictly since v11.7.0 (2018). Cloudflare’s V8 isolates enforce it too. Older Node? Unlikely in prod. Deno or Bun? Untested, but similar guards.

What hurts: Robustness. An attacker probing with bad names DoS-es single requests. Scale to thousands via reflection gadgets, and you strain Workers invocations (billed per CPU-ms). Or in Node clusters, it logs errors, spikes monitoring alerts. Fair skepticism: Is this a vuln or sloppy code? Advisory calls it “primarily affects correctness.” True—fix validates names on write, aligning parse/serialize.

Patch lands in Hono 4.6.3 (released post-advisory). It rejects invalid names early, throwing Error like the parser. Until then, wrap calls:

const safeName = /^[a-zA-Z0-9!#$%&'*+-.^_`|~]+$/.test(name) ? name : null;
if (!safeName) throw new Error('Invalid cookie name');
c.setCookie(safeName, value);

Why this matters: Edge frameworks like Hono cut corners for perf. 10µs saved per req scales to billions. But users expect libraries to handle edge cases. This flags a pattern—skimpy input validation in hot paths. Check your code: grep for setCookie with vars. Audit cookie libs elsewhere (cookies-next, js-cookie). In crypto/security stacks, bad cookies leak sessions; here, just crashes.

Bottom line: Update Hono. Validate inputs yourself. Runtimes save you from exploits, but not from noisy logs or surprised devs. Hono’s team moved fast—good sign. In a world of 0-days, this is a non-event for security, a nudge for quality.

April 8, 2026 · 3 min · 11 views · Source: GitHub Security

Related