@powerhousedao/switchboard 6.2.3 → 6.3.0-dev.10
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/.tsbuild/test/ai-gateway.test.d.ts +2 -0
- package/.tsbuild/test/ai-gateway.test.d.ts.map +1 -0
- package/.tsbuild/test/attachments/reserve-metadata-parity.test.d.ts +2 -0
- package/.tsbuild/test/attachments/reserve-metadata-parity.test.d.ts.map +1 -0
- package/.tsbuild/test/boot-auth-config.test.d.ts +2 -0
- package/.tsbuild/test/boot-auth-config.test.d.ts.map +1 -0
- package/.tsbuild/test/boot-desktop-auth.test.d.ts +2 -0
- package/.tsbuild/test/boot-desktop-auth.test.d.ts.map +1 -0
- package/.tsbuild/test/boot-env-isolation.test.d.ts +2 -0
- package/.tsbuild/test/boot-env-isolation.test.d.ts.map +1 -0
- package/.tsbuild/test/boot-helpers.d.ts +5 -0
- package/.tsbuild/test/boot-helpers.d.ts.map +1 -0
- package/.tsbuild/test/boot-inspection-enabled.test.d.ts +2 -0
- package/.tsbuild/test/boot-inspection-enabled.test.d.ts.map +1 -0
- package/.tsbuild/test/boot-mcp-auth.test.d.ts +2 -0
- package/.tsbuild/test/boot-mcp-auth.test.d.ts.map +1 -0
- package/.tsbuild/test/boot-port-zero.test.d.ts +2 -0
- package/.tsbuild/test/boot-port-zero.test.d.ts.map +1 -0
- package/.tsbuild/test/boot-preconditions.test.d.ts +2 -0
- package/.tsbuild/test/boot-preconditions.test.d.ts.map +1 -0
- package/.tsbuild/test/boot-privacy.test.d.ts +2 -0
- package/.tsbuild/test/boot-privacy.test.d.ts.map +1 -0
- package/.tsbuild/test/boot-ready-drives.test.d.ts +2 -0
- package/.tsbuild/test/boot-ready-drives.test.d.ts.map +1 -0
- package/.tsbuild/test/boot-undo-v3-flag.test.d.ts +2 -0
- package/.tsbuild/test/boot-undo-v3-flag.test.d.ts.map +1 -0
- package/.tsbuild/test/boot-workflow-singleton-conflict.test.d.ts +2 -0
- package/.tsbuild/test/boot-workflow-singleton-conflict.test.d.ts.map +1 -0
- package/.tsbuild/test/boot-workflow-singleton-retry.test.d.ts +2 -0
- package/.tsbuild/test/boot-workflow-singleton-retry.test.d.ts.map +1 -0
- package/.tsbuild/test/config-from-env.test.d.ts +2 -0
- package/.tsbuild/test/config-from-env.test.d.ts.map +1 -0
- package/.tsbuild/test/config-merge.test.d.ts +2 -0
- package/.tsbuild/test/config-merge.test.d.ts.map +1 -0
- package/.tsbuild/test/config-schema.test.d.ts +2 -0
- package/.tsbuild/test/config-schema.test.d.ts.map +1 -0
- package/.tsbuild/test/default-composition.test.d.ts +2 -0
- package/.tsbuild/test/default-composition.test.d.ts.map +1 -0
- package/.tsbuild/test/default-drive-protocol-versions.test.d.ts +2 -0
- package/.tsbuild/test/default-drive-protocol-versions.test.d.ts.map +1 -0
- package/.tsbuild/test/default-drive-slug.test.d.ts +2 -0
- package/.tsbuild/test/default-drive-slug.test.d.ts.map +1 -0
- package/.tsbuild/test/delegated-publish.test.d.ts +2 -0
- package/.tsbuild/test/delegated-publish.test.d.ts.map +1 -0
- package/.tsbuild/test/durable-node-fs-pg16.test.d.ts +2 -0
- package/.tsbuild/test/durable-node-fs-pg16.test.d.ts.map +1 -0
- package/.tsbuild/test/fixtures/mixed-subgraphs/subgraphs/greetings/index.d.ts +13 -0
- package/.tsbuild/test/fixtures/mixed-subgraphs/subgraphs/greetings/index.d.ts.map +1 -0
- package/.tsbuild/test/fixtures/mixed-subgraphs/subgraphs/index.d.ts +3 -0
- package/.tsbuild/test/fixtures/mixed-subgraphs/subgraphs/index.d.ts.map +1 -0
- package/.tsbuild/test/fixtures/mixed-subgraphs/subgraphs/tally/index.d.ts +7 -0
- package/.tsbuild/test/fixtures/mixed-subgraphs/subgraphs/tally/index.d.ts.map +1 -0
- package/.tsbuild/test/inspection-throttle.test.d.ts +2 -0
- package/.tsbuild/test/inspection-throttle.test.d.ts.map +1 -0
- package/.tsbuild/test/keyframe-reclaim-store.test.d.ts +2 -0
- package/.tsbuild/test/keyframe-reclaim-store.test.d.ts.map +1 -0
- package/.tsbuild/test/observability-signals.test.d.ts +2 -0
- package/.tsbuild/test/observability-signals.test.d.ts.map +1 -0
- package/.tsbuild/test/permissions-backfill.test.d.ts +2 -0
- package/.tsbuild/test/permissions-backfill.test.d.ts.map +1 -0
- package/.tsbuild/test/pglite-preflight.test.d.ts +2 -0
- package/.tsbuild/test/pglite-preflight.test.d.ts.map +1 -0
- package/.tsbuild/test/pglite-storage-dirs.test.d.ts +2 -0
- package/.tsbuild/test/pglite-storage-dirs.test.d.ts.map +1 -0
- package/.tsbuild/test/pglite-store.test.d.ts +2 -0
- package/.tsbuild/test/pglite-store.test.d.ts.map +1 -0
- package/.tsbuild/test/plugin-config-schemas.test.d.ts +2 -0
- package/.tsbuild/test/plugin-config-schemas.test.d.ts.map +1 -0
- package/.tsbuild/test/plugin-presence.test.d.ts +2 -0
- package/.tsbuild/test/plugin-presence.test.d.ts.map +1 -0
- package/.tsbuild/test/port-probe.test.d.ts +2 -0
- package/.tsbuild/test/port-probe.test.d.ts.map +1 -0
- package/.tsbuild/test/processor-timing-log.test.d.ts +2 -0
- package/.tsbuild/test/processor-timing-log.test.d.ts.map +1 -0
- package/.tsbuild/test/renown-storage-path.test.d.ts +2 -0
- package/.tsbuild/test/renown-storage-path.test.d.ts.map +1 -0
- package/.tsbuild/test/served-schemas-mixed-package.test.d.ts +2 -0
- package/.tsbuild/test/served-schemas-mixed-package.test.d.ts.map +1 -0
- package/.tsbuild/test/served-schemas.test.d.ts +2 -0
- package/.tsbuild/test/served-schemas.test.d.ts.map +1 -0
- package/.tsbuild/test/shutdown.test.d.ts +2 -0
- package/.tsbuild/test/shutdown.test.d.ts.map +1 -0
- package/.tsbuild/test/snapshot-writer.d.ts +18 -0
- package/.tsbuild/test/snapshot-writer.d.ts.map +1 -0
- package/.tsbuild/test/store-lock.test.d.ts +2 -0
- package/.tsbuild/test/store-lock.test.d.ts.map +1 -0
- package/.tsbuild/test/two-switchboards.test.d.ts +2 -0
- package/.tsbuild/test/two-switchboards.test.d.ts.map +1 -0
- package/.tsbuild/test/workflow/oauth-apps-config.test.d.ts +2 -0
- package/.tsbuild/test/workflow/oauth-apps-config.test.d.ts.map +1 -0
- package/.tsbuild/test/workflow/runtime-host.test.d.ts +2 -0
- package/.tsbuild/test/workflow/runtime-host.test.d.ts.map +1 -0
- package/.tsbuild/test/workflow/waitpoint-routes.test.d.ts +2 -0
- package/.tsbuild/test/workflow/waitpoint-routes.test.d.ts.map +1 -0
- package/.tsbuild/test/workflow/waitpoint-runtime.test.d.ts +2 -0
- package/.tsbuild/test/workflow/waitpoint-runtime.test.d.ts.map +1 -0
- package/.tsbuild/test/workflow-reactor-access.test.d.ts +2 -0
- package/.tsbuild/test/workflow-reactor-access.test.d.ts.map +1 -0
- package/.tsbuild/test/workflow-reactor-enforcement.test.d.ts +2 -0
- package/.tsbuild/test/workflow-reactor-enforcement.test.d.ts.map +1 -0
- package/.tsbuild/test/workflows-plugin-config.test.d.ts +2 -0
- package/.tsbuild/test/workflows-plugin-config.test.d.ts.map +1 -0
- package/.tsbuild/tsconfig.tsbuildinfo +1 -1
- package/Auth.md +108 -2
- package/CHANGELOG.md +1122 -2
- package/README.md +69 -24
- package/dist/boot.d.mts +46 -0
- package/dist/boot.d.mts.map +1 -0
- package/dist/boot.mjs +67 -0
- package/dist/boot.mjs.map +1 -0
- package/dist/config/from-env.d.mts +15 -0
- package/dist/config/from-env.d.mts.map +1 -0
- package/dist/config/from-env.mjs +4 -0
- package/dist/config/schema.d.mts +3 -0
- package/dist/config/schema.mjs +4 -0
- package/dist/from-env-DKK_wPrO.mjs +497 -0
- package/dist/from-env-DKK_wPrO.mjs.map +1 -0
- package/dist/index.d.mts +1 -2
- package/dist/index.mjs +40 -219
- package/dist/index.mjs.map +1 -1
- package/dist/mcp-VW52B8KD.mjs +32 -0
- package/dist/mcp-VW52B8KD.mjs.map +1 -0
- package/dist/migrate.mjs +5 -4
- package/dist/migrate.mjs.map +1 -1
- package/dist/observability-CldagyT_.mjs +224 -0
- package/dist/observability-CldagyT_.mjs.map +1 -0
- package/dist/observability.d.mts +30 -0
- package/dist/observability.d.mts.map +1 -0
- package/dist/observability.mjs +4 -0
- package/dist/plugin-host-tTjDTNz0.d.mts +46 -0
- package/dist/plugin-host-tTjDTNz0.d.mts.map +1 -0
- package/dist/plugins/ai.d.mts +37 -0
- package/dist/plugins/ai.d.mts.map +1 -0
- package/dist/plugins/ai.mjs +400 -0
- package/dist/plugins/ai.mjs.map +1 -0
- package/dist/plugins/attachments.d.mts +33 -0
- package/dist/plugins/attachments.d.mts.map +1 -0
- package/dist/plugins/attachments.mjs +4 -0
- package/dist/plugins/mcp.d.mts +31 -0
- package/dist/plugins/mcp.d.mts.map +1 -0
- package/dist/plugins/mcp.mjs +4 -0
- package/dist/plugins/privacy.d.mts +22 -0
- package/dist/plugins/privacy.d.mts.map +1 -0
- package/dist/plugins/privacy.mjs +4 -0
- package/dist/plugins/workflows.d.mts +80 -0
- package/dist/plugins/workflows.d.mts.map +1 -0
- package/dist/plugins/workflows.mjs +4 -0
- package/dist/postgres-url-DTDhBjXX.mjs +11 -0
- package/dist/postgres-url-DTDhBjXX.mjs.map +1 -0
- package/dist/privacy-HQnhqf2H.mjs +91 -0
- package/dist/privacy-HQnhqf2H.mjs.map +1 -0
- package/dist/privacy-UodgY0i_.mjs +44 -0
- package/dist/privacy-UodgY0i_.mjs.map +1 -0
- package/dist/renown-layers-DmbWLt46.mjs +42 -0
- package/dist/renown-layers-DmbWLt46.mjs.map +1 -0
- package/dist/schema-BJ-OFAox.mjs +359 -0
- package/dist/schema-BJ-OFAox.mjs.map +1 -0
- package/dist/schema-w1lShimw.d.mts +258 -0
- package/dist/schema-w1lShimw.d.mts.map +1 -0
- package/dist/server-BS-PFzoK.mjs +2752 -0
- package/dist/server-BS-PFzoK.mjs.map +1 -0
- package/dist/server.d.mts +94 -228
- package/dist/server.d.mts.map +1 -1
- package/dist/server.mjs +5 -4
- package/dist/types-Bk4T4D9F.d.mts +318 -0
- package/dist/types-Bk4T4D9F.d.mts.map +1 -0
- package/dist/utils-Cm0TE6dP.mjs +148 -0
- package/dist/utils-Cm0TE6dP.mjs.map +1 -0
- package/dist/utils.d.mts +29 -3
- package/dist/utils.d.mts.map +1 -1
- package/dist/utils.mjs +5 -4
- package/dist/worker-pool-gyxJnCxA.mjs +304 -0
- package/dist/worker-pool-gyxJnCxA.mjs.map +1 -0
- package/dist/workflow-runtime-CF5pc8mt.d.mts +33 -0
- package/dist/workflow-runtime-CF5pc8mt.d.mts.map +1 -0
- package/dist/workflows-mdYUh3Us.mjs +1947 -0
- package/dist/workflows-mdYUh3Us.mjs.map +1 -0
- package/package.json +86 -24
- package/scripts/install-packages.sh +0 -0
- package/test/ai-gateway.test.ts +612 -0
- package/test/attachment-reference-read-model.test.ts +28 -20
- package/test/attachments/byte-grants.test.ts +4 -1
- package/test/attachments/download-target.test.ts +36 -20
- package/test/attachments/index.test.ts +13 -1
- package/test/attachments/reserve-metadata-parity.test.ts +96 -0
- package/test/attachments/routes-integration.test.ts +1 -0
- package/test/attachments/routes.test.ts +132 -12
- package/test/attachments/service-config.test.ts +28 -16
- package/test/boot-auth-config.test.ts +146 -0
- package/test/boot-create-signature-policy.test.ts +15 -13
- package/test/boot-desktop-auth.test.ts +278 -0
- package/test/boot-env-isolation.test.ts +26 -0
- package/test/boot-helpers.ts +19 -0
- package/test/boot-inspection-enabled.test.ts +140 -0
- package/test/boot-mcp-auth.test.ts +449 -0
- package/test/boot-mcp-enabled.test.ts +27 -27
- package/test/boot-port-zero.test.ts +101 -0
- package/test/boot-preconditions.test.ts +213 -0
- package/test/boot-privacy.test.ts +232 -0
- package/test/boot-ready-drives.test.ts +141 -0
- package/test/boot-undo-v3-flag.test.ts +120 -0
- package/test/boot-unsupported-stored-documents.test.ts +23 -19
- package/test/boot-without-renown.test.ts +11 -9
- package/test/boot-without-require-signatures.test.ts +12 -10
- package/test/boot-worker-trust-policy.test.ts +11 -10
- package/test/boot-workflow-singleton-conflict.test.ts +109 -0
- package/test/boot-workflow-singleton-retry.test.ts +122 -0
- package/test/config-from-env.test.ts +968 -0
- package/test/config-merge.test.ts +351 -0
- package/test/config-schema.test.ts +183 -0
- package/test/default-composition.test.ts +290 -0
- package/test/default-drive-protocol-versions.test.ts +79 -0
- package/test/default-drive-slug.test.ts +265 -0
- package/test/delegated-publish.test.ts +435 -0
- package/test/durable-node-fs-pg16.test.ts +80 -0
- package/test/fatal-shutdown.test.ts +104 -1
- package/test/fixtures/mixed-subgraphs/subgraphs/greetings/index.ts +16 -0
- package/test/fixtures/mixed-subgraphs/subgraphs/index.ts +3 -0
- package/test/fixtures/mixed-subgraphs/subgraphs/tally/index.ts +73 -0
- package/test/goldens/packages.graphql +797 -0
- package/test/goldens/privacy.graphql +853 -0
- package/test/goldens/reactor-drive-nodes.graphql +827 -0
- package/test/goldens/resolvers.json +327 -0
- package/test/goldens/supergraph.graphql +5918 -0
- package/test/goldens/workflow-runtime.graphql +1548 -0
- package/test/inspection-throttle.test.ts +23 -0
- package/test/keyframe-reclaim-store.test.ts +156 -0
- package/test/observability-signals.test.ts +131 -0
- package/test/permissions-backfill.test.ts +486 -0
- package/test/pglite-dialect.test.ts +237 -2
- package/test/pglite-preflight.test.ts +313 -0
- package/test/pglite-storage-dirs.test.ts +113 -0
- package/test/pglite-store.test.ts +441 -0
- package/test/pglite-version.test.ts +9 -1
- package/test/plugin-config-schemas.test.ts +83 -0
- package/test/plugin-presence.test.ts +262 -0
- package/test/port-probe.test.ts +45 -0
- package/test/processor-timing-log.test.ts +109 -0
- package/test/reactor-feature-flags.test.ts +20 -0
- package/test/redact-webhook-path.test.ts +9 -0
- package/test/renown-storage-path.test.ts +40 -0
- package/test/renown-trust-policy.test.ts +7 -9
- package/test/served-schemas-mixed-package.test.ts +251 -0
- package/test/served-schemas.test.ts +134 -0
- package/test/shutdown.test.ts +278 -0
- package/test/snapshot-writer.ts +94 -0
- package/test/store-lock.test.ts +65 -0
- package/test/two-switchboards.test.ts +146 -0
- package/test/worker-pool.test.ts +27 -1
- package/test/workflow/oauth-apps-config.test.ts +66 -0
- package/test/workflow/oauth.test.ts +82 -10
- package/test/workflow/resolvers.test.ts +230 -38
- package/test/workflow/runtime-host.test.ts +19 -0
- package/test/workflow/waitpoint-routes.test.ts +306 -0
- package/test/workflow/waitpoint-runtime.test.ts +204 -0
- package/test/workflow/webhook-trigger-test.test.ts +8 -0
- package/test/workflow-reactor-access.test.ts +384 -0
- package/test/workflow-reactor-enforcement.test.ts +808 -0
- package/test/workflow-read-gate.test.ts +128 -60
- package/test/workflow-runtime.test.ts +1005 -237
- package/test/workflows-plugin-config.test.ts +77 -0
- package/tsconfig.json +5 -2
- package/tsdown.config.ts +12 -0
- package/vitest.config.ts +66 -1
- package/.tsbuild/test/mcp-flag.test.d.ts +0 -2
- package/.tsbuild/test/mcp-flag.test.d.ts.map +0 -1
- package/dist/server-sQoWp21G.mjs +0 -2939
- package/dist/server-sQoWp21G.mjs.map +0 -1
- package/dist/utils-uaBlzyjS.mjs +0 -86
- package/dist/utils-uaBlzyjS.mjs.map +0 -1
- package/test/mcp-flag.test.ts +0 -56
|
@@ -8,12 +8,26 @@ import type {
|
|
|
8
8
|
AttachmentAccessResult,
|
|
9
9
|
IAttachmentAccessService,
|
|
10
10
|
} from "@powerhousedao/reactor-api";
|
|
11
|
+
import type { ILogger } from "document-model";
|
|
11
12
|
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
12
|
-
import {
|
|
13
|
+
import { describe, expect, it, vi } from "vitest";
|
|
13
14
|
import type { AttachmentActorContext } from "../../src/attachments/auth.js";
|
|
14
15
|
import { registerAttachmentRoutes } from "../../src/attachments/index.js";
|
|
15
|
-
import {
|
|
16
|
+
import {
|
|
17
|
+
downloadTargetTtlCapSeconds,
|
|
18
|
+
makeDownloadTargetHandler,
|
|
19
|
+
} from "../../src/attachments/routes.js";
|
|
16
20
|
import { AttachmentUrlSigner } from "../../src/attachments/url-signer.js";
|
|
21
|
+
import { switchboardConfigFromEnv } from "../../src/config/from-env.js";
|
|
22
|
+
|
|
23
|
+
const silentLogger = {
|
|
24
|
+
verbose: () => {},
|
|
25
|
+
debug: () => {},
|
|
26
|
+
info: () => {},
|
|
27
|
+
warn: () => {},
|
|
28
|
+
error: () => {},
|
|
29
|
+
child: () => silentLogger,
|
|
30
|
+
} as unknown as ILogger;
|
|
17
31
|
|
|
18
32
|
const HASH = "a".repeat(64) as AttachmentHash;
|
|
19
33
|
const REF = `attachment://v1:${HASH}`;
|
|
@@ -91,7 +105,13 @@ function makeAccess(result: AttachmentAccessResult): {
|
|
|
91
105
|
spy: ReturnType<typeof vi.fn>;
|
|
92
106
|
} {
|
|
93
107
|
const spy = vi.fn().mockResolvedValue(result);
|
|
94
|
-
return {
|
|
108
|
+
return {
|
|
109
|
+
access: {
|
|
110
|
+
canReadAttachment: spy,
|
|
111
|
+
admitCaller: () => Promise.resolve({ kind: "admitted" }),
|
|
112
|
+
},
|
|
113
|
+
spy,
|
|
114
|
+
};
|
|
95
115
|
}
|
|
96
116
|
|
|
97
117
|
function makeAttachments(opts: {
|
|
@@ -325,23 +345,12 @@ describe("makeDownloadTargetHandler", () => {
|
|
|
325
345
|
expect(prepareDownloadTarget).toHaveBeenCalledWith(HASH, 300);
|
|
326
346
|
});
|
|
327
347
|
|
|
328
|
-
describe("ATTACHMENT_DOWNLOAD_TARGET_MAX_TTL_SECONDS", () => {
|
|
329
|
-
const ORIGINAL = process.env.ATTACHMENT_DOWNLOAD_TARGET_MAX_TTL_SECONDS;
|
|
330
|
-
|
|
331
|
-
afterEach(() => {
|
|
332
|
-
if (ORIGINAL === undefined) {
|
|
333
|
-
delete process.env.ATTACHMENT_DOWNLOAD_TARGET_MAX_TTL_SECONDS;
|
|
334
|
-
} else {
|
|
335
|
-
process.env.ATTACHMENT_DOWNLOAD_TARGET_MAX_TTL_SECONDS = ORIGINAL;
|
|
336
|
-
}
|
|
337
|
-
});
|
|
338
|
-
|
|
348
|
+
describe("ATTACHMENT_DOWNLOAD_TARGET_MAX_TTL_SECONDS, mapped at the edge", () => {
|
|
339
349
|
async function ttlFor(configured: string | undefined, query = "") {
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
}
|
|
350
|
+
const config = switchboardConfigFromEnv(
|
|
351
|
+
{ ATTACHMENT_DOWNLOAD_TARGET_MAX_TTL_SECONDS: configured },
|
|
352
|
+
{ logger: silentLogger },
|
|
353
|
+
);
|
|
345
354
|
const { access } = makeAccess(ALLOWED);
|
|
346
355
|
const prepareDownloadTarget = vi.fn().mockResolvedValue({
|
|
347
356
|
kind: "presigned-get",
|
|
@@ -353,7 +362,14 @@ describe("makeDownloadTargetHandler", () => {
|
|
|
353
362
|
const attachments = makeAttachments({
|
|
354
363
|
backend: { kind: "s3", prepareDownloadTarget } as never,
|
|
355
364
|
});
|
|
356
|
-
const handler = makeDownloadTargetHandler(
|
|
365
|
+
const handler = makeDownloadTargetHandler(
|
|
366
|
+
attachments,
|
|
367
|
+
access,
|
|
368
|
+
SIGNER,
|
|
369
|
+
downloadTargetTtlCapSeconds(
|
|
370
|
+
config.attachments?.downloadTargetMaxTtlSeconds,
|
|
371
|
+
),
|
|
372
|
+
);
|
|
357
373
|
await handler(
|
|
358
374
|
makeReq({
|
|
359
375
|
url: `/attachments/${HASH}/download-target?documentId=${DOC_ID}${query}`,
|
|
@@ -11,6 +11,7 @@ type Captured = {
|
|
|
11
11
|
res: ServerResponse,
|
|
12
12
|
body?: unknown,
|
|
13
13
|
) => void | Promise<void>;
|
|
14
|
+
options?: unknown;
|
|
14
15
|
};
|
|
15
16
|
|
|
16
17
|
type FakeApi = Pick<
|
|
@@ -32,8 +33,9 @@ function makeFakeApi(
|
|
|
32
33
|
method: string,
|
|
33
34
|
path: string,
|
|
34
35
|
handler: Captured["handler"],
|
|
36
|
+
options?: unknown,
|
|
35
37
|
) => {
|
|
36
|
-
captured.push({ method, path, handler });
|
|
38
|
+
captured.push({ method, path, handler, options });
|
|
37
39
|
},
|
|
38
40
|
},
|
|
39
41
|
authService,
|
|
@@ -186,4 +188,14 @@ describe("mountAuthenticatedNodeRoute", () => {
|
|
|
186
188
|
authEnabled: false,
|
|
187
189
|
});
|
|
188
190
|
});
|
|
191
|
+
|
|
192
|
+
it("forwards rawBody to the adapter and keeps it out of the auth options", () => {
|
|
193
|
+
const { api, captured } = makeFakeApi(undefined);
|
|
194
|
+
|
|
195
|
+
mountAuthenticatedNodeRoute(api, "PUT", "/x", vi.fn(), { rawBody: true });
|
|
196
|
+
mountAuthenticatedNodeRoute(api, "GET", "/y", vi.fn());
|
|
197
|
+
|
|
198
|
+
expect(captured[0].options).toEqual({ rawBody: true });
|
|
199
|
+
expect(captured[1].options).toEqual({ rawBody: false });
|
|
200
|
+
});
|
|
189
201
|
});
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import {
|
|
2
|
+
InvalidAttachmentMetadata,
|
|
3
|
+
validateReserveMetadata,
|
|
4
|
+
type ReserveMetadata,
|
|
5
|
+
} from "@powerhousedao/reactor-attachments";
|
|
6
|
+
import { describe, expect, it } from "vitest";
|
|
7
|
+
import { parseReserveOptions } from "../../src/attachments/routes.js";
|
|
8
|
+
|
|
9
|
+
type Case = [name: string, meta: Record<string, unknown>];
|
|
10
|
+
|
|
11
|
+
const base = { mimeType: "text/plain", fileName: "note.txt" };
|
|
12
|
+
|
|
13
|
+
// The download route's setHeader throws on each of these.
|
|
14
|
+
const headerBreaking: Case[] = [
|
|
15
|
+
["CRLF before a parameter", { ...base, mimeType: "text/plain\r\n;a=b" }],
|
|
16
|
+
["LF after a ;", { ...base, mimeType: "text/plain;\na=b" }],
|
|
17
|
+
["a control char quoted", { ...base, mimeType: 'text/plain; a="\x01"' }],
|
|
18
|
+
["a DEL quoted", { ...base, mimeType: 'text/plain; a="\x7f"' }],
|
|
19
|
+
["a char above 0xFF quoted", { ...base, mimeType: 'text/plain;a="\u0100"' }],
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
const cases: Case[] = [
|
|
23
|
+
["plain metadata", base],
|
|
24
|
+
["an extension", { ...base, extension: "txt" }],
|
|
25
|
+
["a null extension", { ...base, extension: null }],
|
|
26
|
+
["a parameter", { ...base, mimeType: "text/plain; charset=utf-8" }],
|
|
27
|
+
["a quoted parameter", { ...base, mimeType: 'text/plain; a="b c"' }],
|
|
28
|
+
["a unicode file name", { ...base, fileName: "résumé.pdf" }],
|
|
29
|
+
["a 255-char mimeType", { ...base, mimeType: `a/${"b".repeat(253)}` }],
|
|
30
|
+
["a 256-char mimeType", { ...base, mimeType: `a/${"b".repeat(254)}` }],
|
|
31
|
+
["a 255-char fileName", { ...base, fileName: "f".repeat(255) }],
|
|
32
|
+
["a 256-char fileName", { ...base, fileName: "f".repeat(256) }],
|
|
33
|
+
["an empty mimeType", { ...base, mimeType: "" }],
|
|
34
|
+
["an empty fileName", { ...base, fileName: "" }],
|
|
35
|
+
["an empty extension", { ...base, extension: "" }],
|
|
36
|
+
["a mimeType with no slash", { ...base, mimeType: "text" }],
|
|
37
|
+
["a mimeType with no subtype", { ...base, mimeType: "text/" }],
|
|
38
|
+
["a mimeType with no type", { ...base, mimeType: "/plain" }],
|
|
39
|
+
["a mimeType with two slashes", { ...base, mimeType: "text/plain/x" }],
|
|
40
|
+
["a mimeType with a space", { ...base, mimeType: "text plain" }],
|
|
41
|
+
["a mimeType with a dangling ;", { ...base, mimeType: "text/plain;" }],
|
|
42
|
+
["a non-string mimeType", { ...base, mimeType: 5 }],
|
|
43
|
+
["a trailing CRLF in mimeType", { ...base, mimeType: "text/plain\r\n" }],
|
|
44
|
+
[
|
|
45
|
+
"an injected header in mimeType",
|
|
46
|
+
{ ...base, mimeType: "text/plain\nX-Injected: 1" },
|
|
47
|
+
],
|
|
48
|
+
["a NUL in mimeType", { ...base, mimeType: "text/pl\x00ain" }],
|
|
49
|
+
...headerBreaking,
|
|
50
|
+
["a NUL in fileName", { ...base, fileName: "a\x00b" }],
|
|
51
|
+
["a LF in fileName", { ...base, fileName: "a\nb" }],
|
|
52
|
+
["a CR in fileName", { ...base, fileName: "a\rb" }],
|
|
53
|
+
["a tab in fileName", { ...base, fileName: "a\tb" }],
|
|
54
|
+
["a DEL in fileName", { ...base, fileName: "a\x7fb" }],
|
|
55
|
+
["a non-string fileName", { ...base, fileName: null }],
|
|
56
|
+
["a slash in extension", { ...base, extension: "a/b" }],
|
|
57
|
+
["a backslash in extension", { ...base, extension: "a\\b" }],
|
|
58
|
+
["a bare slash extension", { ...base, extension: "/" }],
|
|
59
|
+
["a non-string extension", { ...base, extension: 5 }],
|
|
60
|
+
];
|
|
61
|
+
|
|
62
|
+
function routeAccepts(meta: Record<string, unknown>): boolean {
|
|
63
|
+
return parseReserveOptions(meta) !== null;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function validatorAccepts(meta: Record<string, unknown>): boolean {
|
|
67
|
+
try {
|
|
68
|
+
validateReserveMetadata(meta as ReserveMetadata);
|
|
69
|
+
return true;
|
|
70
|
+
} catch (err) {
|
|
71
|
+
if (err instanceof InvalidAttachmentMetadata) return false;
|
|
72
|
+
throw err;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
describe("reserve metadata parity between the route and validateReserveMetadata", () => {
|
|
77
|
+
it("covers both accepted and rejected metadata", () => {
|
|
78
|
+
const verdicts = cases.map(([, meta]) => routeAccepts(meta));
|
|
79
|
+
expect(verdicts).toContain(true);
|
|
80
|
+
expect(verdicts).toContain(false);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it.each(cases)("agrees on %s", (_name, meta) => {
|
|
84
|
+
expect(validatorAccepts(meta)).toBe(routeAccepts(meta));
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
describe("header-breaking mimeType values", () => {
|
|
89
|
+
it.each(headerBreaking)("the route rejects %s", (_name, meta) => {
|
|
90
|
+
expect(routeAccepts(meta)).toBe(false);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it.each(headerBreaking)("the validator rejects %s", (_name, meta) => {
|
|
94
|
+
expect(validatorAccepts(meta)).toBe(false);
|
|
95
|
+
});
|
|
96
|
+
});
|
|
@@ -31,6 +31,7 @@ const ALLOW_ALL: IAttachmentAccessService = {
|
|
|
31
31
|
documentId: request.documentId as never,
|
|
32
32
|
ref: request.attachmentRef as never,
|
|
33
33
|
}),
|
|
34
|
+
admitCaller: () => Promise.resolve({ kind: "admitted" }),
|
|
34
35
|
};
|
|
35
36
|
|
|
36
37
|
describe("attachment routes through the real Express middleware stack", () => {
|
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
} from "@powerhousedao/reactor-attachments";
|
|
6
6
|
import { Kysely } from "kysely";
|
|
7
7
|
import { PGliteDialect } from "kysely-pglite-dialect";
|
|
8
|
-
import { mkdtemp, rm } from "node:fs/promises";
|
|
8
|
+
import { mkdtemp, readdir, rm } from "node:fs/promises";
|
|
9
9
|
import {
|
|
10
10
|
validateHeaderValue,
|
|
11
11
|
type IncomingMessage,
|
|
@@ -39,6 +39,7 @@ const ALLOW_ALL: IAttachmentAccessService = {
|
|
|
39
39
|
documentId: request.documentId as never,
|
|
40
40
|
ref: request.attachmentRef as never,
|
|
41
41
|
}),
|
|
42
|
+
admitCaller: () => Promise.resolve({ kind: "admitted" }),
|
|
42
43
|
};
|
|
43
44
|
const grantedDownload = (attachments: AttachmentBuildResult) =>
|
|
44
45
|
makeDownloadHandler(attachments, ALLOW_ALL, null);
|
|
@@ -100,8 +101,17 @@ function makeRes(): CapturedRes {
|
|
|
100
101
|
getHeader(name: string) {
|
|
101
102
|
return headers[name.toLowerCase()];
|
|
102
103
|
},
|
|
104
|
+
removeHeader(name: string) {
|
|
105
|
+
delete headers[name.toLowerCase()];
|
|
106
|
+
},
|
|
103
107
|
_done: done,
|
|
104
108
|
});
|
|
109
|
+
// Like ServerResponse: headers count as sent once the first byte is written.
|
|
110
|
+
Object.defineProperty(writable, "headersSent", {
|
|
111
|
+
get(): boolean {
|
|
112
|
+
return chunks.length > 0;
|
|
113
|
+
},
|
|
114
|
+
});
|
|
105
115
|
Object.defineProperty(writable, "_body", {
|
|
106
116
|
get(): Buffer {
|
|
107
117
|
return Buffer.concat(chunks);
|
|
@@ -695,6 +705,101 @@ describe("attachment routes", () => {
|
|
|
695
705
|
}
|
|
696
706
|
});
|
|
697
707
|
|
|
708
|
+
async function uploadText(payload: string): Promise<string> {
|
|
709
|
+
const reserveRes = makeRes();
|
|
710
|
+
await makeReserveHandler(attachments)(
|
|
711
|
+
makeReq({
|
|
712
|
+
method: "POST",
|
|
713
|
+
body: JSON.stringify({ mimeType: "text/plain", fileName: "a.txt" }),
|
|
714
|
+
}),
|
|
715
|
+
reserveRes,
|
|
716
|
+
);
|
|
717
|
+
await waitFor(reserveRes);
|
|
718
|
+
const { reservationId } = JSON.parse(reserveRes._body.toString("utf8")) as {
|
|
719
|
+
reservationId: string;
|
|
720
|
+
};
|
|
721
|
+
const uploadRes = makeRes();
|
|
722
|
+
await makeUploadHandler(attachments)(
|
|
723
|
+
makeReq({ method: "PUT", params: { reservationId }, body: payload }),
|
|
724
|
+
uploadRes,
|
|
725
|
+
);
|
|
726
|
+
await waitFor(uploadRes);
|
|
727
|
+
return (JSON.parse(uploadRes._body.toString("utf8")) as { hash: string })
|
|
728
|
+
.hash;
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
// The blob store is a directory; its metadata lives in the database. When
|
|
732
|
+
// the directory is lost (a pod-local tmpdir after a restart) the metadata
|
|
733
|
+
// still says "available". Reading such a blob used to raise an unhandled
|
|
734
|
+
// stream error that took the whole process down.
|
|
735
|
+
it("GET download returns 404 when the blob file is missing from disk", async () => {
|
|
736
|
+
const hash = await uploadText("gone");
|
|
737
|
+
const files = await readdir(storagePath, {
|
|
738
|
+
recursive: true,
|
|
739
|
+
withFileTypes: true,
|
|
740
|
+
});
|
|
741
|
+
const blobs = files.filter((f) => f.isFile() && f.name.startsWith(hash));
|
|
742
|
+
expect(blobs.length).toBeGreaterThan(0);
|
|
743
|
+
for (const f of blobs) await rm(join(f.parentPath, f.name));
|
|
744
|
+
|
|
745
|
+
const res = makeRes();
|
|
746
|
+
await grantedDownload(attachments)(
|
|
747
|
+
makeReq({ method: "GET", params: { hash } }),
|
|
748
|
+
res,
|
|
749
|
+
);
|
|
750
|
+
await waitFor(res);
|
|
751
|
+
|
|
752
|
+
expect(res.statusCode).toBe(404);
|
|
753
|
+
expect(JSON.parse(res._body.toString("utf8"))).toEqual({
|
|
754
|
+
error: "Attachment not found",
|
|
755
|
+
});
|
|
756
|
+
// The download headers were set before the read failed; none may leak
|
|
757
|
+
// onto the JSON error.
|
|
758
|
+
expect(res.getHeader("content-length")).toBeUndefined();
|
|
759
|
+
expect(res.getHeader("content-disposition")).toBeUndefined();
|
|
760
|
+
expect(res.getHeader("attachment-metadata")).toBeUndefined();
|
|
761
|
+
});
|
|
762
|
+
|
|
763
|
+
it("GET download aborts the response when the blob fails mid-stream", async () => {
|
|
764
|
+
const hash = await uploadText("partial");
|
|
765
|
+
const originalGet = attachments.store.get.bind(attachments.store);
|
|
766
|
+
const real = await originalGet(hash as never);
|
|
767
|
+
let sentFirst = false;
|
|
768
|
+
attachments.store.get = () =>
|
|
769
|
+
Promise.resolve({
|
|
770
|
+
header: real.header,
|
|
771
|
+
// First chunk flows to the response; the read fails only after it
|
|
772
|
+
// has been written, the way a disk error partway through a file would.
|
|
773
|
+
body: new ReadableStream<Uint8Array>({
|
|
774
|
+
async pull(controller) {
|
|
775
|
+
if (!sentFirst) {
|
|
776
|
+
sentFirst = true;
|
|
777
|
+
controller.enqueue(new TextEncoder().encode("part"));
|
|
778
|
+
return;
|
|
779
|
+
}
|
|
780
|
+
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
781
|
+
controller.error(new Error("EIO: read failed"));
|
|
782
|
+
},
|
|
783
|
+
}) as never,
|
|
784
|
+
});
|
|
785
|
+
try {
|
|
786
|
+
const res = makeRes();
|
|
787
|
+
const closed = new Promise<void>((resolve) => res.once("close", resolve));
|
|
788
|
+
await grantedDownload(attachments)(
|
|
789
|
+
makeReq({ method: "GET", params: { hash } }),
|
|
790
|
+
res,
|
|
791
|
+
);
|
|
792
|
+
await closed;
|
|
793
|
+
// Bytes were already on the wire: the only honest signal left is to
|
|
794
|
+
// cut the connection, never to finish it as if it were complete.
|
|
795
|
+
expect(res._body.toString("utf8")).toBe("part");
|
|
796
|
+
expect(res.destroyed).toBe(true);
|
|
797
|
+
expect(res.writableFinished).toBe(false);
|
|
798
|
+
} finally {
|
|
799
|
+
attachments.store.get = originalGet;
|
|
800
|
+
}
|
|
801
|
+
});
|
|
802
|
+
|
|
698
803
|
it("identical uploads dedupe to the same hash", async () => {
|
|
699
804
|
const reserveHandler = makeReserveHandler(attachments);
|
|
700
805
|
const uploadHandler = makeUploadHandler(attachments);
|
|
@@ -1098,6 +1203,32 @@ describe("attachment routes", () => {
|
|
|
1098
1203
|
).toBeNull();
|
|
1099
1204
|
});
|
|
1100
1205
|
|
|
1206
|
+
it("parseReserveOptions accepts an empty file in hash-first mode", () => {
|
|
1207
|
+
const emptyHash =
|
|
1208
|
+
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
|
|
1209
|
+
const opts = parseReserveOptions({
|
|
1210
|
+
mimeType: "application/octet-stream",
|
|
1211
|
+
fileName: "empty",
|
|
1212
|
+
clientHash: emptyHash,
|
|
1213
|
+
sizeBytes: 0,
|
|
1214
|
+
});
|
|
1215
|
+
expect(opts).not.toBeNull();
|
|
1216
|
+
expect(opts!.sizeBytes).toBe(0);
|
|
1217
|
+
});
|
|
1218
|
+
|
|
1219
|
+
it("parseReserveOptions rejects a negative or fractional sizeBytes", () => {
|
|
1220
|
+
for (const sizeBytes of [-1, 1.5]) {
|
|
1221
|
+
expect(
|
|
1222
|
+
parseReserveOptions({
|
|
1223
|
+
mimeType: "text/plain",
|
|
1224
|
+
fileName: "file.txt",
|
|
1225
|
+
clientHash: "a".repeat(64),
|
|
1226
|
+
sizeBytes,
|
|
1227
|
+
}),
|
|
1228
|
+
).toBeNull();
|
|
1229
|
+
}
|
|
1230
|
+
});
|
|
1231
|
+
|
|
1101
1232
|
it("parseReserveOptions rejects when clientHash is present but sizeBytes is absent", () => {
|
|
1102
1233
|
expect(
|
|
1103
1234
|
parseReserveOptions({
|
|
@@ -1122,17 +1253,6 @@ describe("attachment routes", () => {
|
|
|
1122
1253
|
});
|
|
1123
1254
|
});
|
|
1124
1255
|
|
|
1125
|
-
it("parseReserveOptions rejects sizeBytes of zero", () => {
|
|
1126
|
-
expect(
|
|
1127
|
-
parseReserveOptions({
|
|
1128
|
-
mimeType: "text/plain",
|
|
1129
|
-
fileName: "file.txt",
|
|
1130
|
-
clientHash: "a".repeat(64),
|
|
1131
|
-
sizeBytes: 0,
|
|
1132
|
-
}),
|
|
1133
|
-
).toBeNull();
|
|
1134
|
-
});
|
|
1135
|
-
|
|
1136
1256
|
it("parseReserveOptions rejects negative sizeBytes", () => {
|
|
1137
1257
|
expect(
|
|
1138
1258
|
parseReserveOptions({
|
|
@@ -13,44 +13,56 @@ import { mkdtemp, rm } from "node:fs/promises";
|
|
|
13
13
|
import type { Server } from "node:http";
|
|
14
14
|
import { tmpdir } from "node:os";
|
|
15
15
|
import { join } from "node:path";
|
|
16
|
-
import { afterAll,
|
|
16
|
+
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
|
17
17
|
import { registerAttachmentRoutes } from "../../src/attachments/index.js";
|
|
18
|
+
import { switchboardConfigFromEnv } from "../../src/config/from-env.js";
|
|
19
|
+
import {
|
|
20
|
+
callerOverEnv,
|
|
21
|
+
switchboardConfigSchema,
|
|
22
|
+
} from "../../src/config/schema.js";
|
|
18
23
|
import { deriveAttachmentServiceConfig } from "../../src/server.mjs";
|
|
19
24
|
|
|
20
25
|
describe("deriveAttachmentServiceConfig", () => {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}
|
|
29
|
-
});
|
|
26
|
+
// An edge's config, as the server derives the options view from it.
|
|
27
|
+
const serviceUrlOf = (env: Record<string, string>, caller?: string) =>
|
|
28
|
+
switchboardConfigSchema.parse(
|
|
29
|
+
callerOverEnv(switchboardConfigFromEnv(env), {
|
|
30
|
+
attachments: { serviceUrl: caller },
|
|
31
|
+
}),
|
|
32
|
+
).attachments.serviceUrl;
|
|
30
33
|
|
|
31
34
|
it("defaults to http://localhost:${port} with no auth when renown is null", () => {
|
|
32
|
-
delete process.env.PH_SWITCHBOARD_PUBLIC_URL;
|
|
33
35
|
const config = deriveAttachmentServiceConfig({}, 4001, null);
|
|
34
36
|
expect(config.remoteUrl).toBe("http://localhost:4001");
|
|
35
37
|
expect(config.jwtHandler).toBeUndefined();
|
|
36
38
|
});
|
|
37
39
|
|
|
38
40
|
it("uses https when options.https is set", () => {
|
|
39
|
-
delete process.env.PH_SWITCHBOARD_PUBLIC_URL;
|
|
40
41
|
const config = deriveAttachmentServiceConfig({ https: true }, 4443, null);
|
|
41
42
|
expect(config.remoteUrl).toBe("https://localhost:4443");
|
|
42
43
|
});
|
|
43
44
|
|
|
44
45
|
it("prefers PH_SWITCHBOARD_PUBLIC_URL over the localhost default", () => {
|
|
45
|
-
|
|
46
|
-
|
|
46
|
+
const config = deriveAttachmentServiceConfig(
|
|
47
|
+
{
|
|
48
|
+
attachmentServiceUrl: serviceUrlOf({
|
|
49
|
+
PH_SWITCHBOARD_PUBLIC_URL: "https://sb.example.com",
|
|
50
|
+
}),
|
|
51
|
+
},
|
|
52
|
+
4001,
|
|
53
|
+
null,
|
|
54
|
+
);
|
|
47
55
|
expect(config.remoteUrl).toBe("https://sb.example.com");
|
|
48
56
|
});
|
|
49
57
|
|
|
50
58
|
it("prefers the explicit attachmentServiceUrl option above all else", () => {
|
|
51
|
-
process.env.PH_SWITCHBOARD_PUBLIC_URL = "https://sb.example.com";
|
|
52
59
|
const config = deriveAttachmentServiceConfig(
|
|
53
|
-
{
|
|
60
|
+
{
|
|
61
|
+
attachmentServiceUrl: serviceUrlOf(
|
|
62
|
+
{ PH_SWITCHBOARD_PUBLIC_URL: "https://sb.example.com" },
|
|
63
|
+
"https://override.example.com",
|
|
64
|
+
),
|
|
65
|
+
},
|
|
54
66
|
4001,
|
|
55
67
|
null,
|
|
56
68
|
);
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import type { ILogger } from "document-model";
|
|
5
|
+
import { describe, expect, it, vi } from "vitest";
|
|
6
|
+
import type { SwitchboardConfigInput } from "../src/config/schema.js";
|
|
7
|
+
import { bootSwitchboard } from "./boot-helpers.js";
|
|
8
|
+
|
|
9
|
+
type Info = ReturnType<typeof vi.fn>;
|
|
10
|
+
|
|
11
|
+
function stubLogger(): ILogger & { info: Info } {
|
|
12
|
+
const logger = {
|
|
13
|
+
verbose: vi.fn(),
|
|
14
|
+
debug: vi.fn(),
|
|
15
|
+
info: vi.fn(),
|
|
16
|
+
warn: vi.fn(),
|
|
17
|
+
error: vi.fn(),
|
|
18
|
+
child: vi.fn(),
|
|
19
|
+
};
|
|
20
|
+
logger.child.mockReturnValue(logger);
|
|
21
|
+
return logger as unknown as ILogger & { info: Info };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const AUTH_VARS = [
|
|
25
|
+
"AUTH_ENABLED",
|
|
26
|
+
"ADMINS",
|
|
27
|
+
"RESOLVE_CALLER_IDENTITY",
|
|
28
|
+
"ACCESS",
|
|
29
|
+
"VETRA_GATE_URL",
|
|
30
|
+
"VETRA_GATE_TOKEN",
|
|
31
|
+
];
|
|
32
|
+
|
|
33
|
+
/** Whether the switchboard enforces auth, from reactor-api's setup log. */
|
|
34
|
+
async function bootsWithAuth(options: {
|
|
35
|
+
file: Record<string, unknown>;
|
|
36
|
+
config?: SwitchboardConfigInput["auth"];
|
|
37
|
+
env?: Record<string, string>;
|
|
38
|
+
}): Promise<boolean> {
|
|
39
|
+
const tempRoot = await mkdtemp(join(tmpdir(), "switchboard-auth-config-"));
|
|
40
|
+
const names = ["PH_REACTOR_DATABASE_URL", ...AUTH_VARS];
|
|
41
|
+
const previous = names.map((name) => [name, process.env[name]] as const);
|
|
42
|
+
for (const name of AUTH_VARS) delete process.env[name];
|
|
43
|
+
process.env.PH_REACTOR_DATABASE_URL = join(tempRoot, "reactor-storage");
|
|
44
|
+
Object.assign(process.env, options.env);
|
|
45
|
+
const configFile = join(tempRoot, "powerhouse.config.json");
|
|
46
|
+
await writeFile(configFile, JSON.stringify(options.file));
|
|
47
|
+
const logger = stubLogger();
|
|
48
|
+
const switchboard = await bootSwitchboard(
|
|
49
|
+
{
|
|
50
|
+
configFile,
|
|
51
|
+
auth: options.config,
|
|
52
|
+
server: { port: 0 },
|
|
53
|
+
storage: { readModel: { url: join(tempRoot, "read-model") } },
|
|
54
|
+
features: { workflows: false, mcp: false },
|
|
55
|
+
packages: { disableLocal: true },
|
|
56
|
+
identity: { keypairPath: join(tempRoot, "identity.json") },
|
|
57
|
+
},
|
|
58
|
+
{ logger },
|
|
59
|
+
).finally(() => {
|
|
60
|
+
for (const [name, value] of previous) {
|
|
61
|
+
if (value === undefined) delete process.env[name];
|
|
62
|
+
else process.env[name] = value;
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
try {
|
|
66
|
+
const setup = logger.info.mock.calls.find(
|
|
67
|
+
([message]) =>
|
|
68
|
+
typeof message === "string" &&
|
|
69
|
+
message.startsWith("Setting up Auth middleware"),
|
|
70
|
+
);
|
|
71
|
+
return setup?.[1] === true;
|
|
72
|
+
} finally {
|
|
73
|
+
await switchboard.shutdown();
|
|
74
|
+
await rm(tempRoot, { recursive: true, force: true });
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const ADMIN = "0x1111111111111111111111111111111111111111";
|
|
79
|
+
|
|
80
|
+
describe("booting Switchboard with auth", () => {
|
|
81
|
+
it("enforces auth the config turns on", async () => {
|
|
82
|
+
expect(
|
|
83
|
+
await bootsWithAuth({
|
|
84
|
+
file: {},
|
|
85
|
+
config: { enabled: true, admins: [ADMIN] },
|
|
86
|
+
}),
|
|
87
|
+
).toBe(true);
|
|
88
|
+
}, 60_000);
|
|
89
|
+
|
|
90
|
+
it("still takes auth from the config file when the config leaves it unset", async () => {
|
|
91
|
+
expect(
|
|
92
|
+
await bootsWithAuth({
|
|
93
|
+
file: { auth: { enabled: true, admins: [ADMIN] } },
|
|
94
|
+
}),
|
|
95
|
+
).toBe(true);
|
|
96
|
+
}, 60_000);
|
|
97
|
+
|
|
98
|
+
it("lets AUTH_ENABLED override the config file, as before", async () => {
|
|
99
|
+
expect(
|
|
100
|
+
await bootsWithAuth({
|
|
101
|
+
file: { auth: { enabled: true, admins: [ADMIN] } },
|
|
102
|
+
env: { AUTH_ENABLED: "false" },
|
|
103
|
+
}),
|
|
104
|
+
).toBe(false);
|
|
105
|
+
}, 60_000);
|
|
106
|
+
|
|
107
|
+
it("lets AUTH_ENABLED override the caller's config, as it overrode options", async () => {
|
|
108
|
+
expect(
|
|
109
|
+
await bootsWithAuth({
|
|
110
|
+
file: {},
|
|
111
|
+
config: { enabled: false },
|
|
112
|
+
env: { AUTH_ENABLED: "true", ADMINS: ADMIN },
|
|
113
|
+
}),
|
|
114
|
+
).toBe(true);
|
|
115
|
+
}, 60_000);
|
|
116
|
+
|
|
117
|
+
it("refuses to boot with ACCESS=members", async () => {
|
|
118
|
+
await expect(
|
|
119
|
+
bootsWithAuth({ file: {}, env: { ACCESS: "members" } }),
|
|
120
|
+
).rejects.toThrow(
|
|
121
|
+
"ACCESS=members is not supported; use ACCESS=licensed with Vetra's gate",
|
|
122
|
+
);
|
|
123
|
+
}, 60_000);
|
|
124
|
+
|
|
125
|
+
it("refuses to boot with ACCESS=licensed and no gate", async () => {
|
|
126
|
+
await expect(
|
|
127
|
+
bootsWithAuth({ file: {}, env: { ACCESS: "licensed" } }),
|
|
128
|
+
).rejects.toThrow(
|
|
129
|
+
"ACCESS=licensed needs VETRA_GATE_URL and VETRA_GATE_TOKEN",
|
|
130
|
+
);
|
|
131
|
+
}, 60_000);
|
|
132
|
+
|
|
133
|
+
it("enforces auth with ACCESS=licensed alone", async () => {
|
|
134
|
+
expect(
|
|
135
|
+
await bootsWithAuth({
|
|
136
|
+
file: {},
|
|
137
|
+
env: {
|
|
138
|
+
ACCESS: "licensed",
|
|
139
|
+
ADMINS: ADMIN,
|
|
140
|
+
VETRA_GATE_URL: "https://vetra.example/gate/check",
|
|
141
|
+
VETRA_GATE_TOKEN: "vgt_test",
|
|
142
|
+
},
|
|
143
|
+
}),
|
|
144
|
+
).toBe(true);
|
|
145
|
+
}, 60_000);
|
|
146
|
+
});
|
|
@@ -8,7 +8,8 @@ import { tmpdir } from "node:os";
|
|
|
8
8
|
import { join } from "node:path";
|
|
9
9
|
import type { ILogger } from "document-model";
|
|
10
10
|
import { describe, expect, it, vi } from "vitest";
|
|
11
|
-
import { startSwitchboard } from "../src/server.mjs";
|
|
11
|
+
import type { startSwitchboard } from "../src/server.mjs";
|
|
12
|
+
import { bootSwitchboard } from "./boot-helpers.js";
|
|
12
13
|
|
|
13
14
|
function stubLogger(): ILogger {
|
|
14
15
|
const logger = {
|
|
@@ -37,19 +38,20 @@ async function withSwitchboard(
|
|
|
37
38
|
let switchboard: Awaited<ReturnType<typeof startSwitchboard>> | undefined;
|
|
38
39
|
|
|
39
40
|
try {
|
|
40
|
-
switchboard = await
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
41
|
+
switchboard = await bootSwitchboard(
|
|
42
|
+
{
|
|
43
|
+
server: { port: 0 },
|
|
44
|
+
storage: { readModel: { url: join(tempRoot, "read-model") } },
|
|
45
|
+
features: { workflows: false, mcp: false },
|
|
46
|
+
packages: { disableLocal: true },
|
|
47
|
+
identity: { keypairPath: join(tempRoot, "identity.json") },
|
|
48
|
+
drive: {
|
|
49
|
+
slug: "default-drive",
|
|
50
|
+
global: { name: "Default Drive" },
|
|
51
|
+
},
|
|
50
52
|
},
|
|
51
|
-
logger: stubLogger(),
|
|
52
|
-
|
|
53
|
+
{ logger: stubLogger() },
|
|
54
|
+
);
|
|
53
55
|
await run(switchboard);
|
|
54
56
|
await switchboard.shutdown();
|
|
55
57
|
switchboard = undefined;
|