fix(chat): secure agent media previews
This commit is contained in:
9 files changed
+149
-73
No files matched your search
@@ -168,9 +168,15 @@ describe("buildThreadFeed", () => {
|
||||
projectedItem: row,
|
||||
},
|
||||
]);
|
||||
expect(
|
||||
buildThreadFeed([projected({ ...imageItem, status: "running", completedAt: null }, 0)]),
|
||||
).toEqual([]);
|
||||
const failed = buildThreadFeed([
|
||||
projected({ ...imageItem, status: "failed", completedAt: imageItem.completedAt }, 0),
|
||||
]);
|
||||
expect(failed).toHaveLength(1);
|
||||
expect(failed[0]?.type).toBe("activity-group");
|
||||
if (failed[0]?.type === "activity-group") {
|
||||
expect(failed[0].activities[0]?.status).toBe("failure");
|
||||
expect(failed[0].activities[0]?.projectedItem.item.type).toBe("image_view");
|
||||
}
|
||||
});
|
||||
|
||||
it("retains inherited and synthetic rows with their original projected identity", () => {
|
||||
|
||||
@@ -737,8 +737,8 @@ export function buildThreadFeed(
|
||||
imagePath: item.path,
|
||||
projectedItem: row,
|
||||
});
|
||||
continue;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
const activity = toFeedActivity(row);
|
||||
entries.push({
|
||||
|
||||
@@ -291,14 +291,16 @@ describe("AssetAccess", () => {
|
||||
}).pipe(Effect.provide(testLayer)),
|
||||
);
|
||||
|
||||
it.effect("issues exact capabilities for V2 image turn items outside the workspace", () =>
|
||||
it.effect("issues exact capabilities for relative V2 image turn items inside the workspace", () =>
|
||||
Effect.gen(function* () {
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
const path = yield* Path.Path;
|
||||
const directory = yield* fileSystem.makeTempDirectoryScoped({
|
||||
const workspaceRoot = yield* fileSystem.makeTempDirectoryScoped({
|
||||
prefix: "t3-asset-image-view-",
|
||||
});
|
||||
const imagePath = path.join(directory, "tool-output.png");
|
||||
const imageDirectory = path.join(workspaceRoot, "images");
|
||||
yield* fileSystem.makeDirectory(imageDirectory);
|
||||
const imagePath = path.join(imageDirectory, "tool-output.png");
|
||||
yield* fileSystem.writeFile(imagePath, new Uint8Array([137, 80, 78, 71]));
|
||||
const canonicalImagePath = yield* fileSystem.realPath(imagePath);
|
||||
const resource = {
|
||||
@@ -309,7 +311,8 @@ describe("AssetAccess", () => {
|
||||
|
||||
const result = yield* issueAssetUrl({
|
||||
resource,
|
||||
threadImagePath: imagePath,
|
||||
threadImagePath: path.join("images", "tool-output.png"),
|
||||
workspaceRoot,
|
||||
});
|
||||
const suffix = result.relativeUrl.slice(`${ASSET_ROUTE_PREFIX}/`.length);
|
||||
const token = suffix.slice(0, suffix.indexOf("/"));
|
||||
@@ -331,10 +334,13 @@ describe("AssetAccess", () => {
|
||||
Effect.gen(function* () {
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
const path = yield* Path.Path;
|
||||
const directory = yield* fileSystem.makeTempDirectoryScoped({
|
||||
const workspaceRoot = yield* fileSystem.makeTempDirectoryScoped({
|
||||
prefix: "t3-asset-image-view-invalid-",
|
||||
});
|
||||
const textPath = path.join(directory, "notes.txt");
|
||||
const outsideRoot = yield* fileSystem.makeTempDirectoryScoped({
|
||||
prefix: "t3-asset-image-view-outside-",
|
||||
});
|
||||
const textPath = path.join(workspaceRoot, "notes.txt");
|
||||
yield* fileSystem.writeFileString(textPath, "not an image");
|
||||
const resource = {
|
||||
_tag: "thread-image" as const,
|
||||
@@ -345,14 +351,37 @@ describe("AssetAccess", () => {
|
||||
const unsupportedError = yield* issueAssetUrl({
|
||||
resource,
|
||||
threadImagePath: textPath,
|
||||
workspaceRoot,
|
||||
}).pipe(Effect.flip);
|
||||
expect(unsupportedError._tag).toBe("AssetThreadImageNotFoundError");
|
||||
|
||||
const imagePath = path.join(directory, "tool-output.png");
|
||||
const otherPath = path.join(directory, "other.png");
|
||||
const outsidePath = path.join(outsideRoot, "outside.png");
|
||||
yield* fileSystem.writeFile(outsidePath, new Uint8Array([137, 80, 78, 71]));
|
||||
const outsideError = yield* issueAssetUrl({
|
||||
resource,
|
||||
threadImagePath: outsidePath,
|
||||
workspaceRoot,
|
||||
}).pipe(Effect.flip);
|
||||
expect(outsideError._tag).toBe("AssetThreadImageNotFoundError");
|
||||
|
||||
const outsideLinkPath = path.join(workspaceRoot, "outside-link.png");
|
||||
yield* fileSystem.symlink(outsidePath, outsideLinkPath);
|
||||
const outsideLinkError = yield* issueAssetUrl({
|
||||
resource,
|
||||
threadImagePath: outsideLinkPath,
|
||||
workspaceRoot,
|
||||
}).pipe(Effect.flip);
|
||||
expect(outsideLinkError._tag).toBe("AssetThreadImageNotFoundError");
|
||||
|
||||
const imagePath = path.join(workspaceRoot, "tool-output.png");
|
||||
const otherPath = path.join(workspaceRoot, "other.png");
|
||||
yield* fileSystem.writeFile(imagePath, new Uint8Array([137, 80, 78, 71]));
|
||||
yield* fileSystem.writeFile(otherPath, new Uint8Array([1, 2, 3]));
|
||||
const result = yield* issueAssetUrl({ resource, threadImagePath: imagePath });
|
||||
const result = yield* issueAssetUrl({
|
||||
resource,
|
||||
threadImagePath: imagePath,
|
||||
workspaceRoot,
|
||||
});
|
||||
const suffix = result.relativeUrl.slice(`${ASSET_ROUTE_PREFIX}/`.length);
|
||||
const token = suffix.slice(0, suffix.indexOf("/"));
|
||||
yield* fileSystem.remove(imagePath);
|
||||
|
||||
@@ -220,7 +220,6 @@ const resolveCanonicalBrowserArtifact = Effect.fn("AssetAccess.resolveCanonicalB
|
||||
const config = yield* ServerConfig.ServerConfig;
|
||||
const path = yield* Path.Path;
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
const platform = yield* HostProcessPlatform;
|
||||
const [canonicalDir, canonicalFile] = yield* Effect.all([
|
||||
optionOnNotFound(fileSystem.realPath(path.join(config.stateDir, "browser-artifacts"))),
|
||||
optionOnNotFound(
|
||||
@@ -233,9 +232,7 @@ const resolveCanonicalBrowserArtifact = Effect.fn("AssetAccess.resolveCanonicalB
|
||||
Effect.orElseSucceed(() => [Option.none<string>(), Option.none<string>()] as const),
|
||||
);
|
||||
if (Option.isNone(canonicalDir) || Option.isNone(canonicalFile)) return null;
|
||||
if (
|
||||
!canonicalPathsMatch(path.join(canonicalDir.value, normalized), canonicalFile.value, platform)
|
||||
) {
|
||||
if (!canonicalPathsMatch(path.join(canonicalDir.value, normalized), canonicalFile.value)) {
|
||||
return null;
|
||||
}
|
||||
if (path.dirname(canonicalFile.value) !== canonicalDir.value) return null;
|
||||
@@ -424,18 +421,12 @@ export const issueAssetUrl = Effect.fn("AssetAccess.issueAssetUrl")(function* (i
|
||||
break;
|
||||
}
|
||||
case "thread-image": {
|
||||
if (
|
||||
!input.threadImagePath ||
|
||||
!path.isAbsolute(input.threadImagePath) ||
|
||||
!isWorkspaceImagePreviewPath(input.threadImagePath)
|
||||
) {
|
||||
if (!input.workspaceRoot || !input.threadImagePath) {
|
||||
return yield* new AssetThreadImageNotFoundError({
|
||||
resource: input.resource,
|
||||
});
|
||||
}
|
||||
const canonicalPath = yield* optionOnNotFound(
|
||||
fileSystem.realPath(input.threadImagePath),
|
||||
).pipe(
|
||||
const workspaceRoot = yield* workspacePaths.normalizeWorkspaceRoot(input.workspaceRoot).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new AssetThreadImageResolutionError({
|
||||
@@ -444,12 +435,18 @@ export const issueAssetUrl = Effect.fn("AssetAccess.issueAssetUrl")(function* (i
|
||||
}),
|
||||
),
|
||||
);
|
||||
if (Option.isNone(canonicalPath)) {
|
||||
const relativePath = path.isAbsolute(input.threadImagePath)
|
||||
? path.relative(workspaceRoot, input.threadImagePath)
|
||||
: input.threadImagePath;
|
||||
if (!isWorkspaceImagePreviewPath(relativePath)) {
|
||||
return yield* new AssetThreadImageNotFoundError({
|
||||
resource: input.resource,
|
||||
});
|
||||
}
|
||||
const info = yield* optionOnNotFound(fileSystem.stat(canonicalPath.value)).pipe(
|
||||
const canonicalPath = yield* resolveCanonicalWorkspaceFile({
|
||||
workspaceRoot,
|
||||
relativePath,
|
||||
}).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new AssetThreadImageResolutionError({
|
||||
@@ -458,11 +455,7 @@ export const issueAssetUrl = Effect.fn("AssetAccess.issueAssetUrl")(function* (i
|
||||
}),
|
||||
),
|
||||
);
|
||||
if (
|
||||
Option.isNone(info) ||
|
||||
info.value.type !== "File" ||
|
||||
!isWorkspaceImagePreviewPath(canonicalPath.value)
|
||||
) {
|
||||
if (!canonicalPath || !isWorkspaceImagePreviewPath(canonicalPath)) {
|
||||
return yield* new AssetThreadImageNotFoundError({
|
||||
resource: input.resource,
|
||||
});
|
||||
@@ -470,10 +463,10 @@ export const issueAssetUrl = Effect.fn("AssetAccess.issueAssetUrl")(function* (i
|
||||
claims = {
|
||||
version: 1,
|
||||
kind: "thread-image",
|
||||
path: canonicalPath.value,
|
||||
path: canonicalPath,
|
||||
expiresAt,
|
||||
};
|
||||
fileName = path.basename(canonicalPath.value);
|
||||
fileName = path.basename(canonicalPath);
|
||||
break;
|
||||
}
|
||||
case "project-favicon": {
|
||||
|
||||
@@ -1,31 +1,63 @@
|
||||
import { describe, expect, it } from "@effect/vitest";
|
||||
// @effect-diagnostics nodeBuiltinImport:off
|
||||
import * as NodeChildProcess from "node:child_process";
|
||||
import * as NodeFS from "node:fs";
|
||||
import * as NodeOS from "node:os";
|
||||
import * as NodePath from "node:path";
|
||||
|
||||
import { canonicalPathsMatch } from "./noFollowFile.ts";
|
||||
import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
|
||||
import { describe, expect, it } from "@effect/vitest";
|
||||
import * as Effect from "effect/Effect";
|
||||
|
||||
import {
|
||||
canonicalPathsMatch,
|
||||
openReadableFileNoFollow,
|
||||
openWritableFileNoFollow,
|
||||
} from "./noFollowFile.ts";
|
||||
|
||||
describe("canonicalPathsMatch", () => {
|
||||
it("accepts canonical Windows paths whose casing differs", () => {
|
||||
it("fails closed when canonical Windows path casing differs", () => {
|
||||
expect(
|
||||
canonicalPathsMatch(
|
||||
String.raw`C:\Users\Example\capture.png`,
|
||||
String.raw`C:\Users\example\CAPTURE.png`,
|
||||
"win32",
|
||||
),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("keeps canonical path comparison case-sensitive on other platforms", () => {
|
||||
expect(
|
||||
canonicalPathsMatch("/Users/Example/capture.png", "/users/example/capture.png", "darwin"),
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("rejects Windows paths that differ by more than casing", () => {
|
||||
it("accepts an unchanged canonical path", () => {
|
||||
expect(canonicalPathsMatch("/Users/Example/capture.png", "/Users/Example/capture.png")).toBe(
|
||||
true,
|
||||
);
|
||||
});
|
||||
|
||||
it("rejects paths that resolve to a different target", () => {
|
||||
expect(
|
||||
canonicalPathsMatch(
|
||||
String.raw`C:\Users\Example\capture.png`,
|
||||
String.raw`C:\Users\Example\outside.png`,
|
||||
"win32",
|
||||
),
|
||||
).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("no-follow file opens", () => {
|
||||
it.effect("rejects FIFOs without waiting for a peer", () =>
|
||||
Effect.gen(function* () {
|
||||
const platform = yield* HostProcessPlatform;
|
||||
if (platform === "win32") return;
|
||||
const directory = yield* Effect.acquireRelease(
|
||||
Effect.sync(() => NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-no-follow-fifo-"))),
|
||||
(directory) =>
|
||||
Effect.sync(() => NodeFS.rmSync(directory, { recursive: true, force: true })),
|
||||
);
|
||||
const fifoPath = NodePath.join(directory, "asset.png");
|
||||
yield* Effect.sync(() => NodeChildProcess.execFileSync("mkfifo", [fifoPath]));
|
||||
yield* Effect.promise(() =>
|
||||
expect(openReadableFileNoFollow(fifoPath, platform)).rejects.toBeDefined(),
|
||||
);
|
||||
yield* Effect.promise(() =>
|
||||
expect(openWritableFileNoFollow(fifoPath, platform)).rejects.toBeDefined(),
|
||||
);
|
||||
}),
|
||||
);
|
||||
});
|
||||
@@ -50,14 +50,13 @@ async function openLinuxPath(
|
||||
async function verifyFallbackHandle(
|
||||
absolutePath: string,
|
||||
handle: NodeFSP.FileHandle,
|
||||
platform: NodeJS.Platform,
|
||||
): Promise<NodeFSP.FileHandle> {
|
||||
try {
|
||||
const handleInfo = await handle.stat();
|
||||
const canonicalPath = await NodeFS.promises.realpath(absolutePath);
|
||||
const pathInfo = await NodeFS.promises.stat(absolutePath);
|
||||
if (
|
||||
!canonicalPathsMatch(absolutePath, canonicalPath, platform) ||
|
||||
!canonicalPathsMatch(absolutePath, canonicalPath) ||
|
||||
pathInfo.dev !== handleInfo.dev ||
|
||||
pathInfo.ino !== handleInfo.ino
|
||||
) {
|
||||
@@ -70,15 +69,8 @@ async function verifyFallbackHandle(
|
||||
}
|
||||
}
|
||||
|
||||
export function canonicalPathsMatch(
|
||||
absolutePath: string,
|
||||
canonicalPath: string,
|
||||
platform: NodeJS.Platform,
|
||||
): boolean {
|
||||
if (canonicalPath === absolutePath) {
|
||||
return true;
|
||||
}
|
||||
return platform === "win32" && canonicalPath.toLowerCase() === absolutePath.toLowerCase();
|
||||
export function canonicalPathsMatch(absolutePath: string, canonicalPath: string): boolean {
|
||||
return canonicalPath === absolutePath;
|
||||
}
|
||||
|
||||
async function openPathNoFollow(
|
||||
@@ -91,16 +83,17 @@ async function openPathNoFollow(
|
||||
throw new Error(`Cannot safely open non-canonical path: ${absolutePath}`);
|
||||
}
|
||||
|
||||
const nonBlockingFlags = flags | NodeFS.constants.O_NONBLOCK;
|
||||
if (platform === "darwin") {
|
||||
return NodeFS.promises.open(absolutePath, flags | DARWIN_O_NOFOLLOW_ANY, mode);
|
||||
return NodeFS.promises.open(absolutePath, nonBlockingFlags | DARWIN_O_NOFOLLOW_ANY, mode);
|
||||
}
|
||||
if (platform === "linux") {
|
||||
return openLinuxPath(absolutePath, flags, mode);
|
||||
return openLinuxPath(absolutePath, nonBlockingFlags, mode);
|
||||
}
|
||||
|
||||
const noFollow = NodeFS.constants.O_NOFOLLOW ?? 0;
|
||||
const handle = await NodeFS.promises.open(absolutePath, flags | noFollow, mode);
|
||||
return verifyFallbackHandle(absolutePath, handle, platform);
|
||||
const handle = await NodeFS.promises.open(absolutePath, nonBlockingFlags | noFollow, mode);
|
||||
return verifyFallbackHandle(absolutePath, handle);
|
||||
}
|
||||
|
||||
export async function openReadableFileNoFollow(
|
||||
|
||||
@@ -1586,9 +1586,24 @@ const makeWsRpcLayer = (
|
||||
resource,
|
||||
});
|
||||
}
|
||||
const project = yield* projectService.getById(projection.thread.projectId).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new AssetThreadImageResolutionError({
|
||||
resource,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
if (Option.isNone(project)) {
|
||||
return yield* new AssetThreadImageNotFoundError({
|
||||
resource,
|
||||
});
|
||||
}
|
||||
return yield* issueAssetUrl({
|
||||
resource,
|
||||
threadImagePath,
|
||||
workspaceRoot: projection.thread.worktreePath ?? project.value.workspaceRoot,
|
||||
});
|
||||
}
|
||||
if (input.resource._tag !== "workspace-file") {
|
||||
|
||||
@@ -408,17 +408,25 @@ describe("V2 session presentation", () => {
|
||||
},
|
||||
]);
|
||||
|
||||
expect(
|
||||
deriveTimelineEntriesFromVisibleTurnItems({
|
||||
visibleTurnItems: [
|
||||
{
|
||||
...projectedItem,
|
||||
item: { ...item, status: "running", completedAt: null },
|
||||
},
|
||||
],
|
||||
optimisticMessages: [],
|
||||
}),
|
||||
).toEqual([]);
|
||||
const failedEntries = deriveTimelineEntriesFromVisibleTurnItems({
|
||||
visibleTurnItems: [
|
||||
{
|
||||
...projectedItem,
|
||||
item: { ...item, status: "failed" },
|
||||
},
|
||||
],
|
||||
optimisticMessages: [],
|
||||
});
|
||||
expect(failedEntries).toHaveLength(1);
|
||||
expect(failedEntries[0]).toMatchObject({
|
||||
id: item.id,
|
||||
kind: "work",
|
||||
entry: {
|
||||
itemType: "image_view",
|
||||
tone: "error",
|
||||
toolLifecycleStatus: "failed",
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("waits for a dispatched turn item before adding queued input to the timeline", () => {
|
||||
|
||||
@@ -627,8 +627,8 @@ export function deriveTimelineEntriesFromVisibleTurnItems(input: {
|
||||
projectedItem: row,
|
||||
...attemptMetadata,
|
||||
});
|
||||
continue;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (STANDALONE_V2_ITEM_TYPES.has(item.type)) {
|
||||
|
||||
Reference in new issue
Block a user