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

Add Security Rules for Hardcoding Secrets in Python Applications #96

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

Conversation

ESS-ENN
Copy link
Collaborator

@ESS-ENN ESS-ENN commented Dec 7, 2024

Summary by CodeRabbit

Release Notes

  • New Features

    • Introduced security rules to detect hardcoded secrets in Python applications, including:
      • python-pyjwt-hardcoded-secret-python: Warns against hardcoded secrets in JWT encoding.
      • python-redis-hardcoded-secret-python: Alerts for hardcoded passwords in Redis connections.
      • python-requests-empty-password-python: Identifies empty passwords in HTTP authentication methods.
  • Tests

    • Added comprehensive test cases for each new security rule to validate proper handling of secrets and authentication scenarios.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


Sakshis seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

Copy link

coderabbitai bot commented Dec 7, 2024

Walkthrough

This pull request introduces three new security rules for Python applications that address the hardcoding of secrets in various contexts: JWT encoding, Redis connections, and empty passwords in HTTP authentication. Each rule is defined in a YAML file and categorized with a severity level of "warning." The rules include utility functions for matching specific patterns in the code to identify potential security risks associated with hardcoded secrets. Additionally, new test configurations and snapshot files are created to validate the implementation of these rules.

Changes

File Path Change Summary
rules/python/security/python-pyjwt-hardcoded-secret-python.yml Added rule python-pyjwt-hardcoded-secret-python to detect hardcoded secrets in JWT encoding.
rules/python/security/python-redis-hardcoded-secret-python.yml Added rule python-redis-hardcoded-secret-python to identify hardcoded passwords in Redis calls.
rules/python/security/python-requests-empty-password-python.yml Added rule python-requests-empty-password-python to detect empty passwords in HTTP authentication.
tests/__snapshots__/python-pyjwt-hardcoded-secret-python-snapshot.yml New snapshot for testing JWT encoding with hardcoded secrets.
tests/__snapshots__/python-redis-hardcoded-secret-python-snapshot.yml New snapshot for testing Redis client configurations with hardcoded secrets.
tests/__snapshots__/python-requests-empty-password-python-snapshot.yml New snapshot for testing HTTP authentication with empty passwords.
tests/python/python-pyjwt-hardcoded-secret-python-test.yml New test cases for valid and invalid JWT encoding scenarios regarding hardcoded secrets.
tests/python/python-redis-hardcoded-secret-python-test.yml New test cases for valid and invalid Redis client instantiation regarding hardcoded passwords.
tests/python/python-requests-empty-password-python-test.yml New test cases for valid and invalid HTTP authentication scenarios with empty passwords.

Possibly related PRs

Suggested reviewers

  • petrisorcoderabbit

🐰 In the code we hop and play,
Secrets hidden, kept at bay.
No more hardcoded strings in sight,
Use your vaults, keep them tight!
With tests and rules, we take a stand,
Secure our code, it's simply grand! 🌟


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

@coderabbitai coderabbitai bot changed the title @coderabbitai Add Security Rules for Hardcoding Secrets in Python Applications Dec 7, 2024
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.

Actionable comments posted: 8

🧹 Outside diff range and nitpick comments (7)
tests/python/python-pyjwt-hardcoded-secret-python-test.yml (2)

3-4: Consider adding more valid test cases.

The valid section could be expanded to include more secure patterns:

  • Environment variables: os.getenv('JWT_SECRET')
  • Secrets manager: aws_secrets_manager.get_secret('jwt')
  • Configuration files: config.get('jwt_secret')

5-23: Add test cases for additional hardcoding patterns.

The invalid section could be enhanced with more patterns:

  • F-strings: jwt.encode(payload, f"secret{123}", algorithm="HS256")
  • String concatenation: "secret" + "123"
  • Bytes literals: b"secret"
  • Multi-line strings: """secret"""
tests/python/python-redis-hardcoded-secret-python-test.yml (1)

10-34: Add test cases for additional Redis connection patterns.

Consider adding invalid test cases for:

  • Redis connection strings: redis://user:password@localhost:6379
  • URL-encoded passwords: redis://user:pass%40word@localhost:6379
  • Cluster configurations
  • Sentinel configurations
tests/python/python-requests-empty-password-python-test.yml (1)

1-54: Consider adding security documentation.

