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 rules for detecting hard-coded secrets in Python and Ruby applications #111

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 12, 2024

Summary by CodeRabbit

  • New Features

    • Introduced rules for detecting hard-coded secrets in Python applications using psycopg2, and Ruby applications using Excon and Octokit.
  • Bug Fixes

    • Added warnings for hard-coded credentials to enhance security practices.
  • Tests

    • Created new test cases and snapshots for validating secret management in Python and Ruby applications.

@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 12, 2024

Walkthrough

This pull request introduces new rules for detecting hard-coded secrets in Python and Ruby applications. Specifically, it adds rules for the psycopg2, Excon, and Octokit libraries, categorized with a severity level of "warning." Each rule includes guidance on avoiding hard-coded credentials, referencing CWE-798, and suggesting the use of environment variables or secure vaults. Additionally, test cases and snapshot files are created to validate these rules, ensuring that developers adhere to secure coding practices regarding sensitive information.

Changes

File Change Summary
rules/python/security/python-psycopg2-hardcoded-secret-python.yml Added rule python-psycopg2-hardcoded-secret-python to detect hard-coded secrets in psycopg2.connect.
rules/ruby/security/ruby-excon-hardcoded-secret-ruby.yml Introduced rule ruby-excon-hardcoded-secret-ruby for identifying hard-coded secrets in Excon library usage.
rules/ruby/security/ruby-octokit-hardcoded-secret-ruby.yml Added rule ruby-octokit-hardcoded-secret-ruby for detecting hard-coded secrets in Octokit library usage.
tests/snapshots/python-psycopg2-hardcoded-secret-python-snapshot.yml Created snapshot for testing hard-coded secrets in psycopg2 usage.
tests/snapshots/ruby-excon-hardcoded-secret-ruby-snapshot.yml Introduced snapshot for testing password handling in Excon library.
tests/snapshots/ruby-octokit-hardcoded-secret-ruby-snapshot.yml Created snapshot for testing Octokit client instantiation with various parameters.
tests/python/python-psycopg2-hardcoded-secret-python-test.yml Added test cases for valid and invalid usages of hard-coded secrets in psycopg2.
tests/ruby/ruby-excon-hardcoded-secret-ruby-test.yml Introduced test cases for valid and invalid usages of hard-coded secrets in Excon.
tests/ruby/ruby-octokit-hardcoded-secret-ruby-test.yml Added test cases for valid and invalid usages of hard-coded secrets in Octokit client.

Possibly related PRs

Suggested reviewers

  • rohit121

🐰 In the code where secrets hide,
A rabbit hops with eyes open wide.
No hard-coded keys, let’s keep them away,
Use env vars instead, that’s the way!
With tests and rules, we’ll keep it tight,
Secure our code, and sleep well at night! 🌙


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>, 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 generate docstrings to generate docstrings for this PR. (Experiment)
  • @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 rules for detecting hard-coded secrets in Python and Ruby applications Dec 12, 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: 4

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

2-5: Consider adding more test cases for the valid section.

The valid section could benefit from additional examples showing recommended practices:

  • Using environment variables (e.g., os.environ.get('DB_PASSWORD'))
  • Using secure configuration management
  • Using connection strings from environment variables

6-13: Add test cases for additional scenarios.

Consider adding these test cases to the invalid section:

  • Empty string password: psycopg2.connect(password="")
  • Multi-line connection string
  • Different variable names for password (e.g., pwd, passwd)
  • String concatenation: psycopg2.connect(password="pass" + "word")
rules/python/security/python-psycopg2-hardcoded-secret-python.yml (1)

4-9: Enhance the message with specific remediation steps.

The current message could be more actionable by including:

  • Example of using environment variables
  • Reference to specific secure vault solutions (e.g., HashiCorp Vault, AWS Secrets Manager)
  • Link to language-specific best practices for secrets management
tests/__snapshots__/python-psycopg2-hardcoded-secret-python-snapshot.yml (1)

