|
| 1 | +/** |
| 2 | + * Fetch and parse a brand.json document for the resolver. |
| 3 | + * |
| 4 | + * 32 KB body cap per spec §"SSRF and rate-limit hardening". JSON parse |
| 5 | + * errors map to `request_signature_brand_json_unreachable` (we treat |
| 6 | + * "structurally invalid" the same as "couldn't reach it" for verifier |
| 7 | + * purposes — both leave us without a trust root). |
| 8 | + */ |
| 9 | +import { AgentResolverError } from "./errors.js"; |
| 10 | +import { |
| 11 | + strictGet, |
| 12 | + etldPlusOne, |
| 13 | + type StrictFetchOptions, |
| 14 | + type StrictFetchResult, |
| 15 | +} from "./safe-fetch-strict.js"; |
| 16 | + |
| 17 | +export const BRAND_JSON_MAX_BYTES = 32 * 1024; |
| 18 | + |
| 19 | +export interface BrandAgentEntry { |
| 20 | + type?: string; |
| 21 | + url: string; |
| 22 | + id?: string; |
| 23 | + jwks_uri?: string; |
| 24 | + description?: string; |
| 25 | +} |
| 26 | + |
| 27 | +export interface AuthorizedOperator { |
| 28 | + domain: string; |
| 29 | + brands?: string[]; |
| 30 | + countries?: string[]; |
| 31 | +} |
| 32 | + |
| 33 | +export interface ParsedBrandJson { |
| 34 | + agents: BrandAgentEntry[]; |
| 35 | + authorized_operators: AuthorizedOperator[]; |
| 36 | + raw: Record<string, unknown>; |
| 37 | +} |
| 38 | + |
| 39 | +export interface BrandJsonFetchResult { |
| 40 | + parsed: ParsedBrandJson; |
| 41 | + fetch: StrictFetchResult; |
| 42 | + brand_url: string; |
| 43 | +} |
| 44 | + |
| 45 | +export async function fetchBrandJson( |
| 46 | + brandUrl: string, |
| 47 | + opts: Omit<StrictFetchOptions, "maxBytes" | "hostBucketKey"> & { |
| 48 | + rateLimiter?: StrictFetchOptions["rateLimiter"]; |
| 49 | + }, |
| 50 | +): Promise<BrandJsonFetchResult> { |
| 51 | + const u = new URL(brandUrl); |
| 52 | + const result = await strictGet(brandUrl, { |
| 53 | + maxBytes: BRAND_JSON_MAX_BYTES, |
| 54 | + hostBucketKey: etldPlusOne(u.hostname), |
| 55 | + rateLimiter: opts.rateLimiter, |
| 56 | + timeoutMs: opts.timeoutMs, |
| 57 | + acceptHeader: "application/json", |
| 58 | + }); |
| 59 | + if (result.status < 200 || result.status >= 300) { |
| 60 | + throw new AgentResolverError( |
| 61 | + "request_signature_brand_json_unreachable", |
| 62 | + { |
| 63 | + brand_url: brandUrl, |
| 64 | + http_status: result.status, |
| 65 | + last_attempt_at: new Date().toISOString(), |
| 66 | + }, |
| 67 | + ); |
| 68 | + } |
| 69 | + let raw: unknown; |
| 70 | + try { |
| 71 | + raw = JSON.parse(result.body.toString("utf8")); |
| 72 | + } catch { |
| 73 | + throw new AgentResolverError( |
| 74 | + "request_signature_brand_json_unreachable", |
| 75 | + { |
| 76 | + brand_url: brandUrl, |
| 77 | + reason: "brand.json is not valid JSON", |
| 78 | + last_attempt_at: new Date().toISOString(), |
| 79 | + }, |
| 80 | + ); |
| 81 | + } |
| 82 | + if (!raw || typeof raw !== "object" || Array.isArray(raw)) { |
| 83 | + throw new AgentResolverError( |
| 84 | + "request_signature_brand_json_unreachable", |
| 85 | + { |
| 86 | + brand_url: brandUrl, |
| 87 | + reason: "brand.json must be a JSON object", |
| 88 | + last_attempt_at: new Date().toISOString(), |
| 89 | + }, |
| 90 | + ); |
| 91 | + } |
| 92 | + const obj = raw as Record<string, unknown>; |
| 93 | + const agents = parseAgents(obj.agents); |
| 94 | + const authorized_operators = parseAuthorizedOperators(obj.authorized_operators); |
| 95 | + return { |
| 96 | + parsed: { agents, authorized_operators, raw: obj }, |
| 97 | + fetch: result, |
| 98 | + brand_url: brandUrl, |
| 99 | + }; |
| 100 | +} |
| 101 | + |
| 102 | +function parseAgents(value: unknown): BrandAgentEntry[] { |
| 103 | + if (!Array.isArray(value)) return []; |
| 104 | + const out: BrandAgentEntry[] = []; |
| 105 | + for (const item of value) { |
| 106 | + if (!item || typeof item !== "object") continue; |
| 107 | + const a = item as Record<string, unknown>; |
| 108 | + if (typeof a.url !== "string") continue; |
| 109 | + out.push({ |
| 110 | + type: typeof a.type === "string" ? a.type : undefined, |
| 111 | + url: a.url, |
| 112 | + id: typeof a.id === "string" ? a.id : undefined, |
| 113 | + jwks_uri: typeof a.jwks_uri === "string" ? a.jwks_uri : undefined, |
| 114 | + description: typeof a.description === "string" ? a.description : undefined, |
| 115 | + }); |
| 116 | + } |
| 117 | + return out; |
| 118 | +} |
| 119 | + |
| 120 | +function parseAuthorizedOperators(value: unknown): AuthorizedOperator[] { |
| 121 | + if (!Array.isArray(value)) return []; |
| 122 | + const out: AuthorizedOperator[] = []; |
| 123 | + for (const item of value) { |
| 124 | + if (!item || typeof item !== "object") continue; |
| 125 | + const a = item as Record<string, unknown>; |
| 126 | + if (typeof a.domain !== "string") continue; |
| 127 | + out.push({ |
| 128 | + domain: a.domain.toLowerCase(), |
| 129 | + brands: Array.isArray(a.brands) ? a.brands.filter((x): x is string => typeof x === "string") : undefined, |
| 130 | + countries: Array.isArray(a.countries) ? a.countries.filter((x): x is string => typeof x === "string") : undefined, |
| 131 | + }); |
| 132 | + } |
| 133 | + return out; |
| 134 | +} |
0 commit comments