Download tests/reader/security/test_pdf_ocr_runtime_boundary.mjs from VoiceOfML/Search: direct link, hf CLI and curl.
- Browser
- Download file 3.03 kB
-
https://huggingface.co/spaces/VoiceOfML/Search/resolve/main/tests/reader/security/test_pdf_ocr_runtime_boundary.mjs
- Command line
-
hf download hf://spaces/VoiceOfML/Search/tests/reader/security/test_pdf_ocr_runtime_boundary.mjs
-
curl -L -o test_pdf_ocr_runtime_boundary.mjs https://huggingface.co/spaces/VoiceOfML/Search/resolve/main/tests/reader/security/test_pdf_ocr_runtime_boundary.mjs
3.03 kB
| import assert from 'node:assert/strict'; | |
| import { readFileSync } from 'node:fs'; | |
| import vm from 'node:vm'; | |
| import { createReaderPdfTextRuntime } from '../../../static/reader/pdf/reader-pdf-text-runtime.mjs'; | |
| const sandbox = { self: {} }; | |
| vm.runInNewContext(readFileSync(new URL('../../../static/reader/core/reader-security.js', import.meta.url), 'utf8'), sandbox); | |
| globalThis.VoiceOfMLReaderSecurity = sandbox.self.VoiceOfMLReaderSecurity; | |
| globalThis.location = new URL('https://example.org/'); | |
| const hash = 'a'.repeat(64), root = `objects/aa/${hash}`; | |
| const entry = { p: 1, o: `${root}/ocr/page-000001.json.gz`, os: hash, ob: 1 }; | |
| const manifest = { version: 1, kind: 'pdf-ocr', complete: true, source_sha256: hash, | |
| profile: 'layout-v1', page_count: 1, pages: [entry], | |
| book_text: { path: `${root}/ocr/book-text.json.gz`, sha256: hash, bytes: 1 } }; | |
| let input, fetches = 0; | |
| const ctx = { ocrManifestUrl: '/manifest', documentState: {pageCount: 1}, pdfOcrManifest: null, | |
| pdfOcrManifestPromise: null, pdfOcrPagePromises: new Map(), pdfOcrSource: () => ({assetUrl: () => '/page'}), | |
| readerAbortController: new AbortController(), | |
| assertReaderActive() {}, cachedPdfOcrPage: () => null, cachePdfOcrPage() {}, | |
| fetchReaderUrl: async () => { fetches++; return new Response('x'); } }; | |
| const runtime = createReaderPdfTextRuntime(ctx); | |
| Object.assign(ctx, runtime); | |
| ctx.cachedPdfOcrPage = () => null; | |
| ctx.pdfOcrSource = () => ({assetUrl: () => '/page'}); | |
| ctx.readPdfOcrJson = async (...args) => args[0] === '/manifest' ? input : runtime.readPdfOcrJson(...args); | |
| for (input of [null, {}, { ...manifest, pages: [null] }]) { | |
| ctx.pdfOcrManifest = null; | |
| await assert.rejects(runtime.loadPdfOcrManifest(), /PDF_OCR_MANIFEST_INVALID/); | |
| } | |
| input = { ...manifest, page_count: 2 }; | |
| await assert.rejects(runtime.loadPdfOcrManifest(), /PDF_OCR_MANIFEST_INVALID/); | |
| input = { ...manifest, page_count: 1 }; | |
| ctx.documentState.pageCount = 2; | |
| await assert.rejects(runtime.loadPdfOcrManifest(), /PDF_OCR_PAGE_COUNT_MISMATCH/); | |
| ctx.documentState.pageCount = 1; | |
| for (const patch of [{os: undefined}, {ob: undefined}, {os: null}, {ob: null}, {os: ''}, {ob: 0}, {os: 'z'.repeat(64)}, {ob: -1}]) { | |
| input = { ...manifest, pages: [{ ...entry, ...patch }] }; | |
| ctx.pdfOcrManifest = null; ctx.pdfOcrManifestPromise = null; | |
| // Old manifests remain usable for book search even without page integrity metadata. | |
| assert.equal((await runtime.loadPdfOcrManifest()).book_text.path, manifest.book_text.path); | |
| await assert.rejects(runtime.loadPdfOcrPage(1, "high", true), /PDF_OCR_MANIFEST_INVALID/); | |
| assert.equal(fetches, 0); | |
| } | |
| input = manifest; ctx.pdfOcrManifest = null; ctx.pdfOcrManifestPromise = null; | |
| await assert.rejects(runtime.loadPdfOcrPage(1, "high", true), /PDF_OCR_HASH_MISMATCH/); | |
| assert.equal(fetches, 1); | |
| input = { ...manifest, pages: [{ ...entry, ob: 2 }] }; ctx.pdfOcrManifest = null; ctx.pdfOcrManifestPromise = null; | |
| await assert.rejects(runtime.loadPdfOcrPage(1, "high", true), /PDF_OCR_SIZE_MISMATCH/); | |
| assert.equal(fetches, 2); | |