7-55: Simplify overlapping label ranges.

The current snapshot contains multiple overlapping ranges for the same code segments. Consider:

  1. Reducing redundant labels (e.g., lines 44-51 duplicate lines 48-55)
  2. Using more specific ranges to avoid overlap
rules/ruby/security/ruby-octokit-hardcoded-secret-ruby.yml (1)

4-9: Enhance the warning message with specific guidance.

Consider adding specific examples of secure alternatives in the message:

  • Using environment variables (e.g., ENV['GITHUB_TOKEN'])
  • Using credential management services
  • Using configuration files outside version control
  message: >-
    A secret is hard-coded in the application. Secrets stored in source
    code, such as credentials, identifiers, and other types of sensitive data,
    can be leaked and used by internal or external malicious actors. Use
-   environment variables to securely provide credentials and other secrets or
-   retrieve them from a secure vault or Hardware Security Module (HSM).
+   environment variables (e.g., ENV['GITHUB_TOKEN']) to securely provide credentials,
+   or retrieve them from secure vaults (e.g., AWS Secrets Manager, HashiCorp Vault)
+   or Hardware Security Modules (HSM). Never commit credentials to version control.
tests/ruby/ruby-octokit-hardcoded-secret-ruby-test.yml (1)

6-25: Consider adding more edge cases to the test suite.

While the current test cases cover the main scenarios, consider adding:

  1. Multi-line string credentials
  2. Credentials with special characters
  3. Base64 encoded credentials
  4. Template string credentials
invalid:
  # Existing cases...
+  - |
+    require 'octokit'
+    token = <<~EOF
+      abcdef1234567890
+    EOF
+    Octokit::Client.new(access_token: token)
+  - |
+    require 'octokit'
+    Octokit::Client.new(access_token: Base64.encode64('secret'))
tests/__snapshots__/ruby-octokit-hardcoded-secret-ruby-snapshot.yml (1)

7-8: Enhance style labels for better clarity.

Consider using more descriptive style labels to indicate the purpose of each section:

  • 'primary' → 'violation_pattern'
  • 'secondary' → 'context_info'
  : labels:
    - source: 'Octokit::Client.new(access_token: "<your 40 char token>", per_page: 100)'
-     style: primary
+     style: violation_pattern

Also applies to: 118-123

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 52b2182 and 87d0093.

📒 Files selected for processing (9)
  • rules/python/security/python-psycopg2-hardcoded-secret-python.yml (1 hunks)
  • rules/ruby/security/ruby-excon-hardcoded-secret-ruby.yml (1 hunks)
  • rules/ruby/security/ruby-octokit-hardcoded-secret-ruby.yml (1 hunks)
  • tests/__snapshots__/python-psycopg2-hardcoded-secret-python-snapshot.yml (1 hunks)
  • tests/__snapshots__/ruby-excon-hardcoded-secret-ruby-snapshot.yml (1 hunks)
  • tests/__snapshots__/ruby-octokit-hardcoded-secret-ruby-snapshot.yml (1 hunks)
  • tests/python/python-psycopg2-hardcoded-secret-python-test.yml (1 hunks)
  • tests/ruby/ruby-excon-hardcoded-secret-ruby-test.yml (1 hunks)
  • tests/ruby/ruby-octokit-hardcoded-secret-ruby-test.yml (1 hunks)
🧰 Additional context used
🪛 yamllint (1.35.1)
rules/ruby/security/ruby-excon-hardcoded-secret-ruby.yml

[warning] 17-17: wrong indentation: expected 2 but found 5

(indentation)


[warning] 24-24: wrong indentation: expected 8 but found 11

(indentation)


[warning] 27-27: wrong indentation: expected 8 but found 11

(indentation)


[error] 31-31: trailing spaces

(trailing-spaces)


[error] 39-39: trailing spaces

(trailing-spaces)


[warning] 42-42: wrong indentation: expected 12 but found 14

(indentation)


[warning] 45-45: wrong indentation: expected 15 but found 17

