keryx 0.42.2 → 0.42.4

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.
package/README.md CHANGED
@@ -252,6 +252,16 @@ If you're coming from ActionHero, the biggest changes are: unified controllers (
252
252
 
253
253
  Each application has its own `Dockerfile`, and a `docker-compose.yml` runs them together. You probably won't use this exact setup in production, but it shows how the pieces fit together.
254
254
 
255
+ **Run `keryx build` at image build time.** It pre-generates the OpenAPI response schemas into `.cache/swagger-schemas.json` so the server doesn't have to derive them from your action return types on every boot:
256
+
257
+ ```dockerfile
258
+ COPY . .
259
+ RUN cd backend && bun keryx.ts build
260
+ CMD ["bun", "keryx.ts", "start"]
261
+ ```
262
+
263
+ See the [deployment guide](https://keryxjs.com/guide/deployment#build-step) for details.
264
+
255
265
  ## Documentation
256
266
 
257
267
  Full docs at [keryxjs.com](https://keryxjs.com), including:
@@ -5,6 +5,7 @@ import {
5
5
  computeActionsHash,
6
6
  generateSwaggerSchemas,
7
7
  loadCachedSchemas,
8
+ SCHEMA_CACHE_RELATIVE_PATH,
8
9
  writeSchemasCache,
9
10
  } from "../util/swaggerSchemaGenerator";
10
11
 
@@ -34,6 +35,18 @@ export class SwaggerInitializer extends Initializer {
34
35
  return { responseSchemas: cached.responseSchemas };
35
36
  }
36
37
 
38
+ // No usable cache, so we fall through to ts-morph. This is by far the most
39
+ // expensive thing that happens at boot (it can peak over 1 GB of RSS on a
40
+ // large app) and it is the usual cause of an unexplained OOM kill during
41
+ // startup on a memory-capped host. Warn *before* doing the work — a process
42
+ // that cannot afford the allocation never reaches a message logged after it.
43
+ const cacheState = cached
44
+ ? `swagger schema cache at ${SCHEMA_CACHE_RELATIVE_PATH} is stale`
45
+ : `no swagger schema cache found at ${SCHEMA_CACHE_RELATIVE_PATH}`;
46
+ logger.warn(
47
+ `${cacheState}; generating response schemas via ts-morph. This takes seconds and can peak over 1 GB of RSS, which will OOM a memory-capped host. Run \`keryx build\` at build time to pre-generate them and skip this step — see \`keryx build --help\`.`,
48
+ );
49
+
37
50
  // Generate schemas via ts-morph
38
51
  let responseSchemas: Record<string, JSONSchema> = {};
39
52
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "keryx",
3
- "version": "0.42.2",
3
+ "version": "0.42.4",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/util/cli.ts CHANGED
@@ -107,7 +107,9 @@ export async function buildProgram(opts: {
107
107
  .description(
108
108
  "Run build-time code generation. Analyzes Action return types via ts-morph\n" +
109
109
  "and writes .cache/swagger-schemas.json so the server can skip this step at\n" +
110
- "startup. Recommended for memory-constrained environments (e.g., Docker).\n\n" +
110
+ "startup. Strongly recommended for memory-constrained environments (e.g.,\n" +
111
+ "Docker): the analysis can peak over 1 GB of RSS and will OOM a small\n" +
112
+ "container if it runs at boot instead. Needs no database and no Redis.\n\n" +
111
113
  "Example Dockerfile usage:\n" +
112
114
  " COPY . .\n" +
113
115
  " RUN bun keryx.ts build\n" +
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. */
@@ -2,6 +2,13 @@ import { mkdir } from "fs/promises";
2
2
  import path from "path";
3
3
  import type { Type } from "ts-morph";
4
4
 
5
+ /** Directory (relative to the app root) holding build-time generated artifacts. */
6
+ export const SCHEMA_CACHE_DIR = ".cache";
7
+ /** File name of the pre-generated swagger response schema cache. */
8
+ export const SCHEMA_CACHE_FILE = "swagger-schemas.json";
9
+ /** Human-readable path to the schema cache, for log and doc messages. */
10
+ export const SCHEMA_CACHE_RELATIVE_PATH = `${SCHEMA_CACHE_DIR}/${SCHEMA_CACHE_FILE}`;
11
+
5
12
  export type JSONSchema = {
6
13
  type?: string;
7
14
  properties?: Record<string, JSONSchema>;
@@ -202,7 +209,7 @@ export async function loadCachedSchemas(rootDir: string): Promise<{
202
209
  hash: string;
203
210
  responseSchemas: Record<string, JSONSchema>;
204
211
  } | null> {
205
- const cacheFile = path.join(rootDir, ".cache", "swagger-schemas.json");
212
+ const cacheFile = path.join(rootDir, SCHEMA_CACHE_DIR, SCHEMA_CACHE_FILE);
206
213
  const cacheFileHandle = Bun.file(cacheFile);
207
214
  if (await cacheFileHandle.exists()) {
208
215
  try {
@@ -325,10 +332,10 @@ export async function writeSchemasCache(
325
332
  rootDir: string,
326
333
  data: { hash: string; responseSchemas: Record<string, JSONSchema> },
327
334
  ): Promise<void> {
328
- const cacheDir = path.join(rootDir, ".cache");
335
+ const cacheDir = path.join(rootDir, SCHEMA_CACHE_DIR);
329
336
  await mkdir(cacheDir, { recursive: true });
330
337
  await Bun.write(
331
- path.join(cacheDir, "swagger-schemas.json"),
338
+ path.join(cacheDir, SCHEMA_CACHE_FILE),
332
339
  JSON.stringify(data, null, 2),
333
340
  );
334
341
  }