-
Notifications
You must be signed in to change notification settings - Fork 0
feat(gateway): trust forwarded IPs only from trusted proxies #131
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
Merged
seonghobae
merged 14 commits into
fix/pin-hosted-runner-20260902
from
codex/trusted-proxy-admission
Sep 1, 2026
Merged
Changes from all commits
Commits
Show all changes
14 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 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
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,150 @@ | ||
| #![no_main] | ||
| //! Fuzz trusted client-IP attribution for forwarded proxy headers. | ||
| //! | ||
| //! `effective_client_ip` is a trust-boundary parser: it decides whether | ||
| //! attacker-controlled forwarding headers can influence rate limiting, DNSBL | ||
| //! checks, and audit/event attribution. Arbitrary chains, invalid hops, IPv4, | ||
| //! IPv6, trusted peers, and untrusted peers must never panic, and the trusted | ||
| //! peer path must match the documented right-to-left selection rule. | ||
|
|
||
| use arbitrary::Arbitrary; | ||
| use libfuzzer_sys::fuzz_target; | ||
| use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; | ||
| use waf_ids_ai_soc::{IpNet, effective_client_ip}; | ||
|
|
||
| #[derive(Arbitrary, Debug, Clone)] | ||
| enum AnyIp { | ||
| V4(u32), | ||
| V6(u128), | ||
| } | ||
|
|
||
| impl AnyIp { | ||
| fn into_ip(self) -> IpAddr { | ||
| match self { | ||
| Self::V4(raw) => IpAddr::V4(Ipv4Addr::from(raw)), | ||
| Self::V6(raw) => IpAddr::V6(Ipv6Addr::from(raw)), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn normalized_ip(addr: IpAddr) -> IpAddr { | ||
| match addr { | ||
| IpAddr::V6(ip) => ip | ||
| .to_ipv4_mapped() | ||
| .map(IpAddr::V4) | ||
| .unwrap_or(IpAddr::V6(ip)), | ||
| IpAddr::V4(ip) => IpAddr::V4(ip), | ||
| } | ||
| } | ||
|
|
||
| fn is_trusted_single_host(ip: IpAddr, trusted_proxy_ip: IpAddr) -> bool { | ||
| normalized_ip(ip) == normalized_ip(trusted_proxy_ip) | ||
| } | ||
|
|
||
| #[derive(Arbitrary, Debug)] | ||
| enum Hop { | ||
| Ip(AnyIp), | ||
| Invalid(String), | ||
| Empty, | ||
| } | ||
|
|
||
| impl Hop { | ||
| fn into_text(self) -> String { | ||
| match self { | ||
| Self::Ip(ip) => ip.into_ip().to_string(), | ||
| Self::Invalid(raw) => raw, | ||
| Self::Empty => " ".to_string(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Arbitrary, Debug)] | ||
| struct Input { | ||
| trusted_proxy: AnyIp, | ||
| peer_ip: Option<AnyIp>, | ||
| trust_peer: bool, | ||
| forwarded_hops: Vec<Hop>, | ||
| x_real_ip: Option<Hop>, | ||
| } | ||
|
|
||
| fn expected_client_ip( | ||
| peer_ip: Option<IpAddr>, | ||
| x_forwarded_for: Option<&str>, | ||
| x_real_ip: Option<&str>, | ||
| trusted_proxy_ip: IpAddr, | ||
| trust_peer: bool, | ||
| ) -> Option<IpAddr> { | ||
| let peer_ip = match (peer_ip, trust_peer) { | ||
| (Some(_), false) => peer_ip, | ||
| (Some(peer_ip), true) => Some(peer_ip), | ||
| (None, _) => return None, | ||
| }?; | ||
|
|
||
| if !trust_peer { | ||
| return Some(peer_ip); | ||
| } | ||
|
|
||
| if let Some(forwarded) = x_forwarded_for { | ||
| for hop in forwarded.split(',').rev() { | ||
| let hop = hop.trim(); | ||
| if hop.is_empty() { | ||
| continue; | ||
| } | ||
| let Ok(ip) = hop.parse::<IpAddr>() else { | ||
| continue; | ||
| }; | ||
| if is_trusted_single_host(ip, trusted_proxy_ip) { | ||
| continue; | ||
| } | ||
| return Some(ip); | ||
| } | ||
| } | ||
|
|
||
| x_real_ip | ||
| .and_then(|value| value.trim().parse::<IpAddr>().ok()) | ||
| .or(Some(peer_ip)) | ||
| } | ||
|
|
||
| fuzz_target!(|input: Input| { | ||
| let trusted_proxy_ip = input.trusted_proxy.clone().into_ip(); | ||
| let peer_ip = input.peer_ip.map(AnyIp::into_ip); | ||
| let trusted_cidr = match trusted_proxy_ip { | ||
| IpAddr::V4(ip) => format!("{ip}/32"), | ||
| IpAddr::V6(ip) => format!("{ip}/128"), | ||
| }; | ||
| let trusted_proxy = IpNet::parse(&trusted_cidr).expect("single-host CIDR must parse"); | ||
| let trusted_proxies = vec![trusted_proxy.clone()]; | ||
| let peer_ip = if input.trust_peer && peer_ip.is_some() { | ||
| Some(trusted_proxy_ip) | ||
| } else { | ||
| peer_ip | ||
| }; | ||
| let trust_peer = peer_ip | ||
| .map(|peer_ip| is_trusted_single_host(peer_ip, trusted_proxy_ip)) | ||
| .unwrap_or(false); | ||
| let x_forwarded_for = if input.forwarded_hops.is_empty() { | ||
| None | ||
| } else { | ||
| Some( | ||
| input | ||
| .forwarded_hops | ||
| .into_iter() | ||
| .map(Hop::into_text) | ||
| .collect::<Vec<_>>() | ||
| .join(","), | ||
| ) | ||
| }; | ||
| let x_real_ip = input.x_real_ip.map(Hop::into_text); | ||
| let resolved = effective_client_ip(peer_ip, x_forwarded_for.as_deref(), x_real_ip.as_deref(), &trusted_proxies); | ||
| let expected = expected_client_ip( | ||
| peer_ip, | ||
| x_forwarded_for.as_deref(), | ||
| x_real_ip.as_deref(), | ||
| trusted_proxy_ip, | ||
| trust_peer, | ||
| ); | ||
| assert_eq!( | ||
| resolved, expected, | ||
| "trusted client attribution must match the right-to-left trust model" | ||
| ); | ||
| }); |
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.