[PR #8819] [MERGED] fix(server): hold game assets, previews and the avatar cleanup to their real folders #8827

Closed
opened 2026-11-07 17:40:00 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/8819
Author: @SpicyMarinara
Created: 11/4/2026
Status: ✅ Merged
Merged: 11/4/2026
Merged by: @SpicyMarinara

Base: staging ← Head: fix/hardening-game-assets-links


📝 Commits (6)

  • 8f02173 fix(server): hold game assets and the avatar cleanup to their real folders
  • 2b4096f fix(server): hold the asset seed and previews to their real folders
  • dab827a fix(server): record the default assets seed only once every folder received it
  • b0faf3f fix(server): check an app-folder path as written before its real path, and compare Windows paths by ASCII case
  • 2ace0e7 fix(server): list folder links that stay inside game-assets, and log a refused preview folder once
  • 66f9a0b fix(server): tell game-assets folders apart by file identity, so each is entered through a link once

📊 Changes

9 files changed (+1423 additions, -178 deletions)

View changed files

📝 packages/server/src/app.ts (+19 -5)
📝 packages/server/src/db/seed-game-assets.ts (+79 -18)
📝 packages/server/src/routes/game-assets.routes.ts (+109 -101)
📝 packages/server/src/services/game/asset-manifest.service.ts (+206 -33)
📝 packages/server/src/services/image/avatar-file-lifecycle.ts (+33 -6)
📝 packages/server/src/services/image/image-thumbnail.ts (+25 -6)
📝 packages/server/src/utils/app-folder.ts (+58 -9)
➕ scripts/regressions/app-folder-bounds.regression.ts (+62 -0)
➕ scripts/regressions/game-assets-link-hardening.regression.ts (+832 -0)

📄 Description

Important

Contributions target staging. Only SpicyMarinara may promote this repository's staging branch or a same-repository hotfix/* branch to main.

Linked issue

Refs #8797 (closed as completed on 2026-11-03; this is its listed follow-up). No separate issue exists for the folder-delete guard fix below: it was found while reworking the same route.

Why this change

#8802 made mari images read, write and delete only inside the real app folders. Code it calls, or shares with the app, still used paths as written: the game-asset manifest build, the default assets seed, the Game Assets routes, the image preview cache and the shared avatar cleanup. #8802 listed these as its follow-up. This holds them to their real folders the same way, without breaking normal installs: a data dir behind a link (Docker volumes, macOS /tmp) and a game-assets folder moved to another disk keep working.

It also fixes three robustness problems in the same code:

  • Startup could fail when something in game-assets was a broken link, or when game-assets was linked to a disk that is not connected. The server now logs a warning and keeps booting.
  • DELETE /game-assets/folders/sprites/ (with a trailing slash, /., or different letter case) slipped past the root-category guard and deleted the whole category.
  • On Windows, the real-path helper from #8802 could loop forever when a drive or network share is offline.

What changed

  • game-assets is held to its own real path, the bound the asset file route already uses. A game-assets folder that is itself a link (for example, moved to another disk) keeps working. A link inside it that leads out is skipped or refused, and its target is never touched.
  • The asset manifest build creates folders, runs the legacy flat-music migration and scans only inside the real folder. A folder link (a category, a music/ folder or any sub-folder) is entered by its own name only while it leads to a folder inside game-assets. A linked file is listed only while it leads to a file inside, and a link that leads out is skipped. A link cycle ends with a warning: each walk enters a linked folder at most once, and never follows a link to the folder being listed or one above it. If two links lead to the same folder, only the first one the walk reaches is listed. lstat, not the readdir entry type, decides what counts as a link, so a Windows reparse point that is not a link (such as a cloud placeholder) is listed normally. Anything skipped logs a warning and never throws.
  • Game Assets scan and tree now tell folders apart by file identity (device and file number, read as bigint), not by path. A folder reached through links spelled in another case (or another Unicode form, or a Windows short name) is entered through a link at most once per walk, so the scan's work stays bounded by the number of folders in game-assets.
  • A link to the folder being listed, or to one above it, is skipped under any spelling, because the folder and each folder above it are compared by identity.
  • manifest.json is written as a new file and renamed into place; a link at that name is left alone with a warning, and the manifest is read only when it is a plain file. The user-backgrounds part of the scan is held to the real backgrounds folder, as #8802 does for the other app folders.
  • The default assets seed creates and copies only into folders whose real path is inside game-assets, never replaces a name that already exists, writes its hash and .native markers as new files, and reads its hash only from a plain file.
  • Every Game Assets route that uploads, renames, moves, copies, edits, reads or deletes uses one shared set of game-assets helpers. A path whose folder or linked name leads out is refused with a 400 ("Path escapes game assets directory", "Target escapes game assets directory", or "Cannot save over a linked file" for a text edit); bulk actions list such a path under failed. Edits and meta.json are written with a temporary file and a rename, copies never overwrite an existing name, and meta.json is read only when it is a plain file.
  • DELETE /game-assets/folders/* checks the root-category guard on the normalized, lower-cased folder name.
  • Image previews for game assets, backgrounds and the gallery are cached only in the real data/backgrounds-thumbs folder, and only a plain file counts as a cached preview. If that folder leads out of its place in the data folder or cannot be resolved, the original image is served and one rate-limited warning naming the folder is logged (at most once a minute), not one per request.
  • The shared avatar cleanup removes a file only by its name in the real avatar folder; if the folder's real path is outside, it keeps the file and logs a warning, as the gallery cleanup does since #8802. This covers the app's own avatar deletes and the abandoned-avatar sweep.
  • If the startup seed or the startup asset scan fails, the server logs a warning and keeps booting. The seed runs again on the next boot and the manifest is rebuilt on the next rescan.
  • The real-path helper stops at the top of a path instead of looping when a Windows drive or share is offline.
  • The default assets seed records its bundle hash only once every folder received the bundle, so a folder it skipped (a link that leads out, or one to a disk that is not connected) is seeded on a later boot once it is back.
  • The shared real-path helpers check a path against the folder as written (or as its real path) before walking its real path, and on Windows they compare the real path with only ASCII letters ignoring case. Regression: scripts/regressions/app-folder-bounds.regression.ts.
  • Regression added: scripts/regressions/game-assets-link-hardening.regression.ts. Every link, preview, category-delete and boot case fails on the code before this change, and the normal flows pass, both with DATA_DIR behind a link and with game-assets behind a link. It also covers a music/ folder linked inside game-assets (listed in the manifest and the tree with its tracks, while a link inside it that leads out stays unlisted and its target untouched), a link cycle that ends with the scan complete, and a refused preview folder that serves originals and logs once. It also adds a chain of folders linked in upper case. On a case-insensitive disk the manifest and tree stay within n + n(n+1)/2 tracks; the previous commit listed 124 against a bound of 20 for n = 5.

Validation

  • pnpm check passes locally
  • Container (Docker / Podman) built and ran without issue
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Above manual verification completed (describe below)
  • Read and followed CONTRIBUTING.md

Manual verification notes

  • Manually verify in the Game Assets browser on a normal install that all of these still work: upload (drag-and-drop and the Upload dialog), rename, move, copy, bulk move, bulk copy, bulk delete, new folder, folder description, text file edit, Rescan, and deleting a subfolder. Check that the tree, the image previews and Game Mode scenes show the files.
  • Manually verify that deleting a top-level category folder (sprites, backgrounds, music, sfx, ambient) from the Game Assets browser is still refused, and that deleting a subfolder inside one still works.
  • Manually verify that a fresh install, with no data/game-assets folder, starts and installs the default music, ambient and SFX. Restart it and check that the files are not copied again.
  • Manually verify that an install with legacy flat music files in game-assets/music// starts, that the files are moved into / subfolders, and that they play in Game Mode.
  • Manually verify that a Docker install, and a source install with DATA_DIR behind a symlink (for example under macOS /tmp), start cleanly, write game-assets/manifest.json, accept game-asset uploads and show background and gallery thumbnails.
  • Manually verify that with data/game-assets moved to another disk and symlinked back, startup, Rescan, uploads and music playback still work (sprite and background images from that folder were already served as 404 before this change, so check music). Then disconnect the disk and confirm the server still starts, with warnings in the log.
  • Manually verify on Windows that uploads, rename, move, Rescan, startup and thumbnails still work, including a game-assets folder that is a junction to another drive and a DATA_DIR inside a OneDrive-synced Documents or Desktop folder (all game-asset subfolders should still appear in the tree and the manifest).
  • Manually verify that a Game Mode scene background generation and a background-agent background are saved under data/game-assets/backgrounds and show up after Rescan.
  • Manually verify on macOS or Windows: a game-assets folder linked to another folder inside game-assets under a different letter case appears once in the Game Assets tree, and a rescan stays quick.
  • Manually verify that deleting a character or persona avatar from the UI, and the abandoned-avatar cleanup in Settings, still remove the files from data/avatars.

Agent-run checks (for reference), on the head rebased onto staging after #8802 merged:

  • node ./scripts/pnpm-runner.mjs build:shared: ok
  • node ./scripts/run-regressions.mjs --filter game-assets-link-hardening: 1/1 passed
  • node ./scripts/run-regressions.mjs --filter professor-mari/: 27/27 passed (one unidentified failure in a first run under heavy machine load did not reproduce)
  • node ./scripts/run-regressions.mjs --filter regressions/mari/ / game-asset / avatar- / background: 39/39, 1/1, 26/26, 4/4 passed
  • pnpm check: exit 0

Before the rebase, the regression failed on the previous commit for every case the second review raised, and on the #8802 base for every link case, with no normal-flow failure; with the fix it passes 1/1.

Dottore Review on dab827a4e5 (2 LOW, 1 nitpick) is addressed in 2ace0e7862; an independent review of that commit found the folder-link walk could enter one folder under several case spellings (exponential on a case-insensitive disk), fixed in 66f9a0b220 by identifying folders by device and file number. CodeQL's path-injection alerts were traced one by one: two real gaps (an outside path walked before the bounds check, and Windows case folding) are fixed in b0faf3f7c3, and the rest are false positives that pass isSafePath and the real-folder check before any use. Earlier independent adversarial review: changes (1 major and 7 others: the default assets seed, meta.json reads, Windows reparse points, the offline-drive loop, the preview cache, regression leftovers, the folder-delete guard and the PR text), all addressed in the second commit.

Known limits and risks:

  • Two links to the same folder inside game-assets list that folder only under the first link the walk reaches; a cycle through two or more links is listed once around before it ends.
  • A deliberately linked subfolder inside game-assets that leads outside it is now skipped by the scan and refused by the routes. A game-assets folder that is itself a link keeps working.
  • Writes replace the file by renaming a temp file over it; a crash can leave a temp file next to it.
  • The Windows-only parts (the lstat-based link check, the offline-drive stop and junction handling) were reasoned through but not run; on POSIX they cannot be reproduced.
  • The check-then-use limit described in #8802's realPathInAppFolder comment (ponytail) applies here too; the shell sandbox from #8802 covers Professor Mari's shell.
  • Her structured file tools still run in the server outside the shell sandbox, as before (#8802 Known limits); a follow-up can hold them to the same folders.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README) as needed
  • Translated docs on the docs-i18n branch updated to match, or a [docs-i18n] follow-up issue opened (see CONTRIBUTING.md § Translated documentation)
  • Version/release files updated (only if this PR includes a version bump)

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/8819 **Author:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Created:** 11/4/2026 **Status:** ✅ Merged **Merged:** 11/4/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/hardening-game-assets-links` --- ### 📝 Commits (6) - [`8f02173`](https://github.com/Pasta-Devs/Marinara-Engine/commit/8f02173f4ed5597a466c12787e2ac7db98c2d134) fix(server): hold game assets and the avatar cleanup to their real folders - [`2b4096f`](https://github.com/Pasta-Devs/Marinara-Engine/commit/2b4096f7d0e763a13165b1d96453199679718678) fix(server): hold the asset seed and previews to their real folders - [`dab827a`](https://github.com/Pasta-Devs/Marinara-Engine/commit/dab827a4e55218a27bf5b7e3d188ba7ada1416a1) fix(server): record the default assets seed only once every folder received it - [`b0faf3f`](https://github.com/Pasta-Devs/Marinara-Engine/commit/b0faf3f7c37c133f456ff1b89eeb9dd8345f9d70) fix(server): check an app-folder path as written before its real path, and compare Windows paths by ASCII case - [`2ace0e7`](https://github.com/Pasta-Devs/Marinara-Engine/commit/2ace0e7862f9f66bd8b4be87e1376d2f6142fada) fix(server): list folder links that stay inside game-assets, and log a refused preview folder once - [`66f9a0b`](https://github.com/Pasta-Devs/Marinara-Engine/commit/66f9a0b2201afd2d4f6924979f115ca2d853db78) fix(server): tell game-assets folders apart by file identity, so each is entered through a link once ### 📊 Changes **9 files changed** (+1423 additions, -178 deletions) <details> <summary>View changed files</summary> 📝 `packages/server/src/app.ts` (+19 -5) 📝 `packages/server/src/db/seed-game-assets.ts` (+79 -18) 📝 `packages/server/src/routes/game-assets.routes.ts` (+109 -101) 📝 `packages/server/src/services/game/asset-manifest.service.ts` (+206 -33) 📝 `packages/server/src/services/image/avatar-file-lifecycle.ts` (+33 -6) 📝 `packages/server/src/services/image/image-thumbnail.ts` (+25 -6) 📝 `packages/server/src/utils/app-folder.ts` (+58 -9) ➕ `scripts/regressions/app-folder-bounds.regression.ts` (+62 -0) ➕ `scripts/regressions/game-assets-link-hardening.regression.ts` (+832 -0) </details> ### 📄 Description > [!IMPORTANT] > Contributions target `staging`. Only `SpicyMarinara` may promote this repository's `staging` branch or a same-repository `hotfix/*` branch to `main`. ## Linked issue Refs #8797 (closed as completed on 2026-11-03; this is its listed follow-up). No separate issue exists for the folder-delete guard fix below: it was found while reworking the same route. ## Why this change #8802 made `mari images` read, write and delete only inside the real app folders. Code it calls, or shares with the app, still used paths as written: the game-asset manifest build, the default assets seed, the Game Assets routes, the image preview cache and the shared avatar cleanup. #8802 listed these as its follow-up. This holds them to their real folders the same way, without breaking normal installs: a data dir behind a link (Docker volumes, macOS /tmp) and a game-assets folder moved to another disk keep working. It also fixes three robustness problems in the same code: - Startup could fail when something in game-assets was a broken link, or when game-assets was linked to a disk that is not connected. The server now logs a warning and keeps booting. - `DELETE /game-assets/folders/sprites/` (with a trailing slash, `/.`, or different letter case) slipped past the root-category guard and deleted the whole category. - On Windows, the real-path helper from #8802 could loop forever when a drive or network share is offline. ## What changed - game-assets is held to its own real path, the bound the asset file route already uses. A game-assets folder that is itself a link (for example, moved to another disk) keeps working. A link inside it that leads out is skipped or refused, and its target is never touched. - The asset manifest build creates folders, runs the legacy flat-music migration and scans only inside the real folder. A folder link (a category, a music/<state> folder or any sub-folder) is entered by its own name only while it leads to a folder inside game-assets. A linked file is listed only while it leads to a file inside, and a link that leads out is skipped. A link cycle ends with a warning: each walk enters a linked folder at most once, and never follows a link to the folder being listed or one above it. If two links lead to the same folder, only the first one the walk reaches is listed. lstat, not the readdir entry type, decides what counts as a link, so a Windows reparse point that is not a link (such as a cloud placeholder) is listed normally. Anything skipped logs a warning and never throws. - Game Assets scan and tree now tell folders apart by file identity (device and file number, read as bigint), not by path. A folder reached through links spelled in another case (or another Unicode form, or a Windows short name) is entered through a link at most once per walk, so the scan's work stays bounded by the number of folders in game-assets. - A link to the folder being listed, or to one above it, is skipped under any spelling, because the folder and each folder above it are compared by identity. - manifest.json is written as a new file and renamed into place; a link at that name is left alone with a warning, and the manifest is read only when it is a plain file. The user-backgrounds part of the scan is held to the real backgrounds folder, as #8802 does for the other app folders. - The default assets seed creates and copies only into folders whose real path is inside game-assets, never replaces a name that already exists, writes its hash and .native markers as new files, and reads its hash only from a plain file. - Every Game Assets route that uploads, renames, moves, copies, edits, reads or deletes uses one shared set of game-assets helpers. A path whose folder or linked name leads out is refused with a 400 ("Path escapes game assets directory", "Target escapes game assets directory", or "Cannot save over a linked file" for a text edit); bulk actions list such a path under failed. Edits and meta.json are written with a temporary file and a rename, copies never overwrite an existing name, and meta.json is read only when it is a plain file. - DELETE /game-assets/folders/* checks the root-category guard on the normalized, lower-cased folder name. - Image previews for game assets, backgrounds and the gallery are cached only in the real data/backgrounds-thumbs folder, and only a plain file counts as a cached preview. If that folder leads out of its place in the data folder or cannot be resolved, the original image is served and one rate-limited warning naming the folder is logged (at most once a minute), not one per request. - The shared avatar cleanup removes a file only by its name in the real avatar folder; if the folder's real path is outside, it keeps the file and logs a warning, as the gallery cleanup does since #8802. This covers the app's own avatar deletes and the abandoned-avatar sweep. - If the startup seed or the startup asset scan fails, the server logs a warning and keeps booting. The seed runs again on the next boot and the manifest is rebuilt on the next rescan. - The real-path helper stops at the top of a path instead of looping when a Windows drive or share is offline. - The default assets seed records its bundle hash only once every folder received the bundle, so a folder it skipped (a link that leads out, or one to a disk that is not connected) is seeded on a later boot once it is back. - The shared real-path helpers check a path against the folder as written (or as its real path) before walking its real path, and on Windows they compare the real path with only ASCII letters ignoring case. Regression: scripts/regressions/app-folder-bounds.regression.ts. - Regression added: scripts/regressions/game-assets-link-hardening.regression.ts. Every link, preview, category-delete and boot case fails on the code before this change, and the normal flows pass, both with DATA_DIR behind a link and with game-assets behind a link. It also covers a music/<state> folder linked inside game-assets (listed in the manifest and the tree with its tracks, while a link inside it that leads out stays unlisted and its target untouched), a link cycle that ends with the scan complete, and a refused preview folder that serves originals and logs once. It also adds a chain of folders linked in upper case. On a case-insensitive disk the manifest and tree stay within n + n(n+1)/2 tracks; the previous commit listed 124 against a bound of 20 for n = 5. ## Validation <!-- These are human tasks. Never tick a box for a check that was not actually performed. --> - [ ] `pnpm check` passes locally - [ ] Container (Docker / Podman) built and ran without issue - [ ] Ran the app, clicked through the changes manually - [ ] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [ ] Above manual verification completed (describe below) - [ ] Read and followed `CONTRIBUTING.md` ### Manual verification notes - Manually verify in the Game Assets browser on a normal install that all of these still work: upload (drag-and-drop and the Upload dialog), rename, move, copy, bulk move, bulk copy, bulk delete, new folder, folder description, text file edit, Rescan, and deleting a subfolder. Check that the tree, the image previews and Game Mode scenes show the files. - Manually verify that deleting a top-level category folder (sprites, backgrounds, music, sfx, ambient) from the Game Assets browser is still refused, and that deleting a subfolder inside one still works. - Manually verify that a fresh install, with no data/game-assets folder, starts and installs the default music, ambient and SFX. Restart it and check that the files are not copied again. - Manually verify that an install with legacy flat music files in game-assets/music/<state>/ starts, that the files are moved into <genre>/<intensity> subfolders, and that they play in Game Mode. - Manually verify that a Docker install, and a source install with DATA_DIR behind a symlink (for example under macOS /tmp), start cleanly, write game-assets/manifest.json, accept game-asset uploads and show background and gallery thumbnails. - Manually verify that with data/game-assets moved to another disk and symlinked back, startup, Rescan, uploads and music playback still work (sprite and background images from that folder were already served as 404 before this change, so check music). Then disconnect the disk and confirm the server still starts, with warnings in the log. - Manually verify on Windows that uploads, rename, move, Rescan, startup and thumbnails still work, including a game-assets folder that is a junction to another drive and a DATA_DIR inside a OneDrive-synced Documents or Desktop folder (all game-asset subfolders should still appear in the tree and the manifest). - Manually verify that a Game Mode scene background generation and a background-agent background are saved under data/game-assets/backgrounds and show up after Rescan. - Manually verify on macOS or Windows: a game-assets folder linked to another folder inside game-assets under a different letter case appears once in the Game Assets tree, and a rescan stays quick. - Manually verify that deleting a character or persona avatar from the UI, and the abandoned-avatar cleanup in Settings, still remove the files from data/avatars. Agent-run checks (for reference), on the head rebased onto staging after #8802 merged: - `node ./scripts/pnpm-runner.mjs build:shared`: ok - `node ./scripts/run-regressions.mjs --filter game-assets-link-hardening`: 1/1 passed - `node ./scripts/run-regressions.mjs --filter professor-mari/`: 27/27 passed (one unidentified failure in a first run under heavy machine load did not reproduce) - `node ./scripts/run-regressions.mjs --filter regressions/mari/ / game-asset / avatar- / background`: 39/39, 1/1, 26/26, 4/4 passed - `pnpm check`: exit 0 Before the rebase, the regression failed on the previous commit for every case the second review raised, and on the #8802 base for every link case, with no normal-flow failure; with the fix it passes 1/1. Dottore Review on dab827a4e5 (2 LOW, 1 nitpick) is addressed in 2ace0e7862; an independent review of that commit found the folder-link walk could enter one folder under several case spellings (exponential on a case-insensitive disk), fixed in 66f9a0b220 by identifying folders by device and file number. CodeQL's path-injection alerts were traced one by one: two real gaps (an outside path walked before the bounds check, and Windows case folding) are fixed in b0faf3f7c3, and the rest are false positives that pass isSafePath and the real-folder check before any use. Earlier independent adversarial review: changes (1 major and 7 others: the default assets seed, meta.json reads, Windows reparse points, the offline-drive loop, the preview cache, regression leftovers, the folder-delete guard and the PR text), all addressed in the second commit. Known limits and risks: - Two links to the same folder inside game-assets list that folder only under the first link the walk reaches; a cycle through two or more links is listed once around before it ends. - A deliberately linked subfolder inside game-assets that leads outside it is now skipped by the scan and refused by the routes. A game-assets folder that is itself a link keeps working. - Writes replace the file by renaming a temp file over it; a crash can leave a temp file next to it. - The Windows-only parts (the lstat-based link check, the offline-drive stop and junction handling) were reasoned through but not run; on POSIX they cannot be reproduced. - The check-then-use limit described in #8802's realPathInAppFolder comment (ponytail) applies here too; the shell sandbox from #8802 covers Professor Mari's shell. - Her structured file tools still run in the server outside the shell sandbox, as before (#8802 Known limits); a follow-up can hold them to the same folders. ## Docs and release impact - [ ] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README) as needed - [ ] Translated docs on the `docs-i18n` branch updated to match, or a `[docs-i18n]` follow-up issue opened (see CONTRIBUTING.md § Translated documentation) - [ ] Version/release files updated (only if this PR includes a version bump) 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-07 17:40:00 +07:00
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#8827
No description provided.