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