#1 Persistent memory for AI coding agents based on real-world benchmarks
TypeScript agentmemoryagentsaiclaudeclaudecodecodexcopilotcursorgenaiharnesshermesmemoryompopenclawopencodepi
fix: replace lstat-only symlink guard with O_NOFOLLOW at write time to close TOCTOU window
Rohit Ghumare05b6a0a370f8
diff --git a/src/functions/compress-file.ts b/src/functions/compress-file.tsindex 53e3e9e9..0a54452b 100644--- a/src/functions/compress-file.ts+++ b/src/functions/compress-file.ts@@ -1,4 +1,5 @@-import { lstat, readFile, writeFile } from "node:fs/promises";+import { constants } from "node:fs";+import { lstat, open, readFile, writeFile } from "node:fs/promises";import { basename, dirname, extname, join, resolve } from "node:path";import type { ISdk } from "iii-sdk";import type { MemoryProvider } from "../types.js";@@ -148,7 +149,23 @@ export function registerCompressFileFunction(const backupPath = resolveBackupPath(absolutePath);await writeFile(backupPath, original, "utf-8");- await writeFile(absolutePath, compressed, "utf-8");++ let fd: Awaited<ReturnType<typeof open>> | null = null;+ try {+ fd = await open(+ absolutePath,+ constants.O_WRONLY | constants.O_CREAT | constants.O_TRUNC | constants.O_NOFOLLOW,+ );+ await fd.writeFile(compressed, "utf-8");+ } catch (err: unknown) {+ const code = (err as NodeJS.ErrnoException).code;+ if (code === "ELOOP" || code === "EINVAL") {+ return { success: false, error: "symlinks are not supported" };+ }+ return { success: false, error: "failed to write compressed file" };+ } finally {+ await fd?.close().catch(() => {});+ }try {await recordAudit(kv, "compress", "mem::compress-file", [], {diff --git a/test/compress-file.test.ts b/test/compress-file.test.tsindex f3a5142a..9b6820b3 100644--- a/test/compress-file.test.ts+++ b/test/compress-file.test.ts@@ -6,11 +6,33 @@ vi.mock("../src/logger.js", () => ({const fileStore = new Map<string, string>();const symlinkPaths = new Set<string>();+const openEloopPaths = new Set<string>();vi.mock("node:fs/promises", () => ({- lstat: vi.fn(async (path: string) => ({- isSymbolicLink: () => symlinkPaths.has(path),- })),+ lstat: vi.fn(async (path: string) => {+ if (symlinkPaths.has(path)) {+ return { isSymbolicLink: () => true };+ }+ if (!fileStore.has(path)) {+ throw Object.assign(new Error(`ENOENT: no such file or directory, lstat '${path}'`), {+ code: "ENOENT",+ });+ }+ return { isSymbolicLink: () => false };+ }),+ open: vi.fn(async (path: string) => {+ if (openEloopPaths.has(path)) {+ throw Object.assign(new Error("ELOOP: too many levels of symbolic links"), {+ code: "ELOOP",+ });+ }+ return {+ writeFile: vi.fn(async (content: string) => {+ fileStore.set(path, content);+ }),+ close: vi.fn(async () => {}),+ };+ }),readFile: vi.fn(async (path: string) => {const value = fileStore.get(path);if (value === undefined) throw new Error("ENOENT");@@ -73,6 +95,7 @@ describe("mem::compress-file", () => {beforeEach(() => {fileStore.clear();symlinkPaths.clear();+ openEloopPaths.clear();sdk = mockSdk();kv = mockKV();summarize = vi.fn();@@ -90,6 +113,26 @@ describe("mem::compress-file", () => {})) as { success: boolean; error: string };expect(result.success).toBe(false);expect(result.error).toContain("symlink");+ expect(summarize).not.toHaveBeenCalled();+ expect(fileStore.size).toBe(0);+ });++ it("rejects TOCTOU symlink swap at write time via O_NOFOLLOW", async () => {+ const path = "/tmp/notes.md";+ fileStore.set(+ path,+ "# Title\n\nVisit https://example.com\n\n```ts\nconst x = 1;\n```\n\nContent.",+ );+ summarize.mockResolvedValue(+ "# Title\n\nVisit https://example.com\n\n```ts\nconst x = 1;\n```\n\nShort.",+ );+ openEloopPaths.add(path);++ const result = (await sdk.trigger("mem::compress-file", {+ filePath: path,+ })) as { success: boolean; error: string };+ expect(result.success).toBe(false);+ expect(result.error).toContain("symlink");});it("rejects non-markdown paths", async () => {@@ -100,6 +143,14 @@ describe("mem::compress-file", () => {expect(result.error).toContain(".md");});+ it("returns file not found for missing paths", async () => {+ const result = (await sdk.trigger("mem::compress-file", {+ filePath: "/tmp/nonexistent.md",+ })) as { success: boolean; error: string };+ expect(result.success).toBe(false);+ expect(result.error).toContain("not found");+ });+it("compresses markdown and writes .original.md backup", async () => {const path = "/tmp/notes.md";fileStore.set(