| ... | @@ -29,17 +29,43 @@ export const SAFE_IMAGE_FILE_EXTENSIONS = new Set([ | ... | @@ -29,17 +29,43 @@ export const SAFE_IMAGE_FILE_EXTENSIONS = new Set([ |
| 29 | ".webp", | 29 | ".webp", |
| 30 | ]); | 30 | ]); |
| 31 | | 31 | |
| | 32 | // Whether `code` is a character the base64 payload may contain, aside from |
| | 33 | // the whitespace handled separately below. |
| | 34 | function isBase64Char(code: number): boolean { |
| | 35 | return ( |
| | 36 | (code >= 0x61 && code <= 0x7a) || // a-z |
| | 37 | (code >= 0x41 && code <= 0x5a) || // A-Z |
| | 38 | (code >= 0x30 && code <= 0x39) || // 0-9 |
| | 39 | code === 0x2b || // + |
| | 40 | code === 0x2f || // / |
| | 41 | code === 0x3d // = |
| | 42 | ); |
| | 43 | } |
| | 44 | |
| | 45 | function isBase64Whitespace(code: number): boolean { |
| | 46 | return code === 0x0d || code === 0x0a || code === 0x20; // \r \n space |
| | 47 | } |
| | 48 | |
| | 49 | // Data URLs carry the full image payload, so this parser must never run a |
| | 50 | // regex across the payload: V8's regex engine borrows the JS call stack, and |
| | 51 | // matching a multi-megabyte string from a deep call stack (e.g. inside fiber |
| | 52 | // execution) throws "Maximum call stack size exceeded". |
| 32 | export function parseBase64DataUrl( | 53 | export function parseBase64DataUrl( |
| 33 | dataUrl: string, | 54 | dataUrl: string, |
| 34 | ): { readonly mimeType: string; readonly base64: string } | null { | 55 | ): { readonly mimeType: string; readonly base64: string } | null { |
| 35 | const match = /^data:([^,]+),([a-z0-9+/=\r\n ]+)$/i.exec(dataUrl.trim()); | 56 | const trimmed = dataUrl.trim(); |
| 36 | if (!match) return null; | 57 | if (trimmed.slice(0, 5).toLowerCase() !== "data:") return null; |
| | 58 | |
| | 59 | const commaIndex = trimmed.indexOf(","); |
| | 60 | if (commaIndex === -1) return null; |
| | 61 | const header = trimmed.slice(5, commaIndex); |
| | 62 | if (header.length === 0) return null; |
| 37 | | 63 | |
| 38 | const headerParts: Array<string> = []; | 64 | const headerParts: Array<string> = []; |
| 39 | for (const part of (match[1] ?? "").split(";")) { | 65 | for (const part of header.split(";")) { |
| 40 | const trimmed = part.trim(); | 66 | const partTrimmed = part.trim(); |
| 41 | if (trimmed.length > 0) { | 67 | if (partTrimmed.length > 0) { |
| 42 | headerParts.push(trimmed); | 68 | headerParts.push(partTrimmed); |
| 43 | } | 69 | } |
| 44 | } | 70 | } |
| 45 | if (headerParts.length < 2) { | 71 | if (headerParts.length < 2) { |
| ... | @@ -51,8 +77,37 @@ export function parseBase64DataUrl( | ... | @@ -51,8 +77,37 @@ export function parseBase64DataUrl( |
| 51 | } | 77 | } |
| 52 | | 78 | |
| 53 | const mimeType = headerParts[0]?.toLowerCase(); | 79 | const mimeType = headerParts[0]?.toLowerCase(); |
| 54 | const base64 = match[2]?.replace(/\s+/g, ""); | 80 | if (!mimeType) return null; |
| 55 | if (!mimeType || !base64) return null; | 81 | |
| | 82 | const payload = trimmed.slice(commaIndex + 1); |
| | 83 | const runs: Array<string> = []; |
| | 84 | let runStart = -1; |
| | 85 | for (let index = 0; index < payload.length; index += 1) { |
| | 86 | const code = payload.charCodeAt(index); |
| | 87 | if (isBase64Char(code)) { |
| | 88 | if (runStart === -1) runStart = index; |
| | 89 | continue; |
| | 90 | } |
| | 91 | if (!isBase64Whitespace(code)) return null; |
| | 92 | if (runStart !== -1) { |
| | 93 | runs.push(payload.slice(runStart, index)); |
| | 94 | runStart = -1; |
| | 95 | } |
| | 96 | } |
| | 97 | if (runStart !== -1) { |
| | 98 | runs.push(payload.slice(runStart)); |
| | 99 | } |
| | 100 | const base64 = runs.length === 1 ? runs[0]! : runs.join(""); |
| | 101 | if (base64.length === 0 || base64.length % 4 !== 0) return null; |
| | 102 | const firstPad = base64.indexOf("="); |
| | 103 | if (firstPad !== -1) { |
| | 104 | // '=' is only valid as one or two trailing padding characters; Node's |
| | 105 | // decoder would otherwise silently truncate at the first '='. |
| | 106 | if (base64.length - firstPad > 2) return null; |
| | 107 | for (let index = firstPad; index < base64.length; index += 1) { |
| | 108 | if (base64.charCodeAt(index) !== 0x3d) return null; |
| | 109 | } |
| | 110 | } |
| 56 | | 111 | |
| 57 | return { mimeType, base64 }; | 112 | return { mimeType, base64 }; |
| 58 | } | 113 | } |