These test files implement important security checks. Consider:

  1. Adding comments explaining the security implications of each pattern
  2. Including references to relevant security standards (OWASP, CWE)
  3. Creating a README.md with:
    • Security best practices
    • Common vulnerabilities
    • Remediation strategies
rules/python/security/python-requests-empty-password-python.yml (1)

78-78: Remove trailing spaces.

Several lines contain trailing spaces that should be removed for consistency.

Also applies to: 104-104, 111-111, 137-137, 329-329

🧰 Tools
🪛 yamllint (1.35.1)

[error] 78-78: trailing spaces

(trailing-spaces)

tests/__snapshots__/python-requests-empty-password-python-snapshot.yml (1)

4-6: Consider using a mock server instead of httpbin.org.

Using an external service in tests could lead to:

  1. Flaky tests if the service is down
  2. Potential security implications of sending test credentials
  3. Network-dependent test execution

Consider using a mock HTTP server for testing.

Also applies to: 49-51, 94-96

rules/python/security/python-pyjwt-hardcoded-secret-python.yml (1)

15-107: Consider additional patterns for encoded secrets.

The current patterns might miss base64-encoded secrets. Consider adding patterns to detect common encoding patterns:

  1. Base64-encoded strings
  2. Hex-encoded strings
  3. Environment variable string literals
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 2f95a8e and 175d414.

📒 Files selected for processing (9)
  • rules/python/security/python-pyjwt-hardcoded-secret-python.yml (1 hunks)
  • rules/python/security/python-redis-hardcoded-secret-python.yml (1 hunks)
  • rules/python/security/python-requests-empty-password-python.yml (1 hunks)
  • tests/__snapshots__/python-pyjwt-hardcoded-secret-python-snapshot.yml (1 hunks)
  • tests/__snapshots__/python-redis-hardcoded-secret-python-snapshot.yml (1 hunks)
  • tests/__snapshots__/python-requests-empty-password-python-snapshot.yml (1 hunks)
  • tests/python/python-pyjwt-hardcoded-secret-python-test.yml (1 hunks)
  • tests/python/python-redis-hardcoded-secret-python-test.yml (1 hunks)
  • tests/python/python-requests-empty-password-python-test.yml (1 hunks)
🧰 Additional context used
🪛 yamllint (1.35.1)
rules/python/security/python-redis-hardcoded-secret-python.yml

[warning] 37-37: wrong indentation: expected 20 but found 21

(indentation)


[warning] 72-72: wrong indentation: expected 20 but found 19

(indentation)


[warning] 76-76: wrong indentation: expected 20 but found 19

(indentation)


[warning] 79-79: wrong indentation: expected 21 but found 22

(indentation)


[error] 80-80: trailing spaces

(trailing-spaces)


[warning] 103-103: wrong indentation: expected 20 but found 21

(indentation)


[warning] 146-146: wrong indentation: expected 20 but found 19

(indentation)


[warning] 150-150: wrong indentation: expected 20 but found 19

(indentation)


[warning] 153-153: wrong indentation: expected 21 but found 22

(indentation)

rules/python/security/python-requests-empty-password-python.yml

[warning] 63-63: wrong indentation: expected 20 but found 19

(indentation)


[error] 78-78: trailing spaces

(trailing-spaces)


[warning] 96-96: wrong indentation: expected 20 but found 19

(indentation)


[error] 104-104: trailing spaces

(trailing-spaces)


[error] 111-111: trailing spaces

(trailing-spaces)


[warning] 129-129: wrong indentation: expected 20 but found 19

(indentation)


[error] 137-137: trailing spaces

(trailing-spaces)


[error] 329-329: trailing spaces

(trailing-spaces)

🔇 Additional comments (5)
rules/python/security/python-redis-hardcoded-secret-python.yml (1)

1-168: LGTM for the rule implementation!

The security rule implementation is well-structured and comprehensive:

  • Covers both direct Redis import and from-import scenarios
  • Handles both immediate password strings and variable assignments
  • Includes proper documentation with CWE reference and OWASP link
🧰 Tools
🪛 yamllint (1.35.1)

[warning] 37-37: wrong indentation: expected 20 but found 21

(indentation)


[warning] 72-72: wrong indentation: expected 20 but found 19

(indentation)


[warning] 76-76: wrong indentation: expected 20 but found 19

