-
Notifications
You must be signed in to change notification settings - Fork 0
feat(gateway): trust forwarded IPs only from trusted proxies #148
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
23
commits into
fix/pin-hosted-runner-20260902-v2
from
feat/trusted-proxy-admission-v2
Closed
Changes from all commits
Commits
Show all changes
23 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 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 @@ | ||
| 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
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 |
|
seonghobae marked this conversation as resolved.
|
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,163 @@ | ||
| #![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. Trusted peers may | ||
| //! supply an `X-Forwarded-For` identity only when every hop parses; malformed | ||
| //! chains fail closed to the direct peer. | ||
|
|
||
| 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 { | ||
| let parsed = forwarded | ||
| .split(',') | ||
| .map(|hop| { | ||
| let hop = hop.trim(); | ||
| if hop.is_empty() { | ||
| return Err(()); | ||
| } | ||
| hop.parse::<IpAddr>().map_err(|_| ()) | ||
| }) | ||
| .collect::<Result<Vec<_>, _>>(); | ||
| let Ok(hops) = parsed else { | ||
| return Some(peer_ip); | ||
| }; | ||
| if let Some(client_ip) = hops | ||
| .into_iter() | ||
| .rev() | ||
| .find(|ip| !is_trusted_single_host(*ip, trusted_proxy_ip)) | ||
| { | ||
| return Some(client_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 fail closed on malformed forwarding metadata" | ||
| ); | ||
| }); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Replace the trusted-forwarded-IP corpus seeds with valid
Inputencodings.The fuzz target decodes raw corpus bytes as
Arbitrary<Input>, so these text-only files do not directly exercise the forwarded-hop scenarios named by their filenames. Update the affectedvalid_*,ipv4_mapped_chain, andmalformed_middle_hopseeds to deterministicInputencodings, or add an explicit seed-replay format so each corpus file guarantees its intended regression case.📍 Affects 2 files
fuzz/corpus/fuzz_trusted_forwarded_client_ip/ipv4_mapped_chain#L1-L1(this comment)fuzz/corpus/fuzz_trusted_forwarded_client_ip/valid_ipv4_chain#L1-L1🤖 Prompt for AI Agents