Skip to content

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

Keep malformed rels, bound every inflate and block unpack links

Fei24111b3fda7e92f6
diff --git a/ds-bot/office.js b/ds-bot/office.js
index b4a195d0..6f21a418 100644
--- a/ds-bot/office.js
+++ b/ds-bot/office.js
@@ -6519,7 +6519,7 @@ import { join as join2 } from "node:path";
import { parseArgs } from "node:util";
// src/office/package.js
-import { existsSync, mkdirSync, readFileSync, readdirSync, rmdirSync, statSync, unlinkSync, writeFileSync } from "node:fs";
+import { existsSync, lstatSync, mkdirSync, readFileSync, readdirSync, realpathSync, rmdirSync, statSync, unlinkSync, writeFileSync } from "node:fs";
import { dirname, join, posix, resolve, sep } from "node:path";
// src/office/xml.js
@@ -6686,7 +6686,7 @@ function readZip(buf) {
if (method === 0) data = raw.subarray(0, usize);
else if (method === 8) {
try {
- data = usize === 0 ? Buffer.alloc(0) : inflateRawSync(raw, { maxOutputLength: usize });
+ data = inflateRawSync(raw, { maxOutputLength: Math.max(usize, 1) });
} catch (err) {
if (err instanceof RangeError || err?.code === "ERR_OUT_OF_RANGE") fail(`${name} inflates past its declared size`);
fail(`corrupt data for ${name}`);
@@ -6894,22 +6894,29 @@ var Package = class _Package {
relsName(owner) {
return owner === "" ? "_rels/.rels" : `${posix.dirname(owner)}/_rels/${posix.basename(owner)}.rels`;
}
- /** Write .rels and [Content_Types].xml back into parts from the model — only
- * when the model changed, so untouched members keep their original bytes. */
+ /** Write .rels and [Content_Types].xml back into parts from the model — a
+ * .rels part changes only when its owner part is gone or its map's signature
+ * differs from the load baseline, so malformed or empty-but-valid .rels keep
+ * their bytes instead of being dropped. */
flush() {
- for (const name of [...this.parts.keys()]) if (name.endsWith(".rels") && !this.#ownerStillHasRels(name)) this.parts.delete(name);
+ for (const name of [...this.parts.keys()]) {
+ if (!name.endsWith(".rels")) continue;
+ const owner = this.#relsOwner(name);
+ if (owner !== "" && !this.parts.has(owner)) this.parts.delete(name);
+ }
for (const [owner, map] of this.rels) {
if (owner !== "" && !this.parts.has(owner)) {
this.rels.delete(owner);
continue;
}
const name = this.relsName(owner);
+ const sig = relSig(map);
+ const base = (this.relSigs ??= /* @__PURE__ */ new Map()).get(owner);
if (map.size === 0) {
- this.parts.delete(name);
+ if (base !== void 0 && base !== sig) this.parts.delete(name);
continue;
}
- const sig = relSig(map);
- if (sig === (this.relSigs ??= /* @__PURE__ */ new Map()).get(owner) && this.parts.has(name)) continue;
+ if (sig === base && this.parts.has(name)) continue;
const doc = parseXml('<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"/>');
for (const rel of map.values()) {
const el = doc.createElement("Relationship");
@@ -6943,9 +6950,8 @@ var Package = class _Package {
}
this.parts.delete("");
}
- #ownerStillHasRels(name) {
- const owner = name === "_rels/.rels" ? "" : posix.dirname(posix.dirname(name)) + "/" + posix.basename(name, ".rels");
- return this.rels.get(owner)?.size > 0;
+ #relsOwner(name) {
+ return name === "_rels/.rels" ? "" : posix.dirname(posix.dirname(name)) + "/" + posix.basename(name, ".rels");
}
/** @returns {Buffer} the package as a ZIP */
save() {
@@ -6963,12 +6969,16 @@ var Package = class _Package {
}
mkdirSync(dir, { recursive: true });
const root = resolve(dir);
+ const rootReal = realpathSync(dir);
const keep = /* @__PURE__ */ new Set();
for (const [name] of this.parts) {
const file = join(dir, name);
if (!resolve(file).startsWith(root + sep) && resolve(file) !== root) fail2(`unpack: unsafe member name ${name}`);
keep.add(file);
mkdirSync(dirname(file), { recursive: true });
+ const parentReal = realpathSync(dirname(file));
+ if (parentReal !== rootReal && !parentReal.startsWith(rootReal + sep)) fail2(`unpack: ${name} resolves outside ${dir}`);
+ if (existsSync(file) && lstatSync(file).isSymbolicLink()) fail2(`unpack: ${name} is a link`);
const data = this.get(name);
if (name.endsWith(".xml") || name.endsWith(".rels")) {
try {
@@ -6983,7 +6993,9 @@ var Package = class _Package {
for (const entry of readdirSync(join(dir, rel))) {
const path = rel === "" ? entry : `${rel}/${entry}`;
const full = join(dir, path);
- if (statSync(full).isDirectory()) {
+ const st = lstatSync(full);
+ if (st.isSymbolicLink()) fail2(`unpack: link inside ${dir}`);
+ if (st.isDirectory()) {
sweep(path);
if (readdirSync(full).length === 0) rmdirSync(full);
} else if (!keep.has(full)) unlinkSync(full);
@@ -8408,7 +8420,7 @@ var rowOf = (ref) => Number(/(\d+)$/.exec(ref)[1]);
var colNum = (col) => [...col].reduce((n, c) => n * 26 + c.charCodeAt(0) - 64, 0);
function dateEpoch(pkg) {
const wb = pkg.parts.has("xl/workbook.xml") ? pkg.xml("xl/workbook.xml") : void 0;
- return firstOf(wb?.documentElement, "workbookPr")?.getAttribute("date1904") === "1" ? 24107 : 25569;
+ return /^(1|true)$/i.test(firstOf(wb?.documentElement, "workbookPr")?.getAttribute("date1904") ?? "") ? 24107 : 25569;
}
function cellValue(c, shared, fmt, epoch = 25569) {
const t = c.getAttribute("t"), v = firstOf(c, "v"), f = firstOf(c, "f");
diff --git a/ds-bot/src/office/package.js b/ds-bot/src/office/package.js
index 94296d17..546e2399 100644
--- a/ds-bot/src/office/package.js
+++ b/ds-bot/src/office/package.js
@@ -1,7 +1,7 @@
// The OOXML package: parts, [Content_Types].xml, and .rels files as a small model.
// Loads from a .docx/.pptx/.xlsx ZIP or from an `unpack` directory; saves back
// keeping untouched members' compressed bytes.
-import { existsSync, mkdirSync, readFileSync, readdirSync, rmdirSync, statSync, unlinkSync, writeFileSync } from 'node:fs'
+import { existsSync, lstatSync, mkdirSync, readFileSync, readdirSync, realpathSync, rmdirSync, statSync, unlinkSync, writeFileSync } from 'node:fs'
import { dirname, join, posix, resolve, sep } from 'node:path'
import { parseXml, serializeXml, childrenOf } from './xml.js'
import { readZip, writeZip } from './zip.js'
@@ -126,16 +126,29 @@ export class Package {
return owner === '' ? '_rels/.rels' : `${posix.dirname(owner)}/_rels/${posix.basename(owner)}.rels`
}
- /** Write .rels and [Content_Types].xml back into parts from the model — only
- * when the model changed, so untouched members keep their original bytes. */
+ /** Write .rels and [Content_Types].xml back into parts from the model — a
+ * .rels part changes only when its owner part is gone or its map's signature
+ * differs from the load baseline, so malformed or empty-but-valid .rels keep
+ * their bytes instead of being dropped. */
flush() {
- for (const name of [...this.parts.keys()]) if (name.endsWith('.rels') && !this.#ownerStillHasRels(name)) this.parts.delete(name)
+ for (const name of [...this.parts.keys()]) {
+ if (!name.endsWith('.rels')) continue
+ const owner = this.#relsOwner(name)
+ if (owner !== '' && !this.parts.has(owner)) this.parts.delete(name)
+ }
for (const [owner, map] of this.rels) {
if (owner !== '' && !this.parts.has(owner)) { this.rels.delete(owner); continue }
const name = this.relsName(owner)
- if (map.size === 0) { this.parts.delete(name); continue }
const sig = relSig(map)
- if (sig === (this.relSigs ??= new Map()).get(owner) && this.parts.has(name)) continue
+ const base = (this.relSigs ??= new Map()).get(owner)
+ if (map.size === 0) {
+ // The model emptied a previously non-empty map → drop the part; a map
+ // that was empty at load (malformed, or a real empty Relationships)
+ // leaves the member's bytes alone.
+ if (base !== undefined && base !== sig) this.parts.delete(name)
+ continue
+ }
+ if (sig === base && this.parts.has(name)) continue
const doc = parseXml('<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"/>')
for (const rel of map.values()) {
const el = doc.createElement('Relationship')
@@ -162,9 +175,8 @@ export class Package {
this.parts.delete('') // never emit a directory entry
}
- #ownerStillHasRels(name) {
- const owner = name === '_rels/.rels' ? '' : posix.dirname(posix.dirname(name)) + '/' + posix.basename(name, '.rels')
- return this.rels.get(owner)?.size > 0
+ #relsOwner(name) {
+ return name === '_rels/.rels' ? '' : posix.dirname(posix.dirname(name)) + '/' + posix.basename(name, '.rels')
}
/** @returns {Buffer} the package as a ZIP */
@@ -184,12 +196,17 @@ export class Package {
}
mkdirSync(dir, { recursive: true })
const root = resolve(dir)
+ const rootReal = realpathSync(dir)
const keep = new Set()
for (const [name] of this.parts) {
const file = join(dir, name)
if (!resolve(file).startsWith(root + sep) && resolve(file) !== root) fail(`unpack: unsafe member name ${name}`)
keep.add(file)
mkdirSync(dirname(file), { recursive: true })
+ // A linked subdirectory walks the write outside the destination.
+ const parentReal = realpathSync(dirname(file))
+ if (parentReal !== rootReal && !parentReal.startsWith(rootReal + sep)) fail(`unpack: ${name} resolves outside ${dir}`)
+ if (existsSync(file) && lstatSync(file).isSymbolicLink()) fail(`unpack: ${name} is a link`)
const data = this.get(name)
if (name.endsWith('.xml') || name.endsWith('.rels')) {
try { writeFileSync(file, prettyForUnpack(data)); continue } catch { /* fall back to raw */ }
@@ -200,7 +217,9 @@ export class Package {
for (const entry of readdirSync(join(dir, rel))) {
const path = rel === '' ? entry : `${rel}/${entry}`
const full = join(dir, path)
- if (statSync(full).isDirectory()) { sweep(path); if (readdirSync(full).length === 0) rmdirSync(full) }
+ const st = lstatSync(full) // no link following: a link inside the tree is refused
+ if (st.isSymbolicLink()) fail(`unpack: link inside ${dir}`)
+ if (st.isDirectory()) { sweep(path); if (readdirSync(full).length === 0) rmdirSync(full) }
else if (!keep.has(full)) unlinkSync(full)
}
}
diff --git a/ds-bot/src/office/xlsx.js b/ds-bot/src/office/xlsx.js
index ddd4463c..a79af48d 100644
--- a/ds-bot/src/office/xlsx.js
+++ b/ds-bot/src/office/xlsx.js
@@ -42,7 +42,7 @@ const colNum = (col) => [...col].reduce((n, c) => n * 26 + c.charCodeAt(0) - 64,
// the workbook declares <workbookPr date1904="1"/>.
function dateEpoch(pkg) {
const wb = pkg.parts.has('xl/workbook.xml') ? pkg.xml('xl/workbook.xml') : undefined
- return firstOf(wb?.documentElement, 'workbookPr')?.getAttribute('date1904') === '1' ? 24107 : 25569
+ return /^(1|true)$/i.test(firstOf(wb?.documentElement, 'workbookPr')?.getAttribute('date1904') ?? '') ? 24107 : 25569
}
function cellValue(c, shared, fmt, epoch = 25569) {
diff --git a/ds-bot/src/office/zip.js b/ds-bot/src/office/zip.js
index c6fa6317..64a86cd8 100644
--- a/ds-bot/src/office/zip.js
+++ b/ds-bot/src/office/zip.js
@@ -51,9 +51,10 @@ export function readZip(buf) {
if (method === 0) data = raw.subarray(0, usize)
else if (method === 8) {
// Enforce the declared uncompressed size — a zip bomb dies here.
- // inflateRawSync rejects a zero maxOutputLength, so empty members skip it.
+ // inflateRawSync rejects a zero maxOutputLength, so floor it at 1; a
+ // member lying about an empty body then fails on the size check.
try {
- data = usize === 0 ? Buffer.alloc(0) : inflateRawSync(raw, { maxOutputLength: usize })
+ data = inflateRawSync(raw, { maxOutputLength: Math.max(usize, 1) })
} catch (err) {
if (err instanceof RangeError || err?.code === 'ERR_OUT_OF_RANGE') fail(`${name} inflates past its declared size`)
fail(`corrupt data for ${name}`)
diff --git a/ds-bot/test/office.test.js b/ds-bot/test/office.test.js
index c642a1d7..eae9cf3f 100644
--- a/ds-bot/test/office.test.js
+++ b/ds-bot/test/office.test.js
@@ -1,7 +1,7 @@
// Tests for the ds-bot Office CLI (src/office → office.js). Fixtures are the
// small packages under fixtures/office generated by dev/office-fixtures.py.
import { execFileSync } from 'node:child_process'
-import { mkdtempSync, readFileSync, writeFileSync, existsSync } from 'node:fs'
+import { mkdtempSync, mkdirSync, readFileSync, rmSync, symlinkSync, writeFileSync, existsSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join, dirname } from 'node:path'
import { fileURLToPath } from 'node:url'
@@ -625,6 +625,81 @@ describe('re-review pass 2', () => {
})
})
+describe('re-review pass 3', () => {
+ test('malformed .rels survives unpack and save byte-identical, check still reports it', () => {
+ const dir = tmp()
+ const members = readZip(readFileSync(fixture('sample.docx')))
+ const garbage = Buffer.from('<Relationships><broken')
+ for (const m of members) if (m.name === 'word/_rels/document.xml.rels') { delete m.raw; m.data = garbage }
+ const file = join(dir, 'bad.docx')
+ writeFileSync(file, writeZip(members))
+ const pkg = Package.open(file)
+ const out = join(dir, 'un')
+ pkg.unpack(out)
+ assert.equal(readFileSync(join(out, 'word/_rels/document.xml.rels')).toString(), garbage.toString())
+ const saved = readZip(pkg.save()).find(m => m.name === 'word/_rels/document.xml.rels')
+ assert.deepEqual(saved.data, garbage)
+ assert.ok(checkPackage(pkg).length > 0)
+ })
+ test('a valid-but-empty .rels keeps its bytes; a model-emptied map still deletes it', () => {
+ const dir = tmp()
+ const members = readZip(readFileSync(fixture('sample.docx')))
+ const empty = Buffer.from('<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"/>')
+ for (const m of members) if (m.name === 'word/_rels/document.xml.rels') { delete m.raw; m.data = empty }
+ const file = join(dir, 'e.docx')
+ writeFileSync(file, writeZip(members))
+ const pkg = Package.open(file)
+ const out = join(dir, 'un')
+ pkg.unpack(out)
+ assert.equal(readFileSync(join(out, 'word/_rels/document.xml.rels')).toString(), empty.toString())
+ const full = Package.open(fixture('sample.docx'))
+ full.rels.get('word/document.xml').clear()
+ const names = readZip(full.save()).map(m => m.name)
+ assert.ok(!names.includes('word/_rels/document.xml.rels'))
+ })
+ test('xlsx dates honour date1904="true" too', () => {
+ const pkg = Package.open(fixture('sample.xlsx'))
+ const wb = pkg.xml('xl/workbook.xml')
+ const pr = childrenOf(wb.documentElement, 'workbookPr')[0] ?? wb.documentElement.appendChild(wb.createElement('workbookPr'))
+ pr.setAttribute('date1904', 'true')
+ pkg.putXml('xl/workbook.xml', wb)
+ const v = xlsxRead(pkg, 'Sheet1').rows.flatMap(r => Object.values(r.cells).map(c => c.v)).find(v => /^\d{4}-/.test(String(v)))
+ const normal = xlsxRead(Package.open(fixture('sample.xlsx')), 'Sheet1').rows.flatMap(r => Object.values(r.cells).map(c => c.v)).find(v => /^\d{4}-/.test(String(v)))
+ assert.ok(Date.parse(v) > Date.parse(normal))
+ })
+ test('a deflated member declaring 0 bytes but carrying data fails; a real empty member opens', () => {
+ const buf = writeZip([{ name: 'x.xml', data: Buffer.alloc(64, 66) }])
+ buf.writeUInt32LE(0, 22) // lie: declares 0 uncompressed bytes
+ const cdOff = buf.readUInt32LE(buf.length - 22 + 16)
+ buf.writeUInt32LE(0, cdOff + 24)
+ assert.throws(() => readZip(buf))
+ const ok = readZip(writeZip([{ name: 'e.xml', data: Buffer.alloc(0) }]))
+ assert.equal(ok[0].data.length, 0)
+ })
+ test('unpack refuses a linked subdirectory inside the destination', () => {
+ const dir = join(tmp(), 'un')
+ cli('unpack', fixture('sample.docx'), dir)
+ const outside = join(tmp(), 'real')
+ mkdirSync(outside, { recursive: true })
+ writeFileSync(join(outside, 'keep.txt'), 'x')
+ rmSync(join(dir, 'word'), { recursive: true })
+ symlinkSync(outside, join(dir, 'word'), 'junction')
+ assert.throws(() => execFileSync(NODE, [CLI, 'unpack', fixture('sample.docx'), dir, '--json']))
+ assert.ok(!existsSync(join(outside, 'document.xml')))
+ })
+ test('unpack refuses a symlinked file inside the destination', (t) => {
+ const dir = join(tmp(), 'un')
+ cli('unpack', fixture('sample.docx'), dir)
+ const outside = join(tmp(), 'out.xml')
+ writeFileSync(outside, '<x/>')
+ rmSync(join(dir, 'word/document.xml'))
+ try { symlinkSync(outside, join(dir, 'word/document.xml')) }
+ catch (err) { if (err.code === 'EPERM') { t.skip('no symlink privilege'); return } throw err }
+ assert.throws(() => execFileSync(NODE, [CLI, 'unpack', fixture('sample.docx'), dir, '--json']))
+ assert.ok(!readFileSync(outside, 'utf8').includes('w:document'))
+ })
+})
+
describe('JSON findings shape (review 14)', () => {
test('findings come back ok:true with exit 1', () => {
const dir = tmp()