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
582582 "@standard-schema/spec": "^1.1.0",
583583 "fractional-indexing": "^3.2.0",
584584 "oauth4webapi": "^3.8.5",
585+ "tldts": "^7.0.28",
585586 },
586587 "devDependencies": {
587588 "@effect/atom-react": "catalog:",
packages/core/sdk/package.json+2−1
@@ -96,7 +96,8 @@ · 95 unmodified lines
9696 "@executor-js/fumadb": "workspace:*",
9797 "@standard-schema/spec": "^1.1.0",
9898 "fractional-indexing": "^3.2.0",
99− "oauth4webapi": "^3.8.5"
99+ "oauth4webapi": "^3.8.5",
100+ "tldts": "^7.0.28"
100101 },
101102 "devDependencies": {
102103 "@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
183183 );
184184 });
185185
186− test("respects the two-part public suffix carve-out", () => {
186+ test("respects multi-label public suffixes via the full PSL", () => {
187187 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+ );
188199 });
189200
190201 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
1717 // to the registrable origin of `token_url` so the per-AS reuse lookup can
1818 // find it and mint no new duplicate.
1919 //
20−// The DCR classification here is the single source of truth: `oauth-service`'s
21−// `parseOAuthClientOrigin` calls `isDcrClassifiedRow`, and the SQL migrations
22−// mirror the exact same predicate. Keep the three in lockstep.
20+// The predicates here are the SINGLE source of truth, imported (never
21+// re-encoded) by every call site: `oauth-service`'s `parseOAuthClientOrigin`
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.
2327 // ---------------------------------------------------------------------------
2428
29+import { getDomain } from "tldts";
30+
2531 /** Parse a string into a URL, or null when it is not a valid absolute URL. */
2632 export const parseUrl = (value: string): URL | null => {
2733 if (!URL.canParse(value)) return null;
@@ -44,37 +50,16 @@ export const canonicalIssuerUrl = (value: string | null | undefined): string | n · 16 unmodified lines
4450 export const hostOfUrl = (value: string): string | null =>
4551 parseUrl(value)?.host.toLowerCase() ?? null;
4652
47−// Two-label public suffixes we must not collapse past (so `api.foo.co.uk`
48−// registers under `foo.co.uk`, not `co.uk`). A pragmatic short list — the full
49−// PSL is overkill for keying DCR clients.
50−const commonTwoPartPublicSuffixes = new Set([
51− "co.uk",
52− "org.uk",
53− "ac.uk",
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. */
53+/** The registrable domain of a hostname (eTLD+1), backed by the full Public
54+ * Suffix List via `tldts` (so `api.foo.co.uk` → `foo.co.uk`, `x.co.in` →
55+ * `x.co.in`, etc., not a hardcoded suffix subset). `tldts.getDomain` returns
56+ * null for hosts with no registrable domain (`localhost`, bare IPv4/IPv6,
57+ * single-label hosts); those fall back to the raw lowercase hostname so
58+ * local-dev token hosts still key on their exact host. This mirrors how
59+ * `packages/react/src/plugins/use-effective-oauth-client.tsx` uses tldts. */
6860 export const registrableHostname = (hostname: string): string => {
6961 const host = hostname.toLowerCase();
70− if (host === "localhost" || /^\d+\.\d+\.\d+\.\d+$/.test(host) || host.includes(":")) {
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(".");
62+ return getDomain(host) ?? host;
7863 };
7964
8065 /** 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