From f76eb8fabce2bcdfb727cb2b2fdf6636c39e92ed Mon Sep 17 00:00:00 2001 From: Guoliang Chen Date: Wed, 16 Sep 2026 03:54:57 -0700 Subject: [PATCH] app-engine: settings.json is 0600 whatever the old file's mode was MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit QA finding 1. `FileManager.replaceItemAt` copies the *original* item's metadata onto the replacement unless `.usingNewMetadataOnly` is passed, so a `settings.json` that arrived from another machine (README D3: copying the file is a supported way to share the key and the matrix) kept its permissive mode through every later `SettingsStore.update()` — with `SecurityKey` in the clear. Pass the option, and set the mode on the final path afterwards so the guarantee does not depend on which branch wrote the file. Co-Authored-By: Claude Opus 5 (1M context) --- Sources/MWBCore/Settings.swift | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/Sources/MWBCore/Settings.swift b/Sources/MWBCore/Settings.swift index 93a8c62..096ad47 100644 --- a/Sources/MWBCore/Settings.swift +++ b/Sources/MWBCore/Settings.swift @@ -434,11 +434,27 @@ public struct Settings: Sendable, Equatable, Codable { let temp = directory.appendingPathComponent(".\(url.lastPathComponent).new") try data.write(to: temp, options: .atomic) try FileManager.default.setAttributes([.posixPermissions: 0o600], ofItemAtPath: temp.path) + let written: URL if FileManager.default.fileExists(atPath: url.path) { - _ = try FileManager.default.replaceItemAt(url, withItemAt: temp) + // `.usingNewMetadataOnly`: without it `replaceItemAt` copies the + // *original* file's metadata — permissions included — onto the + // replacement, so a settings file that arrived from somewhere else + // (README D3 makes copying `settings.json` between machines a + // supported way to share the key and the matrix) would keep its + // permissive mode forever, and this file holds `SecurityKey` in the + // clear. + written = + try FileManager.default.replaceItemAt( + url, withItemAt: temp, backupItemName: nil, options: .usingNewMetadataOnly) + ?? url } else { try FileManager.default.moveItem(at: temp, to: url) + written = url } + // Belt and braces: whichever path got us here, the file on disk is 0600 + // before this function returns, regardless of what was there before. + try FileManager.default.setAttributes( + [.posixPermissions: 0o600], ofItemAtPath: written.path) } // MARK: - Typed access to the raw tree