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 #100

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

Summary by CodeRabbit

  • New Features

    • Introduced rules to detect hard-coded secrets in C# and Python applications, enhancing security practices.
    • Added a rule to identify empty passwords in PostgreSQL database connections using Peewee ORM.
  • Tests

    • Created new test configurations for validating hard-coded secrets and empty passwords in both C# and Python applications.
    • Added snapshot tests to verify various configurations related to hard-coded credentials and empty password 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 9, 2024

Walkthrough

This pull request introduces new security rules aimed at detecting hard-coded secrets in C# and Python applications. Specifically, it adds a rule for detecting hard-coded credentials in C# using the NetworkCredential class and two rules for Python applications utilizing the Peewee ORM with MySQL and PostgreSQL databases. Additionally, it includes corresponding test configurations and snapshots to validate the implementation of these rules, ensuring best practices in credential management and enhancing overall security.

Changes

File Path Change Summary
rules/csharp/security/networkcredential-hardcoded-secret-csharp.yml New rule added: networkcredential-hardcoded-secret-csharp to detect hard-coded secrets in C# applications.
rules/python/security/python-peewee-mysql-hardcoded-secret-python.yml New rule added: python-peewee-mysql-hardcoded-secret-python to detect hard-coded secrets in MySQL database connections.
rules/python/security/python-peewee-pg-empty-password-python.yml New rule added: python-peewee-pg-empty-password-python to detect empty passwords in PostgreSQL database connections.
tests/snapshots/networkcredential-hardcoded-secret-csharp-snapshot.yml New snapshot added for testing hard-coded secrets in C# with various password handling techniques.
tests/snapshots/python-peewee-mysql-hardcoded-secret-python-snapshot.yml New snapshot added for testing MySQL database connections with hard-coded secrets.
tests/snapshots/python-peewee-pg-empty-password-python-snapshot.yml New snapshot added for testing PostgreSQL database connections with empty passwords.
tests/csharp/networkcredential-hardcoded-secret-csharp-test.yml New test configuration added to validate correct and incorrect handling of hard-coded secrets in C#.
tests/python/python-peewee-mysql-hardcoded-secret-python-test.yml New test configuration added to validate correct and incorrect handling of hard-coded secrets in MySQL connections.
tests/python/python-peewee-pg-empty-password-python-test.yml New test configuration added to validate handling of empty passwords in PostgreSQL connections.

Possibly related PRs

Suggested reviewers

  • petrisorcoderabbit

Poem

🐇 In code where secrets hide away,
A rule now guards the light of day.
With C# and Python, we take a stand,
No hard-coded secrets shall be planned!
So let us code with care and glee,
For safety's path is the best decree! 🌟


🪧 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 9, 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: 3

🧹 Outside diff range and nitpick comments (7)
rules/python/security/python-peewee-mysql-hardcoded-secret-python.yml (1)

22-22: Consider adding more database classes

The regex pattern for database classes is comprehensive but could potentially include more classes from Peewee ecosystem.

Consider adding patterns for:

  • PostgresqlDatabase
  • SqliteDatabase
  • Other database-specific classes if they support password authentication
tests/python/python-peewee-pg-empty-password-python-test.yml (1)

26-26: Remove trailing whitespace

Remove the trailing whitespace at the end of the file.

🧰 Tools
🪛 yamllint (1.35.1)

[error] 26-26: trailing spaces

(trailing-spaces)

rules/csharp/security/networkcredential-hardcoded-secret-csharp.yml (4)

4-14: Enhance security context with additional references

Consider adding these valuable security references to strengthen the guidance:

  • NIST SP 800-53 Rev. 5 (IA-5)
  • CWE-259 (Use of Hard-coded Password)
  • Azure Key Vault or AWS Secrets Manager documentation for practical implementation

43-101: Simplify pattern matching for better maintainability

The current nested pattern matching is complex. Consider breaking it down into smaller, reusable patterns:

  1. Extract the Password property access pattern
  2. Extract the string literal validation pattern
  3. Create a shared utility for NetworkCredential type checking

