refactor(schemas): share portable identifier and base64 patterns (#1227)

* refactor(schemas): share identifier and canonical base64 patterns

* test: hold the offset datetime schema to the validator it replaces

The module documents that its accepted set matches z.string().datetime({ offset: true }), and nothing in the suite checked that claim. Adds table-driven accept and reject cases (leap years including the 1900/2000 century pair, month, day, hour and offset ranges, fractional and omitted seconds, missing offset, basic format) plus a differential assertion that classifies every case exactly as the replaced validator does, and asserts the emitted pattern compiles under u and v while the replaced pattern does not compile under v.

---------

Co-authored-by: felipeatp <felipe.troncosopt@gmail.com>
This commit is contained in:
morluto
2026-10-09 16:47:09 +08:00
committed by GitHub
co-authored by felipeatp
parent fef38e4d22
commit 954badbaaa
14 changed files with 224 additions and 29 deletions
+9 -9
View File
@@ -122,8 +122,9 @@ const cspSource = (
allowedOrigins: ReadonlySet<string>,
): ResponseMetadata["csp"]["directives"][number]["sources"][number] => {
const lower = token.toLowerCase();
if (/^'[a-z0-9-]+'$/u.test(lower)) return { kind: "keyword", value: lower };
if (/^[a-z][a-z0-9+.-]*:$/u.test(lower))
if (/^'[a-z0-9\x2d]+'$/u.test(lower))
return { kind: "keyword", value: lower };
if (/^[a-z][a-z0-9+.\x2d]*:$/u.test(lower))
return { kind: "scheme", value: lower };
const hostOrigin = cspHostSourceOrigin(token, baseUrl);
if (hostOrigin === undefined) return { kind: "other", value: null };
@@ -143,16 +144,15 @@ const cspHostSourceOrigin = (
token: string,
baseUrl: string,
): string | null | undefined => {
const match = /^(?:([a-z][a-z0-9+.-]*):\/\/)?([^/?#]+)(?:\/[^?#]*)?$/iu.exec(
token,
);
const match =
/^(?:([a-z][a-z0-9+.\x2d]*):\/\/)?([^\x2f?#]+)(?:\/[^?#]*)?$/iu.exec(token);
const authority = match?.[2];
if (authority === undefined || authority.length === 0) return undefined;
const scheme = (match?.[1] ?? schemeOf(baseUrl))?.toLowerCase();
if (scheme === undefined || !/^[a-z][a-z0-9+.-]*$/u.test(scheme))
if (scheme === undefined || !/^[a-z][a-z0-9+.\x2d]*$/u.test(scheme))
return undefined;
if (
!/^(?:\*|(?:\*\.)?[a-z0-9-]+(?:\.[a-z0-9-]+)*\.?)(?::(?:\*|\d+))?$/iu.test(
!/^(?:\*|(?:\*\.)?[a-z0-9\x2d]+(?:\.[a-z0-9\x2d]+)*\.?)(?::(?:\*|\d+))?$/iu.test(
authority,
)
)
@@ -332,7 +332,7 @@ const referrerPolicy = (value: string | undefined): string | null => {
for (const raw of (value ?? "").split(",")) {
const token = raw.trim().toLowerCase();
if (token === "") continue;
if (!/^[a-z-]+$/u.test(token)) return null;
if (!/^[a-z\x2d]+$/u.test(token)) return null;
if (referrerPolicies.has(token)) policy = token;
}
return policy;
@@ -340,7 +340,7 @@ const referrerPolicy = (value: string | undefined): string | null => {
const policyToken = (value: string | undefined): string | null => {
const token = (value ?? "").trim().toLowerCase();
return /^[a-z][a-z0-9_.-]*$/u.test(token) ? token : null;
return /^[a-z][a-z0-9_.\x2d]*$/u.test(token) ? token : null;
};
const nonnegativeInteger = (value: string | undefined): number | null => {
+3 -3
View File
@@ -1,5 +1,7 @@
import { z } from "zod";
import { IDENTIFIER_PATTERN } from "./stringPatterns.js";
import { isAbsoluteLocalPath, localPathStringSchema } from "./localPath.js";
import { browserNetworkContentSelectionSchema } from "./browserNetworkEvidence.js";
@@ -8,9 +10,7 @@ import {
browserOriginSchema,
} from "./browserObservation.js";
export const scenarioIdentifierSchema = z
.string()
.regex(/^[A-Za-z][A-Za-z0-9._\x2d]*$/u);
export const scenarioIdentifierSchema = z.string().regex(IDENTIFIER_PATTERN);
const browserExecutablePathSchema = localPathStringSchema.refine(
isAbsoluteLocalPath,
+3 -1
View File
@@ -3,6 +3,8 @@ import { createHash } from "node:crypto";
import canonicalize from "canonicalize";
import { z } from "zod";
import { IDENTIFIER_PATTERN } from "./stringPatterns.js";
import {
createEvidenceCompletionLedger,
evidenceCompletionRecordSchema,
@@ -11,7 +13,7 @@ import {
import { digestSchema } from "./../domain/digests.js";
import { prefixedDigestSchema } from "./../domain/digests.js";
const identifierSchema = z.string().regex(/^[A-Za-z][A-Za-z0-9._\x2d]*$/u);
const identifierSchema = z.string().regex(IDENTIFIER_PATTERN);
const identitySchema = z.strictObject({
id: identifierSchema,
version: z.string().min(1),
+3 -1
View File
@@ -3,6 +3,8 @@ import { createHash } from "node:crypto";
import canonicalize from "canonicalize";
import { z } from "zod";
import { IDENTIFIER_PATTERN } from "./stringPatterns.js";
import { evidenceEnvelopeSchema } from "./evidence.js";
import { evidenceBundleSchema } from "./evidenceBundle.js";
import { err, ok, type Result } from "./result.js";
@@ -10,7 +12,7 @@ import { prefixedDigestSchema } from "./../domain/digests.js";
const conformancePackageIdSchema = prefixedDigestSchema("cp");
const scenarioIdSchema = z.string().regex(/^[A-Za-z][A-Za-z0-9._\x2d]*$/u);
const scenarioIdSchema = z.string().regex(IDENTIFIER_PATTERN);
const scenarioManifestSchema = z.strictObject({
scenario_id: scenarioIdSchema,
+3 -1
View File
@@ -2,10 +2,12 @@ import { createHash } from "node:crypto";
import canonicalize from "canonicalize";
import { z } from "zod";
import { IDENTIFIER_PATTERN } from "./stringPatterns.js";
import { prefixedDigestSchema } from "./../domain/digests.js";
const evidenceIdSchema = prefixedDigestSchema("ev");
const claimIdSchema = z.string().regex(/^[A-Za-z][A-Za-z0-9._\x2d]*$/u);
const claimIdSchema = z.string().regex(IDENTIFIER_PATTERN);
/** Terminal outcome recorded for one verifier claim. */
export const evidenceCompletionStatusSchema = z.enum([
+3 -5
View File
@@ -1,14 +1,12 @@
import { z } from "zod";
import { CANONICAL_BASE64_PATTERN } from "../stringPatterns.js";
const unsignedHex = z.string().regex(/^0x(?:0|[1-9a-f][0-9a-f]{0,15})$/);
const index = z.number().int().nonnegative();
const scalar = z.union([z.string(), z.number().int()]);
const range = z.strictObject({ offset: unsignedHex, bytes: unsignedHex });
const canonicalBase64 = z
.string()
.regex(
/^(?:[A-Za-z0-9+\x2f]{4})*(?:[A-Za-z0-9+\x2f][AQgw]==|[A-Za-z0-9+\x2f]{2}[AEIMQUYcgkosw048]=)?$/,
);
const canonicalBase64 = z.string().regex(CANONICAL_BASE64_PATTERN);
const name = z.strictObject({
display: z.string(),
bytes_base64: canonicalBase64.nullable(),
+3 -5
View File
@@ -1,14 +1,12 @@
import { z } from "zod";
import { CANONICAL_BASE64_PATTERN } from "../stringPatterns.js";
import { recordedNoteCoverageIssues } from "./recordedCrashNoteCoverage.js";
const hex = z.string().regex(/^0x(?:0|[1-9a-f][0-9a-f]{0,15})$/);
const index = z.number().int().nonnegative();
const signed32 = z.number().int().min(-2147483648).max(2147483647);
const base64 = z
.string()
.regex(
/^(?:[A-Za-z0-9+\x2f]{4})*(?:[A-Za-z0-9+\x2f][AQgw]==|[A-Za-z0-9+\x2f]{2}[AEIMQUYcgkosw048]=)?$/,
);
const base64 = z.string().regex(CANONICAL_BASE64_PATTERN);
const range = z.strictObject({ offset: hex, bytes: hex });
const diagnostics = z.strictObject({
stdout: z.string(),
+101
View File
@@ -0,0 +1,101 @@
import { describe, expect, it } from "vitest";
import { z } from "zod";
import { offsetDateTimeSchema } from "./offsetDateTime.js";
/** The validator this schema replaces; it remains the oracle for the accepted set. */
const replaced = z.string().datetime({ offset: true });
const ACCEPTED: readonly (readonly [string, string])[] = [
["a UTC timestamp", "2026-10-09T04:00:00Z"],
["a positive offset", "2026-10-09T04:00:00+02:00"],
["a negative offset", "2026-10-09T04:00:00-03:00"],
["a half-hour offset", "2026-10-09T04:00:00+05:30"],
["a zero offset written negatively", "2026-10-09T04:00:00-00:00"],
["omitted seconds", "2026-10-09T04:00Z"],
["fractional seconds", "2026-10-09T04:00:00.123Z"],
[
"long fractional seconds with an offset",
"2026-10-09T04:00:00.123456+05:30",
],
["the last second of a day", "2026-10-09T23:59:59Z"],
["a leap day in a divisible-by-four year", "2024-02-29T00:00:00Z"],
["a leap day in a divisible-by-400 century year", "2000-02-29T00:00:00Z"],
];
const REJECTED: readonly (readonly [string, string])[] = [
["a leap day in a non-leap year", "2023-02-29T00:00:00Z"],
["a leap day in a divisible-by-100 but not 400 year", "1900-02-29T00:00:00Z"],
["a day beyond the month length", "2026-04-31T00:00:00Z"],
["a month above twelve", "2026-13-01T00:00:00Z"],
["a month below one", "2026-00-10T00:00:00Z"],
["an hour above twenty-three", "2026-10-09T24:00:00Z"],
["an offset hour above twenty-three", "2026-10-09T04:00:00+24:00"],
["a missing offset", "2026-10-09T04:00:00"],
["a lowercase offset designator", "2026-10-09T04:00:00z"],
["a repeated offset designator", "2026-10-09T04:00:00ZZ"],
["a basic-format timestamp", "20261009T040000Z"],
["a date without a time", "2026-10-09"],
["an offset without a colon", "2026-10-09T04:00:00+0200"],
["a lowercase time separator", "2026-10-09t04:00:00Z"],
["an empty string", ""],
["unrelated text", "not a timestamp"],
];
const CASES = [...ACCEPTED, ...REJECTED];
describe("offset datetime schema", () => {
it.each(ACCEPTED)("accepts %s", (_label, value) => {
expect(offsetDateTimeSchema.safeParse(value).success).toBe(true);
});
it.each(REJECTED)("rejects %s", (_label, value) => {
expect(offsetDateTimeSchema.safeParse(value).success).toBe(false);
});
it("classifies every case exactly as the replaced Zod validator does", () => {
const divergences = CASES.filter(
([, value]) =>
offsetDateTimeSchema.safeParse(value).success !==
replaced.safeParse(value).success,
).map(([label]) => label);
expect(divergences).toEqual([]);
});
it("emits a pattern that compiles in every mode a client may use", () => {
const emitted = z.toJSONSchema(
z.object({ observed_at: offsetDateTimeSchema }),
{ target: "draft-2020-12" },
);
const pattern = findPattern(emitted);
expect(pattern).toBeDefined();
if (pattern === undefined) return;
// Annex B and `u` accept the pattern the replaced validator emitted; `v` does
// not, which is the reason this schema exists.
expect(() => new RegExp(pattern, "u")).not.toThrow();
expect(() => new RegExp(pattern, "v")).not.toThrow();
expect(() => new RegExp(replacedSource(), "v")).toThrow();
});
});
const findPattern = (value: unknown): string | undefined => {
if (value === null || typeof value !== "object") return undefined;
if ("pattern" in value && typeof value.pattern === "string")
return value.pattern;
for (const entry of Object.values(value)) {
const found = findPattern(entry);
if (found !== undefined) return found;
}
return undefined;
};
/** The pattern the replaced Zod validator projects, recovered through the same path. */
const replacedSource = (): string => {
const emitted = z.toJSONSchema(z.object({ observed_at: replaced }), {
target: "draft-2020-12",
});
const pattern = findPattern(emitted);
if (pattern === undefined)
throw new Error("The replaced validator must project a pattern");
return pattern;
};
@@ -1,6 +1,8 @@
import canonicalize from "canonicalize";
import { z } from "zod";
import { IDENTIFIER_PATTERN } from "../stringPatterns.js";
import { jsonValueSchema } from "../jsonValue.js";
import {
comparableProcessObservationPayload,
@@ -8,7 +10,7 @@ import {
type ProcessObservationSource,
} from "./processObservation.js";
const identifierSchema = z.string().regex(/^[A-Za-z][A-Za-z0-9._\x2d]*$/u);
const identifierSchema = z.string().regex(IDENTIFIER_PATTERN);
/** Process observation families admitted by a declared trace specification. */
export const processTraceSourceSchema = processObservationSourceSchema;
@@ -1,11 +1,13 @@
import { z } from "zod";
import { STABLE_IDENTIFIER_PATTERN } from "./stringPatterns.js";
import { evidenceBundleSchema } from "./evidenceBundle.js";
import { digestSchema } from "./../domain/digests.js";
import { prefixedDigestSchema } from "./../domain/digests.js";
const evidenceIdSchema = prefixedDigestSchema("ev");
const stableIdSchema = z.string().regex(/^[A-Za-z][A-Za-z0-9._:\x2f\x2d]*$/u);
const stableIdSchema = z.string().regex(STABLE_IDENTIFIER_PATTERN);
const textSchema = z.string().trim().min(1);
export const obligationStatusSchema = z.enum([
+3 -1
View File
@@ -1,12 +1,14 @@
import { z } from "zod";
import { STABLE_IDENTIFIER_PATTERN } from "./stringPatterns.js";
import { evidenceBundleSchema } from "./evidenceBundle.js";
import { reconstructionObligationLedgerSchema } from "./reconstructionObligationLedgerSchemas.js";
import { digestSchema } from "./../domain/digests.js";
import { prefixedDigestSchema } from "./../domain/digests.js";
const evidenceIdSchema = prefixedDigestSchema("ev");
const stableIdSchema = z.string().regex(/^[A-Za-z][A-Za-z0-9._:\x2f\x2d]*$/u);
const stableIdSchema = z.string().regex(STABLE_IDENTIFIER_PATTERN);
const boundedTextSchema = z.string().trim().min(1);
export const readinessStatusSchema = z.enum([
@@ -1,11 +1,13 @@
import { z } from "zod";
import { IDENTIFIER_PATTERN } from "./stringPatterns.js";
import { digestSchema } from "./../domain/digests.js";
import { prefixedDigestSchema } from "./../domain/digests.js";
import { PROCESS_COMPARISON_DIMENSIONS } from "./process/processComparison.js";
const evidenceIdSchema = prefixedDigestSchema("ev");
const unknownIdSchema = prefixedDigestSchema("unk");
const claimIdSchema = z.string().regex(/^[A-Za-z][A-Za-z0-9._\x2d]*$/u);
const claimIdSchema = z.string().regex(IDENTIFIER_PATTERN);
const titleSchema = z.string().trim().min(1);
const verificationStatusSchema = z.enum(["pass", "fail", "unknown"]);
const commonClaim = {
+75
View File
@@ -0,0 +1,75 @@
import { expect, it } from "vitest";
import {
CANONICAL_BASE64_PATTERN,
IDENTIFIER_PATTERN,
STABLE_IDENTIFIER_PATTERN,
} from "./stringPatterns.js";
it("preserves identifier grammars under Unicode and Unicode-set compilation", () => {
const variants = [
{ pattern: IDENTIFIER_PATTERN, previous: /^[A-Za-z][A-Za-z0-9._-]*$/u },
{
pattern: STABLE_IDENTIFIER_PATTERN,
previous: /^[A-Za-z][A-Za-z0-9._:/-]*$/u,
},
];
const samples = [
"",
"a",
"A",
"_id",
"1id",
"é",
"aé",
"a/b:c",
"a-b.c_1",
"a\n",
"a\\b",
];
for (let code = 0; code < 128; code += 1) {
const character = String.fromCharCode(code);
samples.push(character, `A${character}`, `A${character}B`);
}
for (const { pattern, previous } of variants) {
for (const flag of ["u", "v"]) {
const projected = new RegExp(pattern.source, flag);
for (const sample of samples)
expect(projected.test(sample), JSON.stringify({ flag, sample })).toBe(
previous.test(sample),
);
}
}
});
it("preserves canonical base64 padding and pad-bit validation", () => {
const previous =
/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/][AQgw]==|[A-Za-z0-9+/]{2}[AEIMQUYcgkosw048]=)?$/;
const samples = [
"",
"AA==",
"AB==",
"AAA=",
"AAB=",
"////",
"A",
"AA",
"AAA",
"====",
"AA==\n",
"AA-_",
"a b c",
];
const alphabet =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
for (const first of alphabet)
for (const second of alphabet)
samples.push(`${first}${second}==`, `A${first}${second}=`);
for (const flag of ["u", "v"]) {
const projected = new RegExp(CANONICAL_BASE64_PATTERN.source, flag);
for (const sample of samples)
expect(projected.test(sample), JSON.stringify({ flag, sample })).toBe(
previous.test(sample),
);
}
});
+9
View File
@@ -0,0 +1,9 @@
/** Portable ASCII identifier starting with a letter. */
export const IDENTIFIER_PATTERN = /^[A-Za-z][A-Za-z0-9._\x2d]*$/u;
/** Stable evidence identifier, also admitting namespace and path separators. */
export const STABLE_IDENTIFIER_PATTERN = /^[A-Za-z][A-Za-z0-9._:\x2f\x2d]*$/u;
/** Canonical padded base64, including zero pad bits and the empty encoding. */
export const CANONICAL_BASE64_PATTERN =
/^(?:[A-Za-z0-9+\x2f]{4})*(?:[A-Za-z0-9+\x2f][AQgw]==|[A-Za-z0-9+\x2f]{2}[AEIMQUYcgkosw048]=)?$/;