onionskin-cli 0.0.3 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (109) hide show
  1. package/CHANGELOG.md +198 -0
  2. package/INSTALL.md +5 -1
  3. package/NOTICE +34 -0
  4. package/README.md +102 -34
  5. package/docs/contracts/batch.schema.json +27 -4
  6. package/docs/contracts/ops.schema.json +117 -5
  7. package/package.json +4 -2
  8. package/scripts/checker/check.py +24 -5
  9. package/scripts/checker/requirements.txt +482 -5
  10. package/scripts/requirements.txt +482 -5
  11. package/scripts/rewriter/apple/main.swift +1 -1
  12. package/scripts/rewriter/apple/onionskin-fm +0 -0
  13. package/scripts/rewriter/apple/onionskin-fm.json +1 -1
  14. package/skills/onionskin/SKILL.md +12 -9
  15. package/skills/onionskin/reference/batch.md +5 -3
  16. package/skills/onionskin/reference/formats.md +5 -1
  17. package/skills/onionskin/reference/ops.md +48 -4
  18. package/skills/onionskin/reference/troubleshooting.md +31 -0
  19. package/src/agent-detect.js +28 -2
  20. package/src/agent-prompt.js +12 -1
  21. package/src/agent-runner.js +110 -20
  22. package/src/anchor-work.js +154 -0
  23. package/src/anchor-worker.js +38 -0
  24. package/src/anchors.js +61 -11
  25. package/src/apply.js +119 -47
  26. package/src/batch.js +32 -8
  27. package/src/check-worker.js +8 -5
  28. package/src/checker.js +238 -33
  29. package/src/cli.js +248 -42
  30. package/src/constants.js +34 -1
  31. package/src/diff.js +3 -1
  32. package/src/doctor.js +12 -3
  33. package/src/docx.js +464 -87
  34. package/src/edit-hash.js +19 -0
  35. package/src/fsutil.js +14 -6
  36. package/src/history-worker.js +8 -8
  37. package/src/history.js +144 -31
  38. package/src/html-active.js +384 -0
  39. package/src/html-envelope.js +51 -0
  40. package/src/html-parser.js +8 -0
  41. package/src/html-render-worker.js +25 -0
  42. package/src/html-render.js +939 -0
  43. package/src/html-utf8.js +65 -0
  44. package/src/html-write-worker.js +14 -0
  45. package/src/html-write.js +380 -0
  46. package/src/html.js +1259 -0
  47. package/src/local.js +68 -4
  48. package/src/merge-package.js +1166 -0
  49. package/src/merge-pptx.js +98 -38
  50. package/src/merge-proof.js +1019 -0
  51. package/src/merge-worker.js +60 -0
  52. package/src/merge-xlsx.js +84 -29
  53. package/src/merge.js +286 -83
  54. package/src/nfc.js +46 -0
  55. package/src/notes.js +122 -0
  56. package/src/offload.js +419 -1
  57. package/src/open-worker.js +27 -0
  58. package/src/ops/common.js +2 -1
  59. package/src/ops/docx-table.js +5 -3
  60. package/src/ops/docx.js +58 -29
  61. package/src/ops/html.js +394 -0
  62. package/src/ops/pptx.js +82 -23
  63. package/src/ops/run.js +23 -3
  64. package/src/ops/runs.js +215 -13
  65. package/src/ops/schema.js +8 -1
  66. package/src/ops/xlsx.js +121 -10
  67. package/src/ops-worker.js +17 -0
  68. package/src/package.js +43 -4
  69. package/src/person.js +2 -1
  70. package/src/pptx.js +323 -23
  71. package/src/question.js +10 -3
  72. package/src/quick-plan.js +13 -6
  73. package/src/quick.js +1 -1
  74. package/src/redline-check.js +12 -2
  75. package/src/redline-worker.js +10 -14
  76. package/src/reply.js +46 -19
  77. package/src/retain.js +223 -0
  78. package/src/review.js +32 -18
  79. package/src/rewrite-check.js +2 -2
  80. package/src/rewriter.js +38 -8
  81. package/src/sanitize.js +16 -3
  82. package/src/server-entry.js +4 -3
  83. package/src/server.js +175 -62
  84. package/src/session.js +1151 -210
  85. package/src/setup.js +20 -2
  86. package/src/stop-gate.js +34 -0
  87. package/src/suggest-reply.js +42 -7
  88. package/src/suggest.js +14 -29
  89. package/src/text.js +161 -11
  90. package/src/update.js +517 -0
  91. package/src/voice-guide.js +15 -9
  92. package/src/voice-learn.js +15 -4
  93. package/src/voice-sample-worker.js +7 -6
  94. package/src/voice-store.js +11 -4
  95. package/src/voice-worker.js +4 -6
  96. package/src/voice.js +2 -1
  97. package/src/worker-host.js +23 -0
  98. package/src/xlsx.js +378 -97
  99. package/src/xml-tree.js +6 -3
  100. package/src/xml.js +82 -34
  101. package/src/zip-write.js +10 -5
  102. package/src/zip.js +73 -18
  103. package/vendor/parse5/LICENSE.txt +32 -0
  104. package/vendor/parse5/parse5.mjs +7822 -0
  105. package/vendor/parse5.lock.json +23 -0
  106. package/web/dist/assets/{index-DS-WST1E.js → index-CjsTf5gj.js} +58 -52
  107. package/web/dist/assets/index-DnbLtHhG.css +1 -0
  108. package/web/dist/index.html +2 -2
  109. package/web/dist/assets/index-BCAEKiOG.css +0 -1
