rpf9dump: harden Util.Safe for cross-platform and Windows filenames #2876

Merged
kirigayakazima merged 1 commit from upstream-pr/2876 into main 2026-12-01 22:51:41 +00:00
kirigayakazima commented 2026-12-01 13:08:56 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by kirigayakazima on 2026-12-01T13:08:56Z.


Claimed in the coordination issue as: RPF9DUMP-UTIL-SAFE-WINDOWS-NAMES (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7312115654)

What and why

Hardens Util.Safe(string s) in tools/rpf9dump/Util.cs against three filename edge cases when exporting archives/textures:

  1. Cross-platform Windows-illegal characters: previously only Path.GetInvalidFileNameChars() and control characters were replaced with '_'. On Linux/macOS that set only includes '\0' and '/', leaving <>:"/\|?* unsanitized across hosts. Util.Safe now sanitizes <>:"/\|?* uniformly everywhere.
  2. Trailing dots/spaces exposed by the 120-char limit: previously .Trim(' ', '.') ran only before [..Math.Min(120, v.Length)], so names like "a" * 119 + ".foo" or "a" + "." * 130 + "b" ended with a trailing . or ' ' after truncation (rejected/stripped on Windows). Util.Safe now trims trailing spaces/dots after truncation as well (returning "x" if empty).
  3. Windows reserved DOS device names: names whose base stem before '.' (trimmed of trailing spaces) is CON, PRN, AUX, NUL, COM1..COM9, or LPT1..LPT9 (case-insensitive) are prefixed with '_' and bounded to 120 chars without trailing dots/spaces.

Tested

Verified locally (Windows 11, .NET SDK 8.0.401, Python 3.14, no game data):

  • Red before fix: dotnet run --project tests/Rpf9DumpDiagnostics/Rpf9DumpDiagnostics.csproj -c Release failed on trailing dot after 120-char truncation.
  • Green after fix:
    • dotnet build tools/rpf9dump/rpf9dump.csproj -c Release: 0 warnings, 0 errors
    • dotnet run --project tests/Rpf9DumpDiagnostics/Rpf9DumpDiagnostics.csproj -c Release: all 7 test groups passed (Util.Safe filename tests passed.)
    • python -I tests/test_rpf9dump_exit_codes.py (with built RPF9DUMP_DLL): 10 passed
    • python -I scripts/check_no_game_data.py & git diff --check: clean

