Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

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

morluto954badbaaadd
diff --git a/src/browser/CdpSafeMetadata.ts b/src/browser/CdpSafeMetadata.ts
index 6921411cf..db9bd3878 100644
--- a/src/browser/CdpSafeMetadata.ts
+++ b/src/browser/CdpSafeMetadata.ts
@@ -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 => {
diff --git a/src/domain/browserScenarioValues.ts b/src/domain/browserScenarioValues.ts
index f6f223b42..dd47a0081 100644
--- a/src/domain/browserScenarioValues.ts
+++ b/src/domain/browserScenarioValues.ts
@@ -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,
diff --git a/src/domain/completionLedgerGeneration.ts b/src/domain/completionLedgerGeneration.ts
index a1f95bdd6..f356b9410 100644
--- a/src/domain/completionLedgerGeneration.ts
+++ b/src/domain/completionLedgerGeneration.ts
@@ -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),
diff --git a/src/domain/conformancePackage.ts b/src/domain/conformancePackage.ts
index 2791c7f61..0185f5363 100644
--- a/src/domain/conformancePackage.ts
+++ b/src/domain/conformancePackage.ts
@@ -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,
diff --git a/src/domain/evidenceCompletionLedger.ts b/src/domain/evidenceCompletionLedger.ts
index ab4c85993..bf3e0f9e9 100644
--- a/src/domain/evidenceCompletionLedger.ts
+++ b/src/domain/evidenceCompletionLedger.ts
@@ -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([
diff --git a/src/domain/native/binaryLayout.ts b/src/domain/native/binaryLayout.ts
index eed280968..e2a8d20cf 100644
--- a/src/domain/native/binaryLayout.ts
+++ b/src/domain/native/binaryLayout.ts
@@ -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(),
diff --git a/src/domain/native/recordedCrash.ts b/src/domain/native/recordedCrash.ts
index 6639c447b..adef5a7ea 100644
--- a/src/domain/native/recordedCrash.ts
+++ b/src/domain/native/recordedCrash.ts
@@ -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(),
diff --git a/src/domain/offsetDateTime.test.ts b/src/domain/offsetDateTime.test.ts
new file mode 100644
index 000000000..6d334109f
--- /dev/null
+++ b/src/domain/offsetDateTime.test.ts
@@ -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;
+};
diff --git a/src/domain/process/processTraceSpecification.ts b/src/domain/process/processTraceSpecification.ts
index 79dea64f8..79e78c0c1 100644
--- a/src/domain/process/processTraceSpecification.ts
+++ b/src/domain/process/processTraceSpecification.ts
@@ -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;
diff --git a/src/domain/reconstructionObligationLedgerSchemas.ts b/src/domain/reconstructionObligationLedgerSchemas.ts
index fe1e1b3e6..bc61969a8 100644
--- a/src/domain/reconstructionObligationLedgerSchemas.ts
+++ b/src/domain/reconstructionObligationLedgerSchemas.ts
@@ -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([
diff --git a/src/domain/reconstructionReadinessSchemas.ts b/src/domain/reconstructionReadinessSchemas.ts
index c32637c76..d79dfcaf7 100644
--- a/src/domain/reconstructionReadinessSchemas.ts
+++ b/src/domain/reconstructionReadinessSchemas.ts
@@ -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([
diff --git a/src/domain/reconstructionVerificationSchemas.ts b/src/domain/reconstructionVerificationSchemas.ts
index 0da3a3158..9831947bc 100644
--- a/src/domain/reconstructionVerificationSchemas.ts
+++ b/src/domain/reconstructionVerificationSchemas.ts
@@ -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 = {
diff --git a/src/domain/stringPatterns.test.ts b/src/domain/stringPatterns.test.ts
new file mode 100644
index 000000000..5e35d2ccd
--- /dev/null
+++ b/src/domain/stringPatterns.test.ts
@@ -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),
+ );
+ }
+});
diff --git a/src/domain/stringPatterns.ts b/src/domain/stringPatterns.ts
new file mode 100644
index 000000000..8aa65cb75
--- /dev/null
+++ b/src/domain/stringPatterns.ts
@@ -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]=)?$/;