-
Notifications
You must be signed in to change notification settings - Fork 60
refactor(signer): Use config-based header to extract IP from #388
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
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
604bec1
Add a config to set a trusted header to extract the IP from
ManuelBilbao df41968
Add docs
ManuelBilbao 38c8738
Merge branch 'sigp-audit-fixes' into trusted_ip_header_config
ManuelBilbao 51a1699
Give option for unique header or comma-separated rightmost value
ManuelBilbao 40ddea5
Update docs
ManuelBilbao 443e95b
Clean up
ManuelBilbao 54c692e
Clippy
ManuelBilbao 4ce547a
Apply suggestions from code review
ManuelBilbao 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 |
|---|---|---|
|
|
@@ -4,3 +4,4 @@ pub mod manager; | |
| mod metrics; | ||
| mod proto; | ||
| pub mod service; | ||
| mod utils; | ||
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,64 @@ | ||
| use std::net::{IpAddr, SocketAddr}; | ||
|
|
||
| use axum::http::HeaderMap; | ||
| use cb_common::config::ReverseProxyHeaderSetup; | ||
|
|
||
| #[derive(Debug, thiserror::Error)] | ||
| pub enum IpError { | ||
| #[error("header `{0}` is not present")] | ||
| NotPresent(String), | ||
| #[error("header value has invalid characters")] | ||
| HasInvalidCharacters, | ||
| #[error("header value is not a valid IP address")] | ||
| InvalidValue, | ||
| #[error("header `{0}` appears multiple times but expected to be unique")] | ||
| NotUnique(String), | ||
| } | ||
|
|
||
| /// Get the true client IP from the request headers or fallback to the socket | ||
| /// address | ||
| pub fn get_true_ip( | ||
| headers: &HeaderMap, | ||
| addr: &SocketAddr, | ||
| reverse_proxy: &ReverseProxyHeaderSetup, | ||
| ) -> Result<IpAddr, IpError> { | ||
| match reverse_proxy { | ||
| ReverseProxyHeaderSetup::None => Ok(addr.ip()), | ||
| ReverseProxyHeaderSetup::Unique(header) => get_ip_from_unique_header(headers, header), | ||
| ReverseProxyHeaderSetup::Rightmost(header) => get_ip_from_rightmost_value(headers, header), | ||
| } | ||
| } | ||
|
|
||
| fn get_ip_from_unique_header(headers: &HeaderMap, header_name: &str) -> Result<IpAddr, IpError> { | ||
| let mut values = headers.get_all(header_name).iter(); | ||
|
|
||
| let first_value = values.next().ok_or(IpError::NotPresent(header_name.to_string()))?; | ||
| let ip = first_value | ||
| .to_str() | ||
| .map_err(|_| IpError::HasInvalidCharacters)? | ||
| .parse::<IpAddr>() | ||
| .map_err(|_| IpError::InvalidValue)?; | ||
|
|
||
| if values.next().is_some() { | ||
| return Err(IpError::NotUnique(header_name.to_string())); | ||
| } | ||
|
|
||
| Ok(ip) | ||
| } | ||
|
|
||
| fn get_ip_from_rightmost_value(headers: &HeaderMap, header_name: &str) -> Result<IpAddr, IpError> { | ||
| let last_value = headers | ||
| .get_all(header_name) | ||
| .iter() | ||
| .next_back() | ||
| .ok_or(IpError::NotPresent(header_name.to_string()))? | ||
| .to_str() | ||
| .map_err(|_| IpError::HasInvalidCharacters)?; | ||
|
|
||
| last_value | ||
| .rsplit_once(",") | ||
| .map(|(_, rightmost)| rightmost) | ||
| .unwrap_or(last_value) | ||
| .parse::<IpAddr>() | ||
| .map_err(|_| IpError::InvalidValue) | ||
| } |
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
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.