Skip to content

A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.

Reject change-log rows whose file is not a flat name on restore

klarkxy46c051c69981
diff --git a/ds-bot/src/host/memory.js b/ds-bot/src/host/memory.js
index a12cbf49..bc8916ed 100644
--- a/ds-bot/src/host/memory.js
+++ b/ds-bot/src/host/memory.js
@@ -1474,6 +1474,10 @@ export function install(rt) {
// less than a whole line would undo a different change than the one it reads.
const exactLines = (file, line) => file.entries.filter(entry => file.lines[entry.line] === line)
const noMatch = message => Object.assign(new Error(message), { code: NO_MATCH })
+ // A row's file is a flat name — MEMORY.md or a topic file, never a path. A row
+ // is read, not trusted: a name with separators would reach another scope's
+ // folder when it is joined back for a rebuild, inside() or not.
+ const flatFile = name => name === MAIN || (typeof name === 'string' && name.endsWith('.md') && cleanTopic(name) === name.slice(0, -3))
async function memoryRestore(body) {
const line = Number(body.line)
@@ -1490,7 +1494,7 @@ export function install(rt) {
throw new Error(`Line ${line} of the change log is not a change that can be restored.`)
}
const scope = journalScope(row.scope)
- if (scope === undefined || typeof row.file !== 'string' || row.file === '') {
+ if (scope === undefined || !flatFile(row.file)) {
throw new Error(`Line ${line} of the change log is not a change that can be restored.`)
}
if (row.op === 'archive') throw new Error('An archived memory cannot be restored from the change log.')
diff --git a/ds-bot/test/memory.test.js b/ds-bot/test/memory.test.js
index 3d873d44..d436bb3d 100644
--- a/ds-bot/test/memory.test.js
+++ b/ds-bot/test/memory.test.js
@@ -439,6 +439,31 @@ test('a restore of a replace whose after reads like its before is no duplicate',
assert.doesNotMatch(read('bots', 'b2', 'MEMORY.md'), /- drinks tea daily/)
})
+test('a restore refuses a change-log row whose file is not a flat name', async (t) => {
+ const { tool, call, read, journal, stop, home } = start()
+ t.after(() => stop())
+ await tool('remember', { text: 'Team drinks tea', scope: 'team' }, 'm1')
+ await tool('remember', { text: 'Likes quiet mornings' }, 'b2')
+ const journalPath = join(home, 'memory', 'journal.jsonl')
+ // A forged row in the shared log can name any path; it is read, not trusted.
+ const forge = (file) => {
+ const row = { id: `forged-${file}`, time: new Date().toISOString(), op: 'remove', scope: 'bot:b2', file, actor: 'user', actorName: 'user', removedTopic: true, before: '- forged entry' }
+ writeFileSync(journalPath, `${read('journal.jsonl')}${JSON.stringify(row)}\n`)
+ return journal().length
+ }
+ for (const file of ['../../team/MEMORY.md', '..\\..\\team\\MEMORY.md', 'topics/deep.md', '..', '.', 'C:\\out\\x.md']) {
+ const line = forge(file)
+ const denied = await call('memory-restore', { line })
+ assert.equal(denied.ok, false, file)
+ assert.match(denied.error.message, /not a change that can be restored/, file)
+ // The file the row pointed at is untouched, and no restore row was logged.
+ assert.match(read('team', 'MEMORY.md'), /- Team drinks tea \[/)
+ assert.doesNotMatch(read('team', 'MEMORY.md'), /forged/)
+ assert.doesNotMatch(read('bots', 'b2', 'MEMORY.md'), /forged|\.\./)
+ assert.equal(journal().length, line)
+ }
+})
+
test('recall finds a continuous Chinese phrase, and keeps space-separated and Latin matching', async (t) => {
const { tool, stop } = start()
t.after(() => stop())