@333eco/origin-verify 0.1.1 → 0.1.2

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 (2) hide show
  1. package/media.mjs +18 -8
  2. package/package.json +1 -1
package/media.mjs CHANGED
@@ -7,14 +7,23 @@ const SIG = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
7
7
  const MAX_PIXELS = 100_000_000;
8
8
  export const isPng = (b) => SIG.every((x, i) => b[i] === x);
9
9
 
10
- async function inflate(data) {
11
- const stream = new Blob([data]).stream().pipeThrough(new DecompressionStream("deflate"));
12
- return new Uint8Array(await new Response(stream).arrayBuffer());
10
+ /** Stops at `limit`: a small file can inflate to gigabytes, and the header already says how big a real one can be. */
11
+ async function inflate(data, limit) {
12
+ const reader = new Blob([data]).stream().pipeThrough(new DecompressionStream("deflate")).getReader(), parts = [];
13
+ let total = 0;
14
+ for (;;) {
15
+ const { done, value } = await reader.read();
16
+ if (done) break;
17
+ total += value.length;
18
+ if (total > limit) { await reader.cancel().catch(() => {}); throw new Error("a PNG that inflates past what its header allows"); }
19
+ parts.push(value);
20
+ }
21
+ return concat(...parts);
13
22
  }
14
23
  const ADAM7 = [[0, 0, 8, 8], [4, 0, 8, 8], [0, 4, 4, 8], [2, 0, 4, 4], [0, 2, 2, 4], [1, 0, 2, 2], [0, 1, 1, 2]];
15
24
 
16
25
  /** Returns { width, height, rgb, alpha } as 8-bit rasters. `rgb` is null for 16-bit gray, which PIL clips rather than reduces, so no hash could match it. */
17
- export async function decodePng(b) {
26
+ export async function decodePng(b, { maxPixels = MAX_PIXELS } = {}) {
18
27
  if (!isPng(b)) throw new Error("not a PNG");
19
28
  const dv = new DataView(b.buffer, b.byteOffset, b.length);
20
29
  let i = 8, width, height, depth, color, interlace, palette = null, trns = null;
@@ -29,11 +38,12 @@ export async function decodePng(b) {
29
38
  else if (type === "IEND") break;
30
39
  i += 12 + len;
31
40
  }
32
- if (!width || !height || !idat.length || width * height > MAX_PIXELS) throw new Error("a PNG this reader will not open");
41
+ if (!width || !height || !idat.length || width * height > maxPixels) throw new Error("a PNG this reader will not open");
33
42
  const channels = { 0: 1, 2: 3, 3: 1, 4: 2, 6: 4 }[color];
34
43
  if (!channels || (color === 3 && !palette)) throw new Error("a PNG with an unknown color type");
35
44
  const bitsPerPixel = channels * depth, bpp = Math.max(1, bitsPerPixel >> 3);
36
- const raw = await inflate(concat(...idat));
45
+ // The most a real image of this size can need; interlacing repeats a line's filter byte in up to seven passes, so a generous bound covers it.
46
+ const raw = await inflate(concat(...idat), (Math.ceil((width * bitsPerPixel) / 8) + 1) * height * (interlace === 1 ? 2 : 1) + 1024);
37
47
  const rgb = new Uint8Array(width * height * 3), alpha = new Uint8Array(width * height).fill(255);
38
48
  let at = 0;
39
49
  const pass = (w, h, put) => {
@@ -136,13 +146,13 @@ export async function canvasDecoder(bytes, mime) {
136
146
  * The hashes a claim carries. `decode(bytes)` is the caller's own decoder for other image types, returning { width, height, rgb, alpha }: it may give `phash`, never `pixels`.
137
147
  * A file that is not a picture, or that nothing can decode, simply has neither: it can still be signed, and found only by its exact bytes.
138
148
  */
139
- export async function hashFile(bytes, { decode } = {}) {
149
+ export async function hashFile(bytes, { decode, maxPixels } = {}) {
140
150
  const b = new Uint8Array(bytes);
141
151
  const sha = new Uint8Array(await crypto.subtle.digest("SHA-256", b));
142
152
  const out = { sha256: hex(sha), bytes: b.length };
143
153
  try {
144
154
  if (isPng(b)) {
145
- const png = await decodePng(b);
155
+ const png = await decodePng(b, maxPixels ? { maxPixels } : {});
146
156
  if (png.rgb) { out.pixels = hex(new Uint8Array(await crypto.subtle.digest("SHA-256", png.rgb))); out.phash = phashOf(png); }
147
157
  } else if (decode) {
148
158
  const r = await decode(b);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@333eco/origin-verify",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Checks a B-Origin record with no help from its operator: signatures, its place in the public log, witness cosignatures, RFC 3161 tokens, OpenTimestamps proofs against Bitcoin, and a signer's reveal. Browser or Node, no runtime dependencies.",
5
5
  "license": "CC0-1.0",
6
6
  "author": "Thon Ly",