|
| 1 | +import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | +import { ThreadId } from "@t3tools/contracts"; |
| 3 | +import { describe, expect, it } from "@effect/vitest"; |
| 4 | +import * as Effect from "effect/Effect"; |
| 5 | +import * as FileSystem from "effect/FileSystem"; |
| 6 | +import * as Layer from "effect/Layer"; |
| 7 | +import * as Path from "effect/Path"; |
| 8 | + |
| 9 | +import * as ServerSecretStore from "../auth/ServerSecretStore.ts"; |
| 10 | +import { ServerConfig } from "../config.ts"; |
| 11 | +import { ProjectFaviconResolverLive } from "../project/Layers/ProjectFaviconResolver.ts"; |
| 12 | +import { WorkspacePathsLive } from "../workspace/Layers/WorkspacePaths.ts"; |
| 13 | +import { ASSET_ROUTE_PREFIX, issueAssetUrl, resolveAsset } from "./AssetAccess.ts"; |
| 14 | + |
| 15 | +const configLayer = ServerConfig.layerTest(process.cwd(), { |
| 16 | + prefix: "t3-asset-access-test-", |
| 17 | +}); |
| 18 | +const testLayer = Layer.mergeAll( |
| 19 | + configLayer, |
| 20 | + WorkspacePathsLive, |
| 21 | + ProjectFaviconResolverLive.pipe(Layer.provide(WorkspacePathsLive)), |
| 22 | + ServerSecretStore.layer.pipe(Layer.provide(configLayer)), |
| 23 | +).pipe(Layer.provideMerge(NodeServices.layer)); |
| 24 | + |
| 25 | +describe("AssetAccess", () => { |
| 26 | + it.effect("issues workspace URLs that resolve the entry file and sibling assets", () => |
| 27 | + Effect.gen(function* () { |
| 28 | + const fileSystem = yield* FileSystem.FileSystem; |
| 29 | + const path = yield* Path.Path; |
| 30 | + const root = yield* fileSystem.makeTempDirectoryScoped({ |
| 31 | + prefix: "t3-asset-workspace-", |
| 32 | + }); |
| 33 | + const htmlPath = path.join(root, "report.html"); |
| 34 | + const cssPath = path.join(root, "report.css"); |
| 35 | + yield* fileSystem.writeFileString(htmlPath, '<link rel="stylesheet" href="report.css">'); |
| 36 | + yield* fileSystem.writeFileString(cssPath, "body { color: red; }"); |
| 37 | + yield* fileSystem.writeFileString(path.join(root, ".env"), "SECRET=value"); |
| 38 | + |
| 39 | + const result = yield* issueAssetUrl({ |
| 40 | + resource: { |
| 41 | + _tag: "workspace-file", |
| 42 | + threadId: ThreadId.make("thread-1"), |
| 43 | + path: htmlPath, |
| 44 | + }, |
| 45 | + workspaceRoot: root, |
| 46 | + }); |
| 47 | + const suffix = result.relativeUrl.slice(`${ASSET_ROUTE_PREFIX}/`.length); |
| 48 | + const separatorIndex = suffix.indexOf("/"); |
| 49 | + const token = suffix.slice(0, separatorIndex); |
| 50 | + |
| 51 | + expect(yield* resolveAsset(token, "report.html")).toEqual({ |
| 52 | + kind: "file", |
| 53 | + path: htmlPath, |
| 54 | + }); |
| 55 | + expect(yield* resolveAsset(token, "report.css")).toEqual({ |
| 56 | + kind: "file", |
| 57 | + path: cssPath, |
| 58 | + }); |
| 59 | + expect(yield* resolveAsset(token, "../secret.txt")).toBeNull(); |
| 60 | + expect(yield* resolveAsset(token, ".env")).toBeNull(); |
| 61 | + expect(yield* resolveAsset(`${token}tampered`, "report.html")).toBeNull(); |
| 62 | + }).pipe(Effect.provide(testLayer)), |
| 63 | + ); |
| 64 | + |
| 65 | + it.effect("rejects workspace files outside the authorized root", () => |
| 66 | + Effect.gen(function* () { |
| 67 | + const fileSystem = yield* FileSystem.FileSystem; |
| 68 | + const path = yield* Path.Path; |
| 69 | + const root = yield* fileSystem.makeTempDirectoryScoped({ |
| 70 | + prefix: "t3-asset-root-", |
| 71 | + }); |
| 72 | + const outside = yield* fileSystem.makeTempDirectoryScoped({ |
| 73 | + prefix: "t3-asset-outside-", |
| 74 | + }); |
| 75 | + const htmlPath = path.join(outside, "report.html"); |
| 76 | + yield* fileSystem.writeFileString(htmlPath, "<p>outside</p>"); |
| 77 | + |
| 78 | + const error = yield* issueAssetUrl({ |
| 79 | + resource: { |
| 80 | + _tag: "workspace-file", |
| 81 | + threadId: ThreadId.make("thread-1"), |
| 82 | + path: htmlPath, |
| 83 | + }, |
| 84 | + workspaceRoot: root, |
| 85 | + }).pipe(Effect.flip); |
| 86 | + expect(error.message).toContain("relative to the project root"); |
| 87 | + }).pipe(Effect.provide(testLayer)), |
| 88 | + ); |
| 89 | + |
| 90 | + it.effect("issues exact attachment capabilities by attachment id", () => |
| 91 | + Effect.gen(function* () { |
| 92 | + const config = yield* ServerConfig; |
| 93 | + const fileSystem = yield* FileSystem.FileSystem; |
| 94 | + const path = yield* Path.Path; |
| 95 | + const attachmentId = "thread-1-00000000-0000-4000-8000-000000000001"; |
| 96 | + const attachmentPath = path.join(config.attachmentsDir, `${attachmentId}.png`); |
| 97 | + yield* fileSystem.makeDirectory(config.attachmentsDir, { recursive: true }); |
| 98 | + yield* fileSystem.writeFile(attachmentPath, new Uint8Array([1, 2, 3])); |
| 99 | + |
| 100 | + const result = yield* issueAssetUrl({ |
| 101 | + resource: { _tag: "attachment", attachmentId }, |
| 102 | + }); |
| 103 | + const suffix = result.relativeUrl.slice(`${ASSET_ROUTE_PREFIX}/`.length); |
| 104 | + const separatorIndex = suffix.indexOf("/"); |
| 105 | + const token = suffix.slice(0, separatorIndex); |
| 106 | + |
| 107 | + expect(yield* resolveAsset(token, "ignored.png")).toEqual({ |
| 108 | + kind: "file", |
| 109 | + path: attachmentPath, |
| 110 | + }); |
| 111 | + }).pipe(Effect.provide(testLayer)), |
| 112 | + ); |
| 113 | + |
| 114 | + it.effect("issues project favicon capabilities with a signed fallback", () => |
| 115 | + Effect.gen(function* () { |
| 116 | + const fileSystem = yield* FileSystem.FileSystem; |
| 117 | + const path = yield* Path.Path; |
| 118 | + const root = yield* fileSystem.makeTempDirectoryScoped({ |
| 119 | + prefix: "t3-asset-favicon-", |
| 120 | + }); |
| 121 | + const faviconPath = path.join(root, "favicon.svg"); |
| 122 | + yield* fileSystem.writeFileString(faviconPath, "<svg />"); |
| 123 | + |
| 124 | + const faviconResult = yield* issueAssetUrl({ |
| 125 | + resource: { _tag: "project-favicon", cwd: root }, |
| 126 | + }); |
| 127 | + const faviconSuffix = faviconResult.relativeUrl.slice(`${ASSET_ROUTE_PREFIX}/`.length); |
| 128 | + const faviconSeparatorIndex = faviconSuffix.indexOf("/"); |
| 129 | + expect( |
| 130 | + yield* resolveAsset( |
| 131 | + faviconSuffix.slice(0, faviconSeparatorIndex), |
| 132 | + faviconSuffix.slice(faviconSeparatorIndex + 1), |
| 133 | + ), |
| 134 | + ).toEqual({ kind: "file", path: faviconPath }); |
| 135 | + |
| 136 | + yield* fileSystem.remove(faviconPath); |
| 137 | + const fallbackResult = yield* issueAssetUrl({ |
| 138 | + resource: { _tag: "project-favicon", cwd: root }, |
| 139 | + }); |
| 140 | + const fallbackSuffix = fallbackResult.relativeUrl.slice(`${ASSET_ROUTE_PREFIX}/`.length); |
| 141 | + const fallbackSeparatorIndex = fallbackSuffix.indexOf("/"); |
| 142 | + expect( |
| 143 | + yield* resolveAsset( |
| 144 | + fallbackSuffix.slice(0, fallbackSeparatorIndex), |
| 145 | + fallbackSuffix.slice(fallbackSeparatorIndex + 1), |
| 146 | + ), |
| 147 | + ).toEqual({ kind: "project-favicon-fallback" }); |
| 148 | + }).pipe(Effect.provide(testLayer)), |
| 149 | + ); |
| 150 | +}); |
0 commit comments