Skip to content

Commit 819ae90

Browse files
cliffhallclaude
andcommitted
fix(memory): refuse a read-only graph file and set the mode before the rename
Follows the cherry-picked fix for #4827. Rather than restoring the mode after the rename with a best-effort chmod, give the temp file the existing graph file's permission bits before the rename, so the new inode never has a looser mode than the file it replaces. Also check the graph file is writable first, so a read-only file fails the save with EACCES as the pre-#4642 fs.writeFile did, instead of being replaced. The characterization test that pinned #4827 now asserts the fixed behavior, and its KNOWN BUG marker is gone; it supersedes the contributed file-permissions test, which is dropped. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 1b01ea6 commit 819ae90

4 files changed

Lines changed: 57 additions & 93 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@modelcontextprotocol/server-memory": patch
3+
---
4+
5+
Saving the knowledge graph keeps the memory file's permission bits (an operator's `0600` no longer comes back `0644`), and a write to a read-only memory file now fails with `EACCES` instead of silently replacing it (#4827).

‎src/memory/__tests__/file-permissions.test.ts‎

Lines changed: 0 additions & 57 deletions
This file was deleted.

‎src/memory/__tests__/persistence.test.ts‎

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -256,11 +256,10 @@ describe("memory persistence over the protocol", () => {
256256
});
257257
});
258258

259-
// KNOWN BUG #4827: pins current (wrong) behavior; the fix changes this assertion.
260-
// Characterizes #4827: saveGraph writes a new temp file and renames it over
261-
// the graph file, so the graph file takes a new file's mode. An operator's
262-
// 0600 is lost, and a read-only file is silently replaced. The fix for #4827
263-
// changes this test. POSIX only: Windows has no mode bits to lose.
259+
// #4827: saveGraph writes a new temp file and renames it over the graph
260+
// file, which replaces the file's inode. The save keeps the graph file's
261+
// own mode rather than a new file's, and refuses a read-only graph file as
262+
// a plain write would. POSIX only: Windows has no mode bits to lose.
264263
describe.skipIf(process.platform === "win32")("file mode (#4827)", () => {
265264
// The mode a file created now gets, which is what the temp file gets.
266265
async function newFileMode(): Promise<number> {
@@ -275,37 +274,43 @@ describe("memory persistence over the protocol", () => {
275274
return (await fs.stat(file)).mode & 0o777;
276275
}
277276

278-
it("replaces a hardened graph file's mode with a new file's mode", async () => {
277+
it("keeps a hardened graph file's mode across a save", async () => {
279278
const { client } = await open();
280279
await call(client, "create_entities", { entities: [alice] });
281280
// 0600 as in the issue, unless the umask already makes new files 0600;
282-
// the original mode must differ from a new file's for the loss to show.
283-
const expected = await newFileMode();
284-
const original = expected === 0o600 ? 0o640 : 0o600;
281+
// the original mode must differ from a new file's for a loss to show.
282+
const original = (await newFileMode()) === 0o600 ? 0o640 : 0o600;
285283
await fs.chmod(filePath, original);
286284

287285
await call(client, "create_entities", { entities: [bob] });
288286

289-
expect(await modeOf(filePath)).toBe(expected);
290-
});
291-
292-
it("silently overwrites a read-only graph file", async () => {
293-
const { client } = await open();
294-
await call(client, "create_entities", { entities: [alice] });
295-
await fs.chmod(filePath, 0o444);
296-
const expected = await newFileMode();
297-
298-
const result = await call(client, "create_entities", {
299-
entities: [bob],
300-
});
301-
302-
expect(result.isError).toBeUndefined();
303-
expect(await modeOf(filePath)).toBe(expected);
287+
expect(await modeOf(filePath)).toBe(original);
304288
expect((await call(client, "read_graph")).structuredContent).toEqual({
305289
entities: [alice, bob],
306290
relations: [],
307291
});
308292
});
293+
294+
// root may write a read-only file, so the refusal shows only without it.
295+
it.skipIf(process.getuid?.() === 0)(
296+
"refuses to overwrite a read-only graph file",
297+
async () => {
298+
const { client } = await open();
299+
await call(client, "create_entities", { entities: [alice] });
300+
await fs.chmod(filePath, 0o444);
301+
const before = await readFileText(filePath);
302+
303+
const result = await call(client, "create_entities", {
304+
entities: [bob],
305+
});
306+
307+
expect(result.isError).toBe(true);
308+
expect(textOf(result)).toMatch(/EACCES/);
309+
expect(await modeOf(filePath)).toBe(0o444);
310+
expect(await readFileText(filePath)).toBe(before);
311+
expect(await fs.readdir(dir)).toEqual(["memory.jsonl"]);
312+
},
313+
);
309314
});
310315

311316
// KNOWN BUG #4797: pins current (wrong) behavior; the fix changes this assertion.

‎src/memory/index.ts‎

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,26 @@ export class KnowledgeGraphManager {
216216
// The temp file is kept in the same directory so the rename stays on one
217217
// filesystem — renaming across mount points fails with EXDEV.
218218
//
219-
// The rename also replaces the target's inode, and the temp file is
220-
// created under the process umask, so a memory file an operator narrowed
221-
// with chmod would come back 0644. Capture the existing bits and restore
222-
// them after the rename, as src/filesystem/lib.ts does for the same
223-
// write pattern. A memory file that does not exist yet has nothing to
224-
// preserve.
225-
const existingStats = await fs.stat(this.memoryFilePath).catch(() => undefined);
219+
// The rename also replaces the graph file's inode, and a new file takes
220+
// the process umask, so the file's own mode would be lost (#4827): an
221+
// operator's 0600 would come back 0644, and a read-only file would be
222+
// silently replaced, since rename(2) needs only a writable directory.
223+
// So refuse a graph file this process may not write, as fs.writeFile
224+
// would (EACCES), and give the temp file the existing file's permission
225+
// bits before the rename, as src/filesystem/lib.ts preserves them for
226+
// the same write pattern. A graph file that does not exist yet has
227+
// nothing to preserve.
228+
let mode: number | undefined;
229+
try {
230+
await fs.access(this.memoryFilePath, fs.constants.W_OK);
231+
mode = (await fs.stat(this.memoryFilePath)).mode & 0o777;
232+
} catch (error) {
233+
if (
234+
!(error instanceof Error && "code" in error && error.code === "ENOENT")
235+
) {
236+
throw error;
237+
}
238+
}
226239

227240
const directory = path.dirname(this.memoryFilePath);
228241
const tempFilePath = path.join(
@@ -232,17 +245,15 @@ export class KnowledgeGraphManager {
232245

233246
try {
234247
await fs.writeFile(tempFilePath, lines.join("\n") + "\n");
248+
if (mode !== undefined) {
249+
await fs.chmod(tempFilePath, mode);
250+
}
235251
await fs.rename(tempFilePath, this.memoryFilePath);
236252
} catch (error) {
237253
// Never leave a stray temp file behind on failure.
238254
await fs.unlink(tempFilePath).catch(() => {});
239255
throw error;
240256
}
241-
242-
if (existingStats) {
243-
// The data is already durable, so a failed chmod must not fail the save.
244-
await fs.chmod(this.memoryFilePath, existingStats.mode & 0o777).catch(() => {});
245-
}
246257
}
247258

248259
async createEntities(entities: Entity[]): Promise<Entity[]> {

0 commit comments

Comments
 (0)