(indentation)


[warning] 48-48: wrong indentation: expected 8 but found 10

(indentation)


[warning] 51-51: wrong indentation: expected 11 but found 13

(indentation)


[warning] 56-56: wrong indentation: expected 2 but found 5

(indentation)


[warning] 63-63: wrong indentation: expected 8 but found 11

(indentation)


[warning] 66-66: wrong indentation: expected 8 but found 11

(indentation)


[error] 70-70: trailing spaces

(trailing-spaces)


[error] 78-78: trailing spaces

(trailing-spaces)


[warning] 81-81: wrong indentation: expected 12 but found 14

(indentation)


[warning] 88-88: wrong indentation: expected 9 but found 11

(indentation)


[warning] 91-91: wrong indentation: expected 11 but found 14

(indentation)


[warning] 92-92: wrong indentation: expected 17 but found 19

(indentation)


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

(indentation)


[warning] 99-99: wrong indentation: expected 20 but found 22

(indentation)


[warning] 102-102: wrong indentation: expected 8 but found 10

(indentation)


[warning] 105-105: wrong indentation: expected 11 but found 13

(indentation)


[warning] 110-110: wrong indentation: expected 2 but found 3

(indentation)


[warning] 112-112: wrong indentation: expected 3 but found 5

(indentation)


[warning] 117-117: wrong indentation: expected 8 but found 11

(indentation)


[warning] 120-120: wrong indentation: expected 8 but found 11

(indentation)


[error] 124-124: trailing spaces

(trailing-spaces)


[error] 132-132: trailing spaces

(trailing-spaces)


[warning] 135-135: wrong indentation: expected 12 but found 14

(indentation)


[warning] 138-138: wrong indentation: expected 14 but found 17

(indentation)


[warning] 139-139: wrong indentation: expected 20 but found 22

(indentation)


[warning] 143-143: wrong indentation: expected 20 but found 22

(indentation)


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

(indentation)


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

(indentation)


[warning] 153-153: wrong indentation: expected 23 but found 25

(indentation)


[warning] 156-156: wrong indentation: expected 26 but found 28

(indentation)


[warning] 159-159: wrong indentation: expected 8 but found 10

(indentation)


[warning] 162-162: wrong indentation: expected 11 but found 13

(indentation)


[warning] 167-167: wrong indentation: expected 2 but found 3

(indentation)


[warning] 169-169: wrong indentation: expected 3 but found 5

(indentation)


[warning] 174-174: wrong indentation: expected 8 but found 11

(indentation)


[warning] 177-177: wrong indentation: expected 8 but found 11

(indentation)


[error] 181-181: trailing spaces

(trailing-spaces)


[error] 189-189: trailing spaces

(trailing-spaces)


[warning] 192-192: wrong indentation: expected 12 but found 14

(indentation)


[warning] 195-195: wrong indentation: expected 14 but found 17

(indentation)


[warning] 196-196: wrong indentation: expected 20 but found 22

(indentation)


[warning] 200-200: wrong indentation: expected 20 but found 22

(indentation)


[warning] 203-203: wrong indentation: expected 20 but found 22

(indentation)


[warning] 207-207: wrong indentation: expected 20 but found 22

(indentation)


[warning] 210-210: wrong indentation: expected 23 but found 25

(indentation)


[warning] 214-214: wrong indentation: expected 8 but found 10

(indentation)


[warning] 217-217: wrong indentation: expected 11 but found 13

(indentation)


[warning] 221-221: wrong indentation: expected 8 but found 10

(indentation)


[warning] 227-227: wrong indentation: expected 11 but found 14

(indentation)


[warning] 228-228: wrong indentation: expected 17 but found 19

(indentation)


[warning] 232-232: wrong indentation: expected 17 but found 19

(indentation)


[warning] 235-235: wrong indentation: expected 20 but found 22

(indentation)


[error] 237-237: trailing spaces

(trailing-spaces)


