History

Key DCR registrable domains off the full PSL via tldts

registrableHostname approximated eTLD+1 with a hardcoded 12-entry two-part suffix set, so hosts under suffixes it didn't list collapsed too far: accounts.example.co.in became co.in, colliding unrelated tenants in the GC deletion predicate and the issuer backfill. Depend on tldts (the version packages/react already pins) and use getDomain, falling back to the raw lowercase hostname when it returns null (localhost, IPs, single-label hosts). This matches how the react picker computes roots, so the reuse key and the picker agree on the same URL. Also refresh the module header: the predicates are one source of truth imported by three call sites, not three copies kept in lockstep.

Rhys Sullivan committed 4 months ago

Commit 07130871 parent 0c2c9654 files · +32 −34Browse files

Unified · Split

4 files
  1. bun.lock +1 −0
  2. packages/core/sdk/package.json +2 −1
  3. packages/core/sdk/src/oauth-gc.test.ts +12 −1
  4. packages/core/sdk/src/oauth-gc.ts +17 −32
bun.lock+1−0
@@ -582,6 +582,7 @@ · 581 unmodified lines
582 "@standard-schema/spec": "^1.1.0",582 "@standard-schema/spec": "^1.1.0",
583 "fractional-indexing": "^3.2.0",583 "fractional-indexing": "^3.2.0",
584 "oauth4webapi": "^3.8.5",584 "oauth4webapi": "^3.8.5",
585 "tldts": "^7.0.28",
585 },586 },
586 "devDependencies": {587 "devDependencies": {
587 "@effect/atom-react": "catalog:",588 "@effect/atom-react": "catalog:",
packages/core/sdk/package.json+2−1
@@ -96,7 +96,8 @@ · 95 unmodified lines
96 "@executor-js/fumadb": "workspace:*",96 "@executor-js/fumadb": "workspace:*",
97 "@standard-schema/spec": "^1.1.0",97 "@standard-schema/spec": "^1.1.0",
98 "fractional-indexing": "^3.2.0",98 "fractional-indexing": "^3.2.0",
99 "oauth4webapi": "^3.8.5"99 "oauth4webapi": "^3.8.5",
100 "tldts": "^7.0.28"
100 },101 },
101 "devDependencies": {102 "devDependencies": {
102 "@effect/atom-react": "catalog:",103 "@effect/atom-react": "catalog:",
packages/core/sdk/src/oauth-gc.test.ts+12−1
@@ -183,8 +183,19 @@ describe("registrableOriginOfUrl (issuer backfill value)", () => { · 182 unmodified lines
183 );183 );
184 });184 });
185 185
186 test("respects the two-part public suffix carve-out", () => {186 test("respects multi-label public suffixes via the full PSL", () => {
187 expect(registrableOriginOfUrl("https://api.foo.co.uk/token")).toBe("https://foo.co.uk");187 expect(registrableOriginOfUrl("https://api.foo.co.uk/token")).toBe("https://foo.co.uk");
188 // Suffixes the old hardcoded 12-entry list missed: .co.in, .com.cn, .co.za.
189 // `accounts.example.co.in` must collapse to `example.co.in`, not `co.in`.
190 expect(registrableOriginOfUrl("https://accounts.example.co.in/token")).toBe(
191 "https://example.co.in",
192 );
193 expect(registrableOriginOfUrl("https://oauth.example.com.cn/token")).toBe(
194 "https://example.com.cn",
195 );
196 expect(registrableOriginOfUrl("https://login.example.co.za/token")).toBe(
197 "https://example.co.za",
198 );
188 });199 });
189 200
190 test("preserves scheme and port for loopback / dev token hosts", () => {201 test("preserves scheme and port for loopback / dev token hosts", () => {
packages/core/sdk/src/oauth-gc.ts+17−32
@@ -17,11 +17,17 @@ · 16 unmodified lines
17// to the registrable origin of `token_url` so the per-AS reuse lookup can17// 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`'s20// The predicates here are the SINGLE source of truth, imported (never
21// `parseOAuthClientOrigin` calls `isDcrClassifiedRow`, and the SQL migrations21// 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
29import { 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. */
26export const parseUrl = (value: string): URL | null => {32export 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
44export const hostOfUrl = (value: string): string | null =>50export 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 full54 * 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
50const 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. */
68export const registrableHostname = (hostname: string): string => {60export 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)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t