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 YAML rules for detecting hard-coded and empty passwords in Java and Python #95

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 rules to identify hard-coded connection passwords in Java and Python applications, enhancing security practices.
    • Added validation tests to ensure proper handling of connection passwords, including valid and invalid scenarios.
  • Bug Fixes

    • Updated test cases to reflect changes in password handling, ensuring accurate detection of security vulnerabilities.
  • Documentation

    • Included references to best practices and resources for managing sensitive information securely.

Copy link

coderabbitai bot commented Dec 7, 2024

Walkthrough

This pull request introduces several new YAML rule files aimed at enhancing security in Java and Python applications by detecting hard-coded connection passwords and empty passwords in Redis connections. The new rules include specific patterns and constraints to identify instances where sensitive credentials may be improperly handled. Additionally, new test configuration files are created to validate correct and incorrect usages of these rules, ensuring adherence to secure coding practices.

Changes

File Path Change Summary
rules/java/security/datanucleus-hardcoded-connection-password-java.yml Added rule for detecting hard-coded connection passwords in Java applications using DataNucleus.
rules/java/security/hardcoded-connection-password-java.yml Introduced rule for identifying hard-coded connection passwords in Java applications.
rules/python/security/python-redis-empty-password-python.yml New rule for detecting empty passwords in Redis connections in Python applications.
tests/__snapshots__/datanucleus-hardcoded-connection-password-java-snapshot.yml Added snapshot for testing hard-coded connection passwords in DataNucleus.
tests/__snapshots__/hardcoded-connection-password-java-snapshot.yml Introduced snapshots for testing the PeopleTest class regarding connection passwords.
tests/__snapshots__/python-redis-empty-password-python-snapshot.yml New snapshot for testing Redis client connections with empty password scenarios.
tests/java/datanucleus-hardcoded-connection-password-java-test.yml Created test configuration for validating hard-coded connection passwords in DataNucleus.
tests/java/hardcoded-connection-password-java-test.yml New test configuration for validating connection passwords in the PeopleTest class.
tests/python/python-redis-empty-password-python-test.yml Introduced test configuration for validating Redis connections with empty password scenarios.

Possibly related PRs

  • Two Java rules 10Oct2024 #16: The rule jedis-jedisfactory-hardcoded-password-java is related as it also addresses hard-coded passwords in Java applications, similar to the main PR's focus on hard-coded connection passwords.
  • Rules - One php and one java rule #20: The rule drivermanager-hardcoded-secret-java is relevant as it detects hard-coded secrets in Java applications, aligning with the main PR's emphasis on identifying hard-coded connection passwords.
  • Add security rules for Java and Swift applications for cookie and secret management #43: The rule cookie-missing-samesite-java is related as it deals with security practices in Java applications, although it focuses on cookie management rather than hard-coded passwords. However, both aim to enhance security in Java applications.

Suggested reviewers

  • petrisorcoderabbit

🐇 In the code, we find a way,
To keep our secrets safe each day.
With patterns clear and tests in sight,
No hard-coded passwords in the light!
So hop along, let's code with care,
For security is a treasure rare! 🌟


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

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

@coderabbitai coderabbitai bot changed the title @coderabbitai Add YAML rules for detecting hard-coded and empty passwords in Java 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 (7)
tests/python/python-redis-empty-password-python-test.yml (1)

14-17: Consider using placeholder hostnames in test cases

The test cases contain production-like hostnames and IP addresses. While these are test cases, it's best practice to use clearly fictional hostnames to prevent any accidental information disclosure.

-    host='prod.redis.example.com',
+    host='test-redis.internal',
-    host='10.0.0.1',
+    host='test-host-1',
-    host='1.2.3.4',
+    host='test-host-2',

Also applies to: 21-24, 27-27

tests/java/hardcoded-connection-password-java-test.yml (1)

4-34: Consolidate duplicate test patterns

The invalid section contains multiple test cases that demonstrate the same anti-pattern of hardcoding passwords. Consider consolidating these into fewer, more distinct examples to improve maintainability.

Additionally, consider adding comments to explain why each pattern is invalid to help developers understand the security implications:

