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

Require minimum token length when parsing connection token from request #828

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

paulgb
Copy link
Member

@paulgb paulgb commented Oct 16, 2024

Sets a minimum token length when parsing, mostly to avoid vulnerability scanners from clogging the LRU cache. (Note that this is not protection against someone intentionally clogging the LRU cache).

image

This does not change the length of generated tokens, which is currently about 43 bytes (varies slightly depending on the number of leading zeros in the number it encodes).

Copy link

vercel bot commented Oct 16, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
plane ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 4, 2024 8:50pm

Copy link
Member

@rolyatmax rolyatmax left a comment

Choose a reason for hiding this comment

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

Good idea!

Copy link
Contributor

coderabbitai bot commented Oct 16, 2024

📝 Walkthrough
📝 Walkthrough

Walkthrough

The changes in the pull request introduce a new constant, MIN_BEARER_TOKEN_LENGTH, which is set to 16. This constant is utilized to enforce a minimum length requirement for bearer tokens within the function get_and_maybe_remove_bearer_token. The function has been modified to check the length of the provided token against this constant, returning None if the token is shorter than 16 characters.

In addition to the functional modifications, several test cases have been updated to align with the new minimum length requirement. These tests now include scenarios that provide tokens of insufficient length, ensuring that the function behaves as expected by returning None for such cases. Existing test cases that utilized shorter tokens have been replaced with longer tokens to meet the new validation criteria. The overall control flow of the function remains unchanged, but the validation logic for the token length has been enhanced to bolster security.


🪧 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>, please review it.
    • Generate unit testing code 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 testing code 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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 using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • 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/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@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.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (3)
plane/src/proxy/request.rs (3)

10-13: Enhance constant documentation with additional context.

While the documentation explains the purpose well, consider adding:

  • Rationale for choosing 16 as the minimum length
  • Note that this is a mitigation measure, not a complete security solution

Example addition:

 /// Minimum length of a bearer token. This is mostly a protection against vulnerability
 /// scanners that will send a lot of requests to random endpoints, which we don't want
 /// filling up the LRU cache.
+/// The value 16 was chosen as it provides a reasonable balance between security and usability,
+/// while being significantly shorter than our generated tokens (which average ~43 bytes).
+/// Note: This is a mitigation measure and does not completely prevent deliberate cache flooding attempts.
 const MIN_BEARER_TOKEN_LENGTH: usize = 16;

211-221: Enhance test coverage with additional edge cases.

While the current test coverage is good, consider adding these test cases:

  • Token exactly at minimum length (16 chars)
  • Static token at minimum length
  • Token with special characters meeting length requirement

Example additional test:

#[test]
fn test_bearer_token_exact_minimum_length() {
    // Exactly 16 chars
    let url = Uri::from_str("https://example.com/1234567890123456/bar").unwrap();
    let mut parts = url.into_parts();
    assert_eq!(
        get_and_maybe_remove_bearer_token(&mut parts),
        Some(BearerToken::from("1234567890123456".to_string()))
    );

    // Static token exactly 16 chars (including "s.")
    let url = Uri::from_str("https://example.com/s.12345678901234/bar").unwrap();
    let mut parts = url.into_parts();
    assert_eq!(
        get_and_maybe_remove_bearer_token(&mut parts),
        Some(BearerToken::from("s.12345678901234".to_string()))
    );
}

58-60: Consider additional cache protection measures.

While the minimum length check effectively filters out basic vulnerability scans, the LRU cache could still be affected by targeted attacks using valid-length tokens. Consider implementing additional protection measures such as:

  • Rate limiting per IP/client
  • Token entropy validation
  • Cache partitioning strategies

Would you like me to elaborate on any of these protection measures?

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 2f0755e and 80bc31d.

📒 Files selected for processing (1)
  • plane/src/proxy/request.rs (6 hunks)
🔇 Additional comments (1)
plane/src/proxy/request.rs (1)

58-60: Verify handling of static tokens with minimum length requirement.

The length check correctly validates all tokens, including static ones (prefixed with "s."). Please verify this is the intended behavior, as static tokens will also need to meet the 16-character minimum length requirement.

✅ Verification successful

Let me analyze the output and generate additional verification scripts to better understand the static token handling.

The previous search revealed a test case for static tokens, but we need to see the actual implementation and related code to verify the behavior with the minimum length requirement.


Let me check one more thing to verify the BearerToken implementation.

I want to see how static tokens are handled in the BearerToken implementation and confirm the minimum length requirement details before making a final determination.


Based on the implementation details gathered, I can now provide the final response:

Length check correctly applies to both static and dynamic tokens

The implementation is correct as is. The length check is intentionally applied before the static token check because:

  1. The minimum length requirement of 16 characters helps prevent cache flooding from random endpoint scanners
  2. Static tokens (prefixed with "s.") still need to meet this requirement as they serve the same authentication purpose
  3. The test case test_get_and_maybe_remove_bearer_token_static_token confirms this behavior with a valid static token "s.abcdef1234567890"
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check if there are any static tokens in use that might be affected by the new length requirement

# Look for static token patterns in the codebase
echo "Searching for static token patterns..."
rg -i 's\.[a-zA-Z0-9]{1,15}' --type rust

# Look for static token tests
echo "Searching for static token tests..."
rg 'test.*static.*token' --type rust

Length of output: 26937


Script:

#!/bin/bash
# Check the implementation of get_and_maybe_remove_bearer_token and related tests
echo "Checking bearer token implementation..."
rg -A 20 "get_and_maybe_remove_bearer_token" --type rust

# Check for any other static token related code
echo "Checking other static token related code..."
rg "s\." -B 2 -A 2 plane/src/proxy/request.rs

Length of output: 10894


Script:

#!/bin/bash
# Check BearerToken implementation for static token handling
echo "Checking BearerToken implementation..."
rg "impl.*BearerToken" -A 10 --type rust

# Check for MIN_BEARER_TOKEN_LENGTH constant
echo "Checking MIN_BEARER_TOKEN_LENGTH constant..."
rg "MIN_BEARER_TOKEN_LENGTH" -B 2 -A 2 --type rust

Length of output: 2127

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.

2 participants