(indentation)


[warning] 79-79: wrong indentation: expected 21 but found 22

(indentation)


[error] 80-80: trailing spaces

(trailing-spaces)


[warning] 103-103: wrong indentation: expected 20 but found 21

(indentation)


[warning] 146-146: wrong indentation: expected 20 but found 19

(indentation)


[warning] 150-150: wrong indentation: expected 20 but found 19

(indentation)


[warning] 153-153: wrong indentation: expected 21 but found 22

(indentation)

tests/__snapshots__/python-redis-hardcoded-secret-python-snapshot.yml (1)

1-261: LGTM for Redis test snapshots!

The test snapshots provide comprehensive coverage:

  • Direct Redis import usage
  • From-import Redis usage
  • Both single-line and multi-line connection strings
  • Various host configurations (IP addresses and domain names)
tests/__snapshots__/python-pyjwt-hardcoded-secret-python-snapshot.yml (1)

1-327: LGTM for JWT test snapshots!

The test snapshots provide comprehensive coverage:

  • Direct jwt.encode usage
  • Variable-based secret usage
  • Aliased jwt import usage
rules/python/security/python-requests-empty-password-python.yml (1)

328-338: Verify pattern matching for edge cases.

The rule should be tested against various edge cases to ensure it doesn't miss any empty password scenarios.

✅ Verification successful

Pattern matching for empty password scenarios is well covered

The test file tests/python/python-requests-empty-password-python-test.yml comprehensively covers all edge cases for empty password scenarios:

  • Direct empty string literals ("") for all auth types:

    • HTTPBasicAuth('user', '')
    • HTTPDigestAuth('user', '')
    • HTTPProxyAuth('user', '')
  • Variable-based empty strings for all auth types:

    asd = ''
    HTTPBasicAuth('user', asd)
    HTTPDigestAuth('user', asd)
    HTTPProxyAuth('user', asd)
  • Both direct and fully qualified imports:

    • from requests.auth import HTTPBasicAuth
    • requests.auth.HTTPBasicAuth

The rule's pattern matching aligns perfectly with these test cases, ensuring no empty password scenarios are missed.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for test coverage of edge cases in the test files

# Look for test cases covering different scenarios
rg -A 5 "HTTPBasicAuth|HTTPDigestAuth|HTTPProxyAuth" tests/

Length of output: 31554

🧰 Tools
🪛 yamllint (1.35.1)

[error] 329-329: trailing spaces

(trailing-spaces)

rules/python/security/python-pyjwt-hardcoded-secret-python.yml (1)

435-441: Verify rule effectiveness with real-world JWT usage patterns.

Let's validate the rule against common JWT implementation patterns.

✅ Verification successful

Let me generate additional scripts to better understand the rule's patterns and test cases.

The previous results showed JWT usage in tests, but we need to verify the rule patterns against these test cases to ensure effectiveness.


Rule effectively covers common JWT hardcoded secret patterns

The rule implementation effectively detects various JWT hardcoded secret patterns through five comprehensive matchers:

  • Direct string secrets in jwt.encode/decode calls
  • Variable-based secrets where the variable is assigned a hardcoded string
  • Aliased JWT imports with hardcoded secrets
  • Different payload and algorithm combinations

Test cases validate these scenarios:

  • Hardcoded string secret: jwt.encode(..., "123", ...)
  • Variable with hardcoded secret: key = "secret"; jwt.encode(..., key, ...)
  • Aliased import with hardcoded secret: jwt as lsadlsaldsa; lsadlsaldsa.encode(..., "123", ...)

The rule's AST patterns correctly identify these security anti-patterns while allowing valid cases where secrets are not hardcoded.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for JWT usage patterns in the codebase

# Look for JWT usage patterns
rg -A 5 "jwt\.(encode|decode)" 

# Look for potential JWT imports
rg "^from.*jwt.*import|^import.*jwt"

Length of output: 5930


Script:

#!/bin/bash
# Check the rule patterns and test cases

# Look at the rule file content
cat rules/python/security/python-pyjwt-hardcoded-secret-python.yml

# Look at the test file content
cat tests/python/python-pyjwt-hardcoded-secret-python-test.yml

# Look at the snapshot file
cat tests/__snapshots__/python-pyjwt-hardcoded-secret-python-snapshot.yml

Length of output: 20442

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