Harden attachment materialization and static file path handling

- Materialize only valid image data URLs and restrict inferred file extensions to a safe whitelist
- Preserve existing message attachments on upsert unless explicitly replaced or cleared
- Serve static `/` via `index.html` and block traversal attempts in static file requests
This commit is contained in:
Julius Marminge committed 2026-02-27 20:18:35 -08:00
1 parent 6845143b9e
commit 9f56dd0193
7 files changed
+364 -15

No files matched your search

@@ -247,6 +247,117 @@ projectionLayer("OrchestrationProjectionPipeline", (it) => {
}),
);
it.effect("materializes only image data URLs and uses a safe extension whitelist", () =>
Effect.gen(function* () {
const projectionPipeline = yield* OrchestrationProjectionPipeline;
const eventStore = yield* OrchestrationEventStore;
const sql = yield* SqlClient.SqlClient;
const now = new Date().toISOString();
const stateDir = fs.mkdtempSync(path.join(os.tmpdir(), "t3-projection-attachments-"));
const serverConfig = {
mode: "web",
port: 0,
host: undefined,
cwd: "/tmp/project-attachments",
keybindingsConfigPath: path.join(stateDir, "keybindings.json"),
stateDir,
staticDir: undefined,
devUrl: undefined,
noBrowser: true,
authToken: undefined,
autoBootstrapProjectFromCwd: false,
logWebSocketEvents: false,
} satisfies ServerConfigShape;
yield* eventStore.append({
type: "thread.message-sent",
eventId: EventId.makeUnsafe("evt-attachments-safe"),
aggregateKind: "thread",
aggregateId: ThreadId.makeUnsafe("thread-attachments-safe"),
occurredAt: now,
commandId: CommandId.makeUnsafe("cmd-attachments-safe"),
causationEventId: null,
correlationId: CommandId.makeUnsafe("cmd-attachments-safe"),
metadata: {},
payload: {
threadId: ThreadId.makeUnsafe("thread-attachments-safe"),
messageId: MessageId.makeUnsafe("message-attachments-safe"),
role: "user",
text: "Inspect this",
attachments: [
{
type: "image",
name: "untrusted.exe",
mimeType: "image/x-unknown",
sizeBytes: 5,
dataUrl: "data:image/x-unknown;base64,SGVsbG8=",
},
{
type: "image",
name: "not-image.png",
mimeType: "image/png",
sizeBytes: 5,
dataUrl: "data:text/plain;base64,SGVsbG8=",
},
],
turnId: null,
streaming: false,
createdAt: now,
updatedAt: now,
},
});
yield* projectionPipeline.bootstrap.pipe(Effect.provideService(ServerConfig, serverConfig));
const rows = yield* sql<{
readonly attachmentsJson: string | null;
}>`
SELECT
attachments_json AS "attachmentsJson"
FROM projection_thread_messages
WHERE message_id = 'message-attachments-safe'
`;
assert.equal(rows.length, 1);
assert.deepEqual(JSON.parse(rows[0]?.attachmentsJson ?? "null"), [
{
type: "image",
name: "untrusted.exe",
mimeType: "image/x-unknown",
sizeBytes: 5,
dataUrl: "/attachments/thread-attachments-safe/message-attachments-safe/0.bin",
},
{
type: "image",
name: "not-image.png",
mimeType: "image/png",
sizeBytes: 5,
dataUrl: "data:text/plain;base64,SGVsbG8=",
},
]);
const firstAttachmentPath = path.join(
stateDir,
"attachments",
"thread-attachments-safe",
"message-attachments-safe",
"0.bin",
);
assert.equal(fs.existsSync(firstAttachmentPath), true);
assert.deepEqual(fs.readFileSync(firstAttachmentPath), Buffer.from("SGVsbG8=", "base64"));
const secondAttachmentPath = path.join(
stateDir,
"attachments",
"thread-attachments-safe",
"message-attachments-safe",
"1.png",
);
assert.equal(fs.existsSync(secondAttachmentPath), false);
fs.rmSync(stateDir, { recursive: true, force: true });
}),
);
it.effect("resumes from projector last_applied_sequence without replaying older events", () =>
Effect.gen(function* () {
const projectionPipeline = yield* OrchestrationProjectionPipeline;
@@ -70,6 +70,17 @@ const IMAGE_EXTENSION_BY_MIME_TYPE: Record<string, string> = {
"image/tiff": ".tiff",
"image/heic": ".heic",
};
const SAFE_IMAGE_FILE_EXTENSIONS = new Set([
".jpg",
".jpeg",
".png",
".gif",
".webp",
".bmp",
".tiff",
".svg",
".ico",
]);
function parseBase64DataUrl(
dataUrl: string,
@@ -87,14 +98,18 @@ function parseBase64DataUrl(
}
function inferImageExtension(attachment: Extract<ChatAttachment, { type: "image" }>): string {
const fromMime = IMAGE_EXTENSION_BY_MIME_TYPE[attachment.mimeType.toLowerCase()];
if (fromMime) {
return fromMime;
const normalizedMimeType = attachment.mimeType.trim().toLowerCase();
if (normalizedMimeType.startsWith("image/")) {
const fromMime = IMAGE_EXTENSION_BY_MIME_TYPE[normalizedMimeType];
if (fromMime) {
return fromMime;
}
}
const fromName = attachment.name.includes(".")
? `.${attachment.name.split(".").slice(-1)[0]?.toLowerCase() ?? ""}`
: "";
if (/^\.[a-z0-9]{1,12}$/.test(fromName)) {
if (/^\.[a-z0-9]{1,12}$/.test(fromName) && SAFE_IMAGE_FILE_EXTENSIONS.has(fromName)) {
return fromName;
}
return ".bin";
@@ -132,13 +147,19 @@ const materializeAttachmentsForProjection = Effect.fn(function* (input: {
if (!parsed) {
return attachment;
}
if (!parsed.mimeType.startsWith("image/")) {
return attachment;
}
const bytes = Buffer.from(parsed.base64, "base64");
if (bytes.byteLength === 0) {
return attachment;
}
const fileName = `${index}${inferImageExtension(attachment)}`;
const fileName = `${index}${inferImageExtension({
...attachment,
mimeType: parsed.mimeType,
})}`;
const relativePath = `${threadSegment}/${messageSegment}/${fileName}`;
const absolutePath = path.join(attachmentsRootDir, threadSegment, messageSegment, fileName);
yield* fileSystem.makeDirectory(path.dirname(absolutePath), { recursive: true });
@@ -0,0 +1,106 @@
import { MessageId, ThreadId } from "@t3tools/contracts";
import { assert, it } from "@effect/vitest";
import { Effect, Layer } from "effect";
import { ProjectionThreadMessageRepository } from "../Services/ProjectionThreadMessages.ts";
import { ProjectionThreadMessageRepositoryLive } from "./ProjectionThreadMessages.ts";
import { SqlitePersistenceMemory } from "./Sqlite.ts";
const layer = it.layer(
ProjectionThreadMessageRepositoryLive.pipe(Layer.provideMerge(SqlitePersistenceMemory)),
);
layer("ProjectionThreadMessageRepository", (it) => {
it.effect("preserves existing attachments when upsert omits attachments", () =>
Effect.gen(function* () {
const repository = yield* ProjectionThreadMessageRepository;
const threadId = ThreadId.makeUnsafe("thread-preserve-attachments");
const messageId = MessageId.makeUnsafe("message-preserve-attachments");
const createdAt = "2026-02-28T19:00:00.000Z";
const updatedAt = "2026-02-28T19:00:01.000Z";
const persistedAttachments = [
{
type: "image" as const,
name: "example.png",
mimeType: "image/png",
sizeBytes: 5,
dataUrl: "/attachments/thread-preserve-attachments/message-preserve-attachments/0.png",
},
];
yield* repository.upsert({
messageId,
threadId,
turnId: null,
role: "user",
text: "initial",
attachments: persistedAttachments,
isStreaming: false,
createdAt,
updatedAt,
});
yield* repository.upsert({
messageId,
threadId,
turnId: null,
role: "user",
text: "updated",
isStreaming: false,
createdAt,
updatedAt: "2026-02-28T19:00:02.000Z",
});
const rows = yield* repository.listByThreadId({ threadId });
assert.equal(rows.length, 1);
assert.equal(rows[0]?.text, "updated");
assert.deepEqual(rows[0]?.attachments, persistedAttachments);
}),
);
it.effect("allows explicit attachment clearing with an empty array", () =>
Effect.gen(function* () {
const repository = yield* ProjectionThreadMessageRepository;
const threadId = ThreadId.makeUnsafe("thread-clear-attachments");
const messageId = MessageId.makeUnsafe("message-clear-attachments");
const createdAt = "2026-02-28T19:10:00.000Z";
yield* repository.upsert({
messageId,
threadId,
turnId: null,
role: "assistant",
text: "with attachment",
attachments: [
{
type: "image",
name: "example.png",
mimeType: "image/png",
sizeBytes: 5,
dataUrl: "/attachments/thread-clear-attachments/message-clear-attachments/0.png",
},
],
isStreaming: false,
createdAt,
updatedAt: "2026-02-28T19:10:01.000Z",
});
yield* repository.upsert({
messageId,
threadId,
turnId: null,
role: "assistant",
text: "cleared",
attachments: [],
isStreaming: false,
createdAt,
updatedAt: "2026-02-28T19:10:02.000Z",
});
const rows = yield* repository.listByThreadId({ threadId });
assert.equal(rows.length, 1);
assert.equal(rows[0]?.text, "cleared");
assert.deepEqual(rows[0]?.attachments, []);
}),
);
});
@@ -23,8 +23,9 @@ const makeProjectionThreadMessageRepository = Effect.gen(function* () {
const upsertProjectionThreadMessageRow = SqlSchema.void({
Request: ProjectionThreadMessage,
execute: (row) =>
sql`
execute: (row) => {
const nextAttachmentsJson = row.attachments !== undefined ? JSON.stringify(row.attachments) : null;
return sql`
INSERT INTO projection_thread_messages (
message_id,
thread_id,
@@ -42,7 +43,14 @@ const makeProjectionThreadMessageRepository = Effect.gen(function* () {
${row.turnId},
${row.role},
${row.text},
${row.attachments !== undefined ? JSON.stringify(row.attachments) : null},
COALESCE(
${nextAttachmentsJson},
(
SELECT attachments_json
FROM projection_thread_messages
WHERE message_id = ${row.messageId}
)
),
${row.isStreaming ? 1 : 0},
${row.createdAt},
${row.updatedAt}
@@ -53,11 +61,15 @@ const makeProjectionThreadMessageRepository = Effect.gen(function* () {
turn_id = excluded.turn_id,
role = excluded.role,
text = excluded.text,
attachments_json = excluded.attachments_json,
attachments_json = COALESCE(
excluded.attachments_json,
projection_thread_messages.attachments_json
),
is_streaming = excluded.is_streaming,
created_at = excluded.created_at,
updated_at = excluded.updated_at
`,
`;
},
});
const listProjectionThreadMessageRows = SqlSchema.findAll({
+62 -1
View File
@@ -296,6 +296,36 @@ async function waitForPush(
return take(maxMessages);
}
async function requestPath(
port: number,
requestPath: string,
): Promise<{ statusCode: number; body: string }> {
return new Promise((resolve, reject) => {
const req = Http.request(
{
hostname: "127.0.0.1",
port,
path: requestPath,
method: "GET",
},
(res) => {
const chunks: Buffer[] = [];
res.on("data", (chunk) => {
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
});
res.on("end", () => {
resolve({
statusCode: res.statusCode ?? 0,
body: Buffer.concat(chunks).toString("utf8"),
});
});
},
);
req.once("error", reject);
req.end();
});
}
function compileKeybindings(bindings: KeybindingsConfig): ResolvedKeybindingsConfig {
const resolved: Array<ResolvedKeybindingsConfig[number]> = [];
for (const binding of bindings) {
@@ -331,6 +361,7 @@ describe("WebSocket Server", () => {
devUrl?: string;
authToken?: string;
stateDir?: string;
staticDir?: string;
providerLayer?: Layer.Layer<ProviderService, never>;
open?: OpenShape;
gitManager?: GitManagerShape;
@@ -354,7 +385,7 @@ describe("WebSocket Server", () => {
cwd: options.cwd ?? "/test/project",
keybindingsConfigPath: path.join(stateDir, "keybindings.json"),
stateDir,
staticDir: undefined,
staticDir: options.staticDir,
devUrl: options.devUrl ? new URL(options.devUrl) : undefined,
noBrowser: true,
authToken: options.authToken,
@@ -456,6 +487,36 @@ describe("WebSocket Server", () => {
expect(bytes).toEqual(Buffer.from("hello-attachment"));
});
it("serves static index for root path", async () => {
const stateDir = makeTempDir("t3code-state-static-root-");
const staticDir = makeTempDir("t3code-static-root-");
fs.writeFileSync(path.join(staticDir, "index.html"), "<h1>static-root</h1>", "utf8");
server = await createTestServer({ cwd: "/test/project", stateDir, staticDir });
const addr = server.address();
const port = typeof addr === "object" && addr !== null ? addr.port : 0;
expect(port).toBeGreaterThan(0);
const response = await fetch(`http://127.0.0.1:${port}/`);
expect(response.status).toBe(200);
expect(await response.text()).toContain("static-root");
});
it("rejects static path traversal attempts", async () => {
const stateDir = makeTempDir("t3code-state-static-traversal-");
const staticDir = makeTempDir("t3code-static-traversal-");
fs.writeFileSync(path.join(staticDir, "index.html"), "<h1>safe</h1>", "utf8");
server = await createTestServer({ cwd: "/test/project", stateDir, staticDir });
const addr = server.address();
const port = typeof addr === "object" && addr !== null ? addr.port : 0;
expect(port).toBeGreaterThan(0);
const response = await requestPath(port, "/..%2f..%2fetc/passwd");
expect(response.statusCode).toBe(400);
expect(response.body).toBe("Invalid static file path");
});
it("bootstraps the cwd project on startup when enabled", async () => {
server = await createTestServer({
cwd: "/test/bootstrap-workspace",
+36 -3
View File
@@ -315,17 +315,50 @@ export const createServer = Effect.fn(function* (): Effect.fn.Return<
return;
}
let filePath = path.join(staticDir, url.pathname);
const staticRoot = path.resolve(staticDir);
const staticRequestPath = url.pathname === "/" ? "/index.html" : url.pathname;
const rawStaticRelativePath = staticRequestPath.replace(/^[/\\]+/, "");
const hasRawLeadingParentSegment = rawStaticRelativePath.startsWith("..");
const staticRelativePath = path
.normalize(rawStaticRelativePath)
.replace(/^[/\\]+/, "");
const hasPathTraversalSegment = staticRelativePath.startsWith("..");
if (
staticRelativePath.length === 0 ||
hasRawLeadingParentSegment ||
hasPathTraversalSegment ||
staticRelativePath.includes("\0")
) {
respond(400, { "Content-Type": "text/plain" }, "Invalid static file path");
return;
}
const isWithinStaticRoot = (candidate: string) =>
candidate === staticRoot ||
candidate.startsWith(
staticRoot.endsWith(path.sep) ? staticRoot : `${staticRoot}${path.sep}`,
);
let filePath = path.resolve(staticRoot, staticRelativePath);
if (!isWithinStaticRoot(filePath)) {
respond(400, { "Content-Type": "text/plain" }, "Invalid static file path");
return;
}
const ext = path.extname(filePath);
if (!ext) {
filePath = path.join(filePath, "index.html");
filePath = path.resolve(filePath, "index.html");
if (!isWithinStaticRoot(filePath)) {
respond(400, { "Content-Type": "text/plain" }, "Invalid static file path");
return;
}
}
const fileInfo = yield* fileSystem
.stat(filePath)
.pipe(Effect.catch(() => Effect.succeed(null)));
if (!fileInfo || fileInfo.type !== "File") {
const indexPath = path.join(staticDir, "index.html");
const indexPath = path.resolve(staticRoot, "index.html");
const indexData = yield* fileSystem
.readFile(indexPath)
.pipe(Effect.catch(() => Effect.succeed(null)));
+6 -1
View File
@@ -210,7 +210,12 @@ function resolveWsHttpOrigin(): string {
try {
const wsUrl = new URL(wsCandidate);
const protocol = wsUrl.protocol === "wss:" ? "https:" : "http:";
const protocol =
wsUrl.protocol === "wss:"
? "https:"
: wsUrl.protocol === "ws:"
? "http:"
: wsUrl.protocol;
return `${protocol}//${wsUrl.host}`;
} catch {
return window.location.origin;