A path value of "." or ".." survives encodeURIComponent unchanged, and URL
resolution (V3 `new URL`, V2 `pathname =` assignment) then collapses it into
a parent path that still sits under the base URL, so `URL_ORIGIN_ESCAPED`
never fired. `/members/{m}` with m=".." pointed at the project instead.
The V3 bridge now returns PATH_VALUE_INVALID and the V2 request builder
returns PATH_PARAMETER_INVALID for exactly those two values.
RED before fix: tests/unit/http-contract-bridge.test.ts "rejects the dot
segment ..." and tests/unit/request-builder.test.ts "rejects the dot segment
..." returned ok/success with a parent URL.
Gates: check:architecture, check:types:app, check:types:test, lint,
test:component pass. test:unit passes except removal-fixture.test.ts, which
needs generated artifacts/ release evidence absent in a fresh worktree
(unrelated to this change).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
138 lines
4.0 KiB
TypeScript
138 lines
4.0 KiB
TypeScript
import type { ApiOperation } from "../../contracts/api-operations.ts";
|
|
|
|
export type OperationRequestInput = Readonly<{
|
|
operationId: string;
|
|
routeId: string;
|
|
pathParams?: Readonly<Record<string, string | number>>;
|
|
searchParams?: unknown;
|
|
body?: unknown;
|
|
signal?: AbortSignal;
|
|
idempotencyKey?: string;
|
|
correlationId?: string;
|
|
}>;
|
|
|
|
export type RequestTargetResult =
|
|
| Readonly<{ success: true; url: URL }>
|
|
| Readonly<{
|
|
success: false;
|
|
code:
|
|
| "BASE_URL_INVALID"
|
|
| "PATH_PARAMETER_MISSING"
|
|
| "PATH_PARAMETER_UNEXPECTED"
|
|
| "PATH_PARAMETER_INVALID"
|
|
| "SEARCH_PARAMETER_INVALID";
|
|
}>;
|
|
|
|
const pathParameterPattern = /:([A-Za-z][A-Za-z0-9_]*)|\{([A-Za-z][A-Za-z0-9_]*)\}/g;
|
|
|
|
export function buildRequestTarget(
|
|
baseUrl: string,
|
|
operation: ApiOperation,
|
|
pathParams: Readonly<Record<string, string | number>> = {},
|
|
parsedSearch: unknown = {},
|
|
): RequestTargetResult {
|
|
let base: URL;
|
|
try {
|
|
base = new URL(baseUrl);
|
|
} catch {
|
|
return { success: false, code: "BASE_URL_INVALID" };
|
|
}
|
|
if (
|
|
(base.protocol !== "https:" &&
|
|
!(
|
|
base.protocol === "http:" &&
|
|
["localhost", "127.0.0.1", "[::1]"].includes(base.hostname)
|
|
)) ||
|
|
base.username ||
|
|
base.password ||
|
|
base.search ||
|
|
base.hash
|
|
) {
|
|
return { success: false, code: "BASE_URL_INVALID" };
|
|
}
|
|
|
|
const placeholders = new Set<string>();
|
|
for (const match of operation.path.matchAll(pathParameterPattern)) {
|
|
placeholders.add(match[1] ?? match[2] ?? "");
|
|
}
|
|
if (Object.keys(pathParams).some((key) => !placeholders.has(key))) {
|
|
return { success: false, code: "PATH_PARAMETER_UNEXPECTED" };
|
|
}
|
|
let missingPathParameter = false;
|
|
let invalidPathParameter = false;
|
|
const pathname = operation.path.replace(
|
|
pathParameterPattern,
|
|
(_token, colonName: string | undefined, braceName: string | undefined) => {
|
|
const name = colonName ?? braceName ?? "";
|
|
const value = pathParams[name];
|
|
if (value === undefined) {
|
|
missingPathParameter = true;
|
|
return "";
|
|
}
|
|
const serialized = String(value);
|
|
if (
|
|
serialized.length === 0 ||
|
|
serialized.length > 512 ||
|
|
// Dot segments are not encoded and pathname assignment normalizes them.
|
|
serialized === "." ||
|
|
serialized === ".." ||
|
|
[...serialized].some((character) => {
|
|
const code = character.codePointAt(0) ?? 0;
|
|
return code < 32 || code === 127;
|
|
})
|
|
) {
|
|
invalidPathParameter = true;
|
|
return "";
|
|
}
|
|
return encodeURIComponent(serialized);
|
|
},
|
|
);
|
|
if (missingPathParameter) {
|
|
return { success: false, code: "PATH_PARAMETER_MISSING" };
|
|
}
|
|
if (invalidPathParameter) {
|
|
return { success: false, code: "PATH_PARAMETER_INVALID" };
|
|
}
|
|
|
|
if (
|
|
parsedSearch === null ||
|
|
typeof parsedSearch !== "object" ||
|
|
Array.isArray(parsedSearch)
|
|
) {
|
|
return { success: false, code: "SEARCH_PARAMETER_INVALID" };
|
|
}
|
|
|
|
const basePrefix = base.pathname.endsWith("/")
|
|
? base.pathname
|
|
: `${base.pathname}/`;
|
|
const relativePath = pathname.replace(/^\/+/, "");
|
|
base.pathname = `${basePrefix}${relativePath}`.replace(/\/{2,}/g, "/");
|
|
const url = base;
|
|
const search = parsedSearch as Readonly<Record<string, unknown>>;
|
|
for (const key of Object.keys(search).sort((left, right) =>
|
|
left.localeCompare(right),
|
|
)) {
|
|
const value = search[key];
|
|
if (value === undefined || value === null) continue;
|
|
const values = Array.isArray(value) ? value : [value];
|
|
for (const item of values) {
|
|
if (
|
|
typeof item !== "string" &&
|
|
typeof item !== "number" &&
|
|
typeof item !== "boolean"
|
|
) {
|
|
return { success: false, code: "SEARCH_PARAMETER_INVALID" };
|
|
}
|
|
url.searchParams.append(key, String(item));
|
|
}
|
|
}
|
|
if (
|
|
operation.maxEncodedSearchBytes !== undefined &&
|
|
new TextEncoder().encode(url.search).byteLength >
|
|
operation.maxEncodedSearchBytes
|
|
) {
|
|
return { success: false, code: "SEARCH_PARAMETER_INVALID" };
|
|
}
|
|
return { success: true, url };
|
|
}
|