101-233: Consider performance implications of complex pattern matching

The nested pattern matching for variable tracking could impact performance on large codebases. Consider:

  1. Implementing a more efficient variable tracking mechanism
  2. Adding early termination conditions
  3. Caching intermediate results

233-238: Consider pattern prioritization for efficiency

The current rule checks all patterns without prioritization. Consider ordering patterns by:

  1. Frequency of occurrence (most common patterns first)
  2. Computational complexity (simpler patterns first)
tests/__snapshots__/networkcredential-hardcoded-secret-csharp-snapshot.yml (1)

1-258: Improve snapshot organization for better maintainability

Consider organizing snapshots by:

  1. Grouping related test cases
  2. Adding descriptive comments for each scenario
  3. Reducing duplicate label definitions
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 2f95a8e and 36c597f.

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

[error] 7-7: trailing spaces

(trailing-spaces)


[error] 24-24: trailing spaces

(trailing-spaces)

rules/python/security/python-peewee-mysql-hardcoded-secret-python.yml

[warning] 30-30: wrong indentation: expected 10 but found 12

(indentation)


[warning] 55-55: wrong indentation: expected 10 but found 12

(indentation)


[error] 67-67: trailing spaces

(trailing-spaces)


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

(indentation)


[warning] 74-74: wrong indentation: expected 14 but found 16

(indentation)


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

(indentation)


[warning] 107-107: wrong indentation: expected 10 but found 12

(indentation)


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

(indentation)


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

(indentation)


[warning] 157-157: wrong indentation: expected 10 but found 12

(indentation)


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

(indentation)


[error] 186-186: trailing spaces

(trailing-spaces)


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

(indentation)


[warning] 193-193: wrong indentation: expected 14 but found 16

(indentation)


[warning] 206-206: wrong indentation: expected 2 but found 4

(indentation)

tests/python/python-peewee-pg-empty-password-python-test.yml

[error] 26-26: trailing spaces

(trailing-spaces)

rules/python/security/python-peewee-pg-empty-password-python.yml

[warning] 31-31: wrong indentation: expected 10 but found 12

(indentation)


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

(indentation)


[warning] 57-57: wrong indentation: expected 10 but found 12

(indentation)


[error] 69-69: trailing spaces

(trailing-spaces)


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

(indentation)


[warning] 76-76: wrong indentation: expected 14 but found 16

(indentation)


[warning] 85-85: wrong indentation: expected 24 but found 23

(indentation)


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

(indentation)


[warning] 110-110: wrong indentation: expected 10 but found 12

(indentation)


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

(indentation)


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

(indentation)


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

(indentation)


[warning] 161-161: wrong indentation: expected 10 but found 12

(indentation)


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

(indentation)


[error] 190-190: trailing spaces

(trailing-spaces)


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

(indentation)


[warning] 197-197: wrong indentation: expected 14 but found 16

(indentation)


[warning] 206-206: wrong indentation: expected 24 but found 23

(indentation)


[warning] 211-211: wrong indentation: expected 2 but found 4

(indentation)

🔇 Additional comments (10)
tests/python/python-peewee-mysql-hardcoded-secret-python-test.yml (1)

6-22: LGTM: Comprehensive test coverage for invalid cases

The test cases effectively cover various scenarios of hard-coded secrets:

  • Direct string passwords
  • Variable-based passwords
  • Different database classes
  • Different parameter names (password/passwd)
🧰 Tools
🪛 yamllint (1.35.1)

[error] 7-7: trailing spaces

(trailing-spaces)

rules/python/security/python-peewee-mysql-hardcoded-secret-python.yml (2)

4-13: LGTM: Well-documented security rule

The rule includes:

  • Clear description of the security risk
  • Reference to CWE-798
  • Link to OWASP Secrets Management guidelines

14-202: LGTM: Comprehensive pattern matching

