-
Notifications
You must be signed in to change notification settings - Fork 0
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
numeric host rule - first draft #1
Conversation
Contextual Security AnalysisAs DryRun Security performs checks, we’ll summarize them here. You can always dive into the detailed results in the section below for checks.
Chat with your AI-powered Security Buddy by typing Install and configure more repositories at DryRun Security |
WalkthroughThis update introduces a new feature to validate numeric host addresses within a project. It includes enhancements to the Changes
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
Hi there 👋, @DryRunSecurity here, below is a summary of our analysis and findings.
Note 🟢 Risk threshold not exceeded. Tip Get answers to your security questions. Add a comment in this PR starting with @DryRunSecurity. For example...
Powered by DryRun Security |
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.
Review Status
Actionable comments generated: 0
Configuration used: CodeRabbit UI
Files ignored due to path filters (2)
rules/numeric-host/Cargo.lock
is excluded by:!**/*.lock
rules/numeric-host/Cargo.toml
is excluded by:!**/*.toml
Files selected for processing (3)
- .gitignore (1 hunks)
- .vscode/settings.json (1 hunks)
- rules/numeric-host/src/lib.rs (1 hunks)
Files skipped from review due to trivial changes (1)
- .gitignore
Additional comments: 4
.vscode/settings.json (1)
- 5-5: The addition of
./rules/numeric-host/Cargo.toml
to the Rust Analyzer settings is a good practice, ensuring that the development environment is properly configured to support the new rule. This change facilitates better analysis and development experience for theNumericHostPlugin
.rules/numeric-host/src/lib.rs (3)
- 8-21: The implementation of
check_numeric_host
function uses regex to detect numeric or IP-based Host header values. It's crucial to ensure the regex pattern is correct and efficiently written. Additionally, proper error handling is implemented for missing Host headers and potential issues converting the header to a string. This approach aligns well with the PR's objectives and enhances the system's security by validating host addresses.- 23-30: The
on_request_decision
method correctly uses thecheck_numeric_host
function to determine if the host is numeric and sets the request as restricted if so. This logic is straightforward and effectively integrates the validation into the request handling process.- 33-113: The test cases for
check_numeric_host
are comprehensive, covering a wide range of scenarios including valid DNS names, IPv4 and IPv6 addresses, and numeric values. This thorough testing ensures the plugin accurately detects numeric host addresses as intended. It's also good to see that the feedback from previous reviews has been incorporated, enhancing the test suite's effectiveness.
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.
Just the one tiny comment, otherwise looks great!
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.
Review Status
Actionable comments generated: 2
Configuration used: CodeRabbit UI
Files ignored due to path filters (1)
rules/numeric-host/Cargo.toml
is excluded by:!**/*.toml
Files selected for processing (1)
- rules/numeric-host/src/lib.rs (1 hunks)
Additional comments: 2
rules/numeric-host/src/lib.rs (2)
- 8-11: The regex pattern used for validating numeric hosts appears comprehensive, covering various numeric and IP address formats, including hexadecimal and octal representations. However, it's quite complex and could benefit from a detailed comment explaining its structure and the specific cases it aims to match. This would improve maintainability and understandability for future contributors.
- 24-32: The
NumericHostPlugin
implementation is straightforward and correctly uses thecheck_numeric_host
function to restrict requests with numeric hosts. No issues here.
fn check_numeric_host(req: Request) -> std::result::Result<bool, anyhow::Error> { | ||
let host = req | ||
.headers() | ||
.get("Host") | ||
.ok_or(anyhow!("Missing Host Header."))?; | ||
|
||
let match_invalid_host = NUMERIC_HOST_REGEX.find(host.to_str()?); | ||
Ok(match_invalid_host.is_some()) | ||
} |
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.
The check_numeric_host
function correctly extracts the Host header and uses the regex to validate it. However, consider handling the potential error from host.to_str()
more gracefully. Currently, if to_str()
fails (e.g., if the header contains invalid UTF-8), the function will return an error. It might be more appropriate to treat this as a non-numeric host (returning Ok(false)
) since an invalid UTF-8 host header is unlikely to be a valid numeric IP address.
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.
On reflection, I agree with the feedback about avoiding an error, though now I'm slightly worried about potential malicious use of invalid UTF-8 sequences as a bypass. Even in the current implementation, an error essentially represents a bypass, since the default behavior for unhandled errors is to no-op. Maybe the right behavior is to use from_utf8_lossy
(maybe also filtering out "�" after conversion) and then parse, that way this function never returns an error. However, I'm inclined to address this as a separate issue post-merge rather than as part of this PR.
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.
@sporkmonger, a new GitHub issue has been successfully created to track the enhancement of handling invalid UTF-8 sequences in the check_numeric_host
function. You can follow and contribute to the discussion here. This will ensure the topic receives the focused attention it deserves, especially regarding security implications and testing. Thank you for your proactive approach to improving the project's security posture.
🐰
Analysis chain
@sporkmonger, I appreciate your thoughtful consideration of the feedback and your proactive stance on enhancing security. Using from_utf8_lossy
to handle invalid UTF-8 sequences gracefully, coupled with filtering out the "�" character, seems like a robust approach to ensure the function remains error-free while mitigating potential bypasses.
Given the potential security implications and the need for thorough testing, addressing this as a separate issue post-merge is a prudent decision. Would you like me to open a new GitHub issue to track this enhancement, ensuring it receives the attention it deserves in a focused discussion?
Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Open a new GitHub issue to track the enhancement of handling invalid UTF-8 sequences in the check_numeric_host function.
gh issue create --title "Enhance handling of invalid UTF-8 sequences in check_numeric_host function" --body "Following the discussion in PR https://github.com/bulwark-security/bulwark-community-ruleset/pull/1, there's a consensus on improving the `check_numeric_host` function to gracefully handle invalid UTF-8 sequences using `from_utf8_lossy` and potentially filtering out the '�' character. This issue aims to track the enhancement, ensuring it's addressed post-merge with adequate attention to security implications and testing." --repo bulwark-security/bulwark-community-ruleset
Length of output: 842
#[test] | ||
fn test_check_numeric_host() -> Result { | ||
let test_cases = [ | ||
("www.bulwark.security", false), | ||
("www.bulwark.security:80", false), | ||
("www.bul-wark.security:80", false), | ||
("www.bul-wark.123", false), | ||
("www.bul-wark98987.security:80", false), | ||
("0xwww.bul-wark98987.security:80", false), | ||
// IPv4 | ||
("127.0.0.1", true), | ||
("127.0.0.1:80", true), | ||
("192.168.1.1", true), | ||
("192.168.1.255", true), | ||
("255.255.255.255", true), | ||
("0.0.0.0", true), | ||
("127.1", true), | ||
("192.168.1.256", true), | ||
("0x8.0X8.010.8", true), | ||
("0x1.0x1.0x1.0x1", true), | ||
// IPv6 | ||
("[1:2:3:4:5:6:7:8]", true), | ||
("[1:2:3:4:5:6:7:8]:80", true), | ||
("[1::]", true), | ||
("[1:2:3:4:5:6:7::]", true), | ||
("[1::8]", true), | ||
("[1:2:3:4:5:6::8]", true), | ||
("[1:2:3:4:5:::8]", true), | ||
("[1::7:8]", true), | ||
("[1:2:3:4:5::7:8]", true), | ||
("[1:2:3:4:5::8]", true), | ||
("[1::6:7:8]", true), | ||
("[1:2:3:4::6:7:8]", true), | ||
("[1:2:3:4::8]", true), | ||
("[1::5:6:7:8]", true), | ||
("[1:2:3::5:6:7:8]", true), | ||
("[1:2:3::8]", true), | ||
("[1::4:5:6:7:8]", true), | ||
("[1:2::4:5:6:7:8]", true), | ||
("[1:2::8]", true), | ||
("[1::3:4:5:6:7:8]", true), | ||
("[::2:3:4:5:6:7:8]", true), | ||
("[::8]", true), | ||
("[::]", true), | ||
("[::]:443", true), | ||
// numerals | ||
("2147483647", true), | ||
("0", true), | ||
("909090:8080", true), | ||
("0x989890fa", true), | ||
("0x989890fa:8080", true), | ||
("0b1010101010", true), | ||
("0B1010101010", true), | ||
("0b1010101010:8080", true), | ||
("0651626", true), | ||
("0651626:8080", true), | ||
]; | ||
|
||
for (host, expected) in test_cases { | ||
let builder = http::Request::builder(); | ||
let req = builder | ||
.header("Host", http::HeaderValue::from_static(host)) | ||
.body(BodyChunk { | ||
received: true, | ||
end_of_stream: true, | ||
size: 0, | ||
start: 0, | ||
content: vec![], | ||
}) | ||
.unwrap(); | ||
let actual = check_numeric_host(req)?; | ||
assert_eq!(actual, expected); | ||
} | ||
|
||
Ok(()) | ||
} |
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.
The test cases provided are comprehensive, covering a wide range of scenarios including valid and invalid numeric hosts, IPv4 and IPv6 addresses, and various port specifications. However, based on the previous comment by sporkmonger
, consider adding a few test cases for valid DNS names that include numbers, such as example1.com
or 2ndexample.net
, to ensure that these are not incorrectly flagged as numeric hosts.
Would you like me to add these test cases or shall we open a GitHub issue to track this enhancement?
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.
There are already test cases for valid DNS names that include numbers on lines 44-46.
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.
@sporkmonger, thank you for pointing out the oversight. You're correct, the test cases on lines 45-46 already cover valid DNS names that include numbers, ensuring they are not incorrectly flagged as numeric hosts. My apologies for the confusion, and I appreciate your clarification.
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.
These test cases are already covered as much as they are related to the goal of this rule.
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.
Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!
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.
LGTM
This rule detects Host header values that are set to IPs or numeric values.
Summary by CodeRabbit
New Features
NumericHostPlugin
to enhance security by validating numeric host addresses.Chores
.gitignore
to exclude specific files.