Skip to content

fix: harden tokeer, http reads, achievement jobs, and pattern cache - #8

Open
t91a60 wants to merge 2 commits into
madoiscool:mainfrom
t91a60:main
Open

t91a60 wants to merge 2 commits into
madoiscool:mainfrom
t91a60:main

Conversation

@t91a60

@t91a60 t91a60 commented Nov 13, 2026

Copy link
Copy Markdown

Hardening pass on the bst:// redeem path, the shared HTTP reader, the achievement job map, and the pattern cache. Five files. No behavior change for a normal redeem code, a normal manifest fetch, or a cache file that was written completely.

Supersedes the unmerged #6 (JSON escape only). Based on current main.

Tokeer (src/Utils/Tokeer/TokeerBridge.cpp)

Redeem() built POST /drm/redeem by concatenating the code into {"code":"..."}. The value comes from bst://, registered under HKCU\Software\Classes\bst, so it is not a locally typed literal. A ", \, or control character terminates the JSON string and changes the request (RFC 8259).

  • JsonEscape() escapes " and \, maps \b \f \n \r \t, and writes the remaining controls as \u00XX. Bytes >= 0x20, including UTF-8, pass through. No snprintf.
  • HandleUri() percent-decodes the argument first (%22 becomes a quote, then gets escaped). + is not treated as a space. A broken % sequence is left as-is.
  • JsonString() used to stop at the first ", so an escaped quote in reason / error truncated the value. It now walks escapes (\", \\, \/, the named controls, \uXXXX as UTF-8) and returns the unescaped string. Hex ticket fields are unchanged.
  • bst://refreshachievements/<arg> deleted appcache/stats/UserGameStatsSchema_<arg>.bin. arg is a URL segment, so .. or a slash walked out of the stats directory. Only all or a decimal appid (1–10 digits) is accepted now.

HTTP body cap (src/OSTPlatform/Windows/Http.cpp)

Execute documented that reading stops at maxBodyBytes, but it resized the buffer by the full WinHttpQueryDataAvailable chunk and only checked afterwards. One chunk could allocate past the cap (manifest fetch is capped at 64 MB, the updater at 16 MB). The read is now clamped to the remaining room before WinHttpReadData.

Achievement job map (src/Hook/Hooks_NetPacket.cpp)

g_JobIdToAppId is written on the stats/achievements send path and erased on recv. Those hooks run on Steam's network threads and the map had no lock. A job whose reply never arrives stayed in the map for the session.

  • RememberJob / TakeJob take a mutex around the map only. The protobuf work stays outside the lock.
  • At 128 entries the map is trimmed back to 64 before a new insert. An in-flight id that is inserted again is updated in place and is not trimmed away.

Pattern cache (src/Utils/SteamMetadata/RemoteToml.cpp)

Cache-first loads <sha>.toml and then refreshes it on a detached thread. That refresh opened the cache path with ofstream, which truncates the good file before the new bytes land. A crash, or a second launch in that window, then trusts a partial TOML (file_size > 0 is the only check).

The write now goes to a per-thread *.tmp and MoveFileEx(MOVEFILE_REPLACE_EXISTING) publishes it. A failed write leaves the previous cache in place.

Manifest 404 cache (src/Utils/SteamMetadata/ManifestCache.cpp)

A definitive 404 is remembered for 10 minutes, but an entry was only erased the next time that same key was looked up. A long session of distinct missing manifests grew the map for the life of the process. On insert, once the map reaches 512 keys, expired entries are dropped and the rest is trimmed back to 256.

Verification

Host-side C++20 checks of the Tokeer logic (not the Windows DLL):

  • JSON escape of ", \, CR/LF/TAB, \0, \x1F; / and UTF-8 unchanged.
  • Percent-decode then escape: %22 becomes a quoted JSON string, + stays +, a short % is left alone.
  • JsonString on say \"hi\", a plain app_id, \n, \u0043, and a null value (rejected).
  • Appid filter rejects .., ../x, and 12a.

A full MSVC build was not run here (Windows-only tree).

Redeem() concatenated the bst:// code straight into {"code":"..."}.
A quote, backslash, or control character in that value terminates the
JSON string and changes the request. The handler receives the URL
argument verbatim, so the value is not a locally typed literal.

Escape per RFC 8259 before embedding. One call site; response parsing
is unchanged.
…ache

- Percent-decode bst:// arguments, then JSON-escape the redeem code.
- Parse JSON string replies with escapes, so a quote inside "reason" does not truncate it.
- Achievement refresh deletes only a decimal appid (or all), so the URL cannot path-escape the stats cache.
- Stop the HTTP reader from allocating past maxBodyBytes.
- Lock and cap the achievement jobid map so unanswered jobs cannot race the recv thread or grow for the session.
- Replace pattern-cache writes atomically so a crash or overlapping refresh cannot leave a partial TOML that cache-first will trust.
- Drop expired manifest 404s so the negative cache cannot grow for a whole session.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant