rpf9dump: harden Util.Safe for cross-platform and Windows filenames #2876
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2876"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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)intools/rpf9dump/Util.csagainst three filename edge cases when exporting archives/textures:Path.GetInvalidFileNameChars()and control characters were replaced with'_'. On Linux/macOS that set only includes'\0'and'/', leaving<>:"/\|?*unsanitized across hosts.Util.Safenow sanitizes<>:"/\|?*uniformly everywhere..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.Safenow trims trailing spaces/dots after truncation as well (returning"x"if empty).'.'(trimmed of trailing spaces) isCON,PRN,AUX,NUL,COM1..COM9, orLPT1..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):
dotnet run --project tests/Rpf9DumpDiagnostics/Rpf9DumpDiagnostics.csproj -c Releasefailed ontrailing dot after 120-char truncation.dotnet build tools/rpf9dump/rpf9dump.csproj -c Release: 0 warnings, 0 errorsdotnet 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 builtRPF9DUMP_DLL): 10 passedpython -I scripts/check_no_game_data.py&git diff --check: cleanGPUs tested: n/a
AI-assisted: yes
Checklist
mainand merges cleanly (a conflicting pull request is labeledneeds rebase).docs/design.md, with how it was confirmed. (n/a)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 soCON .txtmatches) 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/COM10are correctly allowed. Build clean, diagnostics all pass,test_rpf9dump_exit_codes.py10/10.Two real gaps:
<>:"/\|?*list pluschar.IsControlis 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 onwindows-latest(.github/workflows/ci.yml:87,:140). Either drop the platform call soSafeis deterministic and the test becomes real, or run the diagnostics on a Unix runner too._escape creates collisions.CON→_CONand a genuine_CON(likewiseNUL.txt/_NUL.txt) map to the same file, and every exporter treatsFile.Existsas success (Program.cs:313,:327,:360,:157), so the loser is silently skipped and the manifest lists the sameFile/Dirtwice. Prefer a non-colliding escape (CON_,%5F) or uniquify with the entry offset. The same class already exists forfoo./foo,a:b/a*b/a_b,.../""/x, and truncation that drops the extension (119a + ".x"and119a + ".y"both →119a); index-prefixed call sites are immune, stem-derived directories are not.Also worth fixing while here:
a+ 😀 ends in a loneU+D83D. Cut on a code-point boundary.Program.cs:70-71,outDir = Path.Combine(outRoot, Path.GetFileNameWithoutExtension(src))) — the one written component that skipsSafe.\\?\unused; nestedmodels/<120>/<120>_tex/…can exceed MAX_PATH), andCONIN$/CONOUT$/COM¹are still device names.Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-12-01T22:35:40Z.
Merging (added to the merge queue). Head
f2357faa5merges cleanly with mainc4ca57615. 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 mainc4ca57615: 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.