Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

capture v2: no credential attachments from outside the project, home as ~; oversized git configs left out

ogsamrat55ecb5aaf560
diff --git a/apps/server/src/session-archive/attachments.ts b/apps/server/src/session-archive/attachments.ts
index 5780ed4e..6537801a 100644
--- a/apps/server/src/session-archive/attachments.ts
+++ b/apps/server/src/session-archive/attachments.ts
@@ -12,11 +12,34 @@
* sends each once. Identical in the CLI and the desktop app.
*/
import { createHash } from "node:crypto";
-import { appendFile, lstat, mkdir, open, readFile, rm } from "node:fs/promises";
-import { isAbsolute, join, relative, resolve, sep } from "node:path";
+import { appendFile, lstat, mkdir, open, readFile, realpath, rm } from "node:fs/promises";
+import { homedir } from "node:os";
+import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
import { fileURLToPath } from "node:url";
import { stateKey } from "./files.js";
+import { outsideExclusion } from "./outside.js";
+
+/** How the store judges a file attached from outside the workspace (outside.ts's exclusions). */
+export type AttachmentOutsideRules = { appDirs: readonly string[]; includeCredentialFiles: boolean; home?: string | null };
+
+function safeHome(): string | null {
+ try {
+ return homedir() || null;
+ } catch {
+ return null;
+ }
+}
+
+/** An absolute path with the home directory written as `~` (portable `/`), so no account name is stored. */
+export function homeRelativePath(absolute: string, home: string | null): string {
+ const portable = absolute.split(sep).join("/");
+ if (!home) return portable;
+ const rel = relative(home, absolute);
+ if (rel === "") return "~";
+ if (rel === ".." || rel.startsWith(`..${sep}`) || isAbsolute(rel)) return portable;
+ return `~/${rel.split(sep).join("/")}`;
+}
export const ATTACHMENTS_ROOT_NAME = "__attachments__";
/** One attachment's bytes, at most (the desktop's prompt attachment cap). */
@@ -159,6 +182,7 @@ export class AttachmentStore {
constructor(
private readonly dir: string,
private readonly isDenied: (relativePath: string) => boolean = () => false,
+ private readonly outside: AttachmentOutsideRules = { appDirs: [], includeCredentialFiles: false },
) {}
private sessionDir(sessionId: string): string {
@@ -185,11 +209,21 @@ export class AttachmentStore {
let sourcePath: string | null = null;
let bytes = source.data;
if (source.file) {
- const rel = within(resolve(root), source.file);
- // A credential file is never copied, wherever it was attached from.
+ // The file as reached through real folders (a folder link cannot hide where it is).
+ let real = source.file;
+ try {
+ real = join(await realpath(dirname(source.file)), basename(source.file));
+ } catch {
+ continue;
+ }
+ const home = this.outside.home === undefined ? safeHome() : this.outside.home;
+ const rootReal = await realpath(root).catch(() => resolve(root));
+ const rel = within(resolve(root), source.file) ?? within(rootReal, real);
+ // A credential file, an app's state or a system file is never copied, wherever it was attached from.
if (rel !== null && this.isDenied(rel)) continue;
- sourcePath = rel ?? source.file;
- bytes = await readAttachedFile(source.file);
+ if (rel === null && [source.file, real].some((form) => outsideExclusion(form, { ...this.outside, home }) !== null)) continue;
+ sourcePath = rel ?? homeRelativePath(source.file, home);
+ bytes = await readAttachedFile(real);
}
if (!bytes || bytes.length > MAX_ATTACHMENT_BYTES) continue;
await mkdir(dir, { recursive: true, mode: 0o700 });
diff --git a/apps/server/src/session-archive/capture-v2.test.ts b/apps/server/src/session-archive/capture-v2.test.ts
index 28f077c8..2e11561a 100644
--- a/apps/server/src/session-archive/capture-v2.test.ts
+++ b/apps/server/src/session-archive/capture-v2.test.ts
@@ -1,15 +1,15 @@
import { afterAll, describe, expect, test } from "bun:test";
import { execFileSync } from "node:child_process";
import { createHash, randomBytes } from "node:crypto";
-import { chmodSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, statSync, symlinkSync, writeFileSync } from "node:fs";
+import { appendFileSync, chmodSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, statSync, symlinkSync, writeFileSync } from "node:fs";
import http from "node:http";
import { tmpdir } from "node:os";
-import { dirname, join } from "node:path";
+import { basename, dirname, join } from "node:path";
import { pathToFileURL } from "node:url";
import { zstdDecompressSync } from "node:zlib";
import { filterUploadDiff, collectGitBlock, SessionUploader, utf8DiffText } from "../session-uploader.js";
-import { attachmentArchivePath, attachmentsFromMessages } from "./attachments.js";
+import { AttachmentStore, attachmentArchivePath, attachmentsFromMessages, homeRelativePath } from "./attachments.js";
import { binaryReason } from "./binary.js";
import { isGitConfigPath, scrubGitConfig } from "./git-scrub.js";
import { SessionArchiver } from "./index.js";
@@ -208,6 +208,50 @@ describe("capture v2", () => {
expect([rootRepo.remote, rootRepo.stash_count, rootRepo.archived]).toEqual(["https://github.com/o/r.git", 1, "byte_exact"]);
});
+ test("#23: a git config over the scrub cap is left out as too_large, never archived raw; a submodule config too", async () => {
+ const { archiver, open } = rig();
+ const root = temp("omnirush-v2-bigconfig-");
+ write(join(root, "a.txt"), "a\n");
+ git(root, "init", "-q");
+ const padding = `# ${"x".repeat(120)}\n`.repeat(9_000);
+ appendFileSync(join(root, ".git", "config"), `[remote "origin"]\n\turl = https://u:[email protected]/r.git\n${padding}`);
+ write(join(root, ".git", "modules", "sub", "config"), `[http]\n\textraheader = Authorization: Bearer tok_SUBSECRET\n${padding}`);
+ const base = await open(await archiver.captureBase("ses_v2_big_config", root, 0));
+ for (const config of [".git/config", ".git/modules/sub/config"]) {
+ expect(base.entry(config)).toBeUndefined();
+ expect(base.members.has(config)).toBe(false);
+ const item = base.state!.excluded.find((entry) => entry.path === config) as { reason: string; size?: number } | undefined;
+ expect(item?.reason).toBe("too_large");
+ expect(item?.size).toBe(statSync(join(root, ...config.split("/"))).size);
+ }
+ for (const member of base.members.values()) {
+ expect(member.content.includes("tok_BIGSECRET") || member.content.includes("tok_SUBSECRET")).toBe(false);
+ }
+ });
+
+ test("#11: attachments from outside the project never copy credentials (~/.ssh, ~/.aws, .env); source paths name home as ~", async () => {
+ const stateDir = temp("omnirush-v2-attach-state-");
+ const home = temp("omnirush-v2-home-");
+ const root = join(home, "work", "proj");
+ write(join(root, "a.txt"), "a\n");
+ write(join(home, ".ssh", "id_ed25519"), "-----BEGIN OPENSSH PRIVATE KEY-----\n");
+ write(join(home, ".aws", "credentials"), "[default]\naws_secret_access_key = x\n");
+ write(join(home, "Downloads", ".env"), "TOKEN=x\n");
+ const photo = write(join(home, "Pictures", "photo.png"), randomBytes(400));
+ const store = new AttachmentStore(join(stateDir, "att"), () => false, { appDirs: [stateDir], includeCredentialFiles: false, home });
+ const file = (path: string, index: number) => ({ messageId: "msg_priv", index, name: basename(path), mime: "application/octet-stream", data: null, file: path });
+ const added = await store.add("ses_v2_attach_priv", root, [
+ file(join(home, ".ssh", "id_ed25519"), 0),
+ file(join(home, ".aws", "credentials"), 1),
+ file(join(home, "Downloads", ".env"), 2),
+ file(join(home, "Pictures", "photo.png"), 3),
+ ]);
+ expect(added.map((record) => [record.name, record.source_path])).toEqual([["photo.png", "~/Pictures/photo.png"]]);
+ expect(readFileSync(added[0]!.blob).equals(photo)).toBe(true);
+ expect(JSON.stringify(added.map(({ blob: _blob, ...rest }) => rest))).not.toContain(home);
+ expect(homeRelativePath("/opt/data/x.bin", home)).toBe("/opt/data/x.bin");
+ });
+
test("#15: the repository above the folder, one in a subfolder, and one without commits", async () => {
const { archiver, open } = rig();
const outer = temp("omnirush-v2-outer-");
diff --git a/apps/server/src/session-archive/index.ts b/apps/server/src/session-archive/index.ts
index 5e767cc5..80f35b91 100644
--- a/apps/server/src/session-archive/index.ts
+++ b/apps/server/src/session-archive/index.ts
@@ -423,7 +423,6 @@ export class SessionArchiver {
tmp: join(this.dir, "tmp"),
touched: join(this.dir, "touched"),
};
- this.attachments = new AttachmentStore(join(this.dir, "attachments"), (path) => !this.includeCredentials && isUploadPathDenied(path));
this.log = options.log ?? (() => undefined);
this.now = options.now ?? (() => new Date());
this.random = options.random ?? { uuid: () => randomUUID(), bytes: (n) => randomBytes(n) };
@@ -451,6 +450,7 @@ export class SessionArchiver {
this.folderGate = options.folderGate;
this.appDirs = [stateDir, ...(options.excludedDirs ?? []).map((dir) => resolve(dir))];
this.includeCredentials = options.archiveIncludeCredentialFiles === true;
+ this.attachments = new AttachmentStore(join(this.dir, "attachments"), (path) => !this.includeCredentials && isUploadPathDenied(path), { appDirs: this.appDirs, includeCredentialFiles: this.includeCredentials });
this.touched = new TouchedPathStore(this.dirs.touched, {
ready: () => this.start(),
modeOf: async (sessionId) => {
diff --git a/apps/server/src/session-archive/manifest.ts b/apps/server/src/session-archive/manifest.ts
index d069294d..510307d4 100644
--- a/apps/server/src/session-archive/manifest.ts
+++ b/apps/server/src/session-archive/manifest.ts
@@ -26,7 +26,7 @@ export const RESERVED_ROOT_NAME = "__omnirush__";
export const OUTSIDE_ROOT_NAME = "__outside__";
/** Capture v2: prompt attachments (attachments.ts); a workspace entry of that name is reserved too. */
export { ATTACHMENTS_ROOT_NAME };
-/** A git config file larger than this is archived as it is (no real config is): it is never read whole to scrub it. */
+/** A git config file larger than this is left out (`too_large`): it is never read whole to scrub it, and never archived raw. */
const MAX_SCRUBBED_CONFIG_BYTES = 1024 * 1024;
export const STAT_CONCURRENCY = 64;
export const HASH_CONCURRENCY = 6;
@@ -513,11 +513,10 @@ export async function scrubConfigEntry(absolute: string, entry: ScannedEntry, me
metrics.bytesHashed += raw.length;
const scrub = scrubGitConfig(raw.toString("utf8"));
const bytes = scrub.changed ? Buffer.from(scrub.text, "utf8") : raw;
- if (scrub.changed) {
- entry.content = bytes;
- entry.size = bytes.length;
- entry.scrubbed = true;
- }
+ // Pass 2 writes exactly these checked bytes: a config rewritten after this read never reaches the archive.
+ entry.content = bytes;
+ entry.size = bytes.length;
+ if (scrub.changed) entry.scrubbed = true;
entry.sha256 = createHash("sha256").update(bytes).digest("hex");
return true;
} catch {
@@ -623,7 +622,13 @@ export async function scanArchiveTree(root: string, options: ScanOptions = {}):
return;
}
const entry: ScannedEntry = { path: rel, type: "file", size: Number(stats.size), sha256: null, ...statFields(stats) };
- if (isGitConfigPath(rel) && entry.size <= MAX_SCRUBBED_CONFIG_BYTES) {
+ if (isGitConfigPath(rel) && entry.size > MAX_SCRUBBED_CONFIG_BYTES) {
+ // Too large to scrub: never archived raw (it could carry credentials). Listed with its size.
+ excluded.special += 1;
+ note(rel, "too_large", "file", entry.size);
+ return;
+ }
+ if (isGitConfigPath(rel)) {
// Read and scrubbed in the hash pass; never from the cache (its bytes are needed for pass 2).
toHash.push(entry);
entries.push(entry);
@@ -691,7 +696,7 @@ export async function scanArchiveTree(root: string, options: ScanOptions = {}):
const buffers: Buffer[] = [];
await forEachBounded(toHash, options.hashConcurrency ?? HASH_CONCURRENCY, async (entry) => {
signal?.throwIfAborted();
- if (isGitConfigPath(entry.path) && entry.size <= MAX_SCRUBBED_CONFIG_BYTES) {
+ if (isGitConfigPath(entry.path)) {
if (!(await scrubConfigEntry(join(root, ...entry.path.split("/")), entry, metrics))) unreadable.add(entry);
return;
}