Skip to content

fix: validate cookie grammar and stop emitting undefined content types - #546

Closed
Omair-Asdaq wants to merge 7 commits into
tinyhttp:masterfrom
Omair-Asdaq:fix/cookie-and-response-hardening
Closed

Omair-Asdaq wants to merge 7 commits into
tinyhttp:masterfrom
Omair-Asdaq:fix/cookie-and-response-hardening

Conversation

@Omair-Asdaq

@Omair-Asdaq Omair-Asdaq commented Oct 26, 2026 •

Copy link
Copy Markdown

Three small defects I ran into while using these packages. Each one has a regression test that fails on master.

cookie.serialize does not validate names or values

packages/cookie/src/index.ts validated with the RFC 7230 field-content grammar, which accepts almost any printable character:

serialize('a b', 'c')                        // "a b=c"
serialize('foo', 'b;c', { encode: v => v })  // "foo=b;c"

The second appends an attribute to Set-Cookie that the caller did not ask for. Names and values are now checked against cookie-name and cookie-value from RFC 6265 sec 4.1.1, the grammars the cookie module uses. Values encoded with the default encodeURIComponent are unaffected, since percent-encoded output is always valid.

This does reject names and values that used to serialize, for callers passing their own encode, so it is a behavioural change and the changeset is marked patch. Happy to treat it as a breaking change instead if you would rather.

Unknown extensions produce Content-Type: undefined

res.type(), res.attachment() and res.sendFile() passed the mime lookup straight into the header:

res.sendFile('/srv/www/favicon.ico')
// Content-Type: undefined; charset=utf-8

.ico is not in the mrmime table, and neither is an extensionless file. The lookup now falls back to application/octet-stream, as express does.

res.send() with no body throws

res.send() reached bodyToSend.toString() with undefined and threw a TypeError, which the app turned into a 500:

app.get('/ping', (_req, res) => res.send())

It now ends the response with an empty body and no Content-Type, matching res.send(null).

Tests

  • Seven new tests. Six fail on master and pass with the change; the seventh pins the default encoder's output so the grammar is not narrowed past what encodeURIComponent emits.
  • pnpm vitest run --dir tests: 871 passed, 3 skipped (Linux; ten unrelated tests/core cases fail on Windows only, on master too).
  • pnpm build and pnpm biome ci . pass.
  • Changeset included for the three patched packages.

serialize used the RFC 7230 field-content grammar, which accepts any printable
ASCII character. A name such as `a b` was written as `a b=c`, and a value such
as `bar;baz` was written as `bar;baz`, appending an attribute to the
Set-Cookie header that the caller did not ask for.

Names and values are now checked against the cookie-name and cookie-value
grammars from RFC 6265 sec 4.1.1, the same grammars the cookie module uses.
send() with no body reached bodyToSend.toString() and threw a TypeError, which
the app turned into a 500 response. It now ends the response with an empty
body, the same way send(null) already did.

sendFile() used the result of the extension lookup directly in the
Content-Type header. For an extension that the mime table does not know
(including .ico) the header became `undefined; charset=utf-8`. The lookup now
falls back to application/octet-stream.
setContentType() passed the result of the extension lookup straight to the
header, so res.type() and res.attachment() produced `undefined; charset=utf-8`
for an extension that the mime table does not know. The lookup now falls back
to application/octet-stream, like express does.
Describes the patch releases for @tinyhttp/cookie, @tinyhttp/res and
@tinyhttp/send.
The cookie tests only covered a space in the name and a semicolon in the
value, which does not pin the rest of the grammar. They now also assert that a
name with a colon separator is rejected and that a value containing `=` still
serializes through the default encoder.

The undefined-body test now checks that no Content-Type is set, so a fix that
merely normalized undefined to an empty string would not pass.
Name the characters that the cookie name grammar now rejects, and mention that
the MIME fallback also covers paths without an extension.
@Omair-Asdaq
Omair-Asdaq marked this pull request as ready for review October 29, 2026 20:52
Copilot AI balanced review requested due to automatic review settings October 29, 2026 20:52

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@Omair-Asdaq
Omair-Asdaq marked this pull request as draft October 29, 2026 20:52
@Omair-Asdaq
Omair-Asdaq marked this pull request as ready for review October 29, 2026 20:54
@Omair-Asdaq Omair-Asdaq closed this Nov 3, 2026
@Omair-Asdaq
Omair-Asdaq deleted the fix/cookie-and-response-hardening branch November 3, 2026 15:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants