-
Notifications
You must be signed in to change notification settings - Fork 0
feat(gateway): trust forwarded IPs only from trusted proxies #151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
seonghobae
wants to merge
38
commits into
fix/pin-hosted-runner-20260902-v3
from
feat/trusted-proxy-admission-v3
Closed
Changes from 29 commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
d0914ae
feat(gateway): trust forwarded IPs only from trusted proxies
codex 6fd62a7
fix(gateway): harden trusted proxy attribution
codex 6cfec21
test(gateway): fuzz trusted forwarded client attribution
codex b009e87
fix(gateway): accept mapped trusted proxy peers
codex b92c55b
test(gateway): align mapped proxy trust invariants
codex 66dbfd0
docs(gateway): clarify trusted proxy runtime contract
codex 82caad0
fix(gateway): reject invalid mapped proxy cidrs
codex bcbb901
Merge origin/main into codex/trusted-proxy-admission
codex 4a3d452
Merge origin/main into codex/trusted-proxy-admission
codex 597e723
fix(gateway): keep admin credential provenance accurate
seonghobae 629afc0
test(gateway): run trusted-proxy fuzz target in CI
seonghobae a183581
style: apply rustfmt to credential regression
seonghobae 97d9c4b
merge: synchronize trusted-proxy admission with protected main
seonghobae 72ac1a2
chore(stack): integrate pinned hosted-runner prerequisite
seonghobae 84c5fe3
chore(stack): converge trusted-proxy slice on current runner prerequi…
seonghobae 4f7bda1
test(fuzz): seed trusted-forwarding parser corpus
seonghobae 9935392
test(fuzz): seed IPv6 forwarding chain
seonghobae c6b34d7
test(fuzz): seed IPv4-mapped forwarding chain
seonghobae 0d05448
test(fuzz): seed malformed forwarding chain
seonghobae 00e9857
merge: converge trusted-proxy stack on current runner prerequisite
seonghobae c443c80
test(gateway): fail closed on malformed forwarded chains
seonghobae a975edd
test(gateway): make fuzz oracle reject malformed forwarding
seonghobae 2ce8384
test(gateway): fail closed in trusted-forwarding property model
seonghobae cd39417
test(gateway): reproduce trusted-proxy attribution bypasses
seonghobae 50721e5
ci: add exact-head PR151 source repair
seonghobae eb3a015
ci: trigger exact-head PR151 trusted-proxy repair
seonghobae d40b374
chore(ci): trigger trusted-proxy causal repair
seonghobae 02acb9e
docs(security): ground trusted-proxy policy in primary research
seonghobae 01bc90b
chore(ci): retrigger trusted-proxy causal repair
seonghobae a30d7c6
fix(gateway): fail closed on forwarded identity races
opencode-agent[bot] 99ccb76
chore(ci): add exact malformed-chain contract repair
seonghobae 5e90cdd
chore(ci): trigger malformed-chain contract repair
seonghobae 4d7425f
chore(ci): remove unregistered source-fix trigger
seonghobae cdd2d77
chore(ci): remove unregistered source-fix workflow
seonghobae 87a8d49
chore(ci): stage malformed-chain unit repair v2
seonghobae f911954
chore(ci): trigger malformed-chain unit repair v2
seonghobae f8f8a82
test(gateway): fail closed on malformed forwarded chain
opencode-agent[bot] 65a2b7f
docs(security): specify malformed forwarded-chain fallback
seonghobae File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| trigger=2026-09-02T09:06:00+09:00 | ||
| reason=repair exact-head direct-router compatibility and malformed-forwarded fail-closed findings after research traceability | ||
| runner=ubuntu-slim | ||
| head_guard=feat/trusted-proxy-admission-v3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
227 changes: 227 additions & 0 deletions
227
.github/workflows/source-fix-151-trusted-proxy-findings.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,227 @@ | ||
| name: Source fix PR151 trusted-proxy findings | ||
|
|
||
| on: | ||
| push: | ||
| branches: | ||
| - feat/trusted-proxy-admission-v3 | ||
| paths: | ||
| - .github/source-fix-151-trusted-proxy-findings.trigger | ||
|
|
||
| concurrency: | ||
| group: source-fix-pr151-trusted-proxy-findings | ||
| cancel-in-progress: true | ||
|
|
||
| jobs: | ||
| repair: | ||
| permissions: | ||
| contents: write | ||
| runs-on: ubuntu-slim | ||
| steps: | ||
| - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # actions/checkout@v7 | ||
| with: | ||
| ref: feat/trusted-proxy-admission-v3 | ||
| fetch-depth: 0 | ||
| persist-credentials: false | ||
| - uses: dtolnay/rust-toolchain@4be7066ada62dd38de10e7b70166bc74ed198c30 | ||
| with: | ||
| toolchain: stable | ||
| components: rustfmt | ||
| - name: Apply exact-head production repair | ||
| shell: bash | ||
| run: | | ||
| set -euo pipefail | ||
| python - <<'PY' | ||
| from pathlib import Path | ||
|
|
||
| path = Path("src/lib.rs") | ||
| text = path.read_text(encoding="utf-8") | ||
|
|
||
| replacements = { | ||
| ''' let peer_ip = match connect_info.as_ref().map(|peer| peer.0.ip()) { | ||
| Some(peer_ip) => peer_ip, | ||
| None => { | ||
| return error( | ||
| StatusCode::INTERNAL_SERVER_ERROR, | ||
| "gateway requires peer address metadata; serve with `serve(...)` or `into_make_service_with_connect_info::<SocketAddr>()`", | ||
| ); | ||
| } | ||
| };''': ''' let peer_ip = connect_info.as_ref().map(|peer| peer.0.ip());''', | ||
| ''' let client_ip = client_ip_from_request(&headers, Some(peer_ip), &state.trusted_proxies);''': ''' let client_ip = client_ip_from_request(&headers, peer_ip, &state.trusted_proxies);''', | ||
| '''/// Build the HTTP application surface for management, DNSBL, and gateway | ||
| /// traffic. Gateway requests still require peer metadata via | ||
| /// `ConnectInfo<SocketAddr>`; use [`serve`] or | ||
| /// `Router::into_make_service_with_connect_info::<SocketAddr>()`.''': '''/// Build the HTTP application surface for management, DNSBL, and gateway | ||
| /// traffic. Direct router use without `ConnectInfo<SocketAddr>` remains | ||
| /// compatible: forwarded identity is ignored and the existing shared | ||
| /// unknown-client limiter bucket is used. [`serve`] preserves real peer | ||
| /// attribution for production TCP traffic.''', | ||
| ''' match peer_ip { | ||
| Some(peer_ip) if trusted_proxies.iter().any(|proxy| proxy.contains(peer_ip)) => { | ||
| trusted_forwarded_chain_client_ip(x_forwarded_for, trusted_proxies) | ||
| .or_else(|| trusted_real_ip_value(x_real_ip)) | ||
| .or(Some(peer_ip)) | ||
| } | ||
| Some(peer_ip) => Some(peer_ip), | ||
| None => None, | ||
| }''': ''' match peer_ip { | ||
| Some(peer_ip) if trusted_proxies.iter().any(|proxy| proxy.contains(peer_ip)) => { | ||
| match trusted_forwarded_chain_client_ip_checked( | ||
| x_forwarded_for, | ||
| trusted_proxies, | ||
| ) { | ||
| Ok(Some(client_ip)) => Some(client_ip), | ||
| Ok(None) => trusted_real_ip_value(x_real_ip).or(Some(peer_ip)), | ||
| Err(()) => Some(peer_ip), | ||
| } | ||
| } | ||
| Some(peer_ip) => Some(peer_ip), | ||
| None => None, | ||
| }''', | ||
| '''pub fn trusted_forwarded_chain_client_ip( | ||
| x_forwarded_for: Option<&str>, | ||
| trusted_proxies: &[IpNet], | ||
| ) -> Option<IpAddr> { | ||
| let forwarded = x_forwarded_for?; | ||
| let mut client_ip = None; | ||
| for candidate in forwarded.split(',').rev() { | ||
| let candidate = candidate.trim(); | ||
| if candidate.is_empty() { | ||
| continue; | ||
| } | ||
| let ip = match candidate.parse::<IpAddr>() { | ||
| Ok(ip) => ip, | ||
| Err(_) => continue, | ||
| }; | ||
| if trusted_proxies.iter().any(|proxy| proxy.contains(ip)) { | ||
| continue; | ||
| } | ||
| client_ip = Some(ip); | ||
| break; | ||
| } | ||
| client_ip | ||
| }''': '''pub fn trusted_forwarded_chain_client_ip( | ||
| x_forwarded_for: Option<&str>, | ||
| trusted_proxies: &[IpNet], | ||
| ) -> Option<IpAddr> { | ||
| trusted_forwarded_chain_client_ip_checked(x_forwarded_for, trusted_proxies) | ||
| .ok() | ||
| .flatten() | ||
| } | ||
|
|
||
| /// Parse the complete forwarded chain before selecting an identity. Any | ||
| /// empty or unparsable hop invalidates the entire chain so callers can | ||
| /// fall back to the direct peer without consulting `X-Real-IP`. | ||
| fn trusted_forwarded_chain_client_ip_checked( | ||
| x_forwarded_for: Option<&str>, | ||
| trusted_proxies: &[IpNet], | ||
| ) -> Result<Option<IpAddr>, ()> { | ||
| let Some(forwarded) = x_forwarded_for else { | ||
| return Ok(None); | ||
| }; | ||
| let hops = forwarded | ||
| .split(',') | ||
| .map(|candidate| { | ||
| let candidate = candidate.trim(); | ||
| if candidate.is_empty() { | ||
| return Err(()); | ||
| } | ||
| candidate.parse::<IpAddr>().map_err(|_| ()) | ||
| }) | ||
| .collect::<Result<Vec<_>, _>>()?; | ||
| Ok(hops | ||
| .into_iter() | ||
| .rev() | ||
| .find(|ip| !trusted_proxies.iter().any(|proxy| proxy.contains(*ip)))) | ||
| }''', | ||
| ''' #[tokio::test] | ||
| async fn gateway_rejects_missing_connect_info_instead_of_silently_sharing_unknown_ip() { | ||
| let app = build_app(AppState::seeded(None).with_rate_limit(1, 60)); | ||
|
|
||
| let response = app_request( | ||
| &app, | ||
| Request::builder() | ||
| .method(Method::GET) | ||
| .uri("/gateway/demo") | ||
| .body(Body::empty()) | ||
| .unwrap(), | ||
| ) | ||
| .await; | ||
|
|
||
| assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR); | ||
| assert!( | ||
| body_text(response) | ||
| .await | ||
| .contains("gateway requires peer address metadata") | ||
| ); | ||
| }''': ''' #[tokio::test] | ||
| async fn gateway_missing_connect_info_uses_unknown_bucket_without_trusting_headers() { | ||
| let app = build_app(AppState::seeded(None).with_rate_limit(1, 60)); | ||
|
|
||
| let first = app_request( | ||
| &app, | ||
| Request::builder() | ||
| .method(Method::GET) | ||
| .uri("/gateway/demo") | ||
| .header("x-forwarded-for", "203.0.113.9") | ||
| .header("x-real-ip", "203.0.113.10") | ||
| .body(Body::empty()) | ||
| .unwrap(), | ||
| ) | ||
| .await; | ||
| assert_eq!(first.status(), StatusCode::OK); | ||
|
|
||
| let second = app_request( | ||
| &app, | ||
| Request::builder() | ||
| .method(Method::GET) | ||
| .uri("/gateway/demo") | ||
| .header("x-forwarded-for", "198.51.100.7") | ||
| .header("x-real-ip", "198.51.100.8") | ||
| .body(Body::empty()) | ||
| .unwrap(), | ||
| ) | ||
| .await; | ||
| assert_eq!(second.status(), StatusCode::TOO_MANY_REQUESTS); | ||
| }''', | ||
| } | ||
|
|
||
| for old, new in replacements.items(): | ||
| count = text.count(old) | ||
| if count != 1: | ||
| raise SystemExit(f"expected exactly one repair target, found {count}: {old[:80]!r}") | ||
| text = text.replace(old, new) | ||
|
|
||
| path.write_text(text, encoding="utf-8") | ||
| PY | ||
| cargo fmt --all | ||
| git diff --check | ||
| - name: Verify exact regressions | ||
| shell: bash | ||
| run: | | ||
| set -euo pipefail | ||
| cargo test --test trusted_forwarded_fail_closed | ||
| cargo test --test trusted_proxy_admission_regressions | ||
| cargo test gateway_missing_connect_info_uses_unknown_bucket_without_trusting_headers | ||
| cargo test trusted_forwarded_chain_client_ip | ||
| - name: Self-remove and publish non-force repair | ||
| env: | ||
| GH_TOKEN: ${{ github.token }} | ||
| shell: bash | ||
| run: | | ||
| set -euo pipefail | ||
| rm -f \ | ||
| .github/workflows/source-fix-151-trusted-proxy-findings.yml \ | ||
| .github/source-fix-151-trusted-proxy-findings.trigger | ||
| git add -A | ||
| git diff --cached --check | ||
| git config user.name 'opencode-agent[bot]' | ||
| git config user.email '219766164+opencode-agent[bot]@users.noreply.github.com' | ||
| git commit -m 'fix(gateway): fail closed on forwarded identity races' | ||
| git fetch --no-tags origin feat/trusted-proxy-admission-v3 | ||
| remote_head="$(git rev-parse FETCH_HEAD)" | ||
| if ! git merge-base --is-ancestor "$remote_head" HEAD; then | ||
| echo "writer branch moved concurrently; refusing to overwrite" >&2 | ||
| exit 1 | ||
| fi | ||
| git remote set-url origin "https://x-access-token:${GH_TOKEN}@github.com/${GITHUB_REPOSITORY}.git" | ||
| git push origin HEAD:feat/trusted-proxy-admission-v3 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 198.51.100.77, ::ffff:192.0.2.10 |
1 change: 1 addition & 0 deletions
1
fuzz/corpus/fuzz_trusted_forwarded_client_ip/malformed_middle_hop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 198.51.100.77, bad-ip, 192.0.2.10 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 198.51.100.77, 203.0.113.9, 192.0.2.10 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 2001:db8:ffff::1, 2001:db8::10 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.