invalid:
  - |
    # Invalid: Password directly hardcoded in setConnectionPassword
    public class PeopleTest {
      private javax.jdo.PersistenceManagerFactory pmf;
      {
        pmf.setConnectionPassword("asdf");  # Direct hardcoding
      }
    }
  - |
    # Invalid: Password hardcoded in a class field
    public class PeopleTest {
      private javax.jdo.PersistenceManagerFactory pmf;
      private String pw = "asdf";  # Still hardcoded, just moved to a field
      {
        pmf.setConnectionPassword(pw);
      }
    }
tests/java/datanucleus-hardcoded-connection-password-java-test.yml (1)

10-25: Add test cases for additional DataNucleus configuration patterns

The test cases should cover more DataNucleus-specific patterns:

  • Properties file configuration
  • JNDI datasource configuration
  • Connection URL with credentials

Example additional test case:

invalid:
  - |
    # Invalid: Hardcoded password in properties
    public class test {
      public void setUp() throws SQLException {
        Properties props = new Properties();
        props.setProperty("datanucleus.ConnectionPassword", "password");
        JDOPersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(props);
      }
    }
tests/__snapshots__/hardcoded-connection-password-java-snapshot.yml (1)

1-312: Enhance test coverage with additional edge cases.

The test snapshots provide good coverage of basic scenarios but could be enhanced with additional test cases:

  1. Empty string password
  2. Null value handling
  3. Password from environment variables
  4. Password from configuration files
  5. Password from secure vaults

Would you like me to provide additional test cases for these scenarios?

rules/java/security/hardcoded-connection-password-java.yml (2)

15-354: Document pattern differences and fix formatting issues.

The patterns are comprehensive but could benefit from:

  1. Inline documentation explaining the differences between patterns
  2. Examples of what each pattern matches
  3. Consistent indentation (currently varies between 12 and 16 spaces)

Apply this diff to fix formatting issues:

# Fix indentation in method_invocation blocks (examples)
-                stopBy: neighbor
+            stopBy: neighbor
# Remove trailing spaces
-    - matches: PATTERN_1 
+    - matches: PATTERN_1
🧰 Tools
🪛 yamllint (1.35.1)

[warning] 25-25: wrong indentation: expected 12 but found 16

(indentation)


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

(indentation)


[warning] 142-142: wrong indentation: expected 12 but found 16

(indentation)


[warning] 191-191: wrong indentation: expected 12 but found 16

(indentation)


[error] 230-230: trailing spaces

(trailing-spaces)


[warning] 255-255: wrong indentation: expected 12 but found 16

(indentation)


[warning] 297-297: wrong indentation: expected 12 but found 16

(indentation)


[error] 348-348: trailing spaces

(trailing-spaces)


355-360: Consider additional constraints for password validation.

The current constraints only check for non-empty strings. Consider adding:

  1. Minimum password length check
  2. Pattern matching for common hardcoded passwords
  3. Detection of base64 encoded passwords

Would you like me to provide examples of these additional constraints?

rules/java/security/datanucleus-hardcoded-connection-password-java.yml (1)

1-576: Consider refactoring to reduce duplication with base rule.

The DataNucleus-specific rule largely duplicates the base rule structure. Consider:

  1. Creating a base template that both rules can extend
  2. Extracting common patterns into shared utilities
  3. Only defining DataNucleus-specific patterns here

Example structure:

# base-connection-password.yml
id: base-connection-password
severity: warning
language: java
message: ${message}
utils:
  common_patterns:
    # ... shared patterns

# datanucleus-rule.yml
extends: base-connection-password
id: datanucleus-hardcoded-connection-password-java
utils:
  # Only DataNucleus-specific patterns

Would you like me to provide a detailed refactoring plan?

🧰 Tools
🪛 yamllint (1.35.1)

[warning] 24-24: wrong indentation: expected 12 but found 16

(indentation)


[warning] 72-72: wrong indentation: expected 12 but found 16

(indentation)


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

(indentation)


[warning] 183-183: wrong indentation: expected 12 but found 16

(indentation)


[error] 222-222: trailing spaces

(trailing-spaces)


[warning] 246-246: wrong indentation: expected 12 but found 16

(indentation)


[warning] 287-287: wrong indentation: expected 12 but found 16

(indentation)


[warning] 343-343: wrong indentation: expected 12 but found 16

(indentation)


[warning] 391-391: wrong indentation: expected 12 but found 16

(indentation)


[warning] 454-454: wrong indentation: expected 12 but found 16

