app-engine: settings.json is 0600 whatever the old file's mode was

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) <[email protected]>
This commit is contained in:
lucioandClaude Opus 5 committed 2026-09-16 03:54:57 -07:00
1 parent bd7be40186
commit f76eb8fabc
1 file changed
+17 -1
+17 -1
View File
@@ -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