HLE: prevent automatic WinHTTP redirects #997

Closed
Hiiva wants to merge 1 commit from upstream-pr/997 into main
Hiiva commented 2026-11-24 14:47:50 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Hiiva on 2026-11-24T14:47:50Z.


WinHTTP can follow a redirect returned by an allowed loopback server inside the host implementation, without another call through the WinHttpConnect guard.

Set WINHTTP_OPTION_REDIRECT_POLICY_NEVER before returning a new session. Later redirect-policy changes on sessions or requests still apply NEVER. If the host cannot enforce the policy, close the handle and preserve the option failure.

The existing destination and proxy guards remain in place. The option type, supported handles, and behavior follow Microsoft's WinHTTP option documentation.

Synthetic regression coverage:

  • Two loopback listeners: the first returns a 307 and the second detects any follow. All four session/request-policy combinations return 307, retain NEVER, and make zero connections to the second listener.
  • Dynamically loaded WinHttpOpen and WinHttpSetOption resolve to the guarded exports.
  • An option failure is returned with the original error, and a non-null handle that cannot accept the policy is closed. This check runs in an isolated child so parallel tests cannot reuse the closed handle value.

The redirect regression fails with the guard disabled: all four requests reach the second local listener and return 200. With the guard restored, all four return 307 without following the redirect.

Validation:

  • cargo test -p victory-hle modules::net:: -- --nocapture: four tests pass, including the isolated child check.
  • cargo test -p victory-hle --features diagnostics modules::net:: -- --nocapture: four tests pass.
  • cargo clippy -p victory-hle --all-targets --features diagnostics -- -D warnings: passes.
  • cargo xtask policy: passes.
<!-- greyfinch-sync:Marvisak/VIctory:issue:6584468429 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/997). Originally posted by [Hiiva](https://github.com/Hiiva) on 2026-11-24T14:47:50Z. --- WinHTTP can follow a redirect returned by an allowed loopback server inside the host implementation, without another call through the `WinHttpConnect` guard. Set `WINHTTP_OPTION_REDIRECT_POLICY_NEVER` before returning a new session. Later redirect-policy changes on sessions or requests still apply `NEVER`. If the host cannot enforce the policy, close the handle and preserve the option failure. The existing destination and proxy guards remain in place. The option type, supported handles, and behavior follow Microsoft's [WinHTTP option documentation](https://learn.microsoft.com/en-us/windows/win32/winhttp/option-flags#winhttp_option_redirect_policy). Synthetic regression coverage: - Two loopback listeners: the first returns a 307 and the second detects any follow. All four session/request-policy combinations return 307, retain `NEVER`, and make zero connections to the second listener. - Dynamically loaded `WinHttpOpen` and `WinHttpSetOption` resolve to the guarded exports. - An option failure is returned with the original error, and a non-null handle that cannot accept the policy is closed. This check runs in an isolated child so parallel tests cannot reuse the closed handle value. The redirect regression fails with the guard disabled: all four requests reach the second local listener and return 200. With the guard restored, all four return 307 without following the redirect. Validation: - `cargo test -p victory-hle modules::net:: -- --nocapture`: four tests pass, including the isolated child check. - `cargo test -p victory-hle --features diagnostics modules::net:: -- --nocapture`: four tests pass. - `cargo clippy -p victory-hle --all-targets --features diagnostics -- -D warnings`: passes. - `cargo xtask policy`: passes.
mercuryVM commented 2026-11-24 16:04:19 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T15:58:02Z.

Review: CHANGES_REQUESTED


Tested 7d1a0a5 on Windows 11, RTX 5070, MSVC: builds; cargo test -p victory-hle 97 passed, 0 failed, including the three new redirect_tests. The finding below is from reading.

Setting WINHTTP_OPTION_REDIRECT_POLICY_NEVER on every new session, and pinning it against later changes, is the right guard. The redirect test with two loopback listeners demonstrates it convincingly. One problem with the failure handling:

  1. WinHttpSetOption closes a handle the title owns (net.rs:783 via :808). no_http_redirects closes the handle whenever setting the option fails, and the guarded WinHttpSetOption calls it for every WINHTTP_OPTION_REDIRECT_POLICY request. If a title sets the policy on a handle that doesn't support it (a connection handle, as the new test does), we close that handle behind its back. The title's next use or its own WinHttpCloseHandle then hits a closed handle, or one WinHTTP has reused for a different handle in another thread. On Windows and on console, a failed WinHttpSetOption leaves the handle open. Fix: close only in WinHttpOpen, where the session is ours and never reached the title. In WinHttpSetOption, return the failure with its last error and leave the handle alone, which already keeps the policy at NEVER. redirect_policy_failure_is_not_reported_as_success would then assert the connection is still usable instead of closed.
  2. Nit: the test's comment says "The option failure survives the close" and "Do not close it again", which documents the behaviour above as intended. It should change along with the fix.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:review:6042757589 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/997#pullrequestreview-6042757589). Originally posted by [mercuryVM](https://github.com/mercuryVM) on 2026-11-24T15:58:02Z. **Review:** CHANGES_REQUESTED --- Tested 7d1a0a5 on Windows 11, RTX 5070, MSVC: builds; `cargo test -p victory-hle` 97 passed, 0 failed, including the three new `redirect_tests`. The finding below is from reading. Setting `WINHTTP_OPTION_REDIRECT_POLICY_NEVER` on every new session, and pinning it against later changes, is the right guard. The redirect test with two loopback listeners demonstrates it convincingly. One problem with the failure handling: 1. **`WinHttpSetOption` closes a handle the title owns (net.rs:783 via :808).** `no_http_redirects` closes the handle whenever setting the option fails, and the guarded `WinHttpSetOption` calls it for every `WINHTTP_OPTION_REDIRECT_POLICY` request. If a title sets the policy on a handle that doesn't support it (a connection handle, as the new test does), we close that handle behind its back. The title's next use or its own `WinHttpCloseHandle` then hits a closed handle, or one WinHTTP has reused for a different handle in another thread. On Windows and on console, a failed `WinHttpSetOption` leaves the handle open. Fix: close only in `WinHttpOpen`, where the session is ours and never reached the title. In `WinHttpSetOption`, return the failure with its last error and leave the handle alone, which already keeps the policy at NEVER. `redirect_policy_failure_is_not_reported_as_success` would then assert the connection is still usable instead of closed. 2. Nit: the test's comment says "The option failure survives the close" and "Do not close it again", which documents the behaviour above as intended. It should change along with the fix. AI-assisted (Claude Code).
Zipettte commented 2026-11-24 16:05:05 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Zipettte on 2026-11-24T15:12:59Z.


Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at 7d1a0a5: cargo test -p victory-hle passes, 98 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed).

