A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Harden the Office CLI against unsafe zips and review findings
Fei2411a997b521d5df
diff --git a/ds-bot/office.js b/ds-bot/office.jsindex 70625bdf..45fb804e 100644--- a/ds-bot/office.js+++ b/ds-bot/office.js@@ -6520,7 +6520,7 @@ import { parseArgs } from "node:util";// src/office/package.jsimport { existsSync, mkdirSync, readFileSync, readdirSync, rmdirSync, statSync, unlinkSync, writeFileSync } from "node:fs";-import { dirname, join, posix } from "node:path";+import { dirname, join, posix, resolve, sep } from "node:path";// src/office/xml.jsvar xml_exports = {};@@ -6676,14 +6676,22 @@ function readZip(buf) {const localOff = buf.readUInt32LE(cdOff + 42);if (csize === 4294967295 || usize === 4294967295) fail("zip64 archives are not supported");const name = buf.subarray(cdOff + 46, cdOff + 46 + nameLen).toString("utf8");+ const norm = name.replace(/\\/g, "/");+ if (norm.startsWith("/") || /^[A-Za-z]:/.test(norm) || norm.split("/").includes("..")) fail(`zip: unsafe member name ${name}`);if (buf.readUInt32LE(localOff) !== LOCAL) fail(`corrupt local header for ${name}`);const lNameLen = buf.readUInt16LE(localOff + 26);const lExtraLen = buf.readUInt16LE(localOff + 28);const raw = buf.subarray(localOff + 30 + lNameLen + lExtraLen, localOff + 30 + lNameLen + lExtraLen + csize);let data;if (method === 0) data = raw.subarray(0, usize);- else if (method === 8) data = inflateRawSync(raw);- else fail(`unsupported compression method ${method} for ${name}`);+ else if (method === 8) {+ try {+ data = usize === 0 ? Buffer.alloc(0) : inflateRawSync(raw, { maxOutputLength: usize });+ } catch (err) {+ if (err instanceof RangeError || err?.code === "ERR_OUT_OF_RANGE") fail(`${name} inflates past its declared size`);+ fail(`corrupt data for ${name}`);+ }+ } else fail(`unsupported compression method ${method} for ${name}`);if (data.length !== usize) fail(`size mismatch for ${name}`);members.push({ name, data, raw, method, crc, flags: flags & 2048, extra: buf.subarray(cdOff + 46 + nameLen, cdOff + 46 + nameLen + extraLen) });cdOff += 46 + nameLen + extraLen + commentLen;@@ -6946,15 +6954,19 @@ var Package = class _Package {}/** Write an `unpack` directory tree. An existing non-empty dir must hold an* earlier unpack ([Content_Types].xml at its root); files the package no- * longer has are removed. */+ * longer has are removed. The model is flushed first so .rels and content+ * types written to disk match the in-memory state. */unpack(dir) {+ this.flush();if (existsSync(dir) && readdirSync(dir).length > 0 && !existsSync(join(dir, "[Content_Types].xml"))) {fail2(`unpack: ${dir} exists and is not an earlier unpack`);}mkdirSync(dir, { recursive: true });+ const root = resolve(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 data = this.get(name);@@ -7301,6 +7313,7 @@ function fixPackage(pkg) {}}}+ pkg.flush();return { fixed, remaining: checkPackage(pkg) };}function hasIdAnywhere(doc, tag, id) {@@ -7379,7 +7392,7 @@ function docxRead(pkg, { changes = false } = {}) {const body = firstOf(doc.documentElement, "w:body");const blocks = [];let pi = 0, ti = 0;- const commentIds = commentsIndex(pkg);+ const commentIds = commentsIndex(pkg, doc);for (const el of childrenOf(body)) {if (el.tagName === "w:p") {pi++;@@ -7420,10 +7433,9 @@ function docxRead(pkg, { changes = false } = {}) {}return { blocks, sections, notes, comments: commentsList(pkg, changes) };}-function commentsIndex(pkg) {+function commentsIndex(pkg, doc) {const byEl = /* @__PURE__ */ new Map();if (!pkg.parts.has("word/comments.xml")) return byEl;- const doc = pkg.xml("word/document.xml");for (const ref of elementsOf(doc.documentElement, "w:commentReference")) {let p = ref.parentNode;while (p && p.tagName !== "w:p") p = p.parentNode;@@ -7643,6 +7655,16 @@ var nearestPara = (el) => {for (let n = el.parentNode; n; n = n.parentNode) if (n.tagName === "w:p") return n;return null;};+var cloneRun = (doc, src, text) => {+ const clone = src.cloneNode(false);+ const rPr = childrenOf(src, "w:rPr")[0];+ if (rPr) clone.appendChild(rPr.cloneNode(true));+ const t = doc.createElement("w:t");+ t.setAttribute("xml:space", "preserve");+ t.appendChild(doc.createTextNode(text));+ clone.appendChild(t);+ return clone;+};function replaceInDoc(doc, re, withText) {let total = 0;const global = new RegExp(re.source, re.flags.includes("g") ? re.flags : re.flags + "g");@@ -7650,46 +7672,44 @@ function replaceInDoc(doc, re, withText) {const runs = elementsOf(p, "w:r").filter((r) => !isDeleted(p, r) && nearestPara(r) === p);const texts = runs.map((r) => elementsOf(r, "w:t").map((t) => t.textContent).join(""));const joined = texts.join("");- for (const m of [...joined.matchAll(global)].reverse()) {- let cursor = 0, first = -1, last = -1;- for (let i = 0; i < runs.length; i++) {- const end = cursor + texts[i].length;- if (first < 0 && m.index < end) first = i;- if (m.index + m[0].length <= end) {- last = i;- break;- }- cursor = end;- }- if (first < 0) continue;- if (last < 0) last = runs.length - 1;- const startRun = runs[first];- const preStart = texts[first].slice(0, m.index - sumTo(texts, first));- const postEnd = texts[last].slice(m.index + m[0].length - sumTo(texts, last));- const makeRun = (text) => {- const clone = startRun.cloneNode(false);- while (clone.firstChild && clone.firstChild.tagName !== "w:rPr") clone.removeChild(clone.firstChild);- const t = doc.createElement("w:t");- t.setAttribute("xml:space", "preserve");- t.appendChild(doc.createTextNode(text));- clone.appendChild(t);- return clone;- };- setRunText(startRun, preStart);- const repl = makeRun(expand(m, withText));- startRun.parentNode.insertBefore(repl, startRun.nextSibling);- if (first === last) {- if (postEnd !== "") startRun.parentNode.insertBefore(makeRun(postEnd), repl.nextSibling);- } else {- setRunText(runs[last], postEnd);- for (let i = first + 1; i < last; i++) setRunText(runs[i], "");- }- total++;+ const matches = [...joined.matchAll(global)];+ if (matches.length === 0) continue;+ const bounds = [];+ let cursor = 0;+ for (const t of texts) {+ bounds.push([cursor, cursor + t.length]);+ cursor += t.length;+ }+ for (let i = 0; i < runs.length; i++) {+ const [rs, rend] = bounds[i];+ const covers = matches.filter((m) => m.index < rend && (m.index + m[0].length > rs || m.index >= rs && m.index < rend));+ if (covers.length === 0) continue;+ const pieces = [];+ let pos = 0;+ for (const m of covers) {+ const cs = Math.max(0, m.index - rs);+ const ce = Math.min(texts[i].length, m.index + m[0].length - rs);+ if (cs > pos) pieces.push({ text: texts[i].slice(pos, cs) });+ if (m.index >= rs && m.index < rend) pieces.push({ repl: expand(m, withText) });+ pos = ce;+ }+ pieces.push({ text: texts[i].slice(pos) });+ const run = runs[i];+ let anchor = run;+ for (const [k, piece] of pieces.entries()) {+ if (k === 0) {+ setRunText(run, piece.text ?? "");+ if (piece.text !== void 0) continue;+ }+ const clone = cloneRun(doc, run, piece.repl ?? piece.text);+ anchor.parentNode.insertBefore(clone, anchor.nextSibling);+ anchor = clone;+ }+ total += covers.filter((m) => m.index >= rs && m.index < rend).length;}}return total;}-var sumTo = (arr, n) => arr.slice(0, n).reduce((a, b) => a + b.length, 0);function setRunText(run, text) {const ts = elementsOf(run, "w:t");if (ts.length === 0) return;@@ -7763,13 +7783,8 @@ var ancestor = (node, tag) => {return null;};function splitRunAt(doc, run, offset) {- const text = elementsOf(run, "w:t").map((t2) => t2.textContent).join("");- const clone = run.cloneNode(false);- while (clone.firstChild && clone.firstChild.tagName !== "w:rPr") clone.removeChild(clone.firstChild);- const t = doc.createElement("w:t");- t.setAttribute("xml:space", "preserve");- t.appendChild(doc.createTextNode(text.slice(offset)));- clone.appendChild(t);+ const text = elementsOf(run, "w:t").map((t) => t.textContent).join("");+ const clone = cloneRun(doc, run, text.slice(offset));setRunText(run, text.slice(0, offset));run.parentNode.insertBefore(clone, run.nextSibling);return clone;@@ -7791,14 +7806,18 @@ function phraseRuns(doc, p, anchor) {return { i: runs.length - 1, off: texts.at(-1).length };};const e = locate(end), s = locate(start);- let lastRun;+ let lastRun, firstRun;if (e.off === texts[e.i].length) lastRun = runs[e.i];else if (e.off === 0) lastRun = runs[e.i - 1];else {splitRunAt(doc, runs[e.i], e.off);lastRun = runs[e.i];}- const firstRun = s.off === 0 ? runs[s.i] : splitRunAt(doc, runs[s.i], s.off);+ if (s.i === e.i && s.off > 0) {+ firstRun = lastRun = splitRunAt(doc, runs[s.i], s.off);+ } else {+ firstRun = s.off === 0 ? runs[s.i] : splitRunAt(doc, runs[s.i], s.off);+ }return { firstRun, lastRun };}function addComment(pkg, getDoc, changed, { paragraph, text, anchor, author = "Bot", initials = "B" }) {@@ -8139,21 +8158,31 @@ function pptxLayout(pkg, slidesFilter, { autofit = false } = {}) {const fitted = [];for (const slide of inv.slides) {if (!slide.shapes) continue;+ const allBoxes = [];+ const collect = (shapes) => {+ for (const s of shapes) {+ if (s.box) allBoxes.push(s);+ if (s.children) collect(s.children);+ }+ };+ collect(slide.shapes);const boxes = slide.shapes.filter((s) => s.box && s.text?.length);+ for (const s of allBoxes) {+ const a = s.box;+ if (a.x < 0 || a.y < 0 || a.x + a.w > size.cx + 0.01 || a.y + a.h > size.cy + 0.01) {+ findings.push({ slide: slide.position, rule: "off-slide", shape: s.name, box: a });+ }+ }for (let i = 0; i < boxes.length; i++) {for (let j = i + 1; j < boxes.length; j++) {- const a2 = boxes[i].box, b = boxes[j].box;- const ox = Math.max(0, Math.min(a2.x + a2.w, b.x + b.w) - Math.max(a2.x, b.x));- const oy = Math.max(0, Math.min(a2.y + a2.h, b.y + b.h) - Math.max(a2.y, b.y));+ const a = boxes[i].box, b = boxes[j].box;+ const ox = Math.max(0, Math.min(a.x + a.w, b.x + b.w) - Math.max(a.x, b.x));+ const oy = Math.max(0, Math.min(a.y + a.h, b.y + b.h) - Math.max(a.y, b.y));const area = ox * oy;if (area <= 0) continue;- const share = area / Math.min(a2.w * a2.h, b.w * b.h);+ const share = area / Math.min(a.w * a.h, b.w * b.h);findings.push({ slide: slide.position, rule: "overlap", shapes: [boxes[i].name, boxes[j].name], area: +area.toFixed(2), shareOfSmaller: +share.toFixed(2) });}- const a = boxes[i].box;- if (a.x < 0 || a.y < 0 || a.x + a.w > size.cx + 0.01 || a.y + a.h > size.cy + 0.01) {- findings.push({ slide: slide.position, rule: "off-slide", shape: boxes[i].name, box: a });- }}if (autofit) {const names = new Set(findings.filter((f) => f.slide === slide.position).flatMap((f) => f.shapes ?? [f.shape]));@@ -8184,7 +8213,17 @@ function pptxSlides(pkg, order) {const pres = pkg.xml("ppt/presentation.xml");const lst = firstOf(pres.documentElement, "p:sldIdLst");const presRels = pkg.rels.get("ppt/presentation.xml");- const nextIds = { slide: maxNum(slideParts(pkg), /slide(\d+)\.xml$/) + 1, sldId: Math.max(...current.map((e) => Number(e.sldId))) + 1, rid: nextRid(presRels) };+ const usedSldIds = new Set(current.map((e) => Number(e.sldId)));+ const newSlidePart = () => {+ let n = 1;+ while (pkg.parts.has(`ppt/slides/slide${n}.xml`)) n++;+ return n;+ };+ const newSldId = () => {+ let n = Math.max(256, ...usedSldIds) + 1;+ usedSldIds.add(n);+ return n;+ };const seen = /* @__PURE__ */ new Set();const newEntries = [];const duplicated = [];@@ -8196,14 +8235,14 @@ function pptxSlides(pkg, order) {seen.add(pos);continue;}- const dup = String(nextIds.slide++);+ const dup = String(newSlidePart());const newPart = `ppt/slides/slide${dup}.xml`;pkg.parts.set(newPart, { data: pkg.get(source.part) });if (pkg.overrides.has(source.part)) pkg.overrides.set(newPart, pkg.overrides.get(source.part));const mapped = /* @__PURE__ */ new Map([[source.part, newPart]]);- const rid = `rId${nextIds.rid++}`;+ const rid = `rId${nextRid(presRels)}`;presRels.set(rid, { id: rid, type: presRels.get(source.rid)?.type ?? "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide", target: `slides/slide${dup}.xml`, mode: "Internal" });- const sid = String(nextIds.sldId++);+ const sid = String(newSldId());const slideRels = /* @__PURE__ */ new Map();for (const [id, rel] of pkg.relsOf(source.part)) {if (rel.mode === "External") {@@ -8285,7 +8324,7 @@ function pptxClean(pkg, { dryRun = false } = {}) {if (!xml.includes(`"${id}"`) && !xml.includes(`'${id}'`)) map.delete(id);}}- const listed = new Set(deckOrder(pkg).map((e) => e.part));+ const listed = new Set(pkg.parts.has("ppt/presentation.xml") ? deckOrder(pkg).map((e) => e.part) : []);for (const part of slideParts(pkg)) {if (!listed.has(part)) {removed.push(part);@@ -8367,7 +8406,11 @@ function sheetList(pkg) {var colOf = (ref) => /^([A-Z]+)/.exec(ref)[1];var rowOf = (ref) => Number(/(\d+)$/.exec(ref)[1]);var colNum = (col) => [...col].reduce((n, c) => n * 26 + c.charCodeAt(0) - 64, 0);-function cellValue(c, shared, fmt) {+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;+}+function cellValue(c, shared, fmt, epoch = 25569) {const t = c.getAttribute("t"), v = firstOf(c, "v"), f = firstOf(c, "f");let value = v == null ? void 0 : v.textContent;if (t === "s" && value !== void 0) value = shared[Number(value)] ?? `#SST${value}`;@@ -8375,7 +8418,7 @@ function cellValue(c, shared, fmt) {else if (t === "str" || t === "inlineStr") value = t === "inlineStr" ? textOf(firstOf(c, "is") ?? c) : value;else if (t === "e") value = value;else if (value !== void 0 && fmt.get(Number(c.getAttribute("s") ?? 0)) === "date" && !Number.isNaN(Number(value))) {- const days = Number(value) - 25569;+ const days = Number(value) - epoch;value = new Date(days * 864e5).toISOString().slice(0, value.includes(".") ? 19 : 10);}return { value, formula: f?.textContent };@@ -8424,6 +8467,7 @@ function xlsxRead(pkg, sheetName, range) {}const shared = sharedStrings(pkg);const fmt = numFmts(pkg);+ const epoch = dateEpoch(pkg);const doc = pkg.xml(sheet.part);const inRange = (ref) => {if (!range) return true;@@ -8437,7 +8481,7 @@ function xlsxRead(pkg, sheetName, range) {for (const c of childrenOf(row, "c")) {const ref = c.getAttribute("r");if (!inRange(ref)) continue;- const { value, formula } = cellValue(c, shared, fmt);+ const { value, formula } = cellValue(c, shared, fmt, epoch);cells[ref] = formula ? { v: value, f: formula } : { v: value };}if (Object.keys(cells).length > 0) rows.push({ row: Number(row.getAttribute("r")), cells });diff --git a/ds-bot/src/office/check.js b/ds-bot/src/office/check.jsindex 3be4d66f..005d6b3d 100644--- a/ds-bot/src/office/check.js+++ b/ds-bot/src/office/check.js@@ -315,6 +315,9 @@ export function fixPackage(pkg) {if (rel.mode !== 'External' && !pkg.parts.has(pkg.targetOf(owner, rel.target))) { map.delete(id); fixed.push(`${owner || '/'}#${id}`) }}}+ // Flush so the re-check reads .rels parts reflecting the model, not the+ // bytes the package was opened with.+ pkg.flush()return { fixed, remaining: checkPackage(pkg) }}diff --git a/ds-bot/src/office/docx.js b/ds-bot/src/office/docx.jsindex d5e27eb5..680a2c64 100644--- a/ds-bot/src/office/docx.js+++ b/ds-bot/src/office/docx.js@@ -53,7 +53,7 @@ export function docxRead(pkg, { changes = false } = {}) {const body = firstOf(doc.documentElement, 'w:body')const blocks = []let pi = 0, ti = 0- const commentIds = commentsIndex(pkg)+ const commentIds = commentsIndex(pkg, doc)for (const el of childrenOf(body)) {if (el.tagName === 'w:p') {pi++@@ -95,11 +95,12 @@ export function docxRead(pkg, { changes = false } = {}) {return { blocks, sections, notes, comments: commentsList(pkg, changes) }}-// pN ids number only the body's top-level paragraphs — the same ids docxRead gives.-function commentsIndex(pkg) {+// pN ids number only the body's top-level paragraphs — the same ids docxRead+// gives. `doc` must be the parse the blocks come from: element keys from a+// second parse never match.+function commentsIndex(pkg, doc) {const byEl = new Map()if (!pkg.parts.has('word/comments.xml')) return byEl- const doc = pkg.xml('word/document.xml')for (const ref of elementsOf(doc.documentElement, 'w:commentReference')) {let p = ref.parentNodewhile (p && p.tagName !== 'w:p') p = p.parentNode@@ -319,10 +320,20 @@ const expand = (m, withText) =>const nearestPara = (el) => { for (let n = el.parentNode; n; n = n.parentNode) if (n.tagName === 'w:p') return n; return null }-// Replace text across runs inside one document; deletions skipped; formatting of-// the match-start run kept. Matches are found once on the original text and-// applied right-to-left, so offsets never shift and the replacement text itself-// is never re-scanned. Returns the number of replacements.+// A new run holding `text`, with `src`'s w:rPr cloned so formatting survives.+const cloneRun = (doc, src, text) => {+ const clone = src.cloneNode(false)+ const rPr = childrenOf(src, 'w:rPr')[0]+ if (rPr) clone.appendChild(rPr.cloneNode(true))+ const t = doc.createElement('w:t'); t.setAttribute('xml:space', 'preserve')+ t.appendChild(doc.createTextNode(text)); clone.appendChild(t)+ return clone+}++// Replace text across runs inside one document; deletions skipped; formatting+// of the match-start run kept. Each affected run is rebuilt once from its+// original text and every match that touches it, so several matches in one+// run, adjacent matches and spanning matches all compose. Returns the count.function replaceInDoc(doc, re, withText) {let total = 0const global = new RegExp(re.source, re.flags.includes('g') ? re.flags : re.flags + 'g')@@ -330,42 +341,44 @@ function replaceInDoc(doc, re, withText) {const runs = elementsOf(p, 'w:r').filter(r => !isDeleted(p, r) && nearestPara(r) === p)const texts = runs.map(r => elementsOf(r, 'w:t').map(t => t.textContent).join(''))const joined = texts.join('')- for (const m of [...joined.matchAll(global)].reverse()) {- let cursor = 0, first = -1, last = -1- for (let i = 0; i < runs.length; i++) {- const end = cursor + texts[i].length- if (first < 0 && m.index < end) first = i- if (m.index + m[0].length <= end) { last = i; break }- cursor = end- }- if (first < 0) continue- if (last < 0) last = runs.length - 1- const startRun = runs[first]- const preStart = texts[first].slice(0, m.index - sumTo(texts, first))- const postEnd = texts[last].slice(m.index + m[0].length - sumTo(texts, last))- const makeRun = (text) => {- const clone = startRun.cloneNode(false)- while (clone.firstChild && clone.firstChild.tagName !== 'w:rPr') clone.removeChild(clone.firstChild)- const t = doc.createElement('w:t'); t.setAttribute('xml:space', 'preserve')- t.appendChild(doc.createTextNode(text)); clone.appendChild(t)- return clone+ const matches = [...joined.matchAll(global)]+ if (matches.length === 0) continue+ const bounds = []+ let cursor = 0+ for (const t of texts) { bounds.push([cursor, cursor + t.length]); cursor += t.length }+ for (let i = 0; i < runs.length; i++) {+ const [rs, rend] = bounds[i]+ const covers = matches.filter(m => m.index < rend && (m.index + m[0].length > rs || (m.index >= rs && m.index < rend)))+ if (covers.length === 0) continue+ // Pieces in order: kept text fragments alternating with replacement runs+ // (a run emits a replacement only for matches starting inside it).+ const pieces = []+ let pos = 0+ for (const m of covers) {+ const cs = Math.max(0, m.index - rs)+ const ce = Math.min(texts[i].length, m.index + m[0].length - rs)+ if (cs > pos) pieces.push({ text: texts[i].slice(pos, cs) })+ if (m.index >= rs && m.index < rend) pieces.push({ repl: expand(m, withText) })+ pos = ce}- setRunText(startRun, preStart)- const repl = makeRun(expand(m, withText))- startRun.parentNode.insertBefore(repl, startRun.nextSibling)- if (first === last) {- if (postEnd !== '') startRun.parentNode.insertBefore(makeRun(postEnd), repl.nextSibling)- } else {- setRunText(runs[last], postEnd)- for (let i = first + 1; i < last; i++) setRunText(runs[i], '')+ pieces.push({ text: texts[i].slice(pos) })+ const run = runs[i]+ let anchor = run+ for (const [k, piece] of pieces.entries()) {+ if (k === 0) {+ setRunText(run, piece.text ?? '')+ if (piece.text !== undefined) continue+ }+ const clone = cloneRun(doc, run, piece.repl ?? piece.text)+ anchor.parentNode.insertBefore(clone, anchor.nextSibling)+ anchor = clone}- total+++ total += covers.filter(m => m.index >= rs && m.index < rend).length}}return total}-const sumTo = (arr, n) => arr.slice(0, n).reduce((a, b) => a + b.length, 0)function setRunText(run, text) {const ts = elementsOf(run, 'w:t')@@ -436,11 +449,7 @@ const ancestor = (node, tag) => { for (let n = node.parentNode; n; n = n.parentN// rPr takes the rest right after it. Returns the clone.function splitRunAt(doc, run, offset) {const text = elementsOf(run, 'w:t').map(t => t.textContent).join('')- const clone = run.cloneNode(false)- while (clone.firstChild && clone.firstChild.tagName !== 'w:rPr') clone.removeChild(clone.firstChild)- const t = doc.createElement('w:t'); t.setAttribute('xml:space', 'preserve')- t.appendChild(doc.createTextNode(text.slice(offset)))- clone.appendChild(t)+ const clone = cloneRun(doc, run, text.slice(offset))setRunText(run, text.slice(0, offset))run.parentNode.insertBefore(clone, run.nextSibling)return clone@@ -465,11 +474,16 @@ function phraseRuns(doc, p, anchor) {return { i: runs.length - 1, off: texts.at(-1).length }}const e = locate(end), s = locate(start)- let lastRun+ let lastRun, firstRunif (e.off === texts[e.i].length) lastRun = runs[e.i]else if (e.off === 0) lastRun = runs[e.i - 1]else { splitRunAt(doc, runs[e.i], e.off); lastRun = runs[e.i] }- const firstRun = s.off === 0 ? runs[s.i] : splitRunAt(doc, runs[s.i], s.off)+ if (s.i === e.i && s.off > 0) {+ // The phrase lives in the clone the start split creates, not the prefix run.+ firstRun = lastRun = splitRunAt(doc, runs[s.i], s.off)+ } else {+ firstRun = s.off === 0 ? runs[s.i] : splitRunAt(doc, runs[s.i], s.off)+ }return { firstRun, lastRun }}diff --git a/ds-bot/src/office/package.js b/ds-bot/src/office/package.jsindex 42117047..94296d17 100644--- a/ds-bot/src/office/package.js+++ b/ds-bot/src/office/package.js@@ -2,7 +2,7 @@// 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 { dirname, join, posix } from 'node:path'+import { dirname, join, posix, resolve, sep } from 'node:path'import { parseXml, serializeXml, childrenOf } from './xml.js'import { readZip, writeZip } from './zip.js'@@ -175,15 +175,19 @@ export class Package {/** Write an `unpack` directory tree. An existing non-empty dir must hold an* earlier unpack ([Content_Types].xml at its root); files the package no- * longer has are removed. */+ * longer has are removed. The model is flushed first so .rels and content+ * types written to disk match the in-memory state. */unpack(dir) {+ this.flush()if (existsSync(dir) && readdirSync(dir).length > 0 && !existsSync(join(dir, '[Content_Types].xml'))) {fail(`unpack: ${dir} exists and is not an earlier unpack`)}mkdirSync(dir, { recursive: true })+ const root = resolve(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 })const data = this.get(name)diff --git a/ds-bot/src/office/pptx.js b/ds-bot/src/office/pptx.jsindex cbed3a59..1670425b 100644--- a/ds-bot/src/office/pptx.js+++ b/ds-bot/src/office/pptx.js@@ -196,7 +196,18 @@ export function pptxLayout(pkg, slidesFilter, { autofit = false } = {}) {const fitted = []for (const slide of inv.slides) {if (!slide.shapes) continue+ // Off-slide bounds apply to every shape with a resolved box (pictures,+ // charts, tables, group children); overlap stays among text shapes.+ const allBoxes = []+ const collect = (shapes) => { for (const s of shapes) { if (s.box) allBoxes.push(s); if (s.children) collect(s.children) } }+ collect(slide.shapes)const boxes = slide.shapes.filter(s => s.box && s.text?.length)+ for (const s of allBoxes) {+ const a = s.box+ if (a.x < 0 || a.y < 0 || a.x + a.w > size.cx + 0.01 || a.y + a.h > size.cy + 0.01) {+ findings.push({ slide: slide.position, rule: 'off-slide', shape: s.name, box: a })+ }+ }for (let i = 0; i < boxes.length; i++) {for (let j = i + 1; j < boxes.length; j++) {const a = boxes[i].box, b = boxes[j].box@@ -207,10 +218,6 @@ export function pptxLayout(pkg, slidesFilter, { autofit = false } = {}) {const share = area / Math.min(a.w * a.h, b.w * b.h)findings.push({ slide: slide.position, rule: 'overlap', shapes: [boxes[i].name, boxes[j].name], area: +area.toFixed(2), shareOfSmaller: +share.toFixed(2) })}- const a = boxes[i].box- if (a.x < 0 || a.y < 0 || a.x + a.w > size.cx + 0.01 || a.y + a.h > size.cy + 0.01) {- findings.push({ slide: slide.position, rule: 'off-slide', shape: boxes[i].name, box: a })- }}if (autofit) {const names = new Set(findings.filter(f => f.slide === slide.position).flatMap(f => f.shapes ?? [f.shape]))@@ -249,7 +256,11 @@ export function pptxSlides(pkg, order) {const pres = pkg.xml('ppt/presentation.xml')const lst = firstOf(pres.documentElement, 'p:sldIdLst')const presRels = pkg.rels.get('ppt/presentation.xml')- const nextIds = { slide: maxNum(slideParts(pkg), /slide(\d+)\.xml$/) + 1, sldId: Math.max(...current.map(e => Number(e.sldId))) + 1, rid: nextRid(presRels) }+ // Every allocation probes the live map/set — a gap below a live id must not+ // be overwritten by a later duplicate in the same batch.+ const usedSldIds = new Set(current.map(e => Number(e.sldId)))+ const newSlidePart = () => { let n = 1; while (pkg.parts.has(`ppt/slides/slide${n}.xml`)) n++; return n }+ const newSldId = () => { let n = Math.max(256, ...usedSldIds) + 1; usedSldIds.add(n); return n }const seen = new Set()const newEntries = []const duplicated = []@@ -258,14 +269,14 @@ export function pptxSlides(pkg, order) {if (!source) throw new Error(`no slide at position ${pos}`)if (!seen.has(pos)) { newEntries.push({ ...source }); seen.add(pos); continue }// Duplicate: copy slide + notes slide + chart parts + embedded workbooks.- const dup = String(nextIds.slide++)+ const dup = String(newSlidePart())const newPart = `ppt/slides/slide${dup}.xml`pkg.parts.set(newPart, { data: pkg.get(source.part) })if (pkg.overrides.has(source.part)) pkg.overrides.set(newPart, pkg.overrides.get(source.part))const mapped = new Map([[source.part, newPart]])- const rid = `rId${nextIds.rid++}`+ const rid = `rId${nextRid(presRels)}`presRels.set(rid, { id: rid, type: presRels.get(source.rid)?.type ?? 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide', target: `slides/slide${dup}.xml`, mode: 'Internal' })- const sid = String(nextIds.sldId++)+ const sid = String(newSldId())// Copy the slide's .rels, duplicating owned parts (notes, charts, workbooks).const slideRels = new Map()for (const [id, rel] of pkg.relsOf(source.part)) {@@ -358,7 +369,8 @@ export function pptxClean(pkg, { dryRun = false } = {}) {}}// Slides not in sldIdLst.- const listed = new Set(deckOrder(pkg).map(e => e.part))+ // Slides not in sldIdLst — pptx only; docx/xlsx have no presentation part.+ const listed = new Set(pkg.parts.has('ppt/presentation.xml') ? deckOrder(pkg).map(e => e.part) : [])for (const part of slideParts(pkg)) {if (!listed.has(part)) { removed.push(part); if (!dryRun) pkg.remove(part) }}diff --git a/ds-bot/src/office/xlsx.js b/ds-bot/src/office/xlsx.jsindex 90e0778e..ddd4463c 100644--- a/ds-bot/src/office/xlsx.js+++ b/ds-bot/src/office/xlsx.js@@ -38,7 +38,14 @@ const colOf = (ref) => /^([A-Z]+)/.exec(ref)[1]const rowOf = (ref) => Number(/(\d+)$/.exec(ref)[1])const colNum = (col) => [...col].reduce((n, c) => n * 26 + c.charCodeAt(0) - 64, 0)-function cellValue(c, shared, fmt) {+// Serial → unix-day offset: 1899-12-30 (Lotus 1900 compat), or 1904-01-01 when+// 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+}++function cellValue(c, shared, fmt, epoch = 25569) {const t = c.getAttribute('t'), v = firstOf(c, 'v'), f = firstOf(c, 'f')let value = v == null ? undefined : v.textContentif (t === 's' && value !== undefined) value = shared[Number(value)] ?? `#SST${value}`@@ -46,7 +53,7 @@ function cellValue(c, shared, fmt) {else if (t === 'str' || t === 'inlineStr') value = t === 'inlineStr' ? textOf(firstOf(c, 'is') ?? c) : valueelse if (t === 'e') value = value // error text as storedelse if (value !== undefined && fmt.get(Number(c.getAttribute('s') ?? 0)) === 'date' && !Number.isNaN(Number(value))) {- const days = Number(value) - 25569 // 1899-12-30 epoch → unix days (Lotus 1900 bug compat: Excel serial)+ const days = Number(value) - epochvalue = new Date(days * 86400_000).toISOString().slice(0, value.includes('.') ? 19 : 10)}return { value, formula: f?.textContent }@@ -89,6 +96,7 @@ export function xlsxRead(pkg, sheetName, range) {if (!pkg.parts.has(sheet.part)) { const e = new Error(`sheet part ${sheet.part} missing`); e.code = 'args'; throw e }const shared = sharedStrings(pkg)const fmt = numFmts(pkg)+ const epoch = dateEpoch(pkg)const doc = pkg.xml(sheet.part)const inRange = (ref) => {if (!range) return true@@ -102,7 +110,7 @@ export function xlsxRead(pkg, sheetName, range) {for (const c of childrenOf(row, 'c')) {const ref = c.getAttribute('r')if (!inRange(ref)) continue- const { value, formula } = cellValue(c, shared, fmt)+ const { value, formula } = cellValue(c, shared, fmt, epoch)cells[ref] = formula ? { v: value, f: formula } : { v: value }}if (Object.keys(cells).length > 0) rows.push({ row: Number(row.getAttribute('r')), cells })diff --git a/ds-bot/src/office/zip.js b/ds-bot/src/office/zip.jsindex cdff949f..c6fa6317 100644--- a/ds-bot/src/office/zip.js+++ b/ds-bot/src/office/zip.js@@ -39,13 +39,26 @@ export function readZip(buf) {const localOff = buf.readUInt32LE(cdOff + 42)if (csize === 0xffffffff || usize === 0xffffffff) fail('zip64 archives are not supported')const name = buf.subarray(cdOff + 46, cdOff + 46 + nameLen).toString('utf8')+ // Zip slip: a member escaping the destination tree is refused on read so+ // every command fails on such a package, not just unpack.+ const norm = name.replace(/\\/g, '/')+ if (norm.startsWith('/') || /^[A-Za-z]:/.test(norm) || norm.split('/').includes('..')) fail(`zip: unsafe member name ${name}`)if (buf.readUInt32LE(localOff) !== LOCAL) fail(`corrupt local header for ${name}`)const lNameLen = buf.readUInt16LE(localOff + 26)const lExtraLen = buf.readUInt16LE(localOff + 28)const raw = buf.subarray(localOff + 30 + lNameLen + lExtraLen, localOff + 30 + lNameLen + lExtraLen + csize)let dataif (method === 0) data = raw.subarray(0, usize)- else if (method === 8) data = inflateRawSync(raw)+ else if (method === 8) {+ // Enforce the declared uncompressed size — a zip bomb dies here.+ // inflateRawSync rejects a zero maxOutputLength, so empty members skip it.+ try {+ data = usize === 0 ? Buffer.alloc(0) : inflateRawSync(raw, { maxOutputLength: usize })+ } catch (err) {+ if (err instanceof RangeError || err?.code === 'ERR_OUT_OF_RANGE') fail(`${name} inflates past its declared size`)+ fail(`corrupt data for ${name}`)+ }+ }else fail(`unsupported compression method ${method} for ${name}`)if (data.length !== usize) fail(`size mismatch for ${name}`)members.push({ name, data, raw, method, crc, flags: flags & 0x0800, extra: buf.subarray(cdOff + 46 + nameLen, cdOff + 46 + nameLen + extraLen) })diff --git a/ds-bot/test/office.test.js b/ds-bot/test/office.test.jsindex 9cd98099..c642a1d7 100644--- a/ds-bot/test/office.test.js+++ b/ds-bot/test/office.test.js@@ -7,11 +7,11 @@ import { join, dirname } from 'node:path'import { fileURLToPath } from 'node:url'import { describe, test } from 'node:test'import assert from 'node:assert'-import { readZip } from '../src/office/zip.js'+import { readZip, writeZip } from '../src/office/zip.js'import { Package } from '../src/office/package.js'import { checkPackage, fixPackage } from '../src/office/check.js'import { docxRead, docxEdit, resolveChanges } from '../src/office/docx.js'-import { pptxInventory, pptxLayout, pptxClean } from '../src/office/pptx.js'+import { pptxInventory, pptxLayout, pptxClean, pptxSlides } from '../src/office/pptx.js'import { xlsxInventory, xlsxRead } from '../src/office/xlsx.js'import { decodePng, encodePng, contactSheet } from '../src/office/png.js'import { parseXml, elementsOf, childrenOf, textOf } from '../src/office/xml.js'@@ -467,6 +467,164 @@ describe('re-review fixes', () => {})})+describe('re-review pass 2', () => {+ test('zip slip: unsafe member names are refused on open', () => {+ for (const name of ['../evil.xml', 'word/../../x.xml', '/abs/x.xml', 'C:/win/x.xml', 'word\\..\\x.xml']) {+ const buf = writeZip([{ name, data: Buffer.from('x') }])+ const file = join(tmp(), 'z.docx')+ writeFileSync(file, buf)+ assert.throws(() => Package.open(file), /unsafe member name/)+ }+ })+ test('zip bomb: inflate stops at the declared size', () => {+ const buf = writeZip([{ name: 'x.xml', data: Buffer.alloc(5000, 65) }])+ buf.writeUInt32LE(10, 22) // lie: declared usize 10, real 5000+ const cdOff = buf.readUInt32LE(buf.length - 22 + 16)+ buf.writeUInt32LE(10, cdOff + 24)+ assert.throws(() => readZip(buf), /past its declared size/)+ })+ test('replace: several matches in one run, adjacent and spanning', () => {+ const pkg = Package.open(fixture('sample.docx'))+ const doc = pkg.xml('word/document.xml')+ const body = childrenOf(doc.documentElement, 'w:body')[0]+ const addP = (runs) => {+ const p = parseXml(`<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">${runs.map(r => `<w:r><w:t xml:space="preserve">${r}</w:t></w:r>`).join('')}</w:p>`).documentElement+ body.appendChild(doc.importNode(p, true))+ }+ addP(['cat cat'])+ addP(['x x x'])+ addP(['catcat'])+ addP(['ca', 't c', 'at'])+ pkg.putXml('word/document.xml', doc)+ docxEdit(pkg, [{ replace: { find: 'cat', with: 'dog' } }, { replace: { find: 'x', with: 'y' } }])+ const { blocks } = docxRead(pkg)+ const texts = blocks.slice(-4).map(b => b.text)+ assert.equal(texts[0], 'dog dog')+ assert.equal(texts[1], 'y y y')+ assert.equal(texts[2], 'dogdog')+ assert.equal(texts[3], 'dog dog')+ })+ test('replace and comment keep run rPr', () => {+ const pkg = Package.open(fixture('sample.docx'))+ const doc = pkg.xml('word/document.xml')+ const body = childrenOf(doc.documentElement, 'w:body')[0]+ body.appendChild(doc.importNode(parseXml(`<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:r><w:rPr><w:b/></w:rPr><w:t>bold bold</w:t></w:r></w:p>`).documentElement, true))+ pkg.putXml('word/document.xml', doc)+ docxEdit(pkg, [{ replace: { find: 'bold', with: 'strong' } }])+ const fresh = pkg.xml('word/document.xml')+ const strongs = elementsOf(fresh.documentElement, 'w:r').filter(r => elementsOf(r, 'w:t').some(t => t.textContent === 'strong'))+ assert.equal(strongs.length, 2)+ assert.ok(strongs.every(r => childrenOf(childrenOf(r, 'w:rPr')[0], 'w:b')[0] !== undefined))+ })+ test('comment anchors: mid-run, run edges, spanning, hyperlink', () => {+ const mkPkg = (runs) => {+ const pkg = Package.open(fixture('sample.docx'))+ const doc = pkg.xml('word/document.xml')+ const body = childrenOf(doc.documentElement, 'w:body')[0]+ body.appendChild(doc.importNode(parseXml(`<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">${runs}</w:p>`).documentElement, true))+ pkg.putXml('word/document.xml', doc)+ return pkg+ }+ const anchorOk = (pkg, pId, anchor) => {+ docxEdit(pkg, [{ comment: { paragraph: pId, text: 'c', anchor } }])+ const doc = pkg.xml('word/document.xml')+ const p = elementsOf(doc.documentElement, 'w:p').at(-1)+ const order = []+ for (const el of elementsOf(p)) {+ if (el.tagName === 'w:commentRangeStart') order.push('S')+ if (el.tagName === 'w:commentRangeEnd') order.push('E')+ if (el.tagName === 'w:commentReference') order.push('R')+ }+ assert.ok(order.indexOf('S') < order.indexOf('E'), `${anchor}: ${order}`)+ assert.ok(order.indexOf('E') < order.indexOf('R'))+ }+ anchorOk(mkPkg('<w:r><w:t xml:space="preserve">aa bb cc</w:t></w:r>'), 'p7', 'bb')+ anchorOk(mkPkg('<w:r><w:t xml:space="preserve">aa bb cc</w:t></w:r>'), 'p7', 'aa')+ anchorOk(mkPkg('<w:r><w:t xml:space="preserve">aa bb cc</w:t></w:r>'), 'p7', 'cc')+ anchorOk(mkPkg('<w:r><w:t>ab</w:t></w:r><w:r><w:t>cd</w:t></w:r>'), 'p7', 'bc')+ anchorOk(mkPkg('<w:hyperlink w:anchor="t"><w:r><w:t>click here</w:t></w:r></w:hyperlink>'), 'p7', 'here')+ })+ test('check --fix on an unpacked dir flushes the model to disk', () => {+ const dir = join(tmp(), 'un')+ cli('unpack', fixture('sample.docx'), dir)+ const pkg = Package.loadDir(dir)+ pkg.override('word/styles.xml', 'application/xml')+ pkg.relsOf('word/document.xml').set('rId99', { id: 'rId99', type: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/header', target: 'missing.xml', mode: 'Internal' })+ pkg.unpack(dir)+ const res = cli('check', dir, '--fix', '--in-place')+ assert.equal(res.ok, true)+ assert.deepEqual(res.data.findings, [])+ assert.deepEqual(checkPackage(Package.loadDir(dir)), [])+ assert.ok(!readFileSync(join(dir, 'word/_rels/document.xml.rels'), 'utf8').includes('rId99'))+ })+ test('check --fix on a file no longer reports the dangling rel it fixed', () => {+ const dir = tmp()+ const bad = join(dir, 'b.docx')+ const pkg = Package.open(fixture('sample.docx'))+ pkg.relsOf('word/document.xml').set('rId99', { id: 'rId99', type: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/header', target: 'missing.xml', mode: 'Internal' })+ writeFileSync(bad, pkg.save())+ const res = cli('check', bad, '--fix', '-o', join(dir, 'fixed.docx'))+ assert.equal(res.ok, true)+ assert.deepEqual(res.data.findings, [])+ assert.deepEqual(checkPackage(Package.open(join(dir, 'fixed.docx'))), [])+ })+ test('pptx clean works on docx and xlsx', () => {+ const dir = tmp()+ for (const ext of ['docx', 'xlsx']) {+ const res = cli('pptx', 'clean', fixture(`sample.${ext}`), '-o', join(dir, `c.${ext}`))+ assert.equal(res.ok, true, JSON.stringify(res))+ }+ })+ test('duplicated slides never overwrite a live rel id', () => {+ const pkg = Package.open(fixture('sample.pptx'))+ const pres = pkg.xml('ppt/presentation.xml')+ const lst = childrenOf(pres.documentElement, 'p:sldIdLst')[0]+ const entries = childrenOf(lst, 'p:sldId')+ const removedRid = entries[1].getAttribute('r:id')+ lst.removeChild(entries[1]) // gap in the rel ids+ pkg.rels.get('ppt/presentation.xml').delete(removedRid)+ pkg.putXml('ppt/presentation.xml', pres)+ pptxSlides(pkg, [1, 1, 2])+ const after = pkg.xml('ppt/presentation.xml')+ const rids = childrenOf(childrenOf(after.documentElement, 'p:sldIdLst')[0], 'p:sldId').map(e => e.getAttribute('r:id'))+ assert.equal(new Set(rids).size, rids.length)+ for (const rid of rids) {+ const rel = pkg.rels.get('ppt/presentation.xml').get(rid)+ assert.ok(rel && pkg.parts.has(pkg.targetOf('ppt/presentation.xml', rel.target)), `${rid} unresolved`)+ }+ assert.equal(rids.length, 3)+ })+ test('docx read shows comments on their anchored block', () => {+ const { blocks, comments } = docxRead(Package.open(fixture('sample.docx')))+ const p3 = blocks.find(b => b.id === 'p3') // the fixture comment anchors to p3+ assert.ok(comments.length > 0)+ assert.ok(p3.comments?.length > 0)+ })+ test('xlsx dates honour date1904', () => {+ const read = (pkg) => xlsxRead(pkg, 'Sheet1').rows.flatMap(r => Object.values(r.cells).map(c => c.v)).find(v => /^\d{4}-\d{2}-\d{2}/.test(String(v)))+ const normal = read(Package.open(fixture('sample.xlsx')))+ const pkg = Package.open(fixture('sample.xlsx'))+ const wb = pkg.xml('xl/workbook.xml')+ let pr = childrenOf(wb.documentElement, 'workbookPr')[0]+ if (!pr) { pr = wb.createElement('workbookPr'); wb.documentElement.appendChild(pr) }+ pr.setAttribute('date1904', '1')+ pkg.putXml('xl/workbook.xml', wb)+ const shifted = read(pkg)+ assert.notEqual(shifted, normal)+ assert.ok(Date.parse(shifted) > Date.parse(normal))+ })+ test('layout flags a picture past the slide edge', () => {+ const pkg = Package.open(fixture('sample.pptx'))+ const doc = pkg.xml('ppt/slides/slide1.xml')+ const pic = elementsOf(doc.documentElement, 'p:pic')[0]+ const off = elementsOf(pic, 'a:off')[0]+ off.setAttribute('x', String(9144000)) // one inch past a 10in slide+ pkg.putXml('ppt/slides/slide1.xml', doc)+ const { findings } = pptxLayout(pkg)+ assert.ok(findings.some(f => f.rule === 'off-slide' && /picture|pic/i.test(f.shape ?? '')))+ })+})+describe('JSON findings shape (review 14)', () => {test('findings come back ok:true with exit 1', () => {const dir = tmp()