Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

3.6 KB
Raw Blame
import { runInNewContext } from 'node:vm'
import { describe, expect, it } from 'vitest'
import { buildImageDataUri, validateRasterImageDataUri } from './image-data-uri'

function pngBase64(width = 1, height = 1): string {
  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.toString('base64')
}

describe('buildImageDataUri', () => {
  it('builds a data URI from base64 image bytes', () => {
    const content = pngBase64()
    expect(buildImageDataUri('image/png', content)).toBe(`data:image/png;base64,${content}`)
  })

  it('strips whitespace from line-wrapped base64 payloads', () => {
    const content = pngBase64()
    const wrapped = `${content.slice(0, 8)}\n${content.slice(8, 20)}\t ${content.slice(20)}\r\n`
    expect(buildImageDataUri('image/png', wrapped)).toBe(`data:image/png;base64,${content}`)
  })

  it('returns null for an empty payload', () => {
    expect(buildImageDataUri('image/png', '   \n')).toBeNull()
  })

  it('returns null for a missing mime type', () => {
    expect(buildImageDataUri(undefined, 'bmV3')).toBeNull()
  })

  it('returns null for application/pdf (not an <img> source)', () => {
    expect(buildImageDataUri('application/pdf', 'JVBER')).toBeNull()
  })

  it('returns null for a non-image mime such as application/octet-stream', () => {
    expect(buildImageDataUri('application/octet-stream', 'AAAA')).toBeNull()
  })

  it('rejects oversized known rasters before native decode', () => {
    expect(buildImageDataUri('image/png', pngBase64(32_769, 1))).toBeNull()
    expect(buildImageDataUri('image/png', pngBase64(8192, 8192))).toBeNull()
  })

  it('still renders bytes whose header we cannot measure', () => {
    // Unreadable headers do not prove oversize; decoder-supported formats beyond this parser must remain visible.
    expect(buildImageDataUri('image/png', 'bmV3')).toBe('data:image/png;base64,bmV3')
  })

  it('preserves SVG behavior because vectors do not have encoded raster dimensions', () => {
    expect(buildImageDataUri('image/svg+xml', 'PHN2Zy8+')).toBe(
      'data:image/svg+xml;base64,PHN2Zy8+'
    )
  })
})

describe('validateRasterImageDataUri', () => {
  it('preserves malformed parameter lists without exponential backtracking', () => {
    const input = `data:image/png;${';'.repeat(32_768)}`
    const result = runInNewContext(
      'validate(input)',
      { validate: validateRasterImageDataUri, input },
      {
        timeout: 250
      }
    )
    expect(result).toBe(input)
  })

  it('finds base64 among multiple parameters and preserves payload commas', () => {
    const content = pngBase64()
    expect(validateRasterImageDataUri(`data:image/png;charset=utf-8;;BASE64;,${content}`)).toBe(
      `data:image/png;base64,${content}`
    )
    expect(validateRasterImageDataUri('data:text/plain;charset=utf-8,one,two')).toBe(
      'data:text/plain;charset=utf-8,one,two'
    )
  })

  it('accepts a safe inline raster and rejects an oversized one', () => {
    const safe = `data:image/png;base64,${pngBase64()}`
    expect(validateRasterImageDataUri(safe)).toBe(safe)
    expect(validateRasterImageDataUri(`data:image/png;base64,${pngBase64(32_769, 1)}`)).toBeNull()
  })

  it('preserves non-raster data URIs and rejects non-base64 raster data', () => {
    expect(validateRasterImageDataUri('data:image/svg+xml,%3Csvg/%3E')).toBe(
      'data:image/svg+xml,%3Csvg/%3E'
    )
    expect(validateRasterImageDataUri('data:image/png,not-base64')).toBeNull()
  })
})