package/src/xml-tree.js CHANGED
@@ -1,4 +1,4 @@
1
- import { decodeEntities } from "./xml.js";
1
+ import { CapError, MAX_ATTRS_PER_TAG, decodeEntities, tooDeep } from "./xml.js";
2
2
 
3
3
  // A lossless element tree for one OOXML part: every node keeps its raw source text, so serialize(parse(x)) === x exactly and a builder
4
4
  // can rewrite a few elements while every other byte stays as the writer left it. Refuses DTDs and entity declarations; depth 256.
@@ -67,7 +67,7 @@ export function parse(xml) {
67
67
  const el = add({ t: "el", name: m[1], open: tag, close: "", kids: [] });
68
68
  if (!self) {
69
69
  stack.push(el);
70
- if (stack.length - 1 > MAX_DEPTH) throw new Error("XML nesting too deep");
70
+ if (stack.length - 1 > MAX_DEPTH) throw tooDeep();
71
71
  }
72
72
  }
73
73
  }
@@ -96,7 +96,10 @@ export function attrs(el) {
96
96
  const re = /\s([^\s=/>]+)\s*=\s*("([^"]*)"|'([^']*)')/g;
97
97
  const body = el.open.slice(1 + el.name.length);
98
98
  let m;
99
- while ((m = re.exec(body))) out.push([m[1], decodeEntities(m[3] ?? m[4])]);
99
+ while ((m = re.exec(body))) {
100
+ if (out.length >= MAX_ATTRS_PER_TAG) throw new CapError(`a tag in this file has more than ${MAX_ATTRS_PER_TAG} attributes, more than Onionskin can read. The file may be damaged, or made to stall programs that read it: if Word, Excel or PowerPoint opens it, save a copy there and open the copy`, "attributes per tag");
101
+ out.push([m[1], decodeEntities(m[3] ?? m[4])]);
102
+ }
100
103
  return out;
101
104
  }
102
105
  export const attr = (el, name) => attrs(el).find((a) => a[0] === name)?.[1];
package/src/xml.js CHANGED
@@ -2,41 +2,74 @@
2
2
  // Names are kept as written (prefix:local); Word, python-docx and the engine all use the conventional prefixes.
3
3
 
4
4
  const MAX_DEPTH = 256;
5
+ // Attributes one tag may carry. A real OOXML tag has a few; a part's root element carries its namespace declarations (Word writes about
6
+ // 40). Past this the tag is refused: one run with 3 million attributes (7.3 MB) took 2.4 s to read.
7
+ export const MAX_ATTRS_PER_TAG = 512;
5
8
  const ENTITIES = { amp: "&", lt: "<", gt: ">", quot: '"', apos: "'" };
6
9
 