[warning] 239-239: wrong indentation: expected 1 but found 4

(indentation)


[warning] 241-241: wrong indentation: expected 4 but found 8

(indentation)

🔇 Additional comments (8)
tests/__snapshots__/python-psycopg2-hardcoded-secret-python-snapshot.yml (1)

59-87: LGTM! Label positions are accurate for the second test case.

The label positions and styles are well-defined for the direct string password case.

tests/ruby/ruby-excon-hardcoded-secret-ruby-test.yml (1)

7-16: LGTM! Comprehensive coverage of insecure patterns

The invalid test cases effectively cover various patterns of hardcoded secrets:

  1. Variable assignment of hardcoded password
  2. Usage of escape_uri with hardcoded password
  3. Direct usage of hardcoded password
tests/__snapshots__/ruby-excon-hardcoded-secret-ruby-snapshot.yml (1)

1-190: LGTM! Well-structured snapshots for AST matching

The snapshots provide comprehensive coverage of the code patterns with detailed labeling for accurate AST matching.

rules/ruby/security/ruby-excon-hardcoded-secret-ruby.yml (2)

1-14: LGTM! Clear security guidance and references

The message and note sections effectively communicate:

  • The risks of hardcoded secrets
  • Best practices for secure credential management
  • Relevant CWE and OWASP references

238-244: LGTM! Comprehensive pattern matching

The rule effectively captures various patterns of hardcoded secrets:

  1. Direct password usage
  2. Variable-based password usage
  3. Escaped URI password usage
🧰 Tools
🪛 yamllint (1.35.1)

[warning] 239-239: wrong indentation: expected 1 but found 4

(indentation)


[warning] 241-241: wrong indentation: expected 4 but found 8

(indentation)

rules/ruby/security/ruby-octokit-hardcoded-secret-ruby.yml (1)

38-39: Verify pattern coverage for all credential parameters.

The regex pattern correctly covers the main credential parameters. Let's verify if there are any other credential-related parameters in the Octokit client.

✅ Verification successful

Pattern coverage for credential parameters is comprehensive

Based on the test cases and rule implementation, the current regex pattern ^:password|:access_token|:client_secret|password|access_token|client_secret$ correctly covers all authentication methods used in Octokit::Client:

  • Basic authentication (:password)
  • OAuth access token (:access_token)
  • OAuth application credentials (:client_secret)

The test cases demonstrate various real-world usage patterns including both symbol and string forms of these parameters, multiline declarations, and different authentication methods.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for Octokit::Client initialization patterns in the codebase
# to ensure we haven't missed any credential-related parameters

# Look for Client.new method calls
ast-grep --pattern 'Octokit::Client.new($_)'

# Search for documentation or examples mentioning authentication
rg -i "auth|cred|token|secret" --type ruby

Length of output: 240


Script:

#!/bin/bash
# Let's search for any Ruby files in the repository first
fd -e rb

# Then let's look at the content of the rule file itself to understand the context better
cat rules/ruby/security/ruby-octokit-hardcoded-secret-ruby.yml

# Let's also search for any other YAML files that might contain similar patterns
fd -e yml -e yaml | xargs grep -l -i "octokit"

Length of output: 3296


Script:

#!/bin/bash
# Let's check the test file to see what patterns are being tested
cat tests/ruby/ruby-octokit-hardcoded-secret-ruby-test.yml

# And check the snapshot file for any additional context
cat tests/__snapshots__/ruby-octokit-hardcoded-secret-ruby-snapshot.yml

Length of output: 7298

tests/ruby/ruby-octokit-hardcoded-secret-ruby-test.yml (1)

2-5: LGTM: Valid test case demonstrates best practice.

The valid test case correctly demonstrates using a variable for the access token instead of a hard-coded value.

tests/__snapshots__/ruby-octokit-hardcoded-secret-ruby-snapshot.yml (1)

1-270: LGTM: Snapshot structure is comprehensive.

The snapshots provide detailed source mapping and properly capture all test cases.

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