Protect the Steam folder and every Steam library wherever Steam is installed #44

Merged
ethan-hann merged 5 commits from issue-40-protect-steam-folder into main 2026-10-23 23:09:01 -05:00
Owner

Fixes #40.

Checked against the real Steam install first: the registry SteamPath reads c:/program files (x86)/steam; libraryfolders.vdf (5.7 KB, LF, tabs, quoted keys, doubled backslashes) lists four libraries, the client folder, D:/SteamLibrary, X:/SteamLibrary, and an offline N:/SteamLibrary; each existing library root holds libraryfolder.vdf, steam.dll, and steamapps. Test fixtures copy that shape, including the offline library and the older bare-path layout.

  • The Steam client folder SteamFolderLocator finds and every library root libraryfolders.vdf lists are refused as tier 2 on Windows, Linux, and macOS, with the existing Steam refusal. A game folder under /steamapps/common/ is still accepted, and steamapps stays refused by name.
  • The library file is read with shared access (Steam may be writing it), capped at 1 MB and 16 levels, and a failed or missing read is retried at most once every 5 seconds while the client folder stays protected. A malformed or non-absolute entry adds nothing.
  • One registry reader: SteamFolderLocator now uses WindowsSteamRegistry.ReadSteamPath.

No test reads the real registry or Steam folder. Reds and mutation checks are in the commit messages.

Fixes #40. Checked against the real Steam install first: the registry SteamPath reads c:/program files (x86)/steam; libraryfolders.vdf (5.7 KB, LF, tabs, quoted keys, doubled backslashes) lists four libraries, the client folder, D:/SteamLibrary, X:/SteamLibrary, and an offline N:/SteamLibrary; each existing library root holds libraryfolder.vdf, steam.dll, and steamapps. Test fixtures copy that shape, including the offline library and the older bare-path layout. - The Steam client folder SteamFolderLocator finds and every library root libraryfolders.vdf lists are refused as tier 2 on Windows, Linux, and macOS, with the existing Steam refusal. A game folder under <library>/steamapps/common/<dir> is still accepted, and steamapps stays refused by name. - The library file is read with shared access (Steam may be writing it), capped at 1 MB and 16 levels, and a failed or missing read is retried at most once every 5 seconds while the client folder stays protected. A malformed or non-absolute entry adds nothing. - One registry reader: SteamFolderLocator now uses WindowsSteamRegistry.ReadSteamPath. No test reads the real registry or Steam folder. Reds and mutation checks are in the commit messages.
The denylist only knew the default Steam path (Program Files (x86)\Steam)
plus steamapps by name. A client installed elsewhere, or a library root
such as D:\SteamLibrary, could be tracked as a whole. The platforms now
also refuse, as tier 2 with the existing Steam refusal, the folder
SteamFolderLocator finds and every library root listed in
<steam>/steamapps/libraryfolders.vdf. A game's own install folder under
<library>/steamapps/common/<dir> is still accepted.

What the real data showed (read only): HKCU\Software\Valve\Steam
SteamPath is "c:/program files (x86)/steam". libraryfolders.vdf (5.7 KB,
LF line endings, tab separated, quoted keys and values, backslashes
doubled) has the top key "libraryfolders" and one numbered block per
library with "path", "label", "contentid", "totalsize",
"update_clean_bytes_tally", "time_last_update_verified" and an "apps"
block. Four libraries: C:\Program Files (x86)\Steam (the client folder
itself is library 0), D:\SteamLibrary, X:\SteamLibrary, and N:\SteamLibrary
on a drive that is not connected (time_last_update_verified 0, the
folder does not exist). Each existing library root holds
libraryfolder.vdf, steam.dll and steamapps. The fixtures copy that shape
(shortened app lists), and the older layout (a bare path as the value of
each numbered key) is read too.

How: PlatformEnvironment gains GetSteamFolder (the real one wraps
SteamFolderLocator, with the registry read in WindowsSteamRegistry).
SteamLibraryFile parses the VDF text (size, depth and count bounded;
malformed gives nothing). SteamFolders looks once per platform instance
(Lazy), tolerates any lookup failure, keeps the client folder protected
when only the list cannot be read, and adds Exact entries. The parsing
and matching are shared; only the locator and registry stay per OS.
PlatformFactoryTests' two real-machine denylist smoke tests now switch the
Steam lookup off so no test reads the real registry or Steam folder.

Observed reds (against stubs that compile, before the implementation):
SteamLibraryFileTests, 8 assertion failures with Actual [] (real Windows
file, CRLF variant, Linux file, older layout, non-absolute entries
dropped, normalization, escaped quote and comment, capped list).
SteamFolderProtectionTests, 45 of 56 failed: "'E:\Games\Steam' should be
blocked as tier 2 but was accepted" (and the Linux flatpak and macOS
/Volumes equivalents, 'D:\SteamLibrary', 'd:\steamlibrary'), Assert.Single
found no entry for the folder, reads stayed at 0 instead of 1, Assert.Empty
failed on the non-absolute-entry cases.

Tests that passed on their first run were mutation checked, each failing
as expected and then reverted: truncated file returned at EOF (the
unterminated-block case failed), IsAbsolute check removed (2 failed),
depth limit removed (the over-deep file test failed; first version of
that test was blind to it and was rewritten), size cap raised (the three
oversized-file cases failed), library added as Subtree (accepted-install
and exact-entry tests failed), DenylistBuilder tier precedence flipped
(the tier-1 library test failed), steamapps/common name rule removed (the
by-name test failed).
Steam rewrites libraryfolders.vdf while running. The environment's file
reader now opens it with FileShare.ReadWrite | FileShare.Delete, so a
write in progress no longer makes the read fail.

A read that throws, or a file or Steam folder that is not there yet, is no
longer remembered as "no libraries" for the whole session. SteamFolders
keeps what it found (the client folder stays protected throughout) and
looks again on a later table build, at most once per 5 seconds so path
checks stay cheap. A list that was read is kept for good. The platforms
take an optional TimeProvider to pace the retry.

SteamFolderLocator now uses WindowsSteamRegistry.ReadSteamPath, so the
registry read exists once.

Observed reds, before the change:
EnvironmentFileReadTests.A_file_another_program_has_open_for_writing_can_
still_be_read: Expected the file text, Actual null (the old reader asked
for FileShare.Read against an open writer).
SteamFolderProtectionTests, 9 failures across Windows, Linux and macOS:
the retried library was never added after a read that threw once or a file
that appeared later (Assert.Contains filter not matched), and a read that
keeps failing was read once per table instead of the expected counts
(Assert.Equal values differ).

Ran, one class at a time with --max-threads 2: SteamFolderProtectionTests
(62), EnvironmentFileReadTests (2), SteamLibraryFileTests, DenylistTableTests
(22), PlatformFactoryTests (9), SteamFolderLocatorTests (10), and the
Artwork classes that use the locator rig: ArtworkLookupTests (18),
SteamLocalCacheTests (10), ArtworkOnlineTests (14). All pass.
Format the Steam library file reader: private constant names and a missing space
Some checks failed
Gate / linux (pull_request) Has been cancelled
3195c8d5b9
Closes #40
ethan-hann deleted branch issue-40-protect-steam-folder 2026-10-23 23:09:02 -05:00
Sign in to join this conversation.
No reviewers
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.

Dependencies

No dependencies set

Reference
ethan-hann/SavePoint!44
No description provided.