| @@ -17,11 +17,17 @@ · 16 unmodified lines |
| 17 | // to the registrable origin of `token_url` so the per-AS reuse lookup can | 17 | // to the registrable origin of `token_url` so the per-AS reuse lookup can |
| 18 | // find it and mint no new duplicate. | 18 | // find it and mint no new duplicate. |
| 19 | // | 19 | // |
| 20 | // The DCR classification here is the single source of truth: `oauth-service`'s | 20 | // The predicates here are the SINGLE source of truth, imported (never |
| 21 | // `parseOAuthClientOrigin` calls `isDcrClassifiedRow`, and the SQL migrations | 21 | // re-encoded) by every call site: `oauth-service`'s `parseOAuthClientOrigin` |
| 22 | // mirror the exact same predicate. Keep the three in lockstep. | 22 | // calls `isDcrClassifiedRow` for the runtime reuse lookup, and BOTH GC |
| | 23 | // migrations (the local libSQL boot migration and the cloud code migration) |
| | 24 | // import `classifyOAuthClientGc` / `registrableOriginOfUrl` and decide in |
| | 25 | // process rather than rewriting the logic as SQL. There is no parallel SQL copy |
| | 26 | // to drift: changing a predicate here changes all three call sites at once. |
| 23 | // --------------------------------------------------------------------------- | 27 | // --------------------------------------------------------------------------- |
| 24 | | 28 | |
| | 29 | import { getDomain } from "tldts"; |
| | 30 | |
| 25 | /** Parse a string into a URL, or null when it is not a valid absolute URL. */ | 31 | /** Parse a string into a URL, or null when it is not a valid absolute URL. */ |
| 26 | export const parseUrl = (value: string): URL | null => { | 32 | export const parseUrl = (value: string): URL | null => { |
| 27 | if (!URL.canParse(value)) return null; | 33 | if (!URL.canParse(value)) return null; |
| @@ -44,37 +50,16 @@ export const canonicalIssuerUrl = (value: string | null | undefined): string | n · 16 unmodified lines |
| 44 | export const hostOfUrl = (value: string): string | null => | 50 | export const hostOfUrl = (value: string): string | null => |
| 45 | parseUrl(value)?.host.toLowerCase() ?? null; | 51 | parseUrl(value)?.host.toLowerCase() ?? null; |
| 46 | | 52 | |
| 47 | // Two-label public suffixes we must not collapse past (so `api.foo.co.uk` | 53 | /** The registrable domain of a hostname (eTLD+1), backed by the full Public |
| 48 | // registers under `foo.co.uk`, not `co.uk`). A pragmatic short list — the full | 54 | * Suffix List via `tldts` (so `api.foo.co.uk` → `foo.co.uk`, `x.co.in` → |
| 49 | // PSL is overkill for keying DCR clients. | 55 | * `x.co.in`, etc., not a hardcoded suffix subset). `tldts.getDomain` returns |
| 50 | const commonTwoPartPublicSuffixes = new Set([ | 56 | * null for hosts with no registrable domain (`localhost`, bare IPv4/IPv6, |
| 51 | "co.uk", | 57 | * single-label hosts); those fall back to the raw lowercase hostname so |
| 52 | "org.uk", | 58 | * local-dev token hosts still key on their exact host. This mirrors how |
| 53 | "ac.uk", | 59 | * `packages/react/src/plugins/use-effective-oauth-client.tsx` uses tldts. */ |
| 54 | "gov.uk", | | |
| 55 | "com.au", | | |
| 56 | "net.au", | | |
| 57 | "org.au", | | |
| 58 | "co.jp", | | |
| 59 | "co.nz", | | |
| 60 | "com.br", | | |
| 61 | "com.mx", | | |
| 62 | "com.sg", | | |
| 63 | ]); | | |
| 64 | | | |
| 65 | /** The registrable domain of a hostname (eTLD+1), with the pragmatic two-part | | |
| 66 | * public-suffix carve-out. `localhost`, bare IPv4, and anything with a port | | |
| 67 | * are returned unchanged. */ | | |
| 68 | export const registrableHostname = (hostname: string): string => { | 60 | export const registrableHostname = (hostname: string): string => { |
| 69 | const host = hostname.toLowerCase(); | 61 | const host = hostname.toLowerCase(); |
| 70 | if (host === "localhost" || /^\d+\.\d+\.\d+\.\d+$/.test(host) || host.includes(":")) { | 62 | return getDomain(host) ?? host; |
| 71 | return host; | | |
| 72 | } | | |
| 73 | const labels = host.split(".").filter(Boolean); | | |
| 74 | if (labels.length <= 2) return host; | | |
| 75 | const suffix = labels.slice(-2).join("."); | | |
| 76 | const labelCount = commonTwoPartPublicSuffixes.has(suffix) ? 3 : 2; | | |
| 77 | return labels.slice(-labelCount).join("."); | | |
| 78 | }; | 63 | }; |
| 79 | | 64 | |
| 80 | /** The registrable host (incl. port when the hostname is registered verbatim) | 65 | /** The registrable host (incl. port when the hostname is registered verbatim) |