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 detecting hard-coded secrets in C# and Python #98

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

    • Added security rules for detecting hard-coded secrets in C# and Python applications, specifically targeting OracleConnectionStringBuilder and pymssql libraries.
    • Introduced validation tests for secure password handling in database connections using pymssql and pymysql.
  • Bug Fixes

    • Enhanced security mechanisms to prevent the use of hard-coded passwords and empty password scenarios in database connections.
  • Tests

    • Added comprehensive test cases to ensure secure practices in password management for both C# and Python 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 7, 2024

Walkthrough

This pull request introduces multiple security rules across C# and Python applications aimed at detecting hard-coded secrets. Specifically, it adds rules for the OracleConnectionStringBuilder in C#, and for the pymssql and pymysql libraries in Python, focusing on identifying hard-coded credentials and empty passwords. Each rule is defined in a YAML configuration file, includes matching patterns for code analysis, and references security guidelines. Additionally, corresponding test cases and snapshot files are created to validate the implementation of these rules.

Changes

File Path Change Summary
rules/csharp/security/oracleconnectionstringbuilder-hardcoded-secret-csharp.yml New rule added: oracleconnectionstringbuilder-hardcoded-secret-csharp for detecting hard-coded secrets in C#.
rules/python/security/python-pymssql-hardcoded-secret-python.yml New rule added: python-pymssql-hardcoded-secret-python for detecting hard-coded secrets in pymssql.
rules/python/security/python-pymysql-empty-password-python.yml New rule added: python-pymysql-empty-password-python for detecting empty passwords in pymysql.
tests/__snapshots__/oracleconnectionstringbuilder-hardcoded-secret-csharp-snapshot.yml New snapshot for testing OracleConnectionStringBuilder password assignment logic.
tests/__snapshots__/python-pymssql-hardcoded-secret-python-snapshot.yml New snapshot for testing pymssql connection examples with hard-coded credentials.
tests/__snapshots__/python-pymysql-empty-password-python-snapshot.yml New snapshot for testing pymysql.connect with empty password scenarios.
tests/csharp/oracleconnectionstringbuilder-hardcoded-secret-csharp-test.yml New test case added for validating password assignment in OracleConnectionStringBuilder.
tests/python/python-pymssql-hardcoded-secret-python-test.yml New test cases added for validating secure password handling in pymssql connections.
tests/python/python-pymysql-empty-password-python-test.yml New test cases added for validating handling of empty passwords in pymysql.connect.

Possibly related PRs

Suggested reviewers

  • petrisorcoderabbit
  • harjotgill

Poem

In the code where secrets hide,
A rabbit hops with eyes open wide.
With rules to guard, both C# and Python,
No more hard-coded secrets to rely on!
So let’s code safe, with joy and cheer,
For a secure future is drawing near! 🐇✨


🪧 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 detecting hard-coded secrets in C# and Python 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: 4

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

24-53: Consider adding more test cases for comprehensive coverage.

While the current test cases cover basic scenarios, consider adding tests for:

  1. Empty passwords
  2. Connection string URLs
  3. F-string interpolated passwords
  4. Password variables from config files
🧰 Tools
🪛 yamllint (1.35.1)

[error] 47-47: trailing spaces

(trailing-spaces)


47-47: Remove trailing whitespace.

Fix the trailing whitespace in line 47.

-    pswd2 = get_from_config()  
+    pswd2 = get_from_config()
🧰 Tools
🪛 yamllint (1.35.1)

[error] 47-47: trailing spaces

(trailing-spaces)

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

15-236: Consider enhancing pattern matching for edge cases.

The current patterns could miss some security issues. Consider adding patterns for:

  1. Empty passwords
  2. Connection string URLs
  3. F-string interpolated passwords

Example pattern for empty passwords:

                  - has:
                      stopBy: end
                      kind: string
                      field: value
                      all:
+                       - not:
+                           has:
+                               stopBy: end
+                               kind: string_content
                        - has:
                            stopBy: end
                            kind: string_start
rules/python/security/python-pymysql-empty-password-python.yml (1)

17-66: Enhance pattern matching for additional empty password variants

The current pattern only matches literal empty strings (""). Consider extending it to catch:

  1. Single quotes ('')
  2. Triple quotes ("""""" or '''''')
  3. String concatenation ('' + '')

Would you like me to provide the extended pattern matching implementation?

tests/python/python-pymysql-empty-password-python-test.yml (1)

2-6: Expand valid test cases for better coverage

Consider adding these valid scenarios:

  1. Password from secure vault/HSM
  2. Password from encrypted configuration
  3. Password from AWS Secrets Manager or similar services

Would you like me to provide the additional test cases?

tests/csharp/oracleconnectionstringbuilder-hardcoded-secret-csharp-test.yml (2)

3-4: Consider enhancing the valid test cases.

While using command-line arguments is better than hard-coding, consider adding more secure examples such as:

  • Environment variables
  • Azure Key Vault or other secure secret management solutions
  • Configuration providers with encrypted values

6-29: Enhance test coverage with additional scenarios.

The invalid test cases could be expanded to include:

  • Empty passwords
  • Connection strings with embedded credentials
  • Passwords in string interpolation
  • Passwords in constants or readonly fields

Additionally, consider using different password values across test cases to ensure the rule isn't just matching the specific string "reee!".

tests/__snapshots__/oracleconnectionstringbuilder-hardcoded-secret-csharp-snapshot.yml (1)

1-262: Consider adding metadata comments to snapshots.

While the snapshots are technically correct, adding comments to describe:

  • The security pattern being tested
  • Expected detection behavior
  • Relationship to CWE/OWASP guidelines

Would improve maintainability and help other developers understand the test cases.

rules/csharp/security/oracleconnectionstringbuilder-hardcoded-secret-csharp.yml (1)

4-14: Enhance security guidance in the message.

The message provides good basic guidance but could be improved by:

  • Adding specific examples of secure alternatives
  • Mentioning the risks of source control exposure
  • Referencing additional security resources
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 2f95a8e and 4d92264.

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

[error] 47-47: trailing spaces

(trailing-spaces)

🔇 Additional comments (6)
tests/python/python-pymssql-hardcoded-secret-python-test.yml (1)

2-23: LGTM! Valid test cases demonstrate secure password handling practices.

The test cases correctly validate three secure approaches:

  1. Using variable references
  2. Using environment variables
  3. Using getenv calls
tests/__snapshots__/python-pymssql-hardcoded-secret-python-snapshot.yml (1)

1-293: LGTM! Snapshot correctly documents the test cases.

The snapshot file properly captures:

  1. AST node positions
  2. Source code segments
  3. Style information for pattern matching
rules/python/security/python-pymssql-hardcoded-secret-python.yml (2)

1-14: LGTM! Clear documentation with appropriate security references.

The rule includes:

  1. Clear warning message
  2. Relevant CWE-798 reference
  3. OWASP A07:2021 reference
  4. Link to OWASP Secrets Management Cheat Sheet

237-241: LGTM! Rule composition correctly combines all patterns.

The rule properly combines all three patterns using the any operator.

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

1-114: Snapshot file is well-structured but will need updates

The snapshot correctly captures the current test cases. However, it will need to be regenerated after implementing the suggested improvements to the rule and test cases.

rules/csharp/security/oracleconnectionstringbuilder-hardcoded-secret-csharp.yml (1)

242-247: Consider adding pattern validation.

The rule could benefit from additional validation to:

  • Ensure patterns don't conflict
  • Verify no false positives with similar property names
  • Check for performance impact of complex patterns

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