File size: 17,120 Bytes
dac2ca2
 
6d9b255
52361de
 
da87c58
 
 
52361de
e6e90f2
52361de
 
 
 
 
dac2ca2
52361de
9ceac12
52361de
dac2ca2
52361de
 
 
 
 
7b2f60d
 
 
dac2ca2
7b2f60d
 
 
 
 
dac2ca2
7b2f60d
52361de
 
7b2f60d
 
52361de
 
 
 
 
 
 
 
 
 
7b2f60d
 
 
 
dac2ca2
7b2f60d
52361de
 
7b2f60d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52361de
 
 
 
7b2f60d
 
 
 
 
 
 
 
 
 
 
 
dac2ca2
7b2f60d
 
 
52361de
 
 
 
 
 
7b2f60d
 
dac2ca2
 
 
 
52361de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17c28da
 
 
 
 
 
 
 
45ae844
 
dac2ca2
52361de
dac2ca2
 
 
94685f4
52361de
94685f4
 
 
 
39e321e
56b51ea
39e321e
dac2ca2
56b51ea
 
39e321e
dac2ca2
39e321e
 
52361de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2e9cf6
5418715
 
b2e9cf6
 
 
 
 
 
 
 
dac2ca2
b2e9cf6
 
 
 
dac2ca2
52361de
e6e90f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3ce2d9e
39e321e
681df39
dac2ca2
9cb654f
 
 
dac2ca2
 
 
3ce2d9e
dac2ca2
6b67c96
3ce2d9e
 
 
 
 
 
 
 
 
 
 
681df39
9cb654f
39e321e
681df39
39e321e
 
 
 
dac2ca2
39e321e
9cb654f
 
3ce2d9e
 
 
 
9cb654f
 
dac2ca2
9cb654f
3ce2d9e
 
9cb654f
 
3ce2d9e
dac2ca2
3ce2d9e
dac2ca2
 
 
3ce2d9e
 
5418715
 
3ce2d9e
 
 
 
5418715
3ce2d9e
 
 
dac2ca2
3ce2d9e
52361de
 
 
 
 
 
 
 
