Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .changesets/1794464217-f98ed08b.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
id: 1794464217-f98ed08b
features:
- core
targets:
- typescriptv2
type: feat
bump: patch
description: validate path, query and header parameter values against portable schema patterns before sending a request
author: TristanSpeakEasy
date: "2026-11-12"
3 changes: 3 additions & 0 deletions templates/perms.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ var fileMode = map[string]fs.FileMode{
"templates/common/common/modelusage.ts": 0o644,
"templates/common/common/open-unions.ts": 0o644,
"templates/common/common/operations.ts": 0o644,
"templates/common/common/patterns.ts": 0o644,
"templates/common/common/polyfill.ts": 0o644,
"templates/common/common/sanitization.ts": 0o644,
"templates/common/common/sdkdocs.ts": 0o644,
Expand Down Expand Up @@ -2461,6 +2462,7 @@ var fileMode = map[string]fs.FileMode{
"templates/typescriptv2/tests/primary/open_discriminated_union.test.ts": 0o644,
"templates/typescriptv2/tests/primary/pagination.test.ts": 0o644,
"templates/typescriptv2/tests/primary/parameter_additional.test.ts": 0o644,
"templates/typescriptv2/tests/primary/parameter_patterns_additional.test.ts": 0o644,
"templates/typescriptv2/tests/primary/primary_helpers.ts": 0o644,
"templates/typescriptv2/tests/primary/react_query_additional.test.ts": 0o644,
"templates/typescriptv2/tests/primary/request_extras_additional.test.ts": 0o644,
Expand Down Expand Up @@ -2500,6 +2502,7 @@ var fileMode = map[string]fs.FileMode{
"templates/typescriptv2/tests/tertiary/auth_additional.test.ts": 0o644,
"templates/typescriptv2/tests/tertiary/mcp_additional.test.ts": 0o644,
"templates/typescriptv2/tests/tertiary/mcp_compatibility_additional.test.ts": 0o644,
"templates/typescriptv2/tests/tertiary/parameter_patterns_additional.test.ts": 0o644,
"templates/typescriptv2/tests/tertiary/servers.test.ts": 0o644,
"templates/typescriptv2/tests/tertiary/smart_union.test.ts": 0o644,
"templates/typescriptv2/tests/tertiary/webhooks.test.ts": 0o644,
Expand Down
74 changes: 0 additions & 74 deletions templates/templates/cli/includes/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -775,80 +775,6 @@ function collectMultipartMetadata(
}
}

// Escapes with the same meaning in Go's regexp and ECMA-262 Unicode mode.
const portableEscapes = new Set("dDwWbBtnrfv^$\\.*+?()[]{}|/".split(""));
const portableClassEscapes = new Set("dDwWtnrfv^$\\.*+?()[]{}|/-".split(""));

/**
* Consumes a `\` escape at pattern[i] and returns the index after it, or -1
* when the escape is outside the portable set.
*/
function portableEscapeEnd(
pattern: string,
i: number,
allowed: Set<string>,
): number {
const next = pattern[i + 1];
if (next === "x") {
return /^[0-9A-Fa-f]{2}$/.test(pattern.slice(i + 2, i + 4)) ? i + 4 : -1;
}
return next !== undefined && allowed.has(next) ? i + 2 : -1;
}

/**
* Whether a schema pattern can be enforced with Go's regexp without rejecting
* a value the JSON Schema pattern (ECMA-262 with the "u" flag) accepts. The
* CLI rejects mismatches outright, so a pattern is emitted only when it stays
* within the interoperable subset JSON Schema recommends — literals, `.`,
* character classes and ranges, `\d \w \b` and their negations, control and
* `\xHH` escapes, anchors, `(...)`/`(?:...)` groups, alternation, and `* + ?
* {n,m}` quantifiers (greedy or lazy) — and Go compiles it. That excludes, for
* example, `\s` (ASCII-only in Go), `\p{...}`, lookarounds, backreferences, and
* POSIX classes. Go's `.` also matches `\r` and U+2028, so it may accept a
* value ECMA-262 rejects; the server still decides those.
*/
function isPortablePattern(pattern: string): boolean {
if (!isRE2Regex(pattern)) return false;
let i = 0;
while (i < pattern.length) {
const c = pattern[i];
if (c === "\\") {
i = portableEscapeEnd(pattern, i, portableEscapes);
if (i < 0) return false;
} else if (c === "(") {
if (pattern[i + 1] === "?" && pattern[i + 2] !== ":") return false;
i += pattern[i + 1] === "?" ? 3 : 1;
} else if (c === "{") {
// Go reads a bound with a leading zero ({01}) as literal text.
const quantifier = /^\{(0|[1-9]\d*)(,(0|[1-9]\d*)?)?\}/.exec(
pattern.slice(i),
);
if (!quantifier) return false;
i += quantifier[0].length;
} else if (c === "}" || c === "]") {
return false;
} else if (c === "[") {
i++;
if (pattern[i] === "^") i++;
if (pattern[i] === "]") return false;
while (i < pattern.length && pattern[i] !== "]") {
if (pattern[i] === "[") return false;
if (pattern[i] === "\\") {
i = portableEscapeEnd(pattern, i, portableClassEscapes);
if (i < 0) return false;
} else {
i++;
}
}
if (i >= pattern.length) return false;
i++;
} else {
i++;
}
}
return true;
}

/**
* Build a single FlagMeta Go literal for a field.
*/
Expand Down
32 changes: 32 additions & 0 deletions templates/templates/common/common/examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ require("common/faker.ts");
require("common/typematching.ts");
require("common/constants.ts");
require("common/annotations.ts");
require("common/patterns.ts");

const DEBUG_EXAMPLE_LOGGING = false; // Set this to true to enable very verbose logging of example generation results (don't leave this set to true outside local dev)
const DEBUG_EXAMPLE_OP_FILTER = ""; // Set this to a specific operation ID to only log example results for that operation (don't leave this set outside local dev)
Expand Down Expand Up @@ -1124,6 +1125,7 @@ function calculateBasicExample(
} else {
example = templatePlaceholderString(arrayIdx);
}
example = conformExampleToPattern(fieldDef, example);
}

return example;
Expand Down Expand Up @@ -1994,6 +1996,36 @@ function fakeNumber(fieldType: string) {
}
}