Tested with AI assistance (Claude).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7135649156 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/997#issuecomment-7135649156). Originally posted by [Zipettte](https://github.com/Zipettte) on 2026-11-24T15:12:59Z. --- Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at 7d1a0a5: `cargo test -p victory-hle` passes, 98 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed). _Tested with AI assistance (Claude)._
Marvisak commented 2026-11-25 02:06:02 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T01:25:38Z.


Automated triage for the maintainers.

Needs work. The gap is real and this tightens the offline guard rather than loosening it. WinHttpConnect only checks the first host, and WinHTTP follows redirects on its own by default, so a loopback server could send a request off the machine. Setting WINHTTP_OPTION_REDIRECT_POLICY to NEVER on every new session in WinHttpOpen is a good fix. Two things need changing first:

  • In crates/victory-hle/src/modules/net.rs, no_http_redirects calls WinHttpCloseHandle on the title's own handle when WinHttpSetOption fails. From WinHttpSetOption, that handle belongs to the title. For example, a connect handle, which does not accept this option, gets closed and later used or closed again by the title (redirect_policy_failure_is_not_reported_as_success asserts exactly this). Close the handle only in WinHttpOpen, where we created it. In WinHttpSetOption, either return success without forwarding (the session's NEVER policy already carries over to its requests), or forward NEVER and return the host's result as is.
  • net_redirect_tests.rs (322 lines) re-runs the test binary as a child process and starts two TCP listeners. Spawning processes from tests needs the maintainer's decision. Without the closing behaviour, the child-process test is not needed. One loopback 307 test plus a policy check is enough.

CI: not run on this head.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7149533893 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/997#issuecomment-7149533893). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T01:25:38Z. --- <!-- victory-triage v1 head=7d1a0a585b183c4bd2610ca7f270acc4641e1552 verdict=needs-work at=2026-11-25T01:25:36Z --> Automated triage for the maintainers. **Needs work.** The gap is real and this tightens the offline guard rather than loosening it. `WinHttpConnect` only checks the first host, and WinHTTP follows redirects on its own by default, so a loopback server could send a request off the machine. Setting `WINHTTP_OPTION_REDIRECT_POLICY` to NEVER on every new session in `WinHttpOpen` is a good fix. Two things need changing first: - In `crates/victory-hle/src/modules/net.rs`, `no_http_redirects` calls `WinHttpCloseHandle` on the title's own handle when `WinHttpSetOption` fails. From `WinHttpSetOption`, that handle belongs to the title. For example, a connect handle, which does not accept this option, gets closed and later used or closed again by the title (`redirect_policy_failure_is_not_reported_as_success` asserts exactly this). Close the handle only in `WinHttpOpen`, where we created it. In `WinHttpSetOption`, either return success without forwarding (the session's NEVER policy already carries over to its requests), or forward NEVER and return the host's result as is. - `net_redirect_tests.rs` (322 lines) re-runs the test binary as a child process and starts two TCP listeners. Spawning processes from tests needs the maintainer's decision. Without the closing behaviour, the child-process test is not needed. One loopback 307 test plus a policy check is enough. CI: not run on this head. --- _Generated by [Claude Code](https://claude.ai/code)_
upstream-sync closed this pull request 2026-11-26 07:00:40 +00:00

Pull request closed

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
Marvisak/VIctory!997
No description provided.