e6e90f2
3ce2d9e
169f8e1
52361de
dac2ca2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
(/** @param {import('../../../typings/reader-security-strict').SecurityRoot} root */
function (root) {
  "use strict";
  const LIMITS = Object.freeze({
    documentBytes: 512 * 1024 * 1024,
    // PDF is distinct from DjVu and other document formats. Range reads remain
    // preferred for large files to avoid downloading the entire body at once.
    pdfDocumentBytes: 2 * 1024 * 1024 * 1024,
    manifestBytes: 8 * 1024 * 1024,
    imagePageBytes: 64 * 1024 * 1024, // Bucket proxy's READER_RESOURCE_MAX_BYTES.
    pdfPages: 100000,
    chapters: 10000,
    chapterBytes: 8 * 1024 * 1024,
    chapterTotalBytes: 512 * 1024 * 1024
  });
  /** @param {string} code */
  function error(code) {
    return Object.assign(new Error(code), { code });
  }
  /** @template {Response} T @param {T} response @param {number} limit @returns {T} */
  function assertResponseSize(response, limit) {
    const length = Number(response?.headers?.get?.("content-length"));
    if (Number.isFinite(length) && length > limit) throw error("READER_RESOURCE_LIMIT");
    return response;
  }
  function createByteBudget(limit = LIMITS.chapterTotalBytes) {
    if (!Number.isSafeInteger(limit) || limit < 0) throw new RangeError("Invalid byte budget");
    let used = 0;
    /** @param {number} bytes */
    function reserve(bytes) {
      if (!Number.isSafeInteger(bytes) || bytes < 0) throw new RangeError("Invalid byte count");
      if (bytes > limit - used) throw error("READER_RESOURCE_LIMIT");
      used += bytes;
    }
    /** @param {number} bytes */
    function release(bytes) {
      if (!Number.isSafeInteger(bytes) || bytes < 0 || bytes > used)
        throw new RangeError("Invalid byte release");
      used -= bytes;
    }
    return Object.freeze({
      reserve,
      release,
      get used() {
        return used;
      },
      get remaining() {
        return limit - used;
      }
    });
  }

  // Failed reads release their reservations; successful reads retain them until
  // the caller releases discarded content. Share one budget across a book.
  /** @param {Response} response @param {number} [limit] @param {import('../../../typings/reader-security-strict').ByteBudget} [budget] */
  async function readBytes(response, limit = LIMITS.documentBytes, budget) {
    let reader,
      reserved = 0;
    try {
      assertResponseSize(response, limit);
      if (!response.body?.getReader) {
        const bytes = new Uint8Array(await response.arrayBuffer());
        if (bytes.byteLength > limit) throw error("READER_RESOURCE_LIMIT");
        budget?.reserve(bytes.byteLength);
        return bytes;
      }
      reader = response.body.getReader();
      const chunks = [];
      let length = 0;
      while (true) {
        const { value, done } = await reader.read();
        if (done) break;
        if (!value?.byteLength) continue;
        length += value.byteLength;
        if (length > limit) throw error("READER_RESOURCE_LIMIT");
        budget?.reserve(value.byteLength);
        reserved += value.byteLength;
        chunks.push(value);
      }
      const output = new Uint8Array(length);
      let offset = 0;
      for (const chunk of chunks) {
        output.set(chunk, offset);
        offset += chunk.byteLength;
      }
      return output;
    } catch (reason) {
      if (budget && reserved) budget.release(reserved);
      try {
        if (reader) await reader.cancel(reason);
        else await response.body?.cancel?.(reason);
      } catch (_) {}
      throw reason;
    } finally {
      reader?.releaseLock?.();
    }
  }
  /** @param {Response} response @param {number} [limit] @param {string} [encoding] @param {import('../../../typings/reader-security-strict').ByteBudget} [budget] */
  async function readText(response, limit, encoding = "utf-8", budget) {
    const decoder = new TextDecoder(encoding);
    const bytes = await readBytes(response, limit, budget);
    try {
      return decoder.decode(bytes);
    } catch (reason) {
      budget?.release(bytes.byteLength);
      throw reason;
    }
  }

  /** @param {unknown} input @returns {import('../../../typings/reader-security-strict').ValidatedChapterManifest} */
  function validateChapterManifest(input) {
    // The candidate is untrusted: the checks below determine what is accepted.
    const manifest = /** @type {import('../../../typings/reader-security-strict').ChapterManifestCandidate} */ (/** @type {unknown} */ (input));
    if (
      manifest?.version !== 1 ||
      manifest?.kind !== "epub-chapters" ||
      !Array.isArray(manifest.chapters) ||
      !manifest.chapters.length
    )
      throw error("EPUB_INVALID");
    if (manifest.chapters.length > LIMITS.chapters) throw error("READER_RESOURCE_LIMIT");
    let declaredBytes = 0,
      expectedIndex = 1;
    for (const chapter of manifest.chapters) {
      if (!chapter || !Number.isInteger(chapter.index) || chapter.index !== expectedIndex)
        throw error("EPUB_INVALID");
      expectedIndex += 1;
      if (
        typeof chapter.path !== "string" ||
        !chapter.path.trim() ||
        /^[a-z][a-z0-9+.-]*:/i.test(chapter.path) ||
        chapter.path.startsWith("/") ||
        chapter.path.includes("\\") ||
        chapter.path.split("/").some((part) => part === ".." || part === "." || !part)
      )
        throw error("EPUB_INVALID");
      const rawSize = chapter.bytes ?? chapter.size;
      const size = rawSize == null ? 0 : Number(rawSize);
      if (!Number.isFinite(size) || size < 0 || size > LIMITS.chapterBytes)
        throw error("READER_RESOURCE_LIMIT");
      declaredBytes += size;
    }
    if (declaredBytes > LIMITS.chapterTotalBytes) throw error("READER_RESOURCE_LIMIT");
    if (manifest.toc !== undefined) {
      if (!Array.isArray(manifest.toc)) throw error("EPUB_INVALID");
      for (const item of manifest.toc)
        if (!item || typeof item.title !== "string" || !item.title.trim() ||
            !Number.isInteger(item.chapter) || item.chapter < 1 || item.chapter > manifest.chapters.length ||
            !Number.isInteger(item.depth) || item.depth < 0 || item.depth > 64 ||
            typeof item.fragment !== "string") throw error("EPUB_INVALID");
    }
    if (manifest.search_index?.text_profile !== undefined &&
        manifest.search_index.text_profile !== "dom-text-v1") throw error("EPUB_INVALID");
    return /** @type {import('../../../typings/reader-security-strict').ValidatedChapterManifest} */ (/** @type {unknown} */ (manifest));
  }
  /** @param {unknown} input @returns {import('../../../typings/reader-security-strict').ValidatedPdfPageManifest} */
  function validatePdfPageManifest(input) {
    const manifest = /** @type {import('../../../typings/reader-security-strict').PdfPageManifestCandidate} */ (/** @type {unknown} */ (input));
    if (!manifest || manifest.version !== 2 || manifest.kind !== "pdf-pages")
      throw error("PDF_MANIFEST_INVALID");
    if (!Number.isInteger(manifest.page_count) || manifest.page_count < 1 || manifest.pages !== undefined)
      throw error("PDF_MANIFEST_INVALID");
    const pageCount = manifest.page_count;
    if (pageCount > LIMITS.pdfPages) throw error("READER_RESOURCE_LIMIT");
    if (manifest.codec !== undefined && !["webp", "jxl", "png", "mixed"].includes(manifest.codec))
      throw error("PDF_MANIFEST_INVALID");
    if (["jxl", "png", "mixed"].includes(manifest.codec) && (!Array.isArray(manifest.images) || manifest.images.length !== pageCount ||
        manifest.images.some(ref => (typeof ref?.sha256 !== "string" || !/^[0-9a-f]{64}$/.test(ref.sha256)) ||
          !Number.isSafeInteger(ref.bytes) || ref.bytes < 1 || ref.bytes > LIMITS.chapterBytes)))
      throw error("PDF_MANIFEST_INVALID");
    if (["png", "mixed"].includes(manifest.codec) && (manifest.source_format !== "djvu" ||
        (typeof manifest.source_sha256 !== "string" || !/^[0-9a-f]{64}$/.test(manifest.source_sha256)) || manifest.codec === "mixed" &&
        manifest.images.some(ref => !["png", "jxl", "webp"].includes(ref.codec))))
      throw error("PDF_MANIFEST_INVALID");
    if (manifest.toc !== undefined) {
      if (!Array.isArray(manifest.toc) || manifest.toc.length > 2000)
        throw error("PDF_MANIFEST_INVALID");
      for (const item of manifest.toc) {
        if (
          !item ||
          typeof item.title !== "string" ||
          !item.title.trim() ||
          item.title.length > 500 ||
          !Number.isInteger(item.page) ||
          item.page < 1 ||
          item.page > pageCount ||
          !Number.isInteger(item.depth) ||
          item.depth < 0 ||
          item.depth > 32
        )
          throw error("PDF_MANIFEST_INVALID");
      }
    }
    if (manifest.ocr !== undefined) {
      if (typeof manifest.source_sha256 !== "string" || !/^[0-9a-f]{64}$/.test(manifest.source_sha256))
        throw error("PDF_MANIFEST_INVALID");
      if (!Array.isArray(manifest.ocr) || manifest.ocr.length !== pageCount)
        throw error("PDF_MANIFEST_INVALID");
      for (const [index, page] of manifest.ocr.entries()) {
        const expected = String(index + 1).padStart(6, "0"),
          root = typeof page?.o === "string" ? PDF_OCR_ROOT.exec(page.o)?.[1] || "" : "";
        if (!page || page.p !== index + 1 || !PDF_OCR_PATH.test(page.o) ||
            page.o !== `${root}/ocr/page-${expected}.json.gz` ||
            pdfSourceRoot(root) !== `objects/${manifest.source_sha256.slice(0, 2)}/${manifest.source_sha256}` ||
            (typeof page.os !== "string" || !/^[0-9a-f]{64}$/.test(page.os)) ||
            !Number.isSafeInteger(page.ob) || page.ob < 1)
          throw error("PDF_MANIFEST_INVALID");
      }
    }
    return /** @type {import('../../../typings/reader-security-strict').ValidatedPdfPageManifest} */ (/** @type {unknown} */ (manifest));
  }
  /** @param {unknown} input @returns {import('../../../typings/reader-security-strict').ValidatedImagePageManifest} */
  function validateImagePageManifest(input) {
    const manifest = /** @type {{version: number, kind: string, image_format?: unknown, page_count: number, pages: unknown[]} | null} */ (input);
    if (!manifest || manifest.version !== 1 || manifest.kind !== "image-page-stream" ||
        !Number.isInteger(manifest.page_count) || manifest.page_count < 1 ||
        !Array.isArray(manifest.pages) || manifest.pages.length !== manifest.page_count ||
        (manifest.image_format === undefined ? manifest.pages.includes(null) : manifest.image_format !== "avif"))
      throw error("IMAGE_MANIFEST_INVALID");
    if (manifest.page_count > LIMITS.pdfPages) throw error("READER_RESOURCE_LIMIT");
    if (manifest.image_format === undefined) {
      // Legacy WebP URLs were derived from the manifest root and ordinal;
      // entry metadata on that wire format has never been used.
      return { page_count: manifest.page_count };
    }
    const avifPages = manifest.pages.map((value, index) => {
      const page = /** @type {import('../../../typings/reader-security-strict').AvifImagePage | null} */ (value);
      const path = `pages/page-${String(index + 1).padStart(6, "0")}.avif`;
      if (!page || page.path !== path || typeof page.sha256 !== "string" ||
          page.sha256.length !== 64 || !/^[0-9a-f]+$/.test(page.sha256) ||
          !Number.isSafeInteger(page.bytes) || page.bytes < 1 || page.bytes > LIMITS.imagePageBytes)
        throw error("IMAGE_MANIFEST_INVALID");
      return { path, sha256: page.sha256, bytes: page.bytes };
    });
    return { page_count: manifest.page_count, avifPages };
  }
  const PDF_OCR_PATH = /^objects\/[0-9a-f]{2}\/[0-9a-f]{64}\/(?:[0-9a-f]{16}\/)?ocr\/(?:page-[0-9]{6}\.json\.gz|book-text\.json\.gz)$/;
  const PDF_OCR_PAGE_PATH = /^objects\/[0-9a-f]{2}\/[0-9a-f]{64}\/(?:[0-9a-f]{16}\/)?pages\/page-[0-9]{6}\.(?:webp|jxl|png)$/;
  const PDF_OCR_ROOT = /^(objects\/[0-9a-f]{2}\/[0-9a-f]{64}(?:\/[0-9a-f]{16})?)/;
  /** @param {unknown} path */
  function pdfSourceRoot(path) {
    return typeof path === "string" ? path.split("/").slice(0, 3).join("/") : "";
  }
  /** @param {unknown} input @returns {import('../../../typings/reader-security-strict').ValidatedPdfOcrManifest} */
  function validatePdfOcrManifest(input) {
    const manifest = /** @type {import('../../../typings/reader-security-strict').PdfOcrManifestCandidate} */ (/** @type {unknown} */ (input));
    if (!manifest || manifest.version !== 1 || manifest.kind !== "pdf-ocr" ||
        manifest.complete !== true || (typeof manifest.source_sha256 !== "string" || !/^[0-9a-f]{64}$/.test(manifest.source_sha256)) ||
         !/layout-v\d+(?:-|$)/.test(String(manifest.profile || "")) ||
        !Number.isInteger(manifest.page_count) || manifest.page_count < 1 ||
        manifest.page_count > LIMITS.pdfPages || !Array.isArray(manifest.pages) ||
        manifest.pages.length !== manifest.page_count)
      throw error("PDF_OCR_MANIFEST_INVALID");
    let root = "";
    for (const [index, page] of manifest.pages.entries()) {
      const expected = String(index + 1).padStart(6, "0");
      const pageRoot = typeof page?.o === "string" ? PDF_OCR_ROOT.exec(page.o)?.[1] || "" : "";
      if (!page || page.p !== index + 1 || !PDF_OCR_PATH.test(page.o) ||
          page.o !== `${pageRoot}/ocr/page-${expected}.json.gz` ||
          (page.w !== undefined && (typeof page.w !== "string" || !PDF_OCR_PAGE_PATH.test(page.w) ||
            !page.w.endsWith(`/pages/page-${expected}.webp`) ||
            pdfSourceRoot(page.w) !== pdfSourceRoot(pageRoot))) ||
           (page.j !== undefined && (typeof page.j !== "string" || !PDF_OCR_PAGE_PATH.test(page.j) ||
            !page.j.endsWith(`/pages/page-${expected}.jxl`) ||
             pdfSourceRoot(page.j) !== pdfSourceRoot(pageRoot))) ||
          (page.r !== undefined && (typeof page.r !== "string" || !PDF_OCR_PAGE_PATH.test(page.r) ||
            !page.r.endsWith(`/pages/page-${expected}.png`) || pdfSourceRoot(page.r) !== pdfSourceRoot(pageRoot) ||
            manifest.source_format !== "djvu" || page.w !== undefined || page.j !== undefined ||
            page.rbucket !== "vomebook/pdf-pages-v2" || (typeof page.rs !== "string" || !/^[0-9a-f]{64}$/.test(page.rs)) ||
            !Number.isSafeInteger(page.rb) || page.rb < 1)) ||
          pdfSourceRoot(pageRoot) !== `objects/${manifest.source_sha256.slice(0, 2)}/${manifest.source_sha256}` ||
          (root && pdfSourceRoot(root) !== pdfSourceRoot(pageRoot)))
        throw error("PDF_OCR_MANIFEST_INVALID");
      root = pageRoot;
    }
    if (!manifest.book_text || typeof manifest.book_text.path !== "string" ||
        !PDF_OCR_PATH.test(manifest.book_text.path) || !manifest.book_text.path.endsWith('/ocr/book-text.json.gz') ||
        pdfSourceRoot(manifest.book_text.path) !== pdfSourceRoot(root) ||
        (typeof manifest.book_text.sha256 !== "string" || !/^[0-9a-f]{64}$/.test(manifest.book_text.sha256)) ||
        !Number.isSafeInteger(manifest.book_text.bytes) || manifest.book_text.bytes < 1 ||
        (manifest.page_manifest !== undefined &&
          (!manifest.page_manifest || typeof manifest.page_manifest.path !== "string" ||
            !/^objects\/[0-9a-f]{2}\/[0-9a-f]{64}\/(?:[0-9a-f]{16}\/)?page-manifest\.json$/.test(manifest.page_manifest.path) ||
            pdfSourceRoot(manifest.page_manifest.path) !== pdfSourceRoot(root))))
      throw error("PDF_OCR_MANIFEST_INVALID");
    return /** @type {import('../../../typings/reader-security-strict').ValidatedPdfOcrManifest} */ (/** @type {unknown} */ (manifest));
  }
  /** @param {unknown} input @param {number} pageNumber @returns {import('../../../typings/reader-security-strict').ValidatedPdfOcrPage} */
  function validatePdfOcrPage(input, pageNumber) {
    const payload = /** @type {import('../../../typings/reader-security-strict').PdfOcrPageCandidate} */ (/** @type {unknown} */ (input));
    if (!payload || payload.version !== 1 || payload.kind !== "pdf-ocr-page" ||
        payload.page !== pageNumber || !Array.isArray(payload.blocks) || payload.blocks.length > 10000 ||
        !Number.isFinite(payload.width) || payload.width <= 0 ||
        !Number.isFinite(payload.height) || payload.height <= 0)
      throw error("PDF_OCR_PAGE_INVALID");
    for (const block of payload.blocks) {
      const box = block?.b;
      if (!block || typeof block.t !== "string" || block.t.length > 4096 ||
          !Array.isArray(box) || box.length !== 4 || box.some((value) => !Number.isFinite(value)) ||
          box[0] < 0 || box[1] < 0 || box[2] > 1 || box[3] > 1 || box[2] < box[0] || box[3] < box[1])
        throw error("PDF_OCR_PAGE_INVALID");
    }
    return /** @type {import('../../../typings/reader-security-strict').ValidatedPdfOcrPage} */ (/** @type {unknown} */ (payload));
  }
  root.VoiceOfMLReaderSecurity = Object.freeze({
    LIMITS,
    assertResponseSize,
    createByteBudget,
    readBytes,
    readText,
    validateChapterManifest,
    validatePdfPageManifest,
    validateImagePageManifest,
    validatePdfOcrManifest,
    validatePdfOcrPage
  });
})(/** @type {import('../../../typings/reader-security-strict').SecurityRoot} */ (typeof self !== "undefined" ? self : globalThis));