7
10
  export function decodeEntities(s) {
8
11
  if (!s.includes("&")) return s;
9
- let out = "";
12
+ const parts = []; // decoded pieces, joined once: text that is not an entity is copied as one slice, never per "&"
13
+ let last = 0; // s[last..] is not copied yet
10
14
  let i = 0;
11
- while (i < s.length) {
12
- const amp = s.indexOf("&", i);
13
- if (amp < 0) {
14
- out += s.slice(i);
15
- break;
16
- }
17
- out += s.slice(i, amp);
18
- const semi = s.indexOf(";", amp);
19
- if (semi < 0 || semi - amp > 10) {
20
- out += "&";
21
- i = amp + 1;
22
- continue;
15
+ for (let amp = s.indexOf("&"); amp >= 0; amp = s.indexOf("&", i)) {
16
+ i = amp + 1;
17
+ // a name is at most 9 characters, so look for its ";" in the next 10 only: an unbounded search per "&" made a long run of "&" quadratic
18
+ let semi = -1;
19
+ for (let j = amp + 1, lim = Math.min(s.length, amp + 11); j < lim; j++) {
20
+ if (s.charCodeAt(j) === 59) {
21
+ semi = j;
22
+ break;
23
+ }
23
24
  }
25
+ if (semi < 0) continue; // a literal "&"
24
26
  const name = s.slice(amp + 1, semi);
25
27
  let rep;
26
- if (name in ENTITIES) rep = ENTITIES[name];
28
+ if (Object.hasOwn(ENTITIES, name)) rep = ENTITIES[name]; // own names only: "&toString;" is not an entity
27
29
  else if (name[0] === "#") {
28
30
  const code = name[1] === "x" || name[1] === "X" ? parseInt(name.slice(2), 16) : parseInt(name.slice(1), 10);
29
31
  if (Number.isInteger(code) && code > 0 && code <= 0x10ffff) rep = String.fromCodePoint(code);
30
32
  }
31
- if (rep === undefined) {
32
- out += "&";
33
- i = amp + 1;
34
- } else {
35
- out += rep;
36
- i = semi + 1;
37
- }
33
+ if (rep === undefined) continue; // unknown name: the "&" stays, the name is read as text
34
+ parts.push(s.slice(last, amp), rep);
35
+ last = i = semi + 1;
38
36
  }
39
- return out;
37
+ if (!parts.length) return s;
38
+ parts.push(s.slice(last));
39
+ return parts.join("");
40
+ }
41
+
42
+ /**
43
+ * A document too large or too dense for Onionskin to read within its limits. The message is plain words a person can act on; the server
44
+ * answers it with its own status and message, never as an internal error. `limit` names the cap (for tests and logs).
45
+ */
46
+ export class CapError extends Error {
47
+ constructor(message, limit) {
48
+ super(message);
49
+ this.limit = limit;
50
+ }
51
+ }
52
+
53
+ /**
54
+ * What one reading of a document may cost, shared by every part a reader walks (pass the same object to each walkXml call): XML elements,
55
+ * attributes, characters of text and characters of XML walked, counted down as the walk goes. `what` names the document in the message ("document", "deck",
56
+ * "workbook"). Past a limit, walkXml throws a CapError.
57
+ */
58
+ export function readBudget({ elements, attrs, text, chars = MAX_READ_CHARS, what = "document", split = "smaller documents" }) {
59
+ return { elements, attrs, text, chars, caps: { elements, attrs, text, chars }, what, split };
60
+ }
61
+ // Characters of XML one reading may walk, over all its parts (a part of one 60 MB attribute is no element and no text, but walking four
62
+ // of them took 1.5 s). A Word document of 1,600 ordinary pages has about 18 million.
63
+ export const MAX_READ_CHARS = 32_000_000;
64
+ /** The CapError for more than `n` of `noun` in the document a budget reads. */
65
+ export const tooMany = (b, n, noun) => new CapError(`this ${b.what} has more than ${n.toLocaleString("en-US")} ${noun}, more than Onionskin can read. Split it into ${b.split} and open those`, noun);
66
+
67
+ /** The CapError for XML nested deeper than MAX_DEPTH (a limit like the others: answered in plain words, never as an internal error). */
68
+ export const tooDeep = () => new CapError(`this file nests XML more than ${MAX_DEPTH} levels deep, more than Onionskin can read. The file may be damaged, or made to stall programs that read it: if Word, Excel or PowerPoint opens it, save a copy there and open the copy`, "nesting depth");
69
+ /** The first `ch` in src[from..end), or -1. Bounded by the tag: indexOf would scan the rest of the part for every attribute (quadratic). */
70
+ function findIn(src, ch, from, end) {
71
+ for (let k = from; k < end; k++) if (src[k] === ch) return k;
72
+ return -1;
40
73
  }
41
74
 
42
75
  function parseTag(src, start, end) {
@@ -50,36 +83,46 @@ function parseTag(src, start, end) {
50
83
  while (i < end && !/\s/.test(src[i])) i++;
51
84
  const name = src.slice(start, i);
52
85
  const attrs = {};
86
+ let count = 0;
53
87
  while (i < end) {
54
88
  while (i < end && /\s/.test(src[i])) i++;
55
89
  if (i >= end) break;
56
- const eq = src.indexOf("=", i);
57
- if (eq < 0 || eq >= end) break;
90
+ const eq = findIn(src, "=", i, end);
91
+ if (eq < 0) break;
58
92
  const key = src.slice(i, eq).trim();
59
93
  i = eq + 1;
60
94
  while (i < end && /\s/.test(src[i])) i++;
61
95
  const q = src[i];
62
96
  if (q !== '"' && q !== "'") break;
63
- const close = src.indexOf(q, i + 1);
64
- if (close < 0 || close >= end) break;
97
+ const close = findIn(src, q, i + 1, end);
98
+ if (close < 0) break;
99
+ if (++count > MAX_ATTRS_PER_TAG) throw new CapError(`a tag in this file has more than ${MAX_ATTRS_PER_TAG} attributes, more than Onionskin can read. The file may be damaged, or made to stall programs that read it: if Word, Excel or PowerPoint opens it, save a copy there and open the copy`, "attributes per tag");
65
100
  attrs[key] = decodeEntities(src.slice(i + 1, close));
66
101
  i = close + 1;
67
102
  }
68
- return { name, attrs, selfClosing };
103
+ return { name, attrs, selfClosing, count };
69
104
  }
70
105
 
71
- /** Calls onOpen(name, attrs), onText(text), onClose(name) in document order. Throws on DTDs and excess depth. */
72
- export function walkXml(xml, { onOpen, onClose, onText }) {
106
+ /**
107
+ * Calls onOpen(name, attrs), onText(text), onClose(name) in document order. Throws on DTDs, excess depth and a closing tag with nothing
108
+ * open, and a CapError past MAX_ATTRS_PER_TAG or past `budget` (readBudget: counted down across every part of one reading).
109
+ */
110
+ export function walkXml(xml, { onOpen, onClose, onText }, budget = null) {
73
111
  let i = 0;
74
112
  let depth = 0;
75
113
  const n = xml.length;
114
+ if (budget && (budget.chars -= n) < 0) throw tooMany(budget, budget.caps.chars, "characters of XML");
115
+ const text = (t) => {
116
+ if (budget && (budget.text -= t.length) < 0) throw tooMany(budget, budget.caps.text, "characters of text");
117
+ onText(t);
118
+ };
76
119
  while (i < n) {
77
120
  const lt = xml.indexOf("<", i);
78
121
  if (lt < 0) {
79
- if (onText && i < n) onText(decodeEntities(xml.slice(i)));
122
+ if (onText && i < n) text(decodeEntities(xml.slice(i)));
80
123
  break;
81
124
  }
82
- if (lt > i && onText) onText(decodeEntities(xml.slice(i, lt)));
125
+ if (lt > i && onText) text(decodeEntities(xml.slice(i, lt)));
83
126
  if (xml.startsWith("<!--", lt)) {
84
127
  const e = xml.indexOf("-->", lt + 4);
85
128
  if (e < 0) throw new Error("unterminated comment");
@@ -87,7 +130,7 @@ export function walkXml(xml, { onOpen, onClose, onText }) {
87
130
  } else if (xml.startsWith("<![CDATA[", lt)) {
88
131
  const e = xml.indexOf("]]>", lt + 9);
89
132
  if (e < 0) throw new Error("unterminated CDATA");
90
- if (onText) onText(xml.slice(lt + 9, e));
133
+ if (onText) text(xml.slice(lt + 9, e));
91
134
  i = e + 3;
92
135
  } else if (xml.startsWith("<?", lt)) {
93
136
  const e = xml.indexOf("?>", lt + 2);
@@ -109,16 +152,21 @@ export function walkXml(xml, { onOpen, onClose, onText }) {
109
152
  }
110
153
  if (j >= n) throw new Error("unterminated tag");
111
154
  if (xml[lt + 1] === "/") {
112
- depth--;
155
+ // a closing tag with nothing open would let the depth limit drift: callers keep a stack of open names and scan it per element
156
+ if (--depth < 0) throw new Error("closing tag without an open element");
113
157
  if (onClose) onClose(xml.slice(lt + 2, j).trim());
114
158
  } else {
115
159
  const tag = parseTag(xml, lt + 1, j);
160
+ if (budget) {
161
+ if (--budget.elements < 0) throw tooMany(budget, budget.caps.elements, "XML elements");
162
+ if ((budget.attrs -= tag.count) < 0) throw tooMany(budget, budget.caps.attrs, "XML attributes");
163
+ }
116
164
  if (onOpen) onOpen(tag.name, tag.attrs);
117
165
  if (tag.selfClosing) {
118
166
  if (onClose) onClose(tag.name);
119
167
  } else {
120
168
  depth++;
121
- if (depth > MAX_DEPTH) throw new Error("XML nesting too deep");
169
+ if (depth > MAX_DEPTH) throw tooDeep();
122
170
  }
123
171
  }
124
172
  i = j + 1;
package/src/zip-write.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import zlib from "node:zlib";
2
- import { openZip, ZipError } from "./zip.js";
2
+ import { gated } from "./stop-gate.js";
3
+ import { crc32, openZip, ZipError } from "./zip.js";
3
4
 
4
5
  // Rewrites one entry of a ZIP held in memory and copies every other entry as it is: same order, same compressed bytes, same CRC, same
5
6
  // central-directory fields (times, attributes, extra fields, comments). The local headers are written fresh from the central-directory
@@ -17,8 +18,8 @@ export function replaceEntry(bytes, name, data) {
17
18
  const buf = zip.buf;
18
19
  if (!zip.entries.has(name)) throw new ZipError(`no entry ${JSON.stringify(name)} to replace`);
19
20
  const newData = Buffer.from(data);
20
- const newComp = zlib.deflateRawSync(newData);
21
- const newCrc = zlib.crc32(newData) >>> 0;
21
+ const newComp = gated(() => zlib.deflateRawSync(newData)); // never stopped inside zlib (src/stop-gate.js)
22
+ const newCrc = crc32(newData);
22
23
  const parts = [];
23
24
  const central = [];
24
25
  let offset = 0;
@@ -37,9 +38,11 @@ export function replaceEntry(bytes, name, data) {
37
38
  const crc = mine ? newCrc : e.crc;
38
39
  const size = mine ? newData.length : e.size;
39
40
  const flags = e.flags & ~0x8; // sizes are in the local header now: no data descriptor
41
+ if (offset > 0xfffffffe || comp.length > 0xfffffffe || size > 0xfffffffe) throw new ZipError("the archive would need ZIP64");
42
+ const verNeeded = method === 8 ? Math.max(20, record.readUInt16LE(6)) : record.readUInt16LE(6);
40
43
  const lh = Buffer.alloc(30);
41
44
  lh.writeUInt32LE(0x04034b50, 0);
42
- lh.writeUInt16LE(record.readUInt16LE(6), 4); // version needed
45
+ lh.writeUInt16LE(verNeeded, 4); // version needed
43
46
  lh.writeUInt16LE(flags, 6);
44
47
  lh.writeUInt16LE(method, 8);
45
48
  lh.writeUInt16LE(record.readUInt16LE(12), 10); // time
@@ -50,6 +53,7 @@ export function replaceEntry(bytes, name, data) {
50
53
  lh.writeUInt16LE(nameLen, 26);
51
54
  lh.writeUInt16LE(0, 28); // no local extra field
52
55
  parts.push(lh, nameBuf, comp);
56
+ record.writeUInt16LE(verNeeded, 6);
53
57
  record.writeUInt16LE(flags, 8);
54
58
  record.writeUInt16LE(method, 10);
55
59
  record.writeUInt32LE(crc, 16);
@@ -58,8 +62,10 @@ export function replaceEntry(bytes, name, data) {
58
62
  record.writeUInt32LE(offset, 42);
59
63
  central.push(record);
60
64
  offset += 30 + nameLen + comp.length;
65
+ if (offset > 0xfffffffe) throw new ZipError("the archive would need ZIP64");
61
66
  }
62
67
  const cd = Buffer.concat(central);
68
+ if (offset + cd.length > 0xfffffffe) throw new ZipError("the archive would need ZIP64");
63
69
  const commentLen = buf.readUInt16LE(zip.eocd + 20);
64
70
  const end = Buffer.alloc(22);
65
71
  sig(0x06054b50).copy(end, 0);
@@ -69,6 +75,5 @@ export function replaceEntry(bytes, name, data) {
69
75
  end.writeUInt32LE(offset, 16);
70
76
  end.writeUInt16LE(commentLen, 20);
71
77
  const comment = buf.subarray(zip.eocd + 22, zip.eocd + 22 + commentLen);
72
- if (offset + cd.length > 0xfffffffe) throw new ZipError("the archive would need ZIP64");
73
78
  return Buffer.concat([...parts, cd, end, comment]);
74
79
  }
package/src/zip.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import zlib from "node:zlib";
2
+ import { gated } from "./stop-gate.js";
2
3
 
3
4
  const MAX_ENTRIES = 20000;
4
5
  const MAX_ENTRY_BYTES = 512 * 1024 * 1024;
@@ -7,29 +8,78 @@ const MAX_XML_BYTES = 64 * 1024 * 1024;
7
8
  export const MAX_TOTAL_EXPANSION = 256 * 1024 * 1024;
8
9
 
9
10
  export class ZipError extends Error {}
11
+ const MB = (n) => (n >= 1024 * 1024 ? `${Math.round(n / 1024 / 1024)} MB` : `${n.toLocaleString("en-US")} bytes`);
12
+ const SPLIT = "Split it into smaller files, or save it again in Word, Excel or PowerPoint, and open that";
13
+
14
+ const CRC_TABLE = (() => {
15
+ const t = new Uint32Array(256);
16
+ for (let i = 0; i < 256; i++) {
17
+ let c = i;
18
+ for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
19
+ t[i] = c >>> 0;
20
+ }
21
+ return t;
22
+ })();
23
+ export function crc32Pure(buf) {
24
+ let c = 0xffffffff;
25
+ for (let i = 0; i < buf.length; i++) c = CRC_TABLE[(c ^ buf[i]) & 0xff] ^ (c >>> 8);
26
+ return (c ^ 0xffffffff) >>> 0;
27
+ }
28
+ export function crc32(buf) {
29
+ return typeof zlib.crc32 === "function" ? zlib.crc32(buf) >>> 0 : crc32Pure(buf);
30
+ }
31
+
32
+ function validEocd(buf, i) {
33
+ const count = buf.readUInt16LE(i + 10);
34
+ const cdSize = buf.readUInt32LE(i + 12);
35
+ const cdOffset = buf.readUInt32LE(i + 16);
36
+ if (count === 0xffff || cdSize === 0xffffffff || cdOffset === 0xffffffff) return false;
37
+ if (count > MAX_ENTRIES) return false;
38
+ if (cdOffset + cdSize !== i) return false;
39
+ let p = cdOffset;
40
+ for (let k = 0; k < count; k++) {
41
+ if (p + 46 > i || buf.readUInt32LE(p) !== 0x02014b50) return false;
42
+ const nameLen = buf.readUInt16LE(p + 28);
43
+ const extraLen = buf.readUInt16LE(p + 30);
44
+ const commentLen = buf.readUInt16LE(p + 32);
45
+ if (p + 46 + nameLen + extraLen + commentLen > i) return false;
46
+ p += 46 + nameLen + extraLen + commentLen;
47
+ }
48
+ return p === i;
49
+ }
10
50
 
11
51
  /** Reads the central directory of a ZIP held in memory. No ZIP64 (files over 4 GB are refused). */
12
52
  export function openZip(buf, { budget = MAX_TOTAL_EXPANSION } = {}) {
13
53
  if (!Buffer.isBuffer(buf)) buf = Buffer.from(buf);
14
54
  if (buf.length < 22 || buf.readUInt32LE(0) !== 0x04034b50) throw new ZipError("not a ZIP file");
15
- let eocd = -1;
55
+ let exactValid = -1;
56
+ let exactInvalid = -1;
57
+ let fallback = -1;
16
58
  for (let i = buf.length - 22; i >= Math.max(0, buf.length - 22 - 65535); i--) {
17
- if (buf.readUInt32LE(i) === 0x06054b50) {
18
- eocd = i;
19
- break;
59
+ if (buf.readUInt32LE(i) !== 0x06054b50) continue;
60
+ const end = i + 22 + buf.readUInt16LE(i + 20);
61
+ if (end === buf.length) {
62
+ if (validEocd(buf, i)) {
63
+ exactValid = i;
64
+ break;
65
+ }
66
+ if (exactInvalid < 0) exactInvalid = i;
67
+ } else if (end < buf.length && validEocd(buf, i)) {
68
+ fallback = i;
20
69
  }
21
70
  }
71
+ const eocd = exactValid >= 0 ? exactValid : fallback >= 0 ? fallback : exactInvalid;
22
72
  if (eocd < 0) throw new ZipError("ZIP end record not found");
23
73
  const count = buf.readUInt16LE(eocd + 10);
24
74
  const cdSize = buf.readUInt32LE(eocd + 12);
25
75
  const cdOffset = buf.readUInt32LE(eocd + 16);
26
76
  if (count === 0xffff || cdSize === 0xffffffff || cdOffset === 0xffffffff) throw new ZipError("ZIP64 is not supported");
27
- if (count > MAX_ENTRIES) throw new ZipError("too many ZIP entries");
77
+ if (count > MAX_ENTRIES) throw new ZipError(`this file has more than ${MAX_ENTRIES.toLocaleString("en-US")} parts, more than Onionskin can read. ${SPLIT}`);
28
78
  if (cdOffset + cdSize > buf.length) throw new ZipError("ZIP central directory out of range");
29
79
  const entries = new Map();
30
80
  let p = cdOffset;
31
81
  for (let k = 0; k < count; k++) {
32
- if (p + 46 > buf.length || buf.readUInt32LE(p) !== 0x02014b50) throw new ZipError("bad ZIP central directory");
82
+ if (p + 46 > cdOffset + cdSize || buf.readUInt32LE(p) !== 0x02014b50) throw new ZipError("bad ZIP central directory");
33
83
  const flags = buf.readUInt16LE(p + 8);
34
84
  const method = buf.readUInt16LE(p + 10);
35
85
  const crc = buf.readUInt32LE(p + 16);
@@ -39,17 +89,19 @@ export function openZip(buf, { budget = MAX_TOTAL_EXPANSION } = {}) {
39
89
  const extraLen = buf.readUInt16LE(p + 30);
40
90
  const commentLen = buf.readUInt16LE(p + 32);
41
91
  const localOffset = buf.readUInt32LE(p + 42);
92
+ if (p + 46 + nameLen + extraLen + commentLen > cdOffset + cdSize) throw new ZipError("bad ZIP central directory");
93
+ if (compSize === 0xffffffff || size === 0xffffffff || localOffset === 0xffffffff) throw new ZipError("ZIP64 is not supported");
42
94
  const name = buf.toString("utf8", p + 46, p + 46 + nameLen);
43
95
  if (name.startsWith("/") || name.split("/").includes("..") || name.includes("\\") || name.includes("\0")) throw new ZipError(`unsafe ZIP entry name: ${JSON.stringify(name)}`);
44
96
  if (entries.has(name)) throw new ZipError(`duplicate ZIP entry: ${JSON.stringify(name)}`);
45
- if (size > MAX_ENTRY_BYTES) throw new ZipError(`ZIP entry too large: ${JSON.stringify(name)}`);
97
+ if (size > MAX_ENTRY_BYTES) throw new ZipError(`a part of this file (${JSON.stringify(name)}) is larger than ${MB(MAX_ENTRY_BYTES)} unpacked, more than Onionskin can read. ${SPLIT}`);
46
98
  entries.set(name, { name, flags, method, crc, compSize, size, localOffset, cdOffset: p }); // cdOffset: where this entry's central record starts (zip-write.js copies it)
47
99
  p += 46 + nameLen + extraLen + commentLen;
48
100
  }
49
101
  let used = 0;
50
102
  const spend = (name, n) => {
51
103
  used += n;
52
- if (used > budget) throw new ZipError(`ZIP expansion budget exceeded while reading ${JSON.stringify(name)} (more than ${Math.round(budget / 1024 / 1024)} MB in total)`);
104
+ if (used > budget) throw new ZipError(`this file unpacks to more than ${MB(budget)} in the parts Onionskin reads (at ${JSON.stringify(name)}), more than Onionskin can read. ${SPLIT}`);
53
105
  };
54
106
  /** The entry's bytes as stored in the archive (still compressed); null when there is no such entry. Spends no budget: nothing is inflated. */
55
107
  function raw(name) {
@@ -64,25 +116,28 @@ export function openZip(buf, { budget = MAX_TOTAL_EXPANSION } = {}) {
64
116
  function read(name, cap = MAX_XML_BYTES) {
65
117
  const e = entries.get(name);
66
118
  if (!e) return null;
119
+ if (e.flags & 0x1) throw new ZipError(`encrypted ZIP entry ${JSON.stringify(name)}`);
67
120
  cap = Math.min(cap, MAX_ENTRY_BYTES);
68
121
  const data = raw(name);
122
+ let out;
69
123
  if (e.method === 0) {
70
124
  // a stored entry is bounded by the same per-entry cap as an inflated one
71
- if (data.length > cap) throw new ZipError(`ZIP entry ${JSON.stringify(name)} is larger than the ${Math.round(cap / 1024 / 1024)} MB limit for a part that is read`);
72
- spend(name, data.length);
73
- return Buffer.from(data);
74
- }
75
- if (e.method === 8) {
76
- let out;
125
+ if (data.length > cap) throw new ZipError(`a part of this file (${JSON.stringify(name)}) is larger than ${MB(cap)} unpacked, more than Onionskin can read. ${SPLIT}`);
126
+ out = Buffer.from(data);
127
+ } else if (e.method === 8) {
77
128
  try {
78
- out = zlib.inflateRawSync(data, { maxOutputLength: cap });
129
+ out = gated(() => zlib.inflateRawSync(data, { maxOutputLength: cap })); // never stopped inside zlib (src/stop-gate.js)
79
130
  } catch (err) {
131
+ if (err?.code === "ERR_BUFFER_TOO_LARGE") throw new ZipError(`a part of this file (${JSON.stringify(name)}) is larger than ${MB(cap)} unpacked, more than Onionskin can read. ${SPLIT}`);
80
132
  throw new ZipError(`cannot inflate ${JSON.stringify(name)}: ${err.message}`);
81
133
  }
82
- spend(name, out.length);
83
- return out;
134
+ } else {
135
+ throw new ZipError(`unsupported ZIP method ${e.method} for ${JSON.stringify(name)}`);
84
136
  }
85
- throw new ZipError(`unsupported ZIP method ${e.method} for ${JSON.stringify(name)}`);
137
+ if (out.length !== e.size) throw new ZipError(`size mismatch for ${JSON.stringify(name)}`);
138
+ if (crc32(out) !== e.crc) throw new ZipError(`CRC mismatch for ${JSON.stringify(name)} (does not match its stored checksum)`);
139
+ spend(name, out.length);
140
+ return out;
86
141
  }
87
142
  return { names: [...entries.keys()], entries, read, raw, buf, eocd, readText: (name, cap) => read(name, cap)?.toString("utf8") ?? null };
88
143
  }
@@ -0,0 +1,32 @@
1
+ Copyright (c) 2013-2019 Ivan Nikulin (ifaaan@gmail.com, https://github.com/inikulin)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
20
+
21
+ ---- entities ----
22
+ Copyright (c) Felix Böhm
23
+ All rights reserved.
24
+
25
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
26
+
27
+ Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
28
+
29
+ Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
30
+
31
+ THIS IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS,
32
+ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.