keryx 0.42.3 → 0.42.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/util/oauth.ts +56 -8
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "keryx",
3
- "version": "0.42.3",
3
+ "version": "0.42.5",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/util/oauth.ts CHANGED
@@ -10,6 +10,13 @@ const DANGEROUS_SCHEMES = new Set([
10
10
  "file:",
11
11
  ]);
12
12
 
13
+ /**
14
+ * Hostnames RFC 8252 treats as loopback redirect destinations. `new URL()` reports
15
+ * IPv6 hostnames bracketed, so `[::1]` is stored in that form to compare directly
16
+ * against `parsed.hostname`.
17
+ */
18
+ const LOOPBACK_HOSTS = new Set(["localhost", "127.0.0.1", "[::1]"]);
19
+
13
20
  /**
14
21
  * Validate an OAuth redirect URI. Rules by scheme:
15
22
  * - `https:` — allowed for any host (remote web callbacks).
@@ -52,11 +59,7 @@ export function validateRedirectUri(uri: string): {
52
59
  }
53
60
 
54
61
  if (parsed.protocol === "http:") {
55
- const isLoopback =
56
- parsed.hostname === "localhost" ||
57
- parsed.hostname === "127.0.0.1" ||
58
- parsed.hostname === "[::1]";
59
- if (!isLoopback) {
62
+ if (!LOOPBACK_HOSTS.has(parsed.hostname)) {
60
63
  return {
61
64
  valid: false,
62
65
  error: "Redirect URI must use HTTPS for non-localhost URIs",
@@ -69,14 +72,59 @@ export function validateRedirectUri(uri: string): {
69
72
  }
70
73
 
71
74
  /**
72
- * Compare two redirect URIs with exact string matching, as required by
73
- * RFC 6749 §3.1.2.3 and RFC 8252 §8.4.
75
+ * Compare a registered redirect URI against a requested one. The comparison is exact
76
+ * string matching, as required by RFC 6749 §3.1.2.3 and RFC 8252 §8.4, with a single
77
+ * carve-out: for `http:` loopback URIs the **port is ignored**, because RFC 8252 §7.3
78
+ * requires the authorization server to "allow any port to be specified at the time of
79
+ * the request" — a native app takes an ephemeral loopback port from the OS when it
80
+ * starts its callback listener, so it cannot register the port ahead of time. This is
81
+ * what lets a CLI client that declares `http://localhost/callback` authorize with
82
+ * `http://localhost:49152/callback`.
83
+ *
84
+ * Four properties keep the carve-out narrow:
85
+ * - Both sides must be `http:`. Port flexibility is a loopback concession, so
86
+ * `https://example.com/cb` still does not match `https://example.com:8443/cb`.
87
+ * - Hostnames must be equal, and loopback. `localhost` does not match `127.0.0.1`;
88
+ * RFC 8252 §8.3 treats those as distinct registrations.
89
+ * - Path, query, fragment, and userinfo stay exact, so appending `?state=xyz` to a
90
+ * registered URI is still rejected.
91
+ * - The port is loose in both directions — a registered `http://localhost:3000/cb`
92
+ * matches a requested `http://localhost:4000/cb`, since the RFC allows *any* port
93
+ * at request time.
94
+ *
95
+ * @param registeredUri - A redirect URI from the client record (or its Client ID
96
+ * Metadata Document).
97
+ * @param requestedUri - The `redirect_uri` on the authorization request.
98
+ * @returns `true` when the requested URI is authorized by the registered one.
74
99
  */
75
100
  export function redirectUrisMatch(
76
101
  registeredUri: string,
77
102
  requestedUri: string,
78
103
  ): boolean {
79
- return registeredUri === requestedUri;
104
+ if (registeredUri === requestedUri) return true;
105
+
106
+ let registered: URL;
107
+ let requested: URL;
108
+ try {
109
+ registered = new URL(registeredUri);
110
+ requested = new URL(requestedUri);
111
+ } catch {
112
+ return false;
113
+ }
114
+
115
+ if (registered.protocol !== "http:" || requested.protocol !== "http:") {
116
+ return false;
117
+ }
118
+ if (registered.hostname !== requested.hostname) return false;
119
+ if (!LOOPBACK_HOSTS.has(registered.hostname)) return false;
120
+
121
+ return (
122
+ registered.pathname === requested.pathname &&
123
+ registered.search === requested.search &&
124
+ registered.hash === requested.hash &&
125
+ registered.username === requested.username &&
126
+ registered.password === requested.password
127
+ );
80
128
  }
81
129
 
82
130
  /** Encode a byte array as a URL-safe base64 string (no padding). Used for PKCE code challenges. */