@@ -172,6 +172,9 @@ const HOST_READ_DECLARED_TEXT_MIMES = new Set([...HOST_READ_TEXT_PLAIN_ALIASES,
|
172 | 172 | const HOST_READ_DECLARED_TEXT_ERROR = |
173 | 173 | "hostReadCapability permits only validated plain-text documents " + |
174 | 174 | "and trusted generated HTML reports for local reads"; |
| 175 | +const HOST_READ_TEXT_PLAIN_EXTENSION_BY_MIME: Record<string, readonly string[]> = { |
| 176 | +"text/plain": [".txt"], |
| 177 | +}; |
175 | 178 | const MB = 1024 * 1024; |
176 | 179 | |
177 | 180 | function stripLegacyMediaDirectivePrefix(mediaUrl: string): string { |
@@ -315,6 +318,18 @@ function isTrustedGeneratedHostReadHtml(params: {
|
315 | 318 | return text !== undefined && hasHtmlDocumentShape(text); |
316 | 319 | } |
317 | 320 | |
| 321 | +function isAllowedHostReadTextAlias(mime: string | undefined, filePath?: string): boolean { |
| 322 | +if (!mime || !HOST_READ_TEXT_PLAIN_ALIASES.has(mime)) { |
| 323 | +return false; |
| 324 | +} |
| 325 | +const allowedExtensions = HOST_READ_TEXT_PLAIN_EXTENSION_BY_MIME[mime]; |
| 326 | +if (!allowedExtensions) { |
| 327 | +return true; |
| 328 | +} |
| 329 | +const ext = getFileExtension(filePath); |
| 330 | +return !!ext && allowedExtensions.includes(ext); |
| 331 | +} |
| 332 | + |
318 | 333 | function formatMb(bytes: number, digits = 2): string { |
319 | 334 | return (bytes / MB).toFixed(digits); |
320 | 335 | } |
@@ -364,7 +379,7 @@ function assertHostReadMediaAllowed(params: {
|
364 | 379 | return; |
365 | 380 | } |
366 | 381 | if ( |
367 | | -HOST_READ_TEXT_PLAIN_ALIASES.has(declaredMime) && |
| 382 | +isAllowedHostReadTextAlias(declaredMime, params.filePath) && |
368 | 383 | !params.sniffedContentType && |
369 | 384 | params.buffer && |
370 | 385 | isValidatedHostReadText(params.buffer) |
@@ -399,7 +414,7 @@ function assertHostReadMediaAllowed(params: {
|
399 | 414 | if ( |
400 | 415 | !sniffedMime && |
401 | 416 | normalizedMime && |
402 | | -HOST_READ_TEXT_PLAIN_ALIASES.has(normalizedMime) && |
| 417 | +isAllowedHostReadTextAlias(normalizedMime, params.filePath) && |
403 | 418 | params.buffer && |
404 | 419 | isValidatedHostReadText(params.buffer) |
405 | 420 | ) { |
|