Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

test(codex): cover 5 MiB JPEG and combination budgets

Alfredo Lopez5c89ed41acc6
diff --git a/control-api/package-lock.json b/control-api/package-lock.json
index 94148d62e..997496ac6 100644
--- a/control-api/package-lock.json
+++ b/control-api/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-api",
- "version": "0.1.553",
+ "version": "0.1.554",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-api",
- "version": "0.1.553",
+ "version": "0.1.554",
"dependencies": {
"@clerum/display-field": "file:../packages/display-field",
"@clerum/image-policy": "file:../packages/image-policy",
diff --git a/control-api/package.json b/control-api/package.json
index 989dbe548..4a60ece3d 100644
--- a/control-api/package.json
+++ b/control-api/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-api",
- "version": "0.1.553",
+ "version": "0.1.554",
"description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets",
"main": "dist/main.js",
"engines": {
diff --git a/control-api/test/services.llmProviderAttemptAuthorizer.test.ts b/control-api/test/services.llmProviderAttemptAuthorizer.test.ts
index 271599ab3..30caab115 100644
--- a/control-api/test/services.llmProviderAttemptAuthorizer.test.ts
+++ b/control-api/test/services.llmProviderAttemptAuthorizer.test.ts
@@ -70,6 +70,72 @@ const REQUEST = {
messages: [{ role: 'user' as const, content: 'hello' }],
}
+const MIB = 1024 * 1024
+
+/**
+ * Structurally framed JPEG of an exact byte length. Matches the shared
+ * contract helper: SOI, SOF0 dimensions, SOS, entropy pad, EOI.
+ */
+function jpegOfSize(targetBytes: number, width = 2, height = 2): Buffer {
+ const sofPayload = Buffer.alloc(9)
+ sofPayload[0] = 8
+ sofPayload.writeUInt16BE(height, 1)
+ sofPayload.writeUInt16BE(width, 3)
+ sofPayload[5] = 1
+ sofPayload[6] = 1
+ sofPayload[7] = 0x11
+ sofPayload[8] = 0
+ const segmentLength = Buffer.alloc(2)
+ segmentLength.writeUInt16BE(sofPayload.length + 2, 0)
+ const sof = Buffer.concat([Buffer.from([0xff, 0xc0]), segmentLength, sofPayload])
+ const sosPayload = Buffer.alloc(6)
+ const sosLength = Buffer.alloc(2)
+ sosLength.writeUInt16BE(sosPayload.length + 2, 0)
+ const sos = Buffer.concat([Buffer.from([0xff, 0xda]), sosLength, sosPayload])
+ const entropyLength = targetBytes - (2 + sof.length + sos.length + 2)
+ return Buffer.concat([
+ Buffer.from([0xff, 0xd8]),
+ sof,
+ sos,
+ Buffer.alloc(entropyLength, 0x2a),
+ Buffer.from([0xff, 0xd9]),
+ ])
+}
+
+function padPngToSize(pngBase64: string, imageBytes: number): string {
+ const png = Buffer.from(pngBase64, 'base64')
+ const payload = Buffer.alloc(imageBytes - png.length - 12, 65)
+ Buffer.from('Comment\0').copy(payload)
+ const chunk = Buffer.alloc(payload.length + 12)
+ chunk.writeUInt32BE(payload.length)
+ chunk.write('tEXt', 4)
+ payload.copy(chunk, 8)
+ chunk.writeUInt32BE(crc32(chunk.subarray(4, -4)) >>> 0, chunk.length - 4)
+ return Buffer.concat([png.subarray(0, -12), chunk, png.subarray(-12)]).toString('base64')
+}
+
+type VisualImagePart = {
+ type: string
+ mimeType: string
+ data: string
+ source?: { kind: string; attachmentId: string; messageId?: string; toolCallId?: string }
+}
+
+function loadVisualFixtures(): Record<
+ string,
+ { messages: Array<{ contentParts: VisualImagePart[] }> }
+> {
+ return JSON.parse(
+ readFileSync(
+ new URL(
+ '../../packages/llm-provider-attempt-contract/fixtures/visual-requests.json',
+ import.meta.url
+ ),
+ 'utf8'
+ )
+ )
+}
+
function claims(overrides: Partial<McpHostAccessClaims> = {}): McpHostAccessClaims {
return {
sub: 'default/research-host',
@@ -163,18 +229,11 @@ describe('authorizeLlmProviderAttempt', () => {
['jpeg', 'tool', false],
['png', 'attachment', false, 10 * 1024 * 1024],
['png', 'tool', true, 5 * 1024 * 1024],
+ ['jpeg', 'attachment', true, 5 * 1024 * 1024],
] as const)(
'carries %s from %s (tools=%s) through Host authorization and proxy projection',
async (format, origin, withTools, imageBytes = 0) => {
- const fixtures = JSON.parse(
- readFileSync(
- new URL(
- '../../packages/llm-provider-attempt-contract/fixtures/visual-requests.json',
- import.meta.url
- ),
- 'utf8'
- )
- )
+ const fixtures = loadVisualFixtures()
let authorized: Awaited<ReturnType<typeof authorizeLlmProviderAttempt>> | undefined
const authorizer = new ProviderAttemptAuthorizer({
authorizeUrl: 'http://test-control/authorize',
@@ -257,19 +316,10 @@ describe('authorizeLlmProviderAttempt', () => {
(part: { type: string }) => part.type === 'image'
)
if (imageBytes > 0) {
- // Preserve the real PNG pixels and add a standards-compliant ancillary
- // text chunk to exercise actual byte transport, not a forged header.
- const png = Buffer.from(image.data, 'base64')
- const payload = Buffer.alloc(imageBytes - png.length - 12, 65)
- Buffer.from('Comment\0').copy(payload)
- const chunk = Buffer.alloc(payload.length + 12)
- chunk.writeUInt32BE(payload.length)
- chunk.write('tEXt', 4)
- payload.copy(chunk, 8)
- chunk.writeUInt32BE(crc32(chunk.subarray(4, -4)) >>> 0, chunk.length - 4)
- image.data = Buffer.concat([png.subarray(0, -12), chunk, png.subarray(-12)]).toString(
- 'base64'
- )
+ image.data =
+ format === 'png'
+ ? padPngToSize(image.data, imageBytes)
+ : jpegOfSize(imageBytes).toString('base64')
expect(Buffer.from(image.data, 'base64')).toHaveLength(imageBytes)
}
let messages = fixtures[format].messages
@@ -316,7 +366,144 @@ describe('authorizeLlmProviderAttempt', () => {
}
expect(result.content).toBe('image received')
expect(finalize).toHaveBeenCalledOnce()
- }
+ },
+ 60_000
+ )
+
+ it.each([
+ {
+ name: '10MiB PNG + 5MiB JPEG',
+ images: [
+ { format: 'png' as const, bytes: 10 * MIB },
+ { format: 'jpeg' as const, bytes: 5 * MIB },
+ ],
+ },
+ {
+ name: 'three 5MiB PNGs',
+ images: [
+ { format: 'png' as const, bytes: 5 * MIB },
+ { format: 'png' as const, bytes: 5 * MIB },
+ { format: 'png' as const, bytes: 5 * MIB },
+ ],
+ },
+ ])(
+ 'carries $name through Host authorization and proxy projection',
+ async ({ images }) => {
+ const fixtures = loadVisualFixtures()
+ const parts = images.map((item, index) => {
+ const template = fixtures[item.format].messages[0].contentParts.find(
+ part => part.type === 'image'
+ )
+ expect(template).toBeDefined()
+ const data =
+ item.format === 'png'
+ ? padPngToSize(template!.data, item.bytes)
+ : jpegOfSize(item.bytes).toString('base64')
+ expect(Buffer.from(data, 'base64')).toHaveLength(item.bytes)
+ return {
+ ...template!,
+ data,
+ source: {
+ kind: 'attachment' as const,
+ attachmentId: `att-${index}`,
+ messageId: 'msg-1',
+ },
+ }
+ })
+ let authorized: Awaited<ReturnType<typeof authorizeLlmProviderAttempt>> | undefined
+ const authorizer = new ProviderAttemptAuthorizer({
+ authorizeUrl: 'http://test-control/authorize',
+ readPlatformJwt: () => FIXTURE_ACCESS_VALUE,
+ fetchFn: async (_url, init) => {
+ authorized = await authorizeLlmProviderAttempt(
+ claims(),
+ JSON.parse(String(init?.body)),
+ deps()
+ )
+ return Response.json(authorized)
+ },
+ })
+ let projectedBody: Record<string, unknown> | undefined
+ const finalize = vi.fn(async () => ({
+ providerAttemptId: authorized!.providerAttemptId,
+ outcome: 'success' as const,
+ duplicate: false,
+ }))
+ const proxy = new CodexLlmProxyClient({
+ runtimeUrl: 'http://test-proxy/completions',
+ readPlatformJwt: () => FIXTURE_ACCESS_VALUE,
+ fetchFn: async (_url, init) => {
+ expect(authorized).toBeDefined()
+ const envelope = JSON.parse(String(init?.body))
+ const frames: unknown[] = []
+ const result = await streamCodexCompletion({
+ ...envelope,
+ imageInputEnabled: true,
+ ticket: {
+ jti: 'fixture-ticket',
+ hostRef: 'research-host',
+ model: REQUEST.model,
+ requestHash: authorized!.requestHash,
+ providerAttemptId: authorized!.providerAttemptId,
+ },
+ redeem: async () => ({
+ accessToken: FIXTURE_ACCESS_VALUE,
+ chatgptAccountId: 'fixture-account',
+ attemptReceipt: 'a'.repeat(64),
+ expiryClass: 'short_lived',
+ transport: {
+ protocolVersion: 'codex-subscription-transport.v1',
+ completionsOrigin: CODEX_COMPLETIONS_ORIGIN,
+ catalogOrigin: CODEX_CATALOG_ORIGIN,
+ operation: 'completion_stream',
+ servedModel: REQUEST.model,
+ maxStreamDurationMs: 1000,
+ },
+ }),
+ finalize,
+ lookup: async () => [{ address: '1.2.3.4', family: 4 }],
+ fetchFn: async (_upstream, requestInit) => {
+ projectedBody = JSON.parse(String(requestInit?.body))
+ return new Response(
+ 'data: {"type":"response.output_text.delta","delta":"image received"}\n\ndata: {"type":"response.completed"}\n\n'
+ )
+ },
+ onFrame: frame => frames.push(frame),
+ })
+ frames.push({ type: 'done', ...result })
+ return new Response(frames.map(frame => `data: ${JSON.stringify(frame)}\n\n`).join(''))
+ },
+ })
+ const provider = new CodexSubscriptionProvider(REQUEST.model, {
+ authorizer,
+ proxy,
+ imageInputEnabled: true,
+ attemptContext: () => ({
+ policyRevision: 4,
+ policyHash: body().policyHash,
+ hostRef: 'research-host',
+ }),
+ })
+ const result = await provider.completeSingleTurn([
+ {
+ role: 'user',
+ content: 'Describe the attached image',
+ contentParts: [{ type: 'text', text: 'Describe the attached image' }, ...parts],
+ },
+ ])
+ const projectedParts = (
+ projectedBody!.input as Array<{ role?: string; content: unknown[] }>
+ ).find(item => item.role === 'user')!.content
+ for (const part of parts) {
+ expect(projectedParts).toContainEqual({
+ type: 'input_image',
+ image_url: `data:${part.mimeType};base64,${part.data}`,
+ })
+ }
+ expect(result.content).toBe('image received')
+ expect(finalize).toHaveBeenCalledOnce()
+ },
+ 60_000
)
it('checks the V2 proxy envelope inside the transaction, after signing and before commit', async () => {
diff --git a/desktop-app/ui/src/components/agents/__tests__/ComposerPanel.test.tsx b/desktop-app/ui/src/components/agents/__tests__/ComposerPanel.test.tsx
index 88bb542d3..301a2bffa 100644
--- a/desktop-app/ui/src/components/agents/__tests__/ComposerPanel.test.tsx
+++ b/desktop-app/ui/src/components/agents/__tests__/ComposerPanel.test.tsx
@@ -249,8 +249,8 @@ const MIB = 1024 * 1024
* size drives the real code path without allocating and base64-encoding 10MiB
* blobs per case.
*/
-function imageFile(name: string, sizeBytes: number): File {
- const file = new File([new Uint8Array([0x41])], name, { type: 'image/png' })
+function imageFile(name: string, sizeBytes: number, type = 'image/png'): File {
+ const file = new File([new Uint8Array([0x41])], name, { type })
Object.defineProperty(file, 'size', { configurable: true, value: sizeBytes })
return file
}
@@ -307,6 +307,15 @@ describe('ComposerPanel image budget', () => {
expect(screen.queryByRole('alert')).toBeNull()
})
+ it('accepts a 5MiB JPEG at the same budget as PNG', async () => {
+ const { container } = render(<ComposerPanel inline={false} />)
+ pickFiles(container, [imageFile('photo.jpg', 5 * MIB, 'image/jpeg')])
+ await waitFor(() => expect(attachedImages()).toHaveLength(1))
+ expect(attachedImages()[0]?.mimeType).toBe('image/jpeg')
+ expect(attachedImages()[0]?.sizeBytes).toBe(5 * MIB)
+ expect(screen.queryByRole('alert')).toBeNull()
+ })
+
it('refuses an image over the 10MiB limit and names the limit in MiB', async () => {
const { container } = render(<ComposerPanel inline={false} />)
pickFiles(container, [imageFile('over.png', 11 * MIB)])
diff --git a/external-rest-api/src/releaseManifest.ts b/external-rest-api/src/releaseManifest.ts
index 6cef924d4..7a70ede00 100644
--- a/external-rest-api/src/releaseManifest.ts
+++ b/external-rest-api/src/releaseManifest.ts
@@ -9,7 +9,7 @@ export type ReleaseManifest = {
export const releaseManifest: ReleaseManifest = {
releaseId: 'v0.8.1',
externalRestApiVersion: '0.1.82',
- rpcProxyVersion: '0.1.78',
+ rpcProxyVersion: '0.1.79',
desktopVersion: '0.8.1',
minimumDesktopVersion: '0.1.252',
}
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index f05a8e63b..57e1f20e3 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.340",
+ "version": "0.1.341",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.340",
+ "version": "0.1.341",
"license": "SEE LICENSE IN LICENSE",
"dependencies": {
"@anthropic-ai/sdk": "^0.32.0",
diff --git a/mcp-host/package.json b/mcp-host/package.json
index 0b6cff67a..cbe5371b8 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.340",
+ "version": "0.1.341",
"description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude",
"main": "dist/main.js",
"engines": {
diff --git a/mcp-host/src/__tests__/bodylimits.test.ts b/mcp-host/src/__tests__/bodylimits.test.ts
index 6cb3937c9..5e10c9c73 100644
--- a/mcp-host/src/__tests__/bodylimits.test.ts
+++ b/mcp-host/src/__tests__/bodylimits.test.ts
@@ -3,7 +3,7 @@
*
* Proves on a real listening `RPCServer` socket:
* - `POST /v1/runtime/messages` carries the documented image payloads (a 10MiB
- * image, 10MiB + 5MiB, and three 5MiB images) to the message handler instead
+ * image, a 5MiB JPEG, 10MiB + 5MiB, and three 5MiB images) to the message handler instead
* of being rejected as too large.
* - The attachments arrive byte-identical: nothing is dropped, truncated or
* re-encoded on the way through the parser.
@@ -20,6 +20,7 @@ import type { IncomingMessage } from '../server/types'
const MIB = 1024 * 1024
const PNG_SIGNATURE = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
+const JPEG_SIGNATURE = Buffer.from([0xff, 0xd8, 0xff])
const BASE64_ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
let baseUrl: string
@@ -48,19 +49,29 @@ afterAll(async () => {
})
const pngBase64Cache = new Map<number, string>()
+const jpegBase64Cache = new Map<number, string>()
-/** Canonical base64 of a PNG whose decoded length is exactly `sizeBytes`. */
-function pngBase64(sizeBytes: number): string {
- const cached = pngBase64Cache.get(sizeBytes)
+function imageBase64(cache: Map<number, string>, signature: Buffer, sizeBytes: number): string {
+ const cached = cache.get(sizeBytes)
if (cached !== undefined) return cached
const bytes = Buffer.alloc(sizeBytes)
- PNG_SIGNATURE.copy(bytes, 0)
- bytes.fill(0x41, PNG_SIGNATURE.length)
+ signature.copy(bytes, 0)
+ bytes.fill(0x41, signature.length)
const encoded = bytes.toString('base64')
- pngBase64Cache.set(sizeBytes, encoded)
+ cache.set(sizeBytes, encoded)
return encoded
}
+/** Canonical base64 of a PNG whose decoded length is exactly `sizeBytes`. */
+function pngBase64(sizeBytes: number): string {
+ return imageBase64(pngBase64Cache, PNG_SIGNATURE, sizeBytes)
+}
+
+/** Canonical base64 of a JPEG whose decoded length is exactly `sizeBytes`. */
+function jpegBase64(sizeBytes: number): string {
+ return imageBase64(jpegBase64Cache, JPEG_SIGNATURE, sizeBytes)
+}
+
/**
* Flip a spare bit inside the final padded sextet. The payload still decodes to
* the same length, but it is no longer the canonical encoding of those bytes,
@@ -76,14 +87,19 @@ function withNonCanonicalTailBits(base64: string): string {
return base64.slice(0, lastIndex) + mutated + base64.slice(lastIndex + 1)
}
-function imageAttachment(id: string, sizeBytes: number) {
+function imageAttachment(
+ id: string,
+ sizeBytes: number,
+ mimeType: 'image/png' | 'image/jpeg' = 'image/png'
+) {
+ const isJpeg = mimeType === 'image/jpeg'
return {
id,
kind: 'image' as const,
- mimeType: 'image/png' as const,
+ mimeType,
encoding: 'base64' as const,
- dataBase64: pngBase64(sizeBytes),
- filename: `${id}.png`,
+ dataBase64: isJpeg ? jpegBase64(sizeBytes) : pngBase64(sizeBytes),
+ filename: `${id}.${isJpeg ? 'jpg' : 'png'}`,
}
}
@@ -127,6 +143,15 @@ describe('mcp-host runtime message body budget', () => {
expect(captured[0].attachments).toEqual([attachment])
})
+ it('delivers a 5MiB JPEG byte-identical', async () => {
+ captured = []
+ const attachment = imageAttachment('a1', 5 * MIB, 'image/jpeg')
+ const response = await postMessage(messagePayload([attachment]))
+ expect(response.status).toBe(200)
+ expect(captured).toHaveLength(1)
+ expect(captured[0].attachments).toEqual([attachment])
+ })
+
it('delivers 10MiB + 5MiB (the 15MiB total limit) in order', async () => {
captured = []
const attachments = [imageAttachment('a1', 10 * MIB), imageAttachment('a2', 5 * MIB)]
@@ -138,6 +163,18 @@ describe('mcp-host runtime message body budget', () => {
)
})
+ it('delivers 10MiB PNG + 5MiB JPEG in order', async () => {
+ captured = []
+ const attachments = [
+ imageAttachment('a1', 10 * MIB),
+ imageAttachment('a2', 5 * MIB, 'image/jpeg'),
+ ]
+ const response = await postMessage(messagePayload(attachments))
+ expect(response.status).toBe(200)
+ expect(captured).toHaveLength(1)
+ expect(captured[0].attachments).toEqual(attachments)
+ })
+
it('delivers three 5MiB images in order', async () => {
captured = []
const attachments = [
diff --git a/packages/llm-provider-attempt-contract/index.test.cjs b/packages/llm-provider-attempt-contract/index.test.cjs
index 77df9820d..45e8bff54 100644
--- a/packages/llm-provider-attempt-contract/index.test.cjs
+++ b/packages/llm-provider-attempt-contract/index.test.cjs
@@ -881,6 +881,10 @@ test('v2 limits: image count, encoder bound, per-image bytes, total bytes, dimen
v2WithParts([imagePart(jpegOfSize(FORMER_IMAGE_CEILING + 1).toString('base64'), 'image/jpeg')])
)
assert.equal(acceptedJpeg.ok, true, acceptedJpeg.message)
+ const acceptedFiveMiBJpeg = contract.parseCodexCompletionRequest(
+ v2WithParts([imagePart(jpegOfSize(5 * MIB).toString('base64'), 'image/jpeg')])
+ )
+ assert.equal(acceptedFiveMiBJpeg.ok, true, acceptedFiveMiBJpeg.message)
// 3 MiB, and exactly the per-image ceiling, are inside the budget.
const acceptedThreeMiB = contract.parseCodexCompletionRequest(
@@ -914,6 +918,10 @@ test('v2 limits: image count, encoder bound, per-image bytes, total bytes, dimen
v2WithParts([imagePart(tenMiB), imagePart(fiveMiB)])
)
assert.equal(combined.ok, true, combined.message)
+ const combinedWithJpeg = contract.parseCodexCompletionRequest(
+ v2WithParts([imagePart(tenMiB), imagePart(jpegOfSize(5 * MIB).toString('base64'), 'image/jpeg')])
+ )
+ assert.equal(combinedWithJpeg.ok, true, combinedWithJpeg.message)
const overAggregate = contract.parseCodexCompletionRequest(
v2WithParts([
imagePart(tenMiB),
diff --git a/rpc-proxy/package-lock.json b/rpc-proxy/package-lock.json
index 75e769a23..0f20dcc1b 100644
--- a/rpc-proxy/package-lock.json
+++ b/rpc-proxy/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-rpc-proxy",
- "version": "0.1.78",
+ "version": "0.1.79",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-rpc-proxy",
- "version": "0.1.78",
+ "version": "0.1.79",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.21.2",
diff --git a/rpc-proxy/package.json b/rpc-proxy/package.json
index 78252bf44..3c2c83742 100644
--- a/rpc-proxy/package.json
+++ b/rpc-proxy/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-rpc-proxy",
- "version": "0.1.78",
+ "version": "0.1.79",
"description": "External-facing RPC proxy for tenant-scoped MCP access",
"main": "dist/main.js",
"engines": {
diff --git a/rpc-proxy/src/__tests__/bodylimits.test.ts b/rpc-proxy/src/__tests__/bodylimits.test.ts
index fa1cc0674..e3d2c44f6 100644
--- a/rpc-proxy/src/__tests__/bodylimits.test.ts
+++ b/rpc-proxy/src/__tests__/bodylimits.test.ts
@@ -3,7 +3,7 @@
*
* Proves on a real listening `createApp()` socket:
* - `POST /api/v1/rpc/hosts/:hostRef/messages` carries the documented image
- * payloads (a 10MiB image, 10MiB + 5MiB, and three 5MiB images) to the auth
+ * payloads (a 10MiB image, a 5MiB JPEG, 10MiB + 5MiB, and three 5MiB images) to the auth
* boundary instead of being rejected as too large.
* - The larger ceiling is NOT a general text allowance: non-image bytes stay
* capped at 6MiB, and every other route keeps its 6MiB parser.
@@ -20,6 +20,7 @@ import { createApp } from '../app.js'
const MIB = 1024 * 1024
const PNG_SIGNATURE = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
+const JPEG_SIGNATURE = Buffer.from([0xff, 0xd8, 0xff])
const BASE64_ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
let baseUrl: string
@@ -40,19 +41,29 @@ afterAll(async () => {
})
const pngBase64Cache = new Map<number, string>()
+const jpegBase64Cache = new Map<number, string>()
-/** Canonical base64 of a PNG whose decoded length is exactly `sizeBytes`. */
-function pngBase64(sizeBytes: number): string {
- const cached = pngBase64Cache.get(sizeBytes)
+function imageBase64(cache: Map<number, string>, signature: Buffer, sizeBytes: number): string {
+ const cached = cache.get(sizeBytes)
if (cached !== undefined) return cached
const bytes = Buffer.alloc(sizeBytes)
- PNG_SIGNATURE.copy(bytes, 0)
- bytes.fill(0x41, PNG_SIGNATURE.length)
+ signature.copy(bytes, 0)
+ bytes.fill(0x41, signature.length)
const encoded = bytes.toString('base64')
- pngBase64Cache.set(sizeBytes, encoded)
+ cache.set(sizeBytes, encoded)
return encoded
}
+/** Canonical base64 of a PNG whose decoded length is exactly `sizeBytes`. */
+function pngBase64(sizeBytes: number): string {
+ return imageBase64(pngBase64Cache, PNG_SIGNATURE, sizeBytes)
+}
+
+/** Canonical base64 of a JPEG whose decoded length is exactly `sizeBytes`. */
+function jpegBase64(sizeBytes: number): string {
+ return imageBase64(jpegBase64Cache, JPEG_SIGNATURE, sizeBytes)
+}
+
/**
* Flip a spare bit inside the final padded sextet. The payload still decodes to
* the same length, but it is no longer the canonical encoding of those bytes,
@@ -68,14 +79,19 @@ function withNonCanonicalTailBits(base64: string): string {
return base64.slice(0, lastIndex) + mutated + base64.slice(lastIndex + 1)
}
-function imageAttachment(id: string, sizeBytes: number) {
+function imageAttachment(
+ id: string,
+ sizeBytes: number,
+ mimeType: 'image/png' | 'image/jpeg' = 'image/png'
+) {
+ const isJpeg = mimeType === 'image/jpeg'
return {
id,
kind: 'image',
- mimeType: 'image/png',
+ mimeType,
encoding: 'base64',
- dataBase64: pngBase64(sizeBytes),
- filename: `${id}.png`,
+ dataBase64: isJpeg ? jpegBase64(sizeBytes) : pngBase64(sizeBytes),
+ filename: `${id}.${isJpeg ? 'jpg' : 'png'}`,
}
}
@@ -105,6 +121,26 @@ describe('rpc-proxy chat message body budget', () => {
expect(response.status).toBe(401)
})
+ it('carries a 5MiB JPEG to the auth boundary', async () => {
+ const body = JSON.stringify({
+ content: 'look',
+ attachments: [imageAttachment('a1', 5 * MIB, 'image/jpeg')],
+ })
+ expect(Buffer.byteLength(body)).toBeGreaterThan(6 * MIB)
+ const response = await post(MESSAGE_PATH, body)
+ expect(response.status).toBe(401)
+ })
+
+ it('carries 10MiB PNG + 5MiB JPEG to the auth boundary', async () => {
+ const body = JSON.stringify({
+ content: 'look',
+ attachments: [imageAttachment('a1', 10 * MIB), imageAttachment('a2', 5 * MIB, 'image/jpeg')],
+ })
+ expect(Buffer.byteLength(body)).toBeGreaterThan(20 * MIB)
+ const response = await post(MESSAGE_PATH, body)
+ expect(response.status).toBe(401)
+ })
+
it('carries 10MiB + 5MiB (the 15MiB total limit) to the auth boundary', async () => {
const body = JSON.stringify({
content: 'look',