Repository navigation
fix: validate cookie grammar and stop emitting undefined content types - #546
Closed
Omair-Asdaq wants to merge 7 commits into
Closed
Omair-Asdaq wants to merge 7 commits into
Omair-Asdaq wants to merge 7 commits into
Conversation
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
marked this pull request as ready for review
October 29, 2026 20:52
Omair-Asdaq
marked this pull request as draft
October 29, 2026 20:52
Omair-Asdaq
marked this pull request as ready for review
October 29, 2026 20:54
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Three small defects I ran into while using these packages. Each one has a regression test that fails on master.
cookie.serializedoes not validate names or valuespackages/cookie/src/index.tsvalidated with the RFC 7230field-contentgrammar, which accepts almost any printable character:The second appends an attribute to
Set-Cookiethat the caller did not ask for. Names and values are now checked againstcookie-nameandcookie-valuefrom RFC 6265 sec 4.1.1, the grammars thecookiemodule uses. Values encoded with the defaultencodeURIComponentare 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 markedpatch. Happy to treat it as a breaking change instead if you would rather.Unknown extensions produce
Content-Type: undefinedres.type(),res.attachment()andres.sendFile()passed the mime lookup straight into the header:.icois not in the mrmime table, and neither is an extensionless file. The lookup now falls back toapplication/octet-stream, as express does.res.send()with no body throwsres.send()reachedbodyToSend.toString()withundefinedand threw aTypeError, which the app turned into a 500:It now ends the response with an empty body and no
Content-Type, matchingres.send(null).Tests
encodeURIComponentemits.pnpm vitest run --dir tests: 871 passed, 3 skipped (Linux; ten unrelatedtests/corecases fail on Windows only, on master too).pnpm buildandpnpm biome ci .pass.