Repository navigation
fix: validate nested tagged union request bodies #336
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| --- | ||
| "cf": patch | ||
| --- | ||
|
|
||
| Validate nested tagged unions when assembling request bodies from flags. | ||
|
|
||
| Preserve branch-specific required fields, closed-object rules, and available | ||
| array limits, including unions selected by enum values without an explicit | ||
| discriminator. Apply the checks to requests and dry-run previews. | ||
| Preserve the alternatives of enclosing unions and retain explicit `--body` | ||
| input as an escape hatch from these projected checks. |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| import { obj, type Value } from "./js.js"; | ||
| import { deref, unwrapNull } from "./operation.js"; | ||
| import type { ObjectType, Type } from "@cloudflare/forge/plugin"; | ||
|
|
||
| export type BodyUnion = { | ||
| path: string[]; | ||
| } & ( | ||
| | { | ||
| selector: string; | ||
| variants: { values: (string | number | boolean)[]; schema: Value }[]; | ||
| } | ||
| | { schema: Value } | ||
| ); | ||
|
|
||
| // Distinct required enum values select a branch even without an OpenAPI | ||
| // discriminator. | ||
| function taggedMembers( | ||
| type: Type | ||
| ): { selector: string; members: ObjectType[] } | undefined { | ||
| const union = unwrapNull(deref(type)); | ||
| if (union.kind !== "union" || !union.exclusive) return undefined; | ||
| const members = union.members.map((m) => unwrapNull(deref(m))); | ||
| if ( | ||
| members.length < 2 || | ||
| !members.every((m): m is ObjectType => m.kind === "object") | ||
| ) | ||
| return undefined; | ||
| for (const candidate of members[0]?.fields ?? []) { | ||
| const seen: (string | number | boolean)[] = []; | ||
| if ( | ||
| members.every((member) => { | ||
| const field = member.fields.find( | ||
| (f) => f.wireName === candidate.wireName | ||
| ); | ||
| if (!field?.required) return false; | ||
| const t = unwrapNull(deref(field.type)); | ||
| if (t.kind !== "enum" || t.values.some((v) => seen.includes(v))) | ||
| return false; | ||
| seen.push(...t.values); | ||
| return true; | ||
| }) | ||
| ) | ||
| return { selector: candidate.wireName, members }; | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| // Project only constraints the Rust model retains. Nested unions use anyOf: | ||
| // a projection can lose constraints, so counting matches could reject valid input. | ||
| function schema(type: Type, active: readonly Type[] = []): Value { | ||
| const t = deref(type); | ||
| if (active.includes(t) || active.length > 16) return obj(); | ||
| const next = [...active, t]; | ||
| switch (t.kind) { | ||
| case "scalar": | ||
| return t.scalar === "unknown" | ||
| ? obj() | ||
| : obj([ | ||
| [ | ||
| "type", | ||
| t.scalar === "number" | ||
| ? "number" | ||
| : t.scalar === "boolean" || t.scalar === "null" | ||
| ? t.scalar | ||
| : "string", | ||
| ], | ||
| ]); | ||
| case "enum": | ||
| return obj([["enum", [...t.values]]]); | ||
| case "array": | ||
| return obj([ | ||
| ["type", "array"], | ||
| ["items", schema(t.elem, next)], | ||
| ...(t.maxItems === undefined | ||
| ? [] | ||
| : [["maxItems", t.maxItems] as const]), | ||
| ]); | ||
| case "object": | ||
| return obj([ | ||
| ["type", "object"], | ||
| [ | ||
| "properties", | ||
| obj(t.fields.map((f) => [f.wireName, schema(f.type, next)])), | ||
| ], | ||
| [ | ||
| "required", | ||
| t.fields | ||
| .filter((f) => f.required && !f.readOnly) | ||
| .map((f) => f.wireName), | ||
| ], | ||
| [ | ||
| "additionalProperties", | ||
| t.extra === undefined ? false : schema(t.extra, next), | ||
| ], | ||
| ]); | ||
| case "union": | ||
| return obj([["anyOf", t.members.map((m) => schema(m, next))]]); | ||
| case "intersection": | ||
| return obj([["allOf", t.members.map((m) => schema(m, next))]]); | ||
| case "model": | ||
| return obj(); | ||
| } | ||
| } | ||
|
|
||
| export function bodyUnions(type: Type): BodyUnion[] { | ||
| const visit = ( | ||
| raw: Type, | ||
| path: string[], | ||
| active: readonly Type[] | ||
| ): BodyUnion[] => { | ||
| const t = unwrapNull(deref(raw)); | ||
| if (active.includes(t) || path.length > 4) return []; | ||
| const next = [...active, t]; | ||
| const tagged = path.length > 0 ? taggedMembers(t) : undefined; | ||
| if (tagged) { | ||
| return [ | ||
| { | ||
| path, | ||
| selector: tagged.selector, | ||
| variants: tagged.members.map((member) => { | ||
| const field = member.fields.find( | ||
| (f) => f.wireName === tagged.selector | ||
| ); | ||
| const enumType = field && unwrapNull(deref(field.type)); | ||
| return { | ||
| values: enumType?.kind === "enum" ? [...enumType.values] : [], | ||
| schema: schema(member), | ||
| }; | ||
| }), | ||
| }, | ||
| ]; | ||
| } | ||
| if (t.kind === "object") { | ||
| return t.fields.flatMap((field) => | ||
| field.readOnly ? [] : visit(field.type, [...path, field.wireName], next) | ||
| ); | ||
| } | ||
| if (t.kind === "intersection" || t.kind === "union") { | ||
| const nested = t.members.flatMap((member) => visit(member, path, next)); | ||
| // A nested union's requirements only apply within its outer alternative. | ||
| return t.kind === "union" && nested.length > 0 | ||
| ? [{ path, schema: schema(t) }] | ||
| : nested; | ||
| } | ||
| return []; | ||
| }; | ||
| return visit(type, [], []); | ||
| } |
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.