(indentation)


[warning] 502-502: wrong indentation: expected 12 but found 16

(indentation)


[error] 560-560: trailing spaces

(trailing-spaces)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 2f95a8e and 7a9443b.

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

[warning] 25-25: wrong indentation: expected 12 but found 16

(indentation)


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

(indentation)


[warning] 142-142: wrong indentation: expected 12 but found 16

(indentation)


[warning] 191-191: wrong indentation: expected 12 but found 16

(indentation)


[error] 230-230: trailing spaces

(trailing-spaces)


[warning] 255-255: wrong indentation: expected 12 but found 16

(indentation)


[warning] 297-297: wrong indentation: expected 12 but found 16

(indentation)


[error] 348-348: trailing spaces

(trailing-spaces)

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

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

(indentation)


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

(indentation)


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

(indentation)


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

(indentation)


[error] 83-83: trailing spaces

(trailing-spaces)


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

(indentation)


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

(indentation)


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

(indentation)


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

(indentation)

rules/java/security/datanucleus-hardcoded-connection-password-java.yml

[warning] 24-24: wrong indentation: expected 12 but found 16

(indentation)


[warning] 72-72: wrong indentation: expected 12 but found 16

(indentation)


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

(indentation)


[warning] 183-183: wrong indentation: expected 12 but found 16

(indentation)


[error] 222-222: trailing spaces

(trailing-spaces)


[warning] 246-246: wrong indentation: expected 12 but found 16

(indentation)


[warning] 287-287: wrong indentation: expected 12 but found 16

(indentation)


[warning] 343-343: wrong indentation: expected 12 but found 16

(indentation)


[warning] 391-391: wrong indentation: expected 12 but found 16

(indentation)


[warning] 454-454: wrong indentation: expected 12 but found 16

(indentation)


[warning] 502-502: wrong indentation: expected 12 but found 16

(indentation)


[error] 560-560: trailing spaces

(trailing-spaces)

🔇 Additional comments (4)
tests/python/python-redis-empty-password-python-test.yml (1)

4-9: LGTM! Well-structured test cases

The valid test case properly demonstrates secure usage by retrieving the password from an environment variable, while providing a comprehensive set of invalid cases to catch various empty password scenarios.

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

1-241: LGTM! Well-structured snapshot configuration

The snapshot configuration:

  • Accurately captures all test cases
  • Properly labels primary and secondary components
  • Maintains consistent style annotations
  • Provides precise source locations for each component
tests/__snapshots__/datanucleus-hardcoded-connection-password-java-snapshot.yml (1)

1-169: Verify snapshot coverage matches test cases

The snapshots correctly capture the AST patterns for hardcoded passwords, but consider:

  1. Adding snapshots for the valid patterns to ensure they don't trigger false positives
  2. Including snapshots for additional DataNucleus configuration patterns
✅ Verification successful

Snapshots correctly match test cases and provide comprehensive coverage

The snapshot file accurately reflects all test cases from the test file:

  • Contains snapshots for both invalid cases (hardcoded string password and string variable with hardcoded value)
  • Labels are properly positioned for highlighting the relevant code segments
  • Test file includes a valid case for comparison, ensuring no false positives
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Verify that all test cases have corresponding snapshots
diff <(grep -r "^  - |$" tests/java/datanucleus-hardcoded-connection-password-java-test.yml | wc -l) <(grep -r "^  ? |$" tests/__snapshots__/datanucleus-hardcoded-connection-password-java-snapshot.yml | wc -l)

Length of output: 323


Script:

#!/bin/bash
# Let's first check if the test file exists and examine its content
fd -e yml datanucleus-hardcoded-connection-password-java-test

# Then let's look at the content of both files to verify the test cases and snapshots
echo "=== Test file content ==="
cat tests/java/datanucleus-hardcoded-connection-password-java-test.yml || true
echo -e "\n=== Snapshot file content ==="
cat tests/__snapshots__/datanucleus-hardcoded-connection-password-java-snapshot.yml || true

Length of output: 5870

rules/java/security/hardcoded-connection-password-java.yml (1)

1-14: LGTM! Well-documented security rule with proper references.

The rule header is well-structured with:

  • Clear severity level
  • Comprehensive security message
  • Relevant references to OWASP and JDO documentation

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