GPUs tested: n/a

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a)
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5586342931 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2876). Originally posted by [kirigayakazima](https://github.com/kirigayakazima) on 2026-12-01T13:08:56Z. --- Claimed in the coordination issue as: RPF9DUMP-UTIL-SAFE-WINDOWS-NAMES (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7312115654) ### What and why Hardens `Util.Safe(string s)` in `tools/rpf9dump/Util.cs` against three filename edge cases when exporting archives/textures: 1. **Cross-platform Windows-illegal characters**: previously only `Path.GetInvalidFileNameChars()` and control characters were replaced with `'_'`. On Linux/macOS that set only includes `'\0'` and `'/'`, leaving `<>:"/\|?*` unsanitized across hosts. `Util.Safe` now sanitizes `<>:"/\|?*` uniformly everywhere. 2. **Trailing dots/spaces exposed by the 120-char limit**: previously `.Trim(' ', '.')` ran only *before* `[..Math.Min(120, v.Length)]`, so names like `"a" * 119 + ".foo"` or `"a" + "." * 130 + "b"` ended with a trailing `.` or `' '` after truncation (rejected/stripped on Windows). `Util.Safe` now trims trailing spaces/dots after truncation as well (returning `"x"` if empty). 3. **Windows reserved DOS device names**: names whose base stem before `'.'` (trimmed of trailing spaces) is `CON`, `PRN`, `AUX`, `NUL`, `COM1`..`COM9`, or `LPT1`..`LPT9` (case-insensitive) are prefixed with `'_'` and bounded to 120 chars without trailing dots/spaces. ### Tested Verified locally (Windows 11, .NET SDK 8.0.401, Python 3.14, no game data): - Red before fix: `dotnet run --project tests/Rpf9DumpDiagnostics/Rpf9DumpDiagnostics.csproj -c Release` failed on `trailing dot after 120-char truncation`. - Green after fix: - `dotnet build tools/rpf9dump/rpf9dump.csproj -c Release`: 0 warnings, 0 errors - `dotnet run --project tests/Rpf9DumpDiagnostics/Rpf9DumpDiagnostics.csproj -c Release`: all 7 test groups passed (`Util.Safe filename tests passed.`) - `python -I tests/test_rpf9dump_exit_codes.py` (with built `RPF9DUMP_DLL`): 10 passed - `python -I scripts/check_no_game_data.py` & `git diff --check`: clean GPUs tested: n/a AI-assisted: yes ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (n/a)
KelboLive commented 2026-12-01 13:26:50 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by KelboLive on 2026-12-01T13:26:50Z.

Review: COMMENTED


Sanitising every component before Path.Combine, the post-truncation trim, and the reserved-name stem split (first ., spaces trimmed so CON .txt matches) all look right, and the nasty cases stay inert: ../../etc/passwd → _.._etc_passwd, ..\..\windows\system32 → _.._windows_system32, CON → _CON, foo./foo → foo, 300 chars → 120 with no trailing dot. COM0/COM10 are correctly allowed. Build clean, diagnostics all pass, test_rpf9dump_exit_codes.py 10/10.

Two real gaps:

  1. The headline cross-platform rule is untested. The explicit <>:"/\|?* list plus char.IsControl is now a strict superset of Windows' Path.GetInvalidFileNameChars(), so that call is dead code — and deleting the <>:"/\|?* clause leaves all tests green, because the diagnostics job runs only on windows-latest (.github/workflows/ci.yml:87, :140). Either drop the platform call so Safe is deterministic and the test becomes real, or run the diagnostics on a Unix runner too.
  2. The _ escape creates collisions. CON → _CON and a genuine _CON (likewise NUL.txt / _NUL.txt) map to the same file, and every exporter treats File.Exists as success (Program.cs:313, :327, :360, :157), so the loser is silently skipped and the manifest lists the same File/Dir twice. Prefer a non-colliding escape (CON_, %5F) or uniquify with the entry offset. The same class already exists for foo./foo, a:b/a*b/a_b, .../""/x, and truncation that drops the extension (119a + ".x" and 119a + ".y" both → 119a); index-prefixed call sites are immune, stem-derived directories are not.

Also worth fixing while here:

  • Truncation can split a surrogate pair: 119 × a + 😀 ends in a lone U+D83D. Cut on a code-point boundary.
  • The archive base name is never sanitised (Program.cs:70-71, outDir = Path.Combine(outRoot, Path.GetFileNameWithoutExtension(src))) — the one written component that skips Safe.
  • Minor: per-component 120 is bounded but the total path is not (\\?\ unused; nested models/<120>/<120>_tex/… can exceed MAX_PATH), and CONIN$ / CONOUT$ / COM¹ are still device names.
<!-- greyfinch-sync:ubiquitarianism/leonida:review:6132150833 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2876#pullrequestreview-6132150833). Originally posted by [KelboLive](https://github.com/KelboLive) on 2026-12-01T13:26:50Z. **Review:** COMMENTED --- Sanitising every component **before** `Path.Combine`, the post-truncation trim, and the reserved-name stem split (first `.`, spaces trimmed so `CON .txt` matches) all look right, and the nasty cases stay inert: `../../etc/passwd` → `_.._etc_passwd`, `..\..\windows\system32` → `_.._windows_system32`, `CON` → `_CON`, `foo.`/`foo ` → `foo`, 300 chars → 120 with no trailing dot. `COM0`/`COM10` are correctly allowed. Build clean, diagnostics all pass, `test_rpf9dump_exit_codes.py` 10/10. Two real gaps: 1. **The headline cross-platform rule is untested.** The explicit `<>:"/\|?*` list plus `char.IsControl` is now a strict *superset* of Windows' `Path.GetInvalidFileNameChars()`, so that call is dead code — and deleting the `<>:"/\|?*` clause leaves all tests green, because the diagnostics job runs only on `windows-latest` (`.github/workflows/ci.yml:87`, `:140`). Either drop the platform call so `Safe` is deterministic and the test becomes real, or run the diagnostics on a Unix runner too. 2. **The `_` escape creates collisions.** `CON` → `_CON` and a genuine `_CON` (likewise `NUL.txt` / `_NUL.txt`) map to the same file, and every exporter treats `File.Exists` as success (`Program.cs:313`, `:327`, `:360`, `:157`), so the loser is silently skipped and the manifest lists the same `File`/`Dir` twice. Prefer a non-colliding escape (`CON_`, `%5F`) or uniquify with the entry offset. The same class already exists for `foo.`/`foo`, `a:b`/`a*b`/`a_b`, `...`/`""`/`x`, and truncation that drops the extension (`119a + ".x"` and `119a + ".y"` both → `119a`); index-prefixed call sites are immune, stem-derived directories are not. Also worth fixing while here: - **Truncation can split a surrogate pair**: 119 × `a` + 😀 ends in a lone `U+D83D`. Cut on a code-point boundary. - **The archive base name is never sanitised** (`Program.cs:70-71`, `outDir = Path.Combine(outRoot, Path.GetFileNameWithoutExtension(src))`) — the one written component that skips `Safe`. - Minor: per-component 120 is bounded but the total path is not (`\\?\` unused; nested `models/<120>/<120>_tex/…` can exceed MAX_PATH), and `CONIN$` / `CONOUT$` / `COM¹` are still device names.
OreoFR commented 2026-12-01 22:35:40 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-12-01T22:35:40Z.


Merging (added to the merge queue). Head f2357faa5 merges cleanly with main c4ca57615. Claim: RPF9DUMP-UTIL-SAFE-WINDOWS-NAMES (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7312115654). Red/green: KelboLive review on this head (sanitising and reserved names checked case by case); tool-only change. I ran: #2858, #2882, #2788, #2876 and #2884 merged together onto main c4ca57615: cargo tests leonida-shader 420 passed / 0 failed, leonida-loader 85/0, leonida-gpu 644/0. In-game run: RTX 4070 Laptop 8 GB, NVIDIA 621.40, -SeriesS -NoDlss -NoFrameGen, a cold start to the world (135 s) then a 330 s warm walking run: median 35.4 ms (28.2 fps), private memory max 24.4 GB, 0 device lost. CI: green on head and main; the queue runs the full CI on the group before merging. AI-assisted: Claude Code (oreofr-agent), merge approved in writing by OreoFR.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7328070640 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2876#issuecomment-7328070640). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-12-01T22:35:40Z. --- Merging (added to the merge queue). Head f2357faa5 merges cleanly with main c4ca57615. Claim: RPF9DUMP-UTIL-SAFE-WINDOWS-NAMES (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7312115654). Red/green: KelboLive review on this head (sanitising and reserved names checked case by case); tool-only change. I ran: #2858, #2882, #2788, #2876 and #2884 merged together onto main c4ca57615: cargo tests leonida-shader 420 passed / 0 failed, leonida-loader 85/0, leonida-gpu 644/0. In-game run: RTX 4070 Laptop 8 GB, NVIDIA 621.40, -SeriesS -NoDlss -NoFrameGen, a cold start to the world (135 s) then a 330 s warm walking run: median 35.4 ms (28.2 fps), private memory max 24.4 GB, 0 device lost. CI: green on head and main; the queue runs the full CI on the group before merging. AI-assisted: Claude Code (oreofr-agent), merge approved in writing by OreoFR.
Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
ProjectLeonida/mandy!2876
No description provided.