// Targets whose clients reject parameter values that do not match a portable
// schema pattern.
const patternEnforcingLanguages = new Set(["typescript"]);

function conformExampleToPattern(fieldDef: FieldDef, value: string): string {
if (
!patternEnforcingLanguages.has(context.Global.Config.Language) ||
!isParameterField(fieldDef)
) {
return value;
}
const pattern = portableStringPattern(fieldDef.Type);
if (!pattern) {
return value;
}
const rawMaxLength = fieldDef.Type.Validations?.MaxLength;
const maxLength = rawMaxLength == null ? Infinity : Number(rawMaxLength);
const fits = (s: string) => [...s].length <= maxLength;
if (fits(value) && new RegExp(pattern, "u").test(value)) {
return value;
}
for (const repeat of [3, 1, 0]) {
const example = exampleForPortablePattern(pattern, repeat);
if (example !== undefined && fits(example)) {
return example;
}
}
return value;
}

function templatePlaceholderString(arrayIdx?: number) {
return `<value${arrayIdx !== undefined ? ` ${arrayIdx + 1}` : ""}>`;
}
Expand Down
224 changes: 224 additions & 0 deletions templates/templates/common/common/patterns.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
// Escapes with the same meaning in Go's regexp and ECMA-262 Unicode mode.
const portableEscapes = new Set("dDwWbBtnrfv^$\\.*+?()[]{}|/".split(""));
const portableClassEscapes = new Set("dDwWtnrfv^$\\.*+?()[]{}|/-".split(""));

/**
* Consumes a `\` escape at pattern[i] and returns the index after it, or -1
* when the escape is outside the portable set.
*/
function portableEscapeEnd(
pattern: string,
i: number,
allowed: Set<string>,
): number {
const next = pattern[i + 1];
if (next === "x") {
return /^[0-9A-Fa-f]{2}$/.test(pattern.slice(i + 2, i + 4)) ? i + 4 : -1;
}
return next !== undefined && allowed.has(next) ? i + 2 : -1;
}

/**
* Whether a schema pattern means the same thing in Go's regexp and in ECMA-262
* with the "u" flag (the JSON Schema dialect), so a generated client can
* enforce it without rejecting a value the API accepts. A pattern qualifies
* when it stays within the interoperable subset JSON Schema recommends —
* literals, `.`, character classes and ranges, `\d \w \b` and their negations,
* control and `\xHH` escapes, anchors, `(...)`/`(?:...)` groups, alternation,
* and `* + ? {n,m}` quantifiers (greedy or lazy) — and Go compiles it. That
* excludes, for example, `\s` (ASCII-only in Go), `\p{...}`, lookarounds,
* backreferences, and POSIX classes. Go's `.` also matches `\r` and U+2028, so
* it may accept a value ECMA-262 rejects; the server still decides those.
*/
function isPortablePattern(pattern: string): boolean {
if (!isRE2Regex(pattern)) return false;
let i = 0;
while (i < pattern.length) {
const c = pattern[i];
if (c === "\\") {
i = portableEscapeEnd(pattern, i, portableEscapes);
if (i < 0) return false;
} else if (c === "(") {
if (pattern[i + 1] === "?" && pattern[i + 2] !== ":") return false;
i += pattern[i + 1] === "?" ? 3 : 1;
} else if (c === "{") {
// Go reads a bound with a leading zero ({01}) as literal text.
const quantifier = /^\{(0|[1-9]\d*)(,(0|[1-9]\d*)?)?\}/.exec(
pattern.slice(i),
);
if (!quantifier) return false;
i += quantifier[0].length;
} else if (c === "}" || c === "]") {
return false;
} else if (c === "[") {
i++;
if (pattern[i] === "^") i++;
if (pattern[i] === "]") return false;
while (i < pattern.length && pattern[i] !== "]") {
if (pattern[i] === "[") return false;
if (pattern[i] === "\\") {
i = portableEscapeEnd(pattern, i, portableClassEscapes);
if (i < 0) return false;
} else {
i++;
}
}
if (i >= pattern.length) return false;
i++;
} else {
i++;
}
}
return true;
}

