From 80d69c6579df34fef69100e3509c7266599940c2 Mon Sep 17 00:00:00 2001 From: dhivadhiva Date: Tue, 17 Nov 2026 03:09:28 +0530 Subject: [PATCH] Make an existing activity log owner-only on the first write The activity log was restricted to its owner only when the app created it or rotated it. A log left with the default mode by an earlier build stayed readable by the group and others until it grew large enough to rotate. The first write of each run now restricts an existing log too. The folder was already owner-only, so nobody else could reach the file. --- docs/changes/README.md | 1 + docs/changes/bridge-audit-mode.md | 2 ++ lib/services/bridge/bridge_audit.dart | 9 +++++++++ test/bridge_audit_test.dart | 18 ++++++++++++++++++ 4 files changed, 30 insertions(+) create mode 100644 docs/changes/bridge-audit-mode.md diff --git a/docs/changes/README.md b/docs/changes/README.md index cab8465..8e1adab 100644 --- a/docs/changes/README.md +++ b/docs/changes/README.md @@ -29,6 +29,7 @@ One file per piece of work, named for it: | `mosh.md` | Mosh | | `localization.md` | Translations | | `bridge-stop-status.md` | AI Bridge: a stopped command's exit status | +| `bridge-audit-mode.md` | AI Bridge: an existing activity log is made owner-only | Write for the people using the app, in the sections `CHANGELOG.md` uses: `### Added`, `### Changed`, `### Fixed`, `### Security`. One line per change, diff --git a/docs/changes/bridge-audit-mode.md b/docs/changes/bridge-audit-mode.md new file mode 100644 index 0000000..d1f2448 --- /dev/null +++ b/docs/changes/bridge-audit-mode.md @@ -0,0 +1,2 @@ +### Security +- An AI Bridge activity log file that an earlier build left readable by other accounts is now restricted to your user account the first time the app writes to it. Its folder was already restricted, so the file could not be reached. diff --git a/lib/services/bridge/bridge_audit.dart b/lib/services/bridge/bridge_audit.dart index f3dec41..1a2f7d8 100644 --- a/lib/services/bridge/bridge_audit.dart +++ b/lib/services/bridge/bridge_audit.dart @@ -183,6 +183,10 @@ class BridgeAuditLog { /// Where the log lives; null means `/bridge`, found on first use. final Directory? directory; + + /// Whether this run has already made an existing log owner-only. + bool _existingChecked = false; + Directory? _resolved; static const int defaultRotateBytes = 512 * 1024; @@ -301,7 +305,12 @@ class BridgeAuditLog { // spawned on every append. await file.create(); await _ownerOnly(file.path); + } else if (!_existingChecked) { + // A log an earlier build left with the default mode is tightened the + // first time this run writes to it, not only when it is next rotated. + await _ownerOnly(file.path); } + _existingChecked = true; final handle = await file.open(mode: FileMode.append); try { diff --git a/test/bridge_audit_test.dart b/test/bridge_audit_test.dart index 98d38cf..2c287db 100644 --- a/test/bridge_audit_test.dart +++ b/test/bridge_audit_test.dart @@ -504,4 +504,22 @@ void main() { }, skip: Platform.isWindows, ); + + test( + 'a log an earlier build left readable is made owner-only on the first ' + 'write, without waiting for a rotation', + () async { + await recordOne(makeLog()); + await Process.run('chmod', ['664', logFile.path]); + expect(logFile.statSync().mode & 0x1ff, 0x1b4); // 0664 + + // A new run of the app: a fresh log object over the same file. + final log = makeLog(); + await recordOne(log); + expect(logFile.statSync().mode & 0x1ff, 0x180); // 0600 + expect(File('${logFile.path}.1').existsSync(), isFalse); + expect(await log.recent(), hasLength(2)); + }, + skip: Platform.isWindows, + ); }