The rule effectively captures:

  • Direct password strings in constructor
  • Variable-based passwords
  • Different initialization methods
  • Both 'password' and 'passwd' parameter names
🧰 Tools
🪛 yamllint (1.35.1)

[warning] 30-30: wrong indentation: expected 10 but found 12

(indentation)


[warning] 55-55: wrong indentation: expected 10 but found 12

(indentation)


[error] 67-67: trailing spaces

(trailing-spaces)


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

(indentation)


[warning] 74-74: wrong indentation: expected 14 but found 16

(indentation)


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

(indentation)


[warning] 107-107: wrong indentation: expected 10 but found 12

(indentation)


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

(indentation)


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

(indentation)


[warning] 157-157: wrong indentation: expected 10 but found 12

(indentation)


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

(indentation)


[error] 186-186: trailing spaces

(trailing-spaces)


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

(indentation)


[warning] 193-193: wrong indentation: expected 14 but found 16

(indentation)

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

1-308: LGTM: Well-structured test snapshots

The snapshots provide:

  • Accurate source mapping for each code pattern
  • Coverage of different initialization methods
  • Proper labeling of code segments
tests/python/python-peewee-pg-empty-password-python-test.yml (1)

7-24: LGTM! Comprehensive test coverage for empty password scenarios

The test cases effectively cover various scenarios of empty password usage:

  • Direct empty string password
  • Variable-based empty password
  • Empty password in database initialization
  • Empty password across different database classes (PostgreSQL, Pooled, CockroachDB)
rules/python/security/python-peewee-pg-empty-password-python.yml (3)

4-14: LGTM! Clear and informative security message

The message effectively communicates:

  • The security risk of empty passwords
  • The potential impact (unauthorized access)
  • Recommended solutions (environment variables, secure vault, HSM)
  • Relevant security references (OWASP, CWE-287)

15-207: LGTM! Comprehensive pattern matching for empty passwords

The utility patterns effectively cover all scenarios:

  • Direct constructor calls with empty passwords
  • Variable-based empty passwords
  • Database initialization with empty passwords
  • Support for all relevant database classes (PostgreSQL, CockroachDB, pooled variants)
🧰 Tools
🪛 yamllint (1.35.1)

[warning] 31-31: wrong indentation: expected 10 but found 12

(indentation)


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

(indentation)


[warning] 57-57: wrong indentation: expected 10 but found 12

(indentation)


[error] 69-69: trailing spaces

(trailing-spaces)


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

(indentation)


[warning] 76-76: wrong indentation: expected 14 but found 16

(indentation)


[warning] 85-85: wrong indentation: expected 24 but found 23

(indentation)


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

(indentation)


[warning] 110-110: wrong indentation: expected 10 but found 12

(indentation)


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

(indentation)


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

(indentation)


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

(indentation)


[warning] 161-161: wrong indentation: expected 10 but found 12

(indentation)


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

(indentation)


[error] 190-190: trailing spaces

(trailing-spaces)


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

(indentation)


[warning] 197-197: wrong indentation: expected 14 but found 16

(indentation)


[warning] 206-206: wrong indentation: expected 24 but found 23

(indentation)


208-214: LGTM! Well-structured rule definition

The rule effectively combines all utility patterns to catch empty passwords in various scenarios.

🧰 Tools
🪛 yamllint (1.35.1)

[warning] 211-211: wrong indentation: expected 2 but found 4

(indentation)

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

1-312: LGTM! Well-structured test snapshots

The snapshots:

  • Accurately capture all test scenarios
  • Provide detailed source mapping for each component
  • Use appropriate label categorization (primary/secondary)
  • Match the test cases in the configuration file
rules/csharp/security/networkcredential-hardcoded-secret-csharp.yml (1)

16-42: 🛠️ Refactor suggestion

Consider additional string patterns for comprehensive detection

The current pattern might miss hardcoded secrets in these scenarios:

  • String concatenation: new NetworkCredential("user", "pass" + "word")
  • String interpolation: new NetworkCredential("user", $"pass{word}")

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