diff --git a/core/enex/image-downloader.ts b/core/enex/image-downloader.ts index 44251beda1bb..e148cdca0f12 100644 --- a/core/enex/image-downloader.ts +++ b/core/enex/image-downloader.ts @@ -5,7 +5,13 @@ function imageDataUrlType(url: string): string | null { const prefix = /^data:image\/(png|jpeg|gif|webp);base64,/i.exec(url) if (!prefix) return null const payload = url.slice(prefix[0].length) - if (!payload || payload.length % 4 !== 0 || !/^[a-z0-9+/]+={0,2}$/i.test(payload)) return null + if (!payload || payload.length % 4 !== 0) return null + let body = payload + if (payload.endsWith('==')) body = payload.slice(0, -2) + else if (payload.endsWith('=')) body = payload.slice(0, -1) + // A single-character search keeps stack use constant even without V8's + // regexp optimizations; a quantified whole-payload match can overflow. + if (/[^a-z0-9+/]/i.test(body)) return null return prefix[1].toLowerCase() } diff --git a/core/tests/unit/enex-image-services.test.ts b/core/tests/unit/enex-image-services.test.ts index 593510fad2c1..1d28ab8b8a5b 100644 --- a/core/tests/unit/enex-image-services.test.ts +++ b/core/tests/unit/enex-image-services.test.ts @@ -2,6 +2,8 @@ import { ImageDownloader } from '../../enex/image-downloader' import { ImageProcessor } from '../../enex/image-processor' +import { execFileSync } from 'node:child_process' +import { resolve } from 'node:path' describe('ENEX image services', () => { afterEach(() => { @@ -21,9 +23,15 @@ describe('ENEX image services', () => { it('accepts safe base64 image data and rejects unsupported or malformed data URLs', () => { const downloader = new ImageDownloader() const html = '' + + '' + '' + + '' + + '' + '' - expect(downloader.extractImageUrls(html)).toEqual(['data:image/png;base64,AQ==', 'data:image/webp;base64,Ag==', 'https://cdn.test/a.png']) + expect(downloader.extractImageUrls(html)).toEqual([ + 'data:image/png;base64,AQ==', 'data:image/webp;base64,Ag==', + 'data:image/png;base64,AAA=', 'data:image/png;base64,AAAA', 'https://cdn.test/a.png', + ]) }) it('downloads data images locally and never sends their bytes to the optional proxy', async () => { @@ -50,6 +58,27 @@ describe('ENEX image services', () => { expect(result?.fileName).toBe('image.png') }, 15_000) + it('handles a large inline image when V8 regexp optimization is disabled', () => { + const output = execFileSync(process.execPath, ['--no-regexp-optimization', '-e', ` + const { ImageDownloader } = require(process.argv[1]); + console.debug = () => {}; + let calls = 0; + globalThis.fetch = async () => { + calls += 1; + return { ok: true, headers: new Headers({ 'content-type': 'image/png' }), + arrayBuffer: async () => new Uint8Array([1]).buffer }; + }; + const url = 'data:image/png;base64,' + 'A'.repeat(14_000_000); + new ImageDownloader().downloadImage(url).then(resource => { + console.log(JSON.stringify({ resource, calls })); + }); + `, resolve(__dirname, '../../enex/image-downloader.ts')], { encoding: 'utf8', timeout: 5000 }) + expect(JSON.parse(output)).toMatchObject({ + resource: { fileName: 'image.png', mime: 'image/png', data: 'AQ==' }, + calls: 1, + }) + }) + it('downloads an image, computes md5, mime, filename and dimensions', async () => { const downloader = new ImageDownloader() const fetchMock = jest.fn().mockResolvedValue({ diff --git a/docs/ai/implementation/feature-enex-export.md b/docs/ai/implementation/feature-enex-export.md index 44fdf2efd974..2b53c313ec37 100644 --- a/docs/ai/implementation/feature-enex-export.md +++ b/docs/ai/implementation/feature-enex-export.md @@ -367,3 +367,19 @@ async getNotesByIds(noteIds: string[], userId: string): Promise { - Secrets management - Использование существующего Supabase Client (секреты уже настроены) - Не хранить чувствительные данные в экспортируемом файле + +## Large inline image validation (2026-11-04) + +A valid 14 MB base64 image can overflow the quantified whole-payload regexp +when V8 regexp optimization is disabled, causing downloadImage to return null. +The public API failure and RangeError in the original pattern are reproduced +without changing input size. Validation now removes at most two trailing padding +characters, then searches for one invalid alphabet character. This uses constant +regexp stack space and retains the MIME whitelist and multiple-of-four check. + +The existing error redaction prevented observing the precise V8 tier state in +the failed main job; the optimization-dependent failure class is independently +confirmed. A new subprocess regression exercises the real downloader under that +engine mode with synthetic fetch data. Existing export, padding and proxy/privacy +checks remain. See [validation](../testing/feature-inline-image-validation.md) for the +full-context validation and its pre-existing Allure reporting boundary. diff --git a/docs/ai/testing/feature-inline-image-validation.md b/docs/ai/testing/feature-inline-image-validation.md new file mode 100644 index 000000000000..fc8adea9cc5a --- /dev/null +++ b/docs/ai/testing/feature-inline-image-validation.md @@ -0,0 +1,49 @@ +--- +phase: testing +title: Large Inline Image Validation +description: Keep large ENEX inline images valid without relying on regexp optimization +--- + +# Large Inline Image Validation + +## Test Coverage Goals + +Preserve supported image types, base64 alphabet/grouping/padding checks, export +resource fields and embedded-image proxy/privacy behavior. Preserve the existing +14,000,000-character valid-image case and every existing assertion/timeout. + +## Unit Tests + +The ten-case ENEX image suite includes a new isolated Node subprocess with V8 +regexp optimization disabled. It calls the real TypeScript ImageDownloader through +Node 24's native type stripping, uses synthetic fetch bytes and checks the filename, +MIME, encoded data and one fetch call. It does not change parent process flags. +The new case failed against the original validator (null resource) and passes +with constant-stack validation. All ten cases pass through Allure with matched +expectations, complete modeling and no findings. + +Existing extraction checks additionally exercise no/one/two padding characters, +misplaced/excess padding, invalid characters and unsupported formats. + +## Integration Tests + +Run the unchanged combined main command: all three Jest projects, coverage and +runInBand. Verify native Jest JSON reports 2,179 cases, no failures and only the +six existing skips, retaining all 2,178 baseline identities plus the new case. +Allure expects 2,119 visible cases: eleven existing plain-jsdom docblock files +account for the other 60 native cases and remain verified in Jest JSON. This +pre-existing reporting boundary is not a test exclusion. + +## Test Reporting & Coverage + +Store red/green Allure output and native Jest JSON outside git in the chat's +component-runtime evidence directory. A main coverage run exposed the original +failure; a focused default-engine run alone did not reproduce it. The disabled +regexp-optimization subprocess provides a deterministic regression without +weakening the original data size, assertions, timeout or failure propagation. + +## Acceptance + +Require fresh exact-head GitHub tests and quality checks, then verify the main +coverage producer and existing component/Allure/Sonar consumers. No quality gate +or source inclusion setting is changed.