[GH-ISSUE #3062] [Issue]: RunPod image extraction accepts unvalidated base64/image fallback fields, saving an error string as a corrupt image #1630

Closed
opened 2026-10-21 05:41:06 +07:00 by bsakaguchi · 2 comments
Owner

Originally created by @kolacheee on GitHub (Jun 29, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3062

Originally assigned to: @SpicyMarinara on GitHub.

What's wrong

In extractRunPodImage, the primary data field is validated with a base64 character-class regex before being accepted, but the base64/image fallback fields are accepted with NO validation — no regex check and no decoded-byte-length check. detectImageMimeType falls back to image/png for anything it can't sniff. So a COMPLETED RunPod response like { output: { images: [{ base64: "<error/garbage>" }] } } (or an image key holding a status/error string) is returned as a successful result and flows to saveImageToDisk, which does Buffer.from(garbage, "base64") and writes a corrupt file that is served to the user as their generated image. The data-path data-URL branch and the OpenAI/Stability decoders all reject empty/invalid base64; this fallback branch is the only extraction path with no validation.

How to reproduce (try it yourself)

  1. Point a runpod_comfyui connection at an endpoint whose handler returns { output: { images: [{ base64: "model not ready" }] } } (a non-base64 string in the fallback key, no data field).

You'll see: the function returns { base64: "model not ready", mimeType: "image/png", ext: "png" } instead of throwing, and a corrupt file (decoded from a 9-byte garbage buffer) is saved and served rather than the provider error being surfaced.

Expected: an invalid fallback value is skipped and the existing 'Could not extract image' error is surfaced.

Where it is

packages/server/src/services/image/runpod-comfyui.service.ts:

  • Fallback branch lines 244-256 — accepts img.base64/img.image with no regex and no decoded-length check, returning the string verbatim.
  • Contrast: the data branch validates with /^[A-Za-z0-9+/]*={0,2}$/ (line 234) and routes data URLs to decodeDataUrl (which rejects zero-length buffers, line 373).
  • extractRunPodImage has one caller, line 161 (the COMPLETED branch of generateRunPodComfyUI). Result flows to saveImageToDisk (image-generation.ts:222) with no intervening validation.

What's happening

The fallback branch skips the base64 validation the primary data branch applies and never verifies the decoded buffer is non-empty, so any string is treated as a valid image. The images array is typed as records of unknown, so a COMPLETED job whose base64 key holds an error string reaches this defensive fallback and is written to disk.

Suggested fix

Apply the same validation the data field uses to the base64/image fallback, and continue on failure so the loop tries the next image and ultimately hits the existing "Could not extract image" error. Replace the unvalidated block (lines 248-256) with:

if (fallbackBase64) {
  const trimmed = fallbackBase64.trim();
  if (trimmed.startsWith("data:")) {
    return decodeDataUrl(trimmed);
  }
  if (/^[A-Za-z0-9+/]*={0,2}$/.test(trimmed) && Buffer.from(trimmed, "base64").byteLength > 0) {
    const mimeType = detectImageMimeType(trimmed);
    return {
      base64: trimmed,
      mimeType,
      ext: imageExtensionFromMimeType(mimeType),
    };
  }
  // Not valid base64 (e.g. an error/status string) — skip this image.
  continue;
}

(The data: branch is optional parity with the data field; omitting it just lets data URLs fail the regex and fall through. Either way the corrupt-file bug is fixed.)

Regression avoided: the fix is contained in extractRunPodImage (one caller, no shared helper with other providers). A genuine PNG/JPEG base64 still passes the regex and byteLength > 0 check; empty/whitespace strings (already skipped by the if (fallbackBase64) guard, now also by the byteLength check) continue to the existing error fall-through, so error shape is unchanged. A data: URL accidentally placed in the fallback key is now rejected by the char-class regex rather than written as a corrupt raw-base64 file — strictly safer.


AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.

Originally created by @kolacheee on GitHub (Jun 29, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3062 Originally assigned to: @SpicyMarinara on GitHub. **What's wrong** In `extractRunPodImage`, the primary `data` field is validated with a base64 character-class regex before being accepted, but the `base64`/`image` fallback fields are accepted with NO validation — no regex check and no decoded-byte-length check. `detectImageMimeType` falls back to `image/png` for anything it can't sniff. So a COMPLETED RunPod response like `{ output: { images: [{ base64: "<error/garbage>" }] } }` (or an `image` key holding a status/error string) is returned as a successful result and flows to `saveImageToDisk`, which does `Buffer.from(garbage, "base64")` and writes a corrupt file that is served to the user as their generated image. The `data`-path data-URL branch and the OpenAI/Stability decoders all reject empty/invalid base64; this fallback branch is the only extraction path with no validation. **How to reproduce (try it yourself)** 1. Point a `runpod_comfyui` connection at an endpoint whose handler returns `{ output: { images: [{ base64: "model not ready" }] } }` (a non-base64 string in the fallback key, no `data` field). You'll see: the function returns `{ base64: "model not ready", mimeType: "image/png", ext: "png" }` instead of throwing, and a corrupt file (decoded from a 9-byte garbage buffer) is saved and served rather than the provider error being surfaced. Expected: an invalid fallback value is skipped and the existing 'Could not extract image' error is surfaced. **Where it is** `packages/server/src/services/image/runpod-comfyui.service.ts`: - Fallback branch lines 244-256 — accepts `img.base64`/`img.image` with no regex and no decoded-length check, returning the string verbatim. - Contrast: the `data` branch validates with `/^[A-Za-z0-9+/]*={0,2}$/` (line 234) and routes data URLs to `decodeDataUrl` (which rejects zero-length buffers, line 373). - `extractRunPodImage` has one caller, line 161 (the COMPLETED branch of `generateRunPodComfyUI`). Result flows to `saveImageToDisk` (image-generation.ts:222) with no intervening validation. **What's happening** The fallback branch skips the base64 validation the primary `data` branch applies and never verifies the decoded buffer is non-empty, so any string is treated as a valid image. The `images` array is typed as records of `unknown`, so a COMPLETED job whose `base64` key holds an error string reaches this defensive fallback and is written to disk. **Suggested fix** Apply the same validation the `data` field uses to the `base64`/`image` fallback, and `continue` on failure so the loop tries the next image and ultimately hits the existing "Could not extract image" error. Replace the unvalidated block (lines 248-256) with: ```ts if (fallbackBase64) { const trimmed = fallbackBase64.trim(); if (trimmed.startsWith("data:")) { return decodeDataUrl(trimmed); } if (/^[A-Za-z0-9+/]*={0,2}$/.test(trimmed) && Buffer.from(trimmed, "base64").byteLength > 0) { const mimeType = detectImageMimeType(trimmed); return { base64: trimmed, mimeType, ext: imageExtensionFromMimeType(mimeType), }; } // Not valid base64 (e.g. an error/status string) — skip this image. continue; } ``` (The `data:` branch is optional parity with the `data` field; omitting it just lets data URLs fail the regex and fall through. Either way the corrupt-file bug is fixed.) Regression avoided: the fix is contained in `extractRunPodImage` (one caller, no shared helper with other providers). A genuine PNG/JPEG base64 still passes the regex and `byteLength > 0` check; empty/whitespace strings (already skipped by the `if (fallbackBase64)` guard, now also by the byteLength check) `continue` to the existing error fall-through, so error shape is unchanged. A `data:` URL accidentally placed in the fallback key is now rejected by the char-class regex rather than written as a corrupt raw-base64 file — strictly safer. --- *AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.*
Author
Owner

@SpicyMarinara commented on GitHub (Jun 29, 2026):

Claiming this for the current staging sweep in draft PR #3064: https://github.com/Pasta-Devs/Marinara-Engine/pull/3064

<!-- gh-comment-id:4835429882 --> @SpicyMarinara commented on GitHub (Jun 29, 2026): Claiming this for the current staging sweep in draft PR #3064: https://github.com/Pasta-Devs/Marinara-Engine/pull/3064
Author
Owner

@SpicyMarinara commented on GitHub (Jun 29, 2026):

Fixed in PR #3064 on branch codex/issue-sweep-3054-3063 (commit 3ae8ca37). Closing this issue as addressed; the PR remains open for staging review/merge.

<!-- gh-comment-id:4835579875 --> @SpicyMarinara commented on GitHub (Jun 29, 2026): Fixed in PR #3064 on branch `codex/issue-sweep-3054-3063` (commit 3ae8ca37). Closing this issue as addressed; the PR remains open for staging review/merge.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#1630
No description provided.