Skip to content

Commit 463c402

Browse files
cliffhallclaude
andcommitted
fix(memory): create the temp graph file with the preserved mode
The temp file was created at the process default mode and narrowed by chmod only afterwards, so a 0600 graph was briefly readable at 0644 through it. Pass the preserved mode to writeFile as well (the umask can only narrow it), and keep the chmod to restore any bits the umask removed. Addresses Copilot's review of #5013. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: cliffhall <cliff@futurescale.com>
1 parent 819ae90 commit 463c402

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,16 @@ describe("memory persistence over the protocol", () => {
281281
// the original mode must differ from a new file's for a loss to show.
282282
const original = (await newFileMode()) === 0o600 ? 0o640 : 0o600;
283283
await fs.chmod(filePath, original);
284+
const writeFile = vi.spyOn(fs, "writeFile");
284285

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

288+
// The temp file is created with that mode, never a wider one first.
289+
expect(writeFile).toHaveBeenCalledWith(
290+
expect.stringMatching(/\.tmp$/),
291+
expect.any(String),
292+
{ mode: original },
293+
);
287294
expect(await modeOf(filePath)).toBe(original);
288295
expect((await call(client, "read_graph")).structuredContent).toEqual({
289296
entities: [alice, bob],

‎src/memory/index.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,15 @@ export class KnowledgeGraphManager {
244244
);
245245

246246
try {
247-
await fs.writeFile(tempFilePath, lines.join("\n") + "\n");
247+
// Create the temp file with the existing mode, so the graph is never
248+
// readable through it more widely than through the file it replaces;
249+
// the umask can only narrow that. The chmod then restores any bits the
250+
// umask removed.
251+
await fs.writeFile(
252+
tempFilePath,
253+
lines.join("\n") + "\n",
254+
mode === undefined ? undefined : { mode },
255+
);
248256
if (mode !== undefined) {
249257
await fs.chmod(tempFilePath, mode);
250258
}

0 commit comments

Comments
 (0)