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, + ); }