const patternExamplePool =
"abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-_.~ ";
const patternLiteralEscapes: Record<string, string> = {
t: "\t",
n: "\n",
r: "\r",
f: "\f",
v: "\v",
};

/**
* A deterministic value matching a portable pattern (see isPortablePattern),
* or undefined when none could be built. Alternation takes the first branch,
* repetition produces `repeat` items within the quantifier's bounds, and
* character sets draw from a small printable pool.
*/
function exampleForPortablePattern(
pattern: string,
repeat: number = 3,
): string | undefined {
let i = 0;
let picks = 0;

const classEnd = (start: number): number => {
let j = start + 1;
if (pattern[j] === "^") j++;
while (j < pattern.length && pattern[j] !== "]") {
j += pattern[j] === "\\" ? (pattern[j + 1] === "x" ? 4 : 2) : 1;
}
return j;
};

const parseAtom = (): (() => string) | undefined => {
const c = pattern[i];
if (c === "^" || c === "$") {
i++;
return () => "";
}
if (c === "(") {
i += pattern[i + 1] === "?" ? 3 : 1;
const inner = parseAlternation();
if (inner === undefined || pattern[i] !== ")") return undefined;
i++;
return () => inner;
}
let set: string;
if (c === "[") {
const end = classEnd(i);
set = pattern.slice(i, end + 1);
i = end + 1;
} else if (c === "\\") {
const next = pattern[i + 1] ?? "";
if (next === "x") {
const ch = String.fromCharCode(
parseInt(pattern.slice(i + 2, i + 4), 16),
);
i += 4;
return () => ch;
}
i += 2;
if (next === "b" || next === "B") return () => "";
const literal = patternLiteralEscapes[next];
if (literal !== undefined) return () => literal;
if (!"dDwW".includes(next)) return () => next;
set = `\\${next}`;
} else if (c === ".") {
set = ".";
i++;
} else {
i++;
return () => c;
}
const setRE = new RegExp(`^${set}$`, "u");
const choices = patternExamplePool.split("").filter((ch) => setRE.test(ch));
if (choices.length === 0) return undefined;
return () => choices[picks++ % choices.length]!;
};

const parseQuantifier = (): [number, number] => {
let range: [number, number];
const c = pattern[i];
if (c === "*") {
range = [0, Infinity];
i++;
} else if (c === "+") {
range = [1, Infinity];
i++;
} else if (c === "?") {
range = [0, 1];
i++;
} else if (c === "{") {
const bound = /^\{(\d+)(,(\d*))?\}/.exec(pattern.slice(i));
if (!bound) return [1, 1];
const min = Number(bound[1]);
const max =
bound[2] === undefined
? min
: bound[3] === ""
? Infinity
: Number(bound[3]);
range = [min, max];
i += bound[0].length;
} else {
return [1, 1];
}
if (pattern[i] === "?") i++;
return range;
};

const parseSequence = (): string | undefined => {
let out = "";
while (i < pattern.length && pattern[i] !== "|" && pattern[i] !== ")") {
const atom = parseAtom();
if (!atom) return undefined;
const [min, max] = parseQuantifier();
const count = Math.min(max, Math.max(min, repeat));
for (let n = 0; n < count; n++) out += atom();
}
return out;
};

function parseAlternation(): string | undefined {
const first = parseSequence();
while (first !== undefined && pattern[i] === "|") {
i++;
if (parseSequence() === undefined) return undefined;
}
return first;
}

let example: string | undefined;
try {
example = parseAlternation();
} catch {
return undefined;
}
if (example === undefined || i !== pattern.length) return undefined;
return new RegExp(pattern, "u").test(example) ? example : undefined;
}

/**
* The schema pattern of a string type when it is portable, otherwise "".
* Validations.Pattern is pointer-valued in the AST, so it is coerced to a JS
* string first.
*/
function portableStringPattern(typeDef: TypeDef): string {
const raw = typeDef.Validations?.Pattern;
const pattern = raw == null ? "" : String(raw);
return pattern && isPortablePattern(pattern) ? pattern : "";
}
Loading
Loading