[GH-ISSUE #3811] [Issue]: Scene-video endpoints buffer full MP4s and ignore range requests #2055

Closed
opened 2026-10-21 05:44:58 +07:00 by bsakaguchi · 4 comments
Owner

Originally created by @Xelvanis on GitHub (Jul 19, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3811

Originally assigned to: @SpicyMarinara on GitHub.

Summary

The two scene-video file endpoints synchronously read each complete MP4 into a Buffer and do not honor HTTP byte-range requests. Playback and seeking can therefore allocate and transfer an entire video per request while blocking the Node event loop during disk reads.

Affected routes:

  • packages/server/src/routes/gallery.routes.ts — /api/gallery/scene-videos/file/:chatId/:filename
  • packages/server/src/routes/game.routes.ts — /api/game/scene-videos/file/:chatId/:filename

Expected behavior

Scene-video files should be streamed. A valid request such as Range: bytes=0-9 should return 206 Partial Content, Content-Range, Accept-Ranges: bytes, and only the requested bytes. Existing database ownership and path-containment checks must remain in place.

Actual behavior

Both endpoints call readFileSync(filePath) and send the resulting full buffer. Fastify therefore returns 200 with the complete file even when the request contains a valid Range header.

Generated-video downloads are allowed to reach VIDEO_GEN_MAX_RESPONSE_BYTES, which defaults to 167,772,160 bytes (160 MiB). One large playback request can therefore synchronously read and allocate roughly that amount; concurrent playback or repeated seeking can multiply the pressure, especially on Termux.

Steps to reproduce

  1. Generate a scene video in Roleplay or Game mode.
  2. Copy its /api/gallery/scene-videos/file/... or /api/game/scene-videos/file/... URL.
  3. Request a small byte range, for example:
    curl -i -H 'Range: bytes=0-9' 'http://127.0.0.1:7860/api/gallery/scene-videos/file/<chatId>/<filename>' -o /dev/null
    
  4. Observe a full 200 response without Accept-Ranges or Content-Range, rather than a 10-byte 206 response.

A controlled Fastify comparison reproduced the same behavior without a live video: sending a 1,024-byte buffer ignored Range: bytes=0-9 and returned all 1,024 bytes with status 200. Serving a file through the repository's installed reply.sendFile() infrastructure returned status 206 with a 10-byte body and the expected range headers.

Environment

  • Marinara Engine version: 2.3.3 / current source checkout
  • Install type: source; behavior also affects installer and Termux runtimes using these routes
  • OS + version: Platform-independent server behavior; controlled Fastify injection ran on a Windows host (version not recorded)
  • Browser or app shell: Any client using the scene-video <video> sources; browser execution was not required for the HTTP reproduction

Logs, screenshots, or video

Controlled response comparison:

{
  "buffer": {
    "status": 200,
    "length": "1024"
  },
  "sendFile": {
    "status": 206,
    "length": "10",
    "acceptRanges": "bytes",
    "contentRange": "bytes 0-9/1694"
  }
}

Additional context

The endpoints were introduced together in commit 22bb11ef3 as part of the scene-video generation workflow. Client consumers include native <video> elements in ChatImageLightbox.tsx, ChatGallery.tsx, GameSurface.tsx, and GameSessionReplay.tsx.

Smallest fix direction: retain the existing database-row authorization, filename validation, and assertInsideDir checks, then use a streaming path with standard range handling for both endpoints. Prefer the already-installed reply.sendFile()/send implementation if its decorator is guaranteed in API-only mode; otherwise centralize a small, tested streaming helper rather than hand-duplicating range parsing. Cover normal 200 streaming, 206 ranges, suffix/open-ended ranges, invalid 416 ranges, HEAD, and client disconnect cleanup.

No live 160 MiB file or real-browser playback test was run.

Template check

Please uncheck (untick) the box below before submitting so we know you read the template. It is intentionally pre-checked:

  • I DID NOT read this template and provide the requested details.
Originally created by @Xelvanis on GitHub (Jul 19, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3811 Originally assigned to: @SpicyMarinara on GitHub. ## Summary The two scene-video file endpoints synchronously read each complete MP4 into a `Buffer` and do not honor HTTP byte-range requests. Playback and seeking can therefore allocate and transfer an entire video per request while blocking the Node event loop during disk reads. Affected routes: - `packages/server/src/routes/gallery.routes.ts` — `/api/gallery/scene-videos/file/:chatId/:filename` - `packages/server/src/routes/game.routes.ts` — `/api/game/scene-videos/file/:chatId/:filename` ## Expected behavior Scene-video files should be streamed. A valid request such as `Range: bytes=0-9` should return `206 Partial Content`, `Content-Range`, `Accept-Ranges: bytes`, and only the requested bytes. Existing database ownership and path-containment checks must remain in place. ## Actual behavior Both endpoints call `readFileSync(filePath)` and send the resulting full buffer. Fastify therefore returns `200` with the complete file even when the request contains a valid `Range` header. Generated-video downloads are allowed to reach `VIDEO_GEN_MAX_RESPONSE_BYTES`, which defaults to 167,772,160 bytes (160 MiB). One large playback request can therefore synchronously read and allocate roughly that amount; concurrent playback or repeated seeking can multiply the pressure, especially on Termux. ## Steps to reproduce 1. Generate a scene video in Roleplay or Game mode. 2. Copy its `/api/gallery/scene-videos/file/...` or `/api/game/scene-videos/file/...` URL. 3. Request a small byte range, for example: ```sh curl -i -H 'Range: bytes=0-9' 'http://127.0.0.1:7860/api/gallery/scene-videos/file/<chatId>/<filename>' -o /dev/null ``` 4. Observe a full `200` response without `Accept-Ranges` or `Content-Range`, rather than a 10-byte `206` response. A controlled Fastify comparison reproduced the same behavior without a live video: sending a 1,024-byte buffer ignored `Range: bytes=0-9` and returned all 1,024 bytes with status 200. Serving a file through the repository's installed `reply.sendFile()` infrastructure returned status 206 with a 10-byte body and the expected range headers. ## Environment - Marinara Engine version: 2.3.3 / current source checkout - Install type: source; behavior also affects installer and Termux runtimes using these routes - OS + version: Platform-independent server behavior; controlled Fastify injection ran on a Windows host (version not recorded) - Browser or app shell: Any client using the scene-video `<video>` sources; browser execution was not required for the HTTP reproduction ## Logs, screenshots, or video Controlled response comparison: ```json { "buffer": { "status": 200, "length": "1024" }, "sendFile": { "status": 206, "length": "10", "acceptRanges": "bytes", "contentRange": "bytes 0-9/1694" } } ``` ## Additional context The endpoints were introduced together in commit `22bb11ef3` as part of the scene-video generation workflow. Client consumers include native `<video>` elements in `ChatImageLightbox.tsx`, `ChatGallery.tsx`, `GameSurface.tsx`, and `GameSessionReplay.tsx`. Smallest fix direction: retain the existing database-row authorization, filename validation, and `assertInsideDir` checks, then use a streaming path with standard range handling for both endpoints. Prefer the already-installed `reply.sendFile()`/`send` implementation if its decorator is guaranteed in API-only mode; otherwise centralize a small, tested streaming helper rather than hand-duplicating range parsing. Cover normal 200 streaming, 206 ranges, suffix/open-ended ranges, invalid 416 ranges, HEAD, and client disconnect cleanup. No live 160 MiB file or real-browser playback test was run. ## Template check Please **uncheck (untick)** the box below before submitting so we know you read the template. It is intentionally pre-checked: - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-21 05:44:58 +07:00
  • closed this issue
  • added the
    in-pr
    label
Author
Owner

@SpicyMarinara commented on GitHub (Jul 19, 2026):

Claimed by @SpicyMarinara for the assigned issue sweep on agent/assigned-issue-sweep. I will keep the fix scoped to this issue and link the draft PR once the first proof-backed patch is pushed.

<!-- gh-comment-id:5015840520 --> @SpicyMarinara commented on GitHub (Jul 19, 2026): Claimed by @SpicyMarinara for the assigned issue sweep on `agent/assigned-issue-sweep`. I will keep the fix scoped to this issue and link the draft PR once the first proof-backed patch is pushed.
Author
Owner

@SpicyMarinara commented on GitHub (Jul 19, 2026):

Implementation will continue in draft PR #3814 after the launcher slice.

<!-- gh-comment-id:5015880157 --> @SpicyMarinara commented on GitHub (Jul 19, 2026): Implementation will continue in draft PR #3814 after the launcher slice.
Author
Owner

@SpicyMarinara commented on GitHub (Jul 19, 2026):

Fixed in draft PR #3814. Both authenticated scene-video routes retain their row-ownership, filename, existence, and containment checks, then stream through the existing Fastify file sender. The decorator is now available in API-only mode too. Regression coverage passes for full 200 responses, fixed/suffix/open-ended 206 ranges, invalid 416 ranges, HEAD, immutable caching, and both route implementations; server TypeScript validation also passes.

<!-- gh-comment-id:5015910379 --> @SpicyMarinara commented on GitHub (Jul 19, 2026): Fixed in draft PR #3814. Both authenticated scene-video routes retain their row-ownership, filename, existence, and containment checks, then stream through the existing Fastify file sender. The decorator is now available in API-only mode too. Regression coverage passes for full 200 responses, fixed/suffix/open-ended 206 ranges, invalid 416 ranges, HEAD, immutable caching, and both route implementations; server TypeScript validation also passes.
Author
Owner

@SpicyMarinara commented on GitHub (Jul 19, 2026):

Addressed in ready-for-review PR #3814. Both protected scene-video routes retain their authorization and containment checks while streaming MP4 files with GET, HEAD, and byte-range support. Focused HTTP regressions and the full local validation gate pass.

<!-- gh-comment-id:5015945060 --> @SpicyMarinara commented on GitHub (Jul 19, 2026): Addressed in ready-for-review PR #3814. Both protected scene-video routes retain their authorization and containment checks while streaming MP4 files with GET, HEAD, and byte-range support. Focused HTTP regressions and the full local validation gate pass.
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#2055
No description provided.