oom(02/29): bound shared image/media/PDF memory limits (#10295)
* oom(01): A1-shared-readers — reintroduce #10179 subset Files: 18 applied, 0 deleted (from6eb70d8370) Co-authored-by: Orca <help@stably.ai> * oom(02): A2-shared-image-media — reintroduce #10179 subset Files: 7 applied, 0 deleted (from6eb70d8370) Co-authored-by: Orca <help@stably.ai> --------- Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
export const MAX_CUSTOM_PET_FILE_BYTES = 64 * 1024 * 1024
|
||||
|
||||
// Why: sprite processing holds decoded image, canvas, ImageData, PNG, and
|
||||
// optional bitmap copies at once, so encoded bytes alone are not a safe bound.
|
||||
export const MAX_CUSTOM_PET_SHEET_PIXELS = 4 * 1024 * 1024
|
||||
export const MAX_CUSTOM_PET_SHEET_DIMENSION = 8_192
|
||||
export const MAX_CUSTOM_PET_DETECTED_FRAMES = 128
|
||||
|
||||
export function isCustomPetSheetSizeSafe(width: number, height: number): boolean {
|
||||
return (
|
||||
Number.isSafeInteger(width) &&
|
||||
Number.isSafeInteger(height) &&
|
||||
width > 0 &&
|
||||
height > 0 &&
|
||||
width <= MAX_CUSTOM_PET_SHEET_DIMENSION &&
|
||||
height <= MAX_CUSTOM_PET_SHEET_DIMENSION &&
|
||||
width * height <= MAX_CUSTOM_PET_SHEET_PIXELS
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import { measureUtf8ByteLength } from './utf8-byte-limits'
|
||||
|
||||
export const HTML_TO_PDF_MAX_INPUT_BYTES = 32 * 1024 * 1024
|
||||
export const HTML_TO_PDF_MEMORY_LIMIT_ERROR = 'HTML export exceeds the PDF memory limit'
|
||||
|
||||
export function assertHtmlToPdfInputWithinMemoryLimit(
|
||||
html: string,
|
||||
maxBytes = HTML_TO_PDF_MAX_INPUT_BYTES
|
||||
): void {
|
||||
if (measureUtf8ByteLength(html, { stopAfterBytes: maxBytes }).exceededLimit) {
|
||||
throw new Error(HTML_TO_PDF_MEMORY_LIMIT_ERROR)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
import type { RasterImageDimensions } from './raster-image-dimensions'
|
||||
import {
|
||||
assertRasterImagePreviewWithinLimits,
|
||||
isKnownRasterImageMimeType,
|
||||
RASTER_IMAGE_PREVIEW_HEADER_MAX_BYTES
|
||||
} from './raster-image-preview-limits'
|
||||
|
||||
const BASE64_PADDING = -2
|
||||
const INVALID_BASE64 = -1
|
||||
|
||||
function base64Value(code: number): number {
|
||||
if (code >= 65 && code <= 90) {
|
||||
return code - 65
|
||||
}
|
||||
if (code >= 97 && code <= 122) {
|
||||
return code - 71
|
||||
}
|
||||
if (code >= 48 && code <= 57) {
|
||||
return code + 4
|
||||
}
|
||||
if (code === 43) {
|
||||
return 62
|
||||
}
|
||||
if (code === 47) {
|
||||
return 63
|
||||
}
|
||||
if (code === 61) {
|
||||
return BASE64_PADDING
|
||||
}
|
||||
return INVALID_BASE64
|
||||
}
|
||||
|
||||
function isWhitespace(code: number): boolean {
|
||||
return code === 9 || code === 10 || code === 12 || code === 13 || code === 32
|
||||
}
|
||||
|
||||
function writeQuartet(
|
||||
output: Uint8Array,
|
||||
offset: number,
|
||||
quartet: readonly number[]
|
||||
): { bytesWritten: number; padded: boolean } | null {
|
||||
const [a, b, c, d] = quartet
|
||||
if (a === undefined || b === undefined || a < 0 || b < 0) {
|
||||
return null
|
||||
}
|
||||
if (c === BASE64_PADDING) {
|
||||
if (d !== BASE64_PADDING) {
|
||||
return null
|
||||
}
|
||||
if (offset < output.length) {
|
||||
output[offset] = (a << 2) | (b >> 4)
|
||||
}
|
||||
return { bytesWritten: Math.min(1, output.length - offset), padded: true }
|
||||
}
|
||||
if (c === undefined || c < 0) {
|
||||
return null
|
||||
}
|
||||
if (offset < output.length) {
|
||||
output[offset] = (a << 2) | (b >> 4)
|
||||
}
|
||||
if (offset + 1 < output.length) {
|
||||
output[offset + 1] = ((b & 15) << 4) | (c >> 2)
|
||||
}
|
||||
if (d === BASE64_PADDING) {
|
||||
return { bytesWritten: Math.min(2, output.length - offset), padded: true }
|
||||
}
|
||||
if (d === undefined || d < 0) {
|
||||
return null
|
||||
}
|
||||
if (offset + 2 < output.length) {
|
||||
output[offset + 2] = ((c & 3) << 6) | d
|
||||
}
|
||||
return { bytesWritten: Math.min(3, output.length - offset), padded: false }
|
||||
}
|
||||
|
||||
function decodeBase64Prefix(content: string, maxBytes: number): Uint8Array | null {
|
||||
const capacity = Math.min(maxBytes, Math.ceil(content.length / 4) * 3)
|
||||
const output = new Uint8Array(capacity)
|
||||
const quartet: number[] = []
|
||||
let outputLength = 0
|
||||
let padded = false
|
||||
|
||||
for (let index = 0; index < content.length && outputLength < capacity; index += 1) {
|
||||
const code = content.charCodeAt(index)
|
||||
if (isWhitespace(code)) {
|
||||
continue
|
||||
}
|
||||
if (padded) {
|
||||
return null
|
||||
}
|
||||
const value = base64Value(code)
|
||||
if (value === INVALID_BASE64) {
|
||||
return null
|
||||
}
|
||||
quartet.push(value)
|
||||
if (quartet.length !== 4) {
|
||||
continue
|
||||
}
|
||||
const decoded = writeQuartet(output, outputLength, quartet)
|
||||
if (!decoded) {
|
||||
return null
|
||||
}
|
||||
outputLength += decoded.bytesWritten
|
||||
padded = decoded.padded
|
||||
quartet.length = 0
|
||||
}
|
||||
|
||||
if (!padded && outputLength < capacity && quartet.length > 0) {
|
||||
if (quartet.length === 1 || quartet.includes(BASE64_PADDING)) {
|
||||
return null
|
||||
}
|
||||
while (quartet.length < 4) {
|
||||
quartet.push(BASE64_PADDING)
|
||||
}
|
||||
const decoded = writeQuartet(output, outputLength, quartet)
|
||||
if (!decoded) {
|
||||
return null
|
||||
}
|
||||
outputLength += decoded.bytesWritten
|
||||
}
|
||||
return output.subarray(0, outputLength)
|
||||
}
|
||||
|
||||
/** Returns undefined for non-raster MIME types and null for rejected raster bytes. */
|
||||
export function readRasterImagePreviewDimensionsFromBase64(
|
||||
content: string,
|
||||
mimeType: string | undefined
|
||||
): RasterImageDimensions | null | undefined {
|
||||
if (!isKnownRasterImageMimeType(mimeType)) {
|
||||
return undefined
|
||||
}
|
||||
const prefix = decodeBase64Prefix(content, RASTER_IMAGE_PREVIEW_HEADER_MAX_BYTES)
|
||||
if (!prefix) {
|
||||
return null
|
||||
}
|
||||
try {
|
||||
return assertRasterImagePreviewWithinLimits(prefix, mimeType) ?? null
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { readRasterImageDimensions } from './raster-image-dimensions'
|
||||
|
||||
function pngHeader(width: number, height: number): Buffer {
|
||||
const png = Buffer.alloc(24)
|
||||
Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]).copy(png)
|
||||
png.writeUInt32BE(13, 8)
|
||||
png.write('IHDR', 12, 'ascii')
|
||||
png.writeUInt32BE(width, 16)
|
||||
png.writeUInt32BE(height, 20)
|
||||
return png
|
||||
}
|
||||
|
||||
function bmpHeader(width: number, height: number): Buffer {
|
||||
const bmp = Buffer.alloc(26)
|
||||
bmp.write('BM', 0, 'ascii')
|
||||
bmp.writeUInt32LE(40, 14)
|
||||
bmp.writeInt32LE(width, 18)
|
||||
bmp.writeInt32LE(height, 22)
|
||||
return bmp
|
||||
}
|
||||
|
||||
function icoWithPayload(payload: Buffer, width = 1, height = 1): Buffer {
|
||||
const header = Buffer.alloc(22)
|
||||
header.writeUInt16LE(1, 2)
|
||||
header.writeUInt16LE(1, 4)
|
||||
header[6] = width === 256 ? 0 : width
|
||||
header[7] = height === 256 ? 0 : height
|
||||
header.writeUInt32LE(payload.byteLength, 14)
|
||||
header.writeUInt32LE(header.byteLength, 18)
|
||||
return Buffer.concat([header, payload])
|
||||
}
|
||||
|
||||
describe('readRasterImageDimensions', () => {
|
||||
it('reads BMP dimensions including top-down images', () => {
|
||||
expect(readRasterImageDimensions(bmpHeader(640, -480))).toEqual({
|
||||
width: 640,
|
||||
height: 480
|
||||
})
|
||||
})
|
||||
|
||||
it('uses embedded ICO image dimensions instead of forgeable directory values', () => {
|
||||
expect(readRasterImageDimensions(icoWithPayload(pngHeader(40_000, 2)))).toEqual({
|
||||
width: 40_000,
|
||||
height: 2
|
||||
})
|
||||
})
|
||||
|
||||
it('reads a Uint8Array view without depending on its backing-buffer offset', () => {
|
||||
const wrapped = Buffer.concat([Buffer.from('prefix'), pngHeader(320, 240), Buffer.from('tail')])
|
||||
const view = wrapped.subarray(6, 30)
|
||||
|
||||
expect(readRasterImageDimensions(view)).toEqual({ width: 320, height: 240 })
|
||||
})
|
||||
|
||||
it('rejects truncated ICO payloads and zero raster dimensions', () => {
|
||||
const truncated = icoWithPayload(pngHeader(16, 16)).subarray(0, 30)
|
||||
|
||||
expect(readRasterImageDimensions(truncated)).toBeNull()
|
||||
expect(readRasterImageDimensions(bmpHeader(0, 16))).toBeNull()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,252 @@
|
||||
export type RasterImageDimensions = { width: number; height: number }
|
||||
|
||||
const JPEG_DIMENSION_SCAN_MAX_BYTES = 1024 * 1024
|
||||
const JPEG_DIMENSION_SCAN_MAX_MARKERS = 4_096
|
||||
const ICO_MAX_IMAGES = 1_024
|
||||
const JPEG_START_OF_FRAME_MARKERS = new Set([
|
||||
0xc0, 0xc1, 0xc2, 0xc3, 0xc5, 0xc6, 0xc7, 0xc9, 0xca, 0xcb, 0xcd, 0xce, 0xcf
|
||||
])
|
||||
const PNG_SIGNATURE = [137, 80, 78, 71, 13, 10, 26, 10]
|
||||
|
||||
function hasBytes(bytes: Uint8Array, offset: number, length: number): boolean {
|
||||
return offset >= 0 && length >= 0 && offset + length <= bytes.byteLength
|
||||
}
|
||||
|
||||
function matchesBytes(bytes: Uint8Array, offset: number, expected: readonly number[]): boolean {
|
||||
return (
|
||||
hasBytes(bytes, offset, expected.length) &&
|
||||
expected.every((value, index) => bytes[offset + index] === value)
|
||||
)
|
||||
}
|
||||
|
||||
function matchesAscii(bytes: Uint8Array, offset: number, expected: string): boolean {
|
||||
if (!hasBytes(bytes, offset, expected.length)) {
|
||||
return false
|
||||
}
|
||||
for (let index = 0; index < expected.length; index += 1) {
|
||||
if (bytes[offset + index] !== expected.charCodeAt(index)) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
function readUint16Le(bytes: Uint8Array, offset: number): number {
|
||||
return bytes[offset]! | (bytes[offset + 1]! << 8)
|
||||
}
|
||||
|
||||
function readUint16Be(bytes: Uint8Array, offset: number): number {
|
||||
return (bytes[offset]! << 8) | bytes[offset + 1]!
|
||||
}
|
||||
|
||||
function readUint24Le(bytes: Uint8Array, offset: number): number {
|
||||
return bytes[offset]! | (bytes[offset + 1]! << 8) | (bytes[offset + 2]! << 16)
|
||||
}
|
||||
|
||||
function readUint32Le(bytes: Uint8Array, offset: number): number {
|
||||
return (
|
||||
(bytes[offset]! |
|
||||
(bytes[offset + 1]! << 8) |
|
||||
(bytes[offset + 2]! << 16) |
|
||||
(bytes[offset + 3]! << 24)) >>>
|
||||
0
|
||||
)
|
||||
}
|
||||
|
||||
function readUint32Be(bytes: Uint8Array, offset: number): number {
|
||||
return (
|
||||
(((bytes[offset]! << 24) >>> 0) |
|
||||
(bytes[offset + 1]! << 16) |
|
||||
(bytes[offset + 2]! << 8) |
|
||||
bytes[offset + 3]!) >>>
|
||||
0
|
||||
)
|
||||
}
|
||||
|
||||
function readInt32Le(bytes: Uint8Array, offset: number): number {
|
||||
return readUint32Le(bytes, offset) | 0
|
||||
}
|
||||
|
||||
function positiveDimensions(width: number, height: number): RasterImageDimensions | null {
|
||||
return Number.isSafeInteger(width) && Number.isSafeInteger(height) && width > 0 && height > 0
|
||||
? { width, height }
|
||||
: null
|
||||
}
|
||||
|
||||
function readPngDimensions(bytes: Uint8Array): RasterImageDimensions | null {
|
||||
if (
|
||||
!matchesBytes(bytes, 0, PNG_SIGNATURE) ||
|
||||
!hasBytes(bytes, 8, 16) ||
|
||||
readUint32Be(bytes, 8) !== 13 ||
|
||||
!matchesAscii(bytes, 12, 'IHDR')
|
||||
) {
|
||||
return null
|
||||
}
|
||||
return positiveDimensions(readUint32Be(bytes, 16), readUint32Be(bytes, 20))
|
||||
}
|
||||
|
||||
function readGifDimensions(bytes: Uint8Array): RasterImageDimensions | null {
|
||||
if (
|
||||
!hasBytes(bytes, 0, 10) ||
|
||||
(!matchesAscii(bytes, 0, 'GIF87a') && !matchesAscii(bytes, 0, 'GIF89a'))
|
||||
) {
|
||||
return null
|
||||
}
|
||||
return positiveDimensions(readUint16Le(bytes, 6), readUint16Le(bytes, 8))
|
||||
}
|
||||
|
||||
function readJpegDimensions(bytes: Uint8Array): RasterImageDimensions | null {
|
||||
if (!hasBytes(bytes, 0, 4) || bytes[0] !== 0xff || bytes[1] !== 0xd8) {
|
||||
return null
|
||||
}
|
||||
let offset = 2
|
||||
let markersRead = 0
|
||||
const scanEnd = Math.min(bytes.byteLength, JPEG_DIMENSION_SCAN_MAX_BYTES)
|
||||
while (offset < scanEnd && markersRead < JPEG_DIMENSION_SCAN_MAX_MARKERS) {
|
||||
while (offset < scanEnd && bytes[offset] === 0xff) {
|
||||
offset += 1
|
||||
}
|
||||
const marker = bytes[offset]
|
||||
offset += 1
|
||||
markersRead += 1
|
||||
if (marker === undefined || marker === 0x00 || marker === 0xd9 || marker === 0xda) {
|
||||
return null
|
||||
}
|
||||
if (marker === 0x01 || (marker >= 0xd0 && marker <= 0xd8)) {
|
||||
continue
|
||||
}
|
||||
if (!hasBytes(bytes, offset, 2)) {
|
||||
return null
|
||||
}
|
||||
const segmentLength = readUint16Be(bytes, offset)
|
||||
if (segmentLength < 2 || offset + segmentLength > scanEnd) {
|
||||
return null
|
||||
}
|
||||
if (JPEG_START_OF_FRAME_MARKERS.has(marker)) {
|
||||
return segmentLength >= 7
|
||||
? positiveDimensions(readUint16Be(bytes, offset + 5), readUint16Be(bytes, offset + 3))
|
||||
: null
|
||||
}
|
||||
offset += segmentLength
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
function readWebpDimensions(bytes: Uint8Array): RasterImageDimensions | null {
|
||||
if (
|
||||
!hasBytes(bytes, 0, 20) ||
|
||||
!matchesAscii(bytes, 0, 'RIFF') ||
|
||||
!matchesAscii(bytes, 8, 'WEBP')
|
||||
) {
|
||||
return null
|
||||
}
|
||||
|
||||
let offset = 12
|
||||
while (hasBytes(bytes, offset, 8)) {
|
||||
const chunkSize = readUint32Le(bytes, offset + 4)
|
||||
const dataOffset = offset + 8
|
||||
const dataEnd = dataOffset + chunkSize
|
||||
|
||||
if (matchesAscii(bytes, offset, 'VP8X') && chunkSize >= 10 && hasBytes(bytes, dataOffset, 10)) {
|
||||
return positiveDimensions(
|
||||
readUint24Le(bytes, dataOffset + 4) + 1,
|
||||
readUint24Le(bytes, dataOffset + 7) + 1
|
||||
)
|
||||
}
|
||||
if (
|
||||
matchesAscii(bytes, offset, 'VP8L') &&
|
||||
chunkSize >= 5 &&
|
||||
hasBytes(bytes, dataOffset, 5) &&
|
||||
bytes[dataOffset] === 0x2f
|
||||
) {
|
||||
const b0 = bytes[dataOffset + 1]!
|
||||
const b1 = bytes[dataOffset + 2]!
|
||||
const b2 = bytes[dataOffset + 3]!
|
||||
const b3 = bytes[dataOffset + 4]!
|
||||
return positiveDimensions(
|
||||
1 + (((b1 & 0x3f) << 8) | b0),
|
||||
1 + (((b3 & 0x0f) << 10) | (b2 << 2) | ((b1 & 0xc0) >> 6))
|
||||
)
|
||||
}
|
||||
if (
|
||||
matchesAscii(bytes, offset, 'VP8 ') &&
|
||||
chunkSize >= 10 &&
|
||||
hasBytes(bytes, dataOffset, 10) &&
|
||||
bytes[dataOffset + 3] === 0x9d &&
|
||||
bytes[dataOffset + 4] === 0x01 &&
|
||||
bytes[dataOffset + 5] === 0x2a
|
||||
) {
|
||||
return positiveDimensions(
|
||||
readUint16Le(bytes, dataOffset + 6) & 0x3fff,
|
||||
readUint16Le(bytes, dataOffset + 8) & 0x3fff
|
||||
)
|
||||
}
|
||||
if (dataEnd > bytes.byteLength) {
|
||||
return null
|
||||
}
|
||||
offset = dataEnd + (chunkSize % 2)
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
function readDibDimensions(bytes: Uint8Array, offset: number): RasterImageDimensions | null {
|
||||
if (!hasBytes(bytes, offset, 12)) {
|
||||
return null
|
||||
}
|
||||
const headerSize = readUint32Le(bytes, offset)
|
||||
if (headerSize === 12) {
|
||||
return positiveDimensions(readUint16Le(bytes, offset + 4), readUint16Le(bytes, offset + 6))
|
||||
}
|
||||
if (headerSize < 40 || !hasBytes(bytes, offset, 12)) {
|
||||
return null
|
||||
}
|
||||
return positiveDimensions(
|
||||
Math.abs(readInt32Le(bytes, offset + 4)),
|
||||
Math.abs(readInt32Le(bytes, offset + 8))
|
||||
)
|
||||
}
|
||||
|
||||
function readBmpDimensions(bytes: Uint8Array): RasterImageDimensions | null {
|
||||
return matchesAscii(bytes, 0, 'BM') ? readDibDimensions(bytes, 14) : null
|
||||
}
|
||||
|
||||
function readIcoDimensions(bytes: Uint8Array): RasterImageDimensions | null {
|
||||
if (!hasBytes(bytes, 0, 6) || readUint16Le(bytes, 0) !== 0 || readUint16Le(bytes, 2) !== 1) {
|
||||
return null
|
||||
}
|
||||
const imageCount = readUint16Le(bytes, 4)
|
||||
if (imageCount <= 0 || imageCount > ICO_MAX_IMAGES || !hasBytes(bytes, 6, imageCount * 16)) {
|
||||
return null
|
||||
}
|
||||
|
||||
let maxWidth = 0
|
||||
let maxHeight = 0
|
||||
for (let index = 0; index < imageCount; index += 1) {
|
||||
const entryOffset = 6 + index * 16
|
||||
const encodedSize = readUint32Le(bytes, entryOffset + 8)
|
||||
const imageOffset = readUint32Le(bytes, entryOffset + 12)
|
||||
if (encodedSize <= 0 || !hasBytes(bytes, imageOffset, encodedSize)) {
|
||||
return null
|
||||
}
|
||||
const payload = bytes.subarray(imageOffset, imageOffset + encodedSize)
|
||||
const embedded = readPngDimensions(payload) ?? readDibDimensions(payload, 0)
|
||||
const width = embedded?.width ?? (bytes[entryOffset] === 0 ? 256 : bytes[entryOffset]!)
|
||||
const height =
|
||||
embedded?.height ?? (bytes[entryOffset + 1] === 0 ? 256 : bytes[entryOffset + 1]!)
|
||||
maxWidth = Math.max(maxWidth, width)
|
||||
maxHeight = Math.max(maxHeight, height)
|
||||
}
|
||||
return positiveDimensions(maxWidth, maxHeight)
|
||||
}
|
||||
|
||||
/** Reads encoded raster dimensions without invoking a native or browser image decoder. */
|
||||
export function readRasterImageDimensions(bytes: Uint8Array): RasterImageDimensions | null {
|
||||
return (
|
||||
readPngDimensions(bytes) ??
|
||||
readGifDimensions(bytes) ??
|
||||
readJpegDimensions(bytes) ??
|
||||
readWebpDimensions(bytes) ??
|
||||
readBmpDimensions(bytes) ??
|
||||
readIcoDimensions(bytes)
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import {
|
||||
INVALID_RASTER_IMAGE_PREVIEW_ERROR,
|
||||
MAX_RASTER_IMAGE_PREVIEW_DIMENSION_PX,
|
||||
RASTER_IMAGE_PREVIEW_TOO_LARGE_ERROR,
|
||||
assertRasterImagePreviewWithinLimits,
|
||||
isKnownRasterImageMimeType
|
||||
} from './raster-image-preview-limits'
|
||||
|
||||
function pngHeader(width: number, height: number): Buffer {
|
||||
const bytes = Buffer.alloc(24)
|
||||
Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]).copy(bytes)
|
||||
bytes.writeUInt32BE(13, 8)
|
||||
bytes.write('IHDR', 12, 'ascii')
|
||||
bytes.writeUInt32BE(width, 16)
|
||||
bytes.writeUInt32BE(height, 20)
|
||||
return bytes
|
||||
}
|
||||
|
||||
describe('raster image preview limits', () => {
|
||||
it('accepts ordinary 8K images and returns their dimensions', () => {
|
||||
expect(assertRasterImagePreviewWithinLimits(pngHeader(7680, 4320), 'image/png')).toEqual({
|
||||
width: 7680,
|
||||
height: 4320
|
||||
})
|
||||
})
|
||||
|
||||
it('rejects oversized edges and total pixel counts before decode', () => {
|
||||
expect(() =>
|
||||
assertRasterImagePreviewWithinLimits(
|
||||
pngHeader(MAX_RASTER_IMAGE_PREVIEW_DIMENSION_PX + 1, 1),
|
||||
'image/png'
|
||||
)
|
||||
).toThrow(RASTER_IMAGE_PREVIEW_TOO_LARGE_ERROR)
|
||||
expect(() => assertRasterImagePreviewWithinLimits(pngHeader(8192, 8192), 'image/png')).toThrow(
|
||||
RASTER_IMAGE_PREVIEW_TOO_LARGE_ERROR
|
||||
)
|
||||
})
|
||||
|
||||
it('rejects invalid known raster bytes but leaves SVG and PDF unchanged', () => {
|
||||
expect(() => assertRasterImagePreviewWithinLimits(new Uint8Array([1]), 'image/gif')).toThrow(
|
||||
INVALID_RASTER_IMAGE_PREVIEW_ERROR
|
||||
)
|
||||
expect(
|
||||
assertRasterImagePreviewWithinLimits(new Uint8Array([1]), 'image/svg+xml')
|
||||
).toBeUndefined()
|
||||
expect(
|
||||
assertRasterImagePreviewWithinLimits(new Uint8Array([1]), 'application/pdf')
|
||||
).toBeUndefined()
|
||||
})
|
||||
|
||||
it('recognizes supported MIME aliases case-insensitively', () => {
|
||||
expect(isKnownRasterImageMimeType('IMAGE/JPEG; charset=binary')).toBe(true)
|
||||
expect(isKnownRasterImageMimeType('image/vnd.microsoft.icon')).toBe(true)
|
||||
expect(isKnownRasterImageMimeType('image/tiff')).toBe(false)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,69 @@
|
||||
import { readRasterImageDimensions, type RasterImageDimensions } from './raster-image-dimensions'
|
||||
|
||||
export const MAX_RASTER_IMAGE_PREVIEW_DIMENSION_PX = 32_768
|
||||
export const MAX_RASTER_IMAGE_PREVIEW_PIXELS = 32 * 1024 * 1024
|
||||
export const RASTER_IMAGE_PREVIEW_HEADER_MAX_BYTES = 1024 * 1024
|
||||
export const INVALID_RASTER_IMAGE_PREVIEW_ERROR =
|
||||
'Image preview has invalid or unsupported raster dimensions'
|
||||
export const RASTER_IMAGE_PREVIEW_TOO_LARGE_ERROR =
|
||||
'Image dimensions exceed the preview safety limit'
|
||||
|
||||
const RASTER_IMAGE_MIME_TYPES = new Set([
|
||||
'image/apng',
|
||||
'image/bmp',
|
||||
'image/gif',
|
||||
'image/ico',
|
||||
'image/jpeg',
|
||||
'image/jpg',
|
||||
'image/pjpeg',
|
||||
'image/png',
|
||||
'image/vnd.microsoft.icon',
|
||||
'image/webp',
|
||||
'image/x-bmp',
|
||||
'image/x-icon',
|
||||
'image/x-ms-bmp'
|
||||
])
|
||||
|
||||
function normalizeMimeType(mimeType: string | undefined): string | null {
|
||||
const normalized = mimeType?.split(';', 1)[0]?.trim().toLowerCase()
|
||||
return normalized || null
|
||||
}
|
||||
|
||||
export function isKnownRasterImageMimeType(mimeType: string | undefined): boolean {
|
||||
const normalized = normalizeMimeType(mimeType)
|
||||
return normalized !== null && RASTER_IMAGE_MIME_TYPES.has(normalized)
|
||||
}
|
||||
|
||||
export function isRasterImagePreviewDimensions(value: unknown): value is RasterImageDimensions {
|
||||
if (!value || typeof value !== 'object') {
|
||||
return false
|
||||
}
|
||||
const dimensions = value as Partial<RasterImageDimensions>
|
||||
return (
|
||||
Number.isSafeInteger(dimensions.width) &&
|
||||
Number.isSafeInteger(dimensions.height) &&
|
||||
dimensions.width! > 0 &&
|
||||
dimensions.height! > 0 &&
|
||||
dimensions.width! <= MAX_RASTER_IMAGE_PREVIEW_DIMENSION_PX &&
|
||||
dimensions.height! <= MAX_RASTER_IMAGE_PREVIEW_DIMENSION_PX &&
|
||||
dimensions.width! <= Math.floor(MAX_RASTER_IMAGE_PREVIEW_PIXELS / dimensions.height!)
|
||||
)
|
||||
}
|
||||
|
||||
/** Validates encoded raster dimensions without invoking a native image decoder. */
|
||||
export function assertRasterImagePreviewWithinLimits(
|
||||
bytes: Uint8Array,
|
||||
mimeType: string | undefined
|
||||
): RasterImageDimensions | undefined {
|
||||
if (!isKnownRasterImageMimeType(mimeType)) {
|
||||
return undefined
|
||||
}
|
||||
const dimensions = readRasterImageDimensions(bytes)
|
||||
if (!dimensions) {
|
||||
throw new Error(INVALID_RASTER_IMAGE_PREVIEW_ERROR)
|
||||
}
|
||||
if (!isRasterImagePreviewDimensions(dimensions)) {
|
||||
throw new Error(RASTER_IMAGE_PREVIEW_TOO_LARGE_ERROR)
|
||||
}
|
||||
return dimensions
|
||||
}
|
||||
Reference in New Issue
Block a user