Skip to content
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

Merged
merged 3 commits into from
Mar 14, 2024
Merged

Conversation

maliz-bulwark
Copy link
Collaborator

@maliz-bulwark maliz-bulwark commented Feb 13, 2024

This rule detects Host header values that are set to IPs or numeric values.

Summary by CodeRabbit

  • New Features

    • Introduced the NumericHostPlugin to enhance security by validating numeric host addresses.
  • Chores

    • Updated .gitignore to exclude specific files.
    • Adjusted settings for better integration with Rust Analyzer in VSCode.

Copy link

Contextual Security Analysis

As DryRun Security performs checks, we’ll summarize them here. You can always dive into the detailed results in the section below for checks.

Status DryRun Security Check
Sensitive Functions Analyzer
Configured Sensitive Files Check
Sensitive Files Analyzer

Chat with your AI-powered Security Buddy by typing @dryrunsecurity followed by your question into a comment.
Example: @dryrunsecurity What are common security issues with web application cookies?

Install and configure more repositories at DryRun Security

rules/numeric-host/src/lib.rs Outdated Show resolved Hide resolved
rules/numeric-host/src/lib.rs Outdated Show resolved Hide resolved
rules/numeric-host/src/lib.rs Show resolved Hide resolved
rules/numeric-host/src/lib.rs Show resolved Hide resolved
rules/numeric-host/src/lib.rs Outdated Show resolved Hide resolved
rules/numeric-host/src/lib.rs Outdated Show resolved Hide resolved
rules/numeric-host/src/lib.rs Show resolved Hide resolved
Copy link

coderabbitai bot commented Mar 6, 2024

Walkthrough

This update introduces a new feature to validate numeric host addresses within a project. It includes enhancements to the .gitignore file to ignore certain files, updates to the VS Code settings for better integration with Rust Analyzer, and the addition of a new NumericHostPlugin in the Rust project to check for valid numeric host addresses, complete with test cases for validation.

Changes

File(s) Change Summary
.gitignore Added *.wit to the list of ignored files.
.vscode/settings.json Added .../numeric-host/Cargo.toml to Rust Analyzer linked projects.
rules/numeric-host/src/lib.rs Introduced NumericHostPlugin and check_numeric_host function with tests.

"In the realm of code, where logic reigns supreme,
A rabbit hopped in, with a dream so keen.
🐰 With a wiggle of ears, and a tap of a paw,
Numeric hosts it sought, without a flaw.
Through .gitignore paths, it made its way,
Adding *.wit, keeping clutter at bay.
A poem of change, in bytes and bits,
Celebrates the hop, where creativity sits."

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?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

Copy link

dryrunsecurity bot commented Mar 6, 2024

Hi there 👋, @DryRunSecurity here, below is a summary of our analysis and findings.

DryRun Security Status Findings
Sensitive Functions Analyzer 0 findings
Configured Sensitive Files Analyzer 0 findings
Sensitive Files Analyzer 2 findings

Note

🟢 Risk threshold not exceeded.

Tip

Get answers to your security questions. Add a comment in this PR starting with @DryRunSecurity. For example...

@dryrunsecurity What are common security issues with web application cookies?

Powered by DryRun Security

Copy link

@coderabbitai coderabbitai bot left a 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

Commits Files that changed from the base of the PR and between db249a4 and a9ce634.
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 the NumericHostPlugin.
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 the check_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.

Copy link
Contributor

@sporkmonger sporkmonger left a 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!

rules/numeric-host/Cargo.toml Outdated Show resolved Hide resolved
Copy link

@coderabbitai coderabbitai bot left a 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

Commits Files that changed from the base of the PR and between a9ce634 and 7220f16.
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 the check_numeric_host function to restrict requests with numeric hosts. No issues here.

Comment on lines +14 to +22
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())
}
Copy link

@coderabbitai coderabbitai bot Mar 14, 2024

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.

Copy link
Contributor

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.

Copy link

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

Comment on lines +38 to +113
#[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(())
}
Copy link

@coderabbitai coderabbitai bot Mar 14, 2024

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?

Copy link
Contributor

@sporkmonger sporkmonger Mar 14, 2024

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.

Copy link

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.

Copy link
Collaborator Author

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.

Copy link

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!

Copy link
Contributor

@sporkmonger sporkmonger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@maliz-bulwark maliz-bulwark merged commit 4b1823c into main Mar 14, 2024
3 checks passed
@maliz-bulwark maliz-bulwark deleted the maliz-numeric-host-rule branch March 14, 2024 21:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants