fix(cloudflare): Reject unknown keys in options callbacks - #23290
Open
JPeer264 wants to merge 1 commit into
Open
fix(cloudflare): Reject unknown keys in options callbacks#23290JPeer264 wants to merge 1 commit into
JPeer264 wants to merge 1 commit into
Conversation
A plain `CloudflareOptions` parameter already rejects unknown keys when the
caller writes a direct object literal, which is why it is enough everywhere
else. That check does not survive a function boundary though, and since `env`
only exists at request time, a callback is the only way to configure these
APIs — so typos went unreported:
withSentry(() => ({ dsn, tracesSampleRte: 1 }), handler) // compiled fine
`StrictCloudflareOptions<O>` rebuilds the check for the callbacks only: it
infers the returned literal into `O` and intersects it with a `never` map over
its unknown keys. A function-rejecting branch keeps "returned the options
factory instead of calling it" an error, which the intersection would otherwise
allow. The object forms stay on plain `CloudflareOptions`.
Note this is top level only — nested option objects stay unchecked — and it
also rejects a pre-built object carrying extra keys, which a plain excess
property check would allow.
Two bugs this surfaced: the Hono Cloudflare middleware spread the
middleware-only `shouldHandleError` into the SDK options, and `serverName` was
missing from `CloudflareOptions` despite being consumed by `ServerRuntimeClient`.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Contributor
size-limit report 📦
|
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.
Problem
It seemed that TypeScript didn't bother on wrong types in the options. This enables it and the following throws now a TS error:
It also seemed that we forgot to add
serverNamein the available options for CloudflareSolution
Some workaround to make the types stricter within functions: TS Playground
It also seems to be known: microsoft/TypeScript#7547 (comment)
Clanker description:
TypeScript's excess property check does not survive a function boundary, so typos in the options callback went unreported:
StrictCloudflareOptions<O>infers the returned literal intoOand intersects it with anevermap over its unknown keys, restoring the error. Applied to every entry point taking options.Note this is top level only, nested option objects stay unchecked, and it also rejects a pre-built object carrying extra keys, which a plain excess property check would allow.
Two bugs this surfaced: the Hono Cloudflare middleware spread the middleware-only
shouldHandleErrorinto the SDK options, andserverNamewas missing fromCloudflareOptionsdespite being consumed byServerRuntimeClient.