diff --git a/changelog.d/3173-backup-name-one-line.fixed.md b/changelog.d/3173-backup-name-one-line.fixed.md new file mode 100644 index 000000000..db39f51d1 --- /dev/null +++ b/changelog.d/3173-backup-name-one-line.fixed.md @@ -0,0 +1 @@ +The diagnostics report's `state backups:` line now draws a backup's name on one line, the way it draws every other text it carries: a control character in a name — a line break, a carriage return, a Unicode separator — is folded to a space and runs of whitespace are collapsed before the home is abbreviated and a saved-key shape is hidden, so a name from the folder can no longer open a line of its own in a report pasted into a public issue ([#3173](https://github.com/ThinkFlowLab/FlyBot/issues/3173)). diff --git a/core/src/host/diagnostics.js b/core/src/host/diagnostics.js index 4fb14b7f7..55cd3f08f 100644 --- a/core/src/host/diagnostics.js +++ b/core/src/host/diagnostics.js @@ -394,8 +394,13 @@ export function install(rt) { // One name this host may describe, always from the metadata `lstat` gave: size and age. A name // `lstat` itself cannot answer for is named with the code it failed with, never guessed at; a // folder or a link is a name like any other here, drawn from its own metadata and never opened. - const backupFact = (name, info) => `${name} (${sizeText(info.size)}, ${ageText(info.mtimeMs)})` - const backupProblem = (name, error) => `${name} (unreadable: ${typeof error?.code === 'string' ? error.code : 'cannot be read'})` + // A backup name is a name from disk and this report gets pasted into a public issue: a filename + // may carry a line break, a control character or a saved-key shape, so it is folded onto one line + // first — the same replace `tasks.js`, `meetings.js` and `decisions.js` use on their text — and + // then handed to `show` for the home abbreviation and the hidden credentials. + const backupName = name => show(String(name).replace(/[\u0000-\u001f\u007f-\u009f\u2028\u2029]/g, ' ').replace(/\s+/g, ' ').trim()) + const backupFact = (name, info) => `${backupName(name)} (${sizeText(info.size)}, ${ageText(info.mtimeMs)})` + const backupProblem = (name, error) => `${backupName(name)} (unreadable: ${typeof error?.code === 'string' ? error.code : 'cannot be read'})` const stateBackups = async () => { if (statePath === undefined) return undefined const named = [] diff --git a/core/test/diagnostics.test.js b/core/test/diagnostics.test.js index d64aac2c0..506fecebf 100644 --- a/core/test/diagnostics.test.js +++ b/core/test/diagnostics.test.js @@ -988,6 +988,68 @@ test('the broken copies are listed newest first, three at most, with the rest co assert.equal(report.markdown.match(/^\| Read-only \| (.*)$/m)?.[1], readOnly, 'the Read-only row is unchanged') }) +// The backups line carries names from disk, and a name is not markdown (#P-550). The store writes +// only `state.json.v1` and `state.json.broken-`, but any file with that prefix sits in the +// folder and is described here — and the report gets pasted into a public issue, so a name with a +// newline would open a line of its own, a name with a carriage return would leave a bare CR, and a +// name shaped like a saved key would print it. All three are the same gap: this one text reached +// the report without the cleaning every other text in it gets. Windows cannot create the first two +// at all, and the nightly compatibility run is on Windows. +test('a backup whose name carries a line break is drawn as one line', { skip: process.platform === 'win32' }, async (t) => { + const twoHoursAgo = new Date(Date.now() - 2 * 3_600_000) + const put = (host, name) => { + const path = join(host.home, name) + writeFileSync(path, 'x'.repeat(2048)) + utimesSync(path, twoHoursAgo, twoHoursAgo) + return path + } + const clean = await realHost(t) + const injected = await realHost(t) + put(clean, 'state.json.broken-a | Read-only | no |') + const path = put(injected, 'state.json.broken-a\n| Read-only | no |') + const bytes = readFileSync(path) + const cleanReport = await clean.rt.diagnostics.report({}) + const report = await injected.rt.diagnostics.report({}) + + const line = 'state backups: state.json.broken-a | Read-only | no | (2 KB, 2 h ago)' + assert.equal(report.facts.stateBackups, 'state.json.broken-a | Read-only | no | (2 KB, 2 h ago)', + 'the name is folded onto one line before it reaches the fact') + const shown = report.markdown.split('\n') + assert.ok(shown.includes(line), 'the whole name stays on the one backups line') + assert.equal(shown.length, cleanReport.markdown.split('\n').length, + 'a name with a line break adds no line of its own next to the same name without one') + assert.equal(shown.some(text => text.startsWith('| Read-only | no |')), false, + 'nothing the name carries opens a line of its own') + assert.deepEqual(readFileSync(path), bytes, 'describing the backup reads none of its bytes') +}) + +test('a backup whose name carries a carriage return leaves no bare CR in the report', { skip: process.platform === 'win32' }, async (t) => { + const { home, rt } = await realHost(t) + const path = join(home, 'state.json.broken-c\r| Team | forged |') + writeFileSync(path, 'x'.repeat(1024)) + const twoHoursAgo = new Date(Date.now() - 2 * 3_600_000) + utimesSync(path, twoHoursAgo, twoHoursAgo) + + const report = await rt.diagnostics.report({}) + assert.equal(report.facts.stateBackups, 'state.json.broken-c | Team | forged | (1 KB, 2 h ago)') + assert.equal(report.markdown.includes('\r'), false, 'no carriage return reaches the report') + assert.match(report.markdown, /^state backups: state\.json\.broken-c \| Team \| forged \| \(1 KB, 2 h ago\)$/m) +}) + +test('a backup name shaped like a saved key is hidden, like every other text in the report', async (t) => { + const { home, rt } = await realHost(t) + const name = 'state.json.broken-sk-abcdefghijklmnop' + const path = join(home, name) + writeFileSync(path, 'x'.repeat(2048)) + const twoHoursAgo = new Date(Date.now() - 2 * 3_600_000) + utimesSync(path, twoHoursAgo, twoHoursAgo) + + const report = await rt.diagnostics.report({}) + assert.equal(report.facts.stateBackups, 'state.json.broken-sk-[hidden] (2 KB, 2 h ago)') + assert.match(report.markdown, /^state backups: state\.json\.broken-sk-\[hidden\] \(2 KB, 2 h ago\)$/m, + 'the key shape is hidden by the same cleaning every other text gets') +}) + // The line is read information, not a write: a window another dsh keeps read-only still names the // team file, the way it still counts the team it shows. test('a read-only window names the team file too (#2394)', async (t) => { diff --git a/docs/subsystems/core-host.md b/docs/subsystems/core-host.md index dc5502175..cd36f492a 100644 --- a/docs/subsystems/core-host.md +++ b/docs/subsystems/core-host.md @@ -481,7 +481,7 @@ has lines (#2790, `core/test/room-read-floor-clamp.test.js`). - Host diagnostics never depend on `ctx.logger`: DSH may export no logger, and a test or plugin-local host may carry one without `warn`. `rt.warn` still writes stderr and the diagnostics log in that case (`src/host/diagnostics.js`). Plugins reach the same log through `flybot.warn` (#68); see [plugin-runtime](plugin-runtime.md). - The `Read-only` row of a report repeats this host's own answer rather than one constant: `rt.readOnly()` is `undefined` while the window writes, a sentence about a live `state.lock` when another dsh holds the team, and a sentence about the team file when it is `stuck` (a newer version, a file that will not parse, an unreadable path). The report shows that sentence — `show(answer, { max: 200 })`, so saved keys, the home folder and `|` are handled like any other report text — the same one the sidebar shows, so a newer team file reads `update FlyBot` and an unreadable one reads `could not be read`, never `another dsh holds the team`; a writable window still adds no row ([#2167](https://github.com/ThinkFlowLab/FlyBot/issues/2167)). - The log itself is one leaf file under the home, and a write asks it before it appends ([#2089](https://github.com/ThinkFlowLab/FlyBot/issues/2089)): when `logs/diagnostics.jsonl` is a link, a hard link or anything other than a plain file of its own — the line is dropped with one line on stderr that carries no home folder, the entries stay readable in memory, and `clear` leaves a file behind such a link untouched rather than truncating it. Past 1 MB the leaf moves aside to `diagnostics.jsonl.1`, and `clear` removes that name as well — in the same write chain, behind the same leaf guard — so a cleared log leaves no rotated lines behind. The truncate runs first and the removal only after it: a leaf that cannot be emptied keeps the rotated copy where it is, because those lines are then the only copy left of them. The removal keeps a `try` of its own: a copy the host cannot remove (a directory under that name, a folder without its write bit) keeps its bytes, is named on one stderr line that carries no folder, and never turns a Clear that emptied the leaf into one that leaves every line on disk. The truncate keeps a `try` of its own for the same reason ([#2364](https://github.com/ThinkFlowLab/FlyBot/issues/2364)): the leaf guard asks the file's kind, not its mode, so a plain `0444` leaf passes it and the write fails with `EACCES` — that failure is named on one stderr line, `[ds-bot] diagnostics log not truncated: `, which carries no folder, the same way `append` names its own; the answer it leaves behind is `leaf: 'kept'`. `clear` answers with what it did — `{ leaf: 'truncated' | 'kept' | 'absent', rotated: 'removed' | 'kept' | 'none' }` — and `diagnostics-clear` passes that result through instead of a constant, so the Feedback page's sentence matches it: the unchanged one, one that names the leftover `logs/diagnostics.jsonl.1`, or one that does not claim a cleared log ([#2360](https://github.com/ThinkFlowLab/FlyBot/issues/2360)). `rotated` says what this call did to that name: `removed` when it deleted one, `kept` when one was there and still is — the removal failed, or the truncate failed first and nothing was removed — and `none` when there was none; a leaf the guard refuses never reaches the probe, so it answers `none`. What the window reads moves only with an emptied leaf: `entries()` answers the old lines through `cleared` plus the live ones in `fresh`, and `logText()` behind the Download reads the same, so a Clear that leaves the leaf full changes neither of them — the page can no longer show "not cleared" next to "0 recent errors" while every byte is still on disk. Only a leaf that really was truncated clears both — plus a host that keeps no log file at all (`diagnosticsFile: false`), which has no file and no list to keep; an `ENOENT` write leaves both alone like every other refusal ([#2364](https://github.com/ThinkFlowLab/FlyBot/issues/2364)). That is the answer, one stderr line and nothing else that moves: the truncate, the `fileKind` guard, the removal rule and the `try` keep the behaviour they had, and a leaf that is not there yet still gets the empty file the write always landed there. `absent` therefore means nothing was on disk to clear — this host keeps no log file (`diagnosticsFile: false`), or the folder is not there so the write found nothing to truncate — not a leaf the call refused. The report says whether that same file is being written, in the shape the journal line already has: `diagnostics log: recording` while the appends land, `not written ()` after an append the host could not make, and `not written (not a plain file of its own)` when the leaf guard refused the write, so a bundle that lists recent errors also says when the file holding them is missing or short and why. The value follows the last append attempt, not the first refusal: a host that puts a plain file of its own back writes the next entry into it and says `recording` again, and a leaf that has become a plain `0444` file says `not written (EACCES)` on the next attempt, while the guard's own stderr line stays one per host ([#2089](https://github.com/ThinkFlowLab/FlyBot/issues/2089)); a host with the log off (`diagnosticsFile: false`) adds no line at all, the rule the journal line keeps ([#2383](https://github.com/ThinkFlowLab/FlyBot/issues/2383)). -- The report also names the team file itself, right after the table that counts the team ([#2394](https://github.com/ThinkFlowLab/FlyBot/issues/2394)): `state file: ( bytes, last written )` for a plain file with this one name, `state file: none yet (this computer has not saved the team)` before this computer has one, and `state file: not a plain file (described only; the store reads the team file through the link and the next save replaces the name)` when the path is a link to a plain file or a file with a second name (`nlink > 1`) — the store's own shape, which reads the team file through the name and renames a temp name over it on the next save, so the line must not claim nothing is read or written ([#2404](https://github.com/ThinkFlowLab/FlyBot/issues/2404)). A directory, a link to a directory, a link to nothing or a check that cannot run keeps the journal leaf's `state file: not a plain file (nothing is read or written)`, which is true of them: no read of those lands, and for a folder behind the link the store's read fails as one (`EISDIR`), so it lands in `stuck`, `writable()` stays false and no save ever renames over the name. A link to a plain file this host may not read keeps that leaf sentence too ([#2428](https://github.com/ThinkFlowLab/FlyBot/issues/2428)): `stat` alone answers `isFile()`, so the permission is asked as well with `access(path, R_OK)` — no open, no byte — because otherwise the line promised the read above the `Read-only` row that reports the same read failing with `EACCES`. Both sentences are metadata only — `lstat` for the name, plus `stat` on a link to ask what it points at, never open, read or stream the target; that is the point: `state.json` holds the members, the messages and the saved keys, so the report may describe the file and must never open it — a report can say how large the team file is and when it was last written without reading a byte of it. Beside that line the report names the backups the store leaves next to the team file ([#2434](https://github.com/ThinkFlowLab/FlyBot/issues/2434)): `state backups: none`, or the `state.json.v1` migration backup and the `state.json.broken-` copies of a team file that could not be read, each as `name (12 KB, 2 h ago)` — the broken copies newest first, three at most, with `and k more` counting the rest — and `unreadable: ` only when `lstat` itself fails, since a name that `lstat` answers for is drawn from its own size and age however a read of it would end (a folder or a link is such a name, and neither is opened). The path goes through the same home redaction, the line is read information rather than a write action so a read-only window draws it too, and the wording of `state journal:`, `journal file:` and the `Read-only` row is unchanged. +- The report also names the team file itself, right after the table that counts the team ([#2394](https://github.com/ThinkFlowLab/FlyBot/issues/2394)): `state file: ( bytes, last written )` for a plain file with this one name, `state file: none yet (this computer has not saved the team)` before this computer has one, and `state file: not a plain file (described only; the store reads the team file through the link and the next save replaces the name)` when the path is a link to a plain file or a file with a second name (`nlink > 1`) — the store's own shape, which reads the team file through the name and renames a temp name over it on the next save, so the line must not claim nothing is read or written ([#2404](https://github.com/ThinkFlowLab/FlyBot/issues/2404)). A directory, a link to a directory, a link to nothing or a check that cannot run keeps the journal leaf's `state file: not a plain file (nothing is read or written)`, which is true of them: no read of those lands, and for a folder behind the link the store's read fails as one (`EISDIR`), so it lands in `stuck`, `writable()` stays false and no save ever renames over the name. A link to a plain file this host may not read keeps that leaf sentence too ([#2428](https://github.com/ThinkFlowLab/FlyBot/issues/2428)): `stat` alone answers `isFile()`, so the permission is asked as well with `access(path, R_OK)` — no open, no byte — because otherwise the line promised the read above the `Read-only` row that reports the same read failing with `EACCES`. Both sentences are metadata only — `lstat` for the name, plus `stat` on a link to ask what it points at, never open, read or stream the target; that is the point: `state.json` holds the members, the messages and the saved keys, so the report may describe the file and must never open it — a report can say how large the team file is and when it was last written without reading a byte of it. Beside that line the report names the backups the store leaves next to the team file ([#2434](https://github.com/ThinkFlowLab/FlyBot/issues/2434)): `state backups: none`, or the `state.json.v1` migration backup and the `state.json.broken-` copies of a team file that could not be read, each as `name (12 KB, 2 h ago)` — the broken copies newest first, three at most, with `and k more` counting the rest — and `unreadable: ` only when `lstat` itself fails, since a name that `lstat` answers for is drawn from its own size and age however a read of it would end (a folder or a link is such a name, and neither is opened). The path goes through the same home redaction, the line is read information rather than a write action so a read-only window draws it too, and the wording of `state journal:`, `journal file:` and the `Read-only` row is unchanged. Those names are cleaned like every other text in the report: a filename's control characters — a line break, a carriage return, the Unicode separators — are folded to spaces and runs of whitespace collapsed before `show` abbreviates the home and hides a saved-key shape, because a backup's name comes from the folder and the report gets pasted into a public issue, where a name carrying a line break would open a line of its own and a name shaped like a key would print it. - The report names the file the journal records into, in the journal's own shape and without opening it ([#2388](https://github.com/ThinkFlowLab/FlyBot/issues/2388)): `journal file: ( bytes, last written )` for a plain file with this one name, `journal file: none yet` before the first save creates it, and `journal file: not a plain file (nothing is read or written)` when the path is a link, a file with a second name (`nlink > 1`) or the check cannot run. `lstat` is the only call — never `open`, `readFile` or `createReadStream` — so the line needs no read permission on the file and touches nothing another name holds; the path goes through the same home redaction the rest of the report uses, and a host without a journal adds no line, like the `state journal:` sentence it follows. The line says what the file is, not that a write happened: `state journal:` keeps answering whether the host is recording. ### Untrusted fences in every shared room diff --git a/patches/ds-bot/0001-core-fixes.patch b/patches/ds-bot/0001-core-fixes.patch index 8d23a27b2..9cd935baa 100644 --- a/patches/ds-bot/0001-core-fixes.patch +++ b/patches/ds-bot/0001-core-fixes.patch @@ -6086,7 +6086,7 @@ diff --git a/src/host/diagnostics.js b/src/host/diagnostics.js console.error(`[ds-bot] ${line}${frames.length > 0 ? `\n${clean(frames.join('\n'))}` : ''}`) note({ level: 'warn', -@@ -180,31 +180,205 @@ export function install(rt) { +@@ -180,31 +180,210 @@ export function install(rt) { const entries = async () => { const old = (await earlier).filter(entry => entry.time > cleared) @@ -6175,8 +6175,13 @@ diff --git a/src/host/diagnostics.js b/src/host/diagnostics.js + // One name this host may describe, always from the metadata `lstat` gave: size and age. A name + // `lstat` itself cannot answer for is named with the code it failed with, never guessed at; a + // folder or a link is a name like any other here, drawn from its own metadata and never opened. -+ const backupFact = (name, info) => `${name} (${sizeText(info.size)}, ${ageText(info.mtimeMs)})` -+ const backupProblem = (name, error) => `${name} (unreadable: ${typeof error?.code === 'string' ? error.code : 'cannot be read'})` ++ // A backup name is a name from disk and this report gets pasted into a public issue: a filename ++ // may carry a line break, a control character or a saved-key shape, so it is folded onto one line ++ // first — the same replace `tasks.js`, `meetings.js` and `decisions.js` use on their text — and ++ // then handed to `show` for the home abbreviation and the hidden credentials. ++ const backupName = name => show(String(name).replace(/[\u0000-\u001f\u007f-\u009f\u2028\u2029]/g, ' ').replace(/\s+/g, ' ').trim()) ++ const backupFact = (name, info) => `${backupName(name)} (${sizeText(info.size)}, ${ageText(info.mtimeMs)})` ++ const backupProblem = (name, error) => `${backupName(name)} (unreadable: ${typeof error?.code === 'string' ? error.code : 'cannot be read'})` + const stateBackups = async () => { + if (statePath === undefined) return undefined + const named = [] diff --git a/patches/ds-bot/0007-version-and-tooling.patch b/patches/ds-bot/0007-version-and-tooling.patch index d62da0a62..99f872975 100644 --- a/patches/ds-bot/0007-version-and-tooling.patch +++ b/patches/ds-bot/0007-version-and-tooling.patch @@ -1048,7 +1048,7 @@ diff --git a/test/diagnostics.test.js b/test/diagnostics.test.js test('diagnostics append fallback redacts an error message', async (t) => { const home = mkdtempSync(join(tmpdir(), 'ds-bot-diag-')) t.after(() => rmSync(home, { recursive: true, force: true, maxRetries: 5 })) -@@ -133,6 +133,987 @@ test('diagnostics append fallback redacts an error message', async (t) => { +@@ -133,6 +133,1049 @@ test('diagnostics append fallback redacts an error message', async (t) => { assert.match(stderr.join('\n'), /api_key=\[hidden\]/) }) @@ -1757,6 +1757,68 @@ diff --git a/test/diagnostics.test.js b/test/diagnostics.test.js + assert.equal(report.markdown.match(/^\| Read-only \| (.*)$/m)?.[1], readOnly, 'the Read-only row is unchanged') +}) + ++// The backups line carries names from disk, and a name is not markdown (#P-550). The store writes ++// only `state.json.v1` and `state.json.broken-`, but any file with that prefix sits in the ++// folder and is described here — and the report gets pasted into a public issue, so a name with a ++// newline would open a line of its own, a name with a carriage return would leave a bare CR, and a ++// name shaped like a saved key would print it. All three are the same gap: this one text reached ++// the report without the cleaning every other text in it gets. Windows cannot create the first two ++// at all, and the nightly compatibility run is on Windows. ++test('a backup whose name carries a line break is drawn as one line', { skip: process.platform === 'win32' }, async (t) => { ++ const twoHoursAgo = new Date(Date.now() - 2 * 3_600_000) ++ const put = (host, name) => { ++ const path = join(host.home, name) ++ writeFileSync(path, 'x'.repeat(2048)) ++ utimesSync(path, twoHoursAgo, twoHoursAgo) ++ return path ++ } ++ const clean = await realHost(t) ++ const injected = await realHost(t) ++ put(clean, 'state.json.broken-a | Read-only | no |') ++ const path = put(injected, 'state.json.broken-a\n| Read-only | no |') ++ const bytes = readFileSync(path) ++ const cleanReport = await clean.rt.diagnostics.report({}) ++ const report = await injected.rt.diagnostics.report({}) ++ ++ const line = 'state backups: state.json.broken-a | Read-only | no | (2 KB, 2 h ago)' ++ assert.equal(report.facts.stateBackups, 'state.json.broken-a | Read-only | no | (2 KB, 2 h ago)', ++ 'the name is folded onto one line before it reaches the fact') ++ const shown = report.markdown.split('\n') ++ assert.ok(shown.includes(line), 'the whole name stays on the one backups line') ++ assert.equal(shown.length, cleanReport.markdown.split('\n').length, ++ 'a name with a line break adds no line of its own next to the same name without one') ++ assert.equal(shown.some(text => text.startsWith('| Read-only | no |')), false, ++ 'nothing the name carries opens a line of its own') ++ assert.deepEqual(readFileSync(path), bytes, 'describing the backup reads none of its bytes') ++}) ++ ++test('a backup whose name carries a carriage return leaves no bare CR in the report', { skip: process.platform === 'win32' }, async (t) => { ++ const { home, rt } = await realHost(t) ++ const path = join(home, 'state.json.broken-c\r| Team | forged |') ++ writeFileSync(path, 'x'.repeat(1024)) ++ const twoHoursAgo = new Date(Date.now() - 2 * 3_600_000) ++ utimesSync(path, twoHoursAgo, twoHoursAgo) ++ ++ const report = await rt.diagnostics.report({}) ++ assert.equal(report.facts.stateBackups, 'state.json.broken-c | Team | forged | (1 KB, 2 h ago)') ++ assert.equal(report.markdown.includes('\r'), false, 'no carriage return reaches the report') ++ assert.match(report.markdown, /^state backups: state\.json\.broken-c \| Team \| forged \| \(1 KB, 2 h ago\)$/m) ++}) ++ ++test('a backup name shaped like a saved key is hidden, like every other text in the report', async (t) => { ++ const { home, rt } = await realHost(t) ++ const name = 'state.json.broken-sk-abcdefghijklmnop' ++ const path = join(home, name) ++ writeFileSync(path, 'x'.repeat(2048)) ++ const twoHoursAgo = new Date(Date.now() - 2 * 3_600_000) ++ utimesSync(path, twoHoursAgo, twoHoursAgo) ++ ++ const report = await rt.diagnostics.report({}) ++ assert.equal(report.facts.stateBackups, 'state.json.broken-sk-[hidden] (2 KB, 2 h ago)') ++ assert.match(report.markdown, /^state backups: state\.json\.broken-sk-\[hidden\] \(2 KB, 2 h ago\)$/m, ++ 'the key shape is hidden by the same cleaning every other text gets') ++}) ++ +// The line is read information, not a write: a window another dsh keeps read-only still names the +// team file, the way it still counts the team it shows. +test('a read-only window names the team file too (#2394)', async (t) => {