Skip to content

Add YAML-based AST security rules and tests for C#, Java, Ruby #186

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

Merged

Conversation

ESS-ENN
Copy link
Collaborator

@ESS-ENN ESS-ENN commented Mar 26, 2025

Summary by CodeRabbit

  • New Features

    • Introduced enhanced security validations that issue warnings when sensitive error details might be exposed and when outdated or insecure SSL/TLS configurations are used. Additionally, checks now detect hardcoded secrets in Ruby.
  • Tests

    • Expanded test coverage to ensure the new security measures operate as expected across multiple languages and environments.

Sakshis and others added 30 commits December 16, 2024 13:09
@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ ESS-ENN
❌ Sakshis


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 Mar 26, 2025

Walkthrough

This pull request introduces new security rules for C#, Java, and Ruby by adding YAML configuration files that use AST pattern matching to detect insecure practices. The rules flag improper usage of methods that expose sensitive data, such as UseDeveloperExceptionPage in C#, SSLContext.getInstance in Java with weak protocols, and hardcoded RSA passphrases in Ruby. In addition, corresponding snapshot and test files have been added to validate both valid and invalid scenarios across these languages.

Changes

File(s) Change Summary
rules/.../stacktrace-disclosure-csharp.yml
rules/.../weak-ssl-context-java.yml
rules/.../hardcoded-secret-rsa-passphrase-ruby.yml
Added new security rules for C# (detecting misuse of UseDeveloperExceptionPage), Java (flagging weak SSL/TLS protocols), and Ruby (identifying hardcoded RSA passphrases).
tests/__snapshots__/stacktrace-disclosure-csharp-snapshot.yml
tests/__snapshots__/weak-ssl-context-java-snapshot.yml
tests/__snapshots__/hardcoded-secret-rsa-passphrase-ruby-snapshot.yml
Introduced snapshot files containing code examples and labels to validate the new security rules across the three languages.
tests/csharp/stacktrace-disclosure-csharp-test.yml
tests/java/weak-ssl-context-java-test.yml
tests/ruby/hardcoded-secret-rsa-passphrase-ruby-test.yml
Added test configurations to check both correct and incorrect implementations, ensuring that each security rule properly flags insecure practices.

Sequence Diagram(s)

sequenceDiagram
    participant CSharp as C# Code
    participant Analyzer as AST Analyzer
    participant Rule as Stacktrace Disclosure Rule
    CSharp->>Analyzer: Submit source code
    Analyzer->>Rule: Match UseDeveloperExceptionPage usage
    Rule-->>Analyzer: Validate environment context
    Analyzer->>CSharp: Emit warning if misused
Loading
sequenceDiagram
    participant Java as Java Code
    participant Analyzer as AST Analyzer
    participant Rule as Weak SSL Context Rule
    Java->>Analyzer: Submit source code
    Analyzer->>Rule: Identify SSLContext.getInstance call
    Rule-->>Analyzer: Check protocol arguments
    Analyzer->>Java: Flag insecure protocol usage
Loading
sequenceDiagram
    participant Ruby as Ruby Code
    participant Analyzer as AST Analyzer
    participant Rule as RSA Passphrase Rule
    Ruby->>Analyzer: Submit source code
    Analyzer->>Rule: Detect hardcoded passphrase in RSA calls
    Rule-->>Analyzer: Validate secret handling
    Analyzer->>Ruby: Emit warning for hardcoding issues
Loading

Possibly related PRs

Suggested reviewers

  • ganeshpatro321

Poem

In fields of code, I hop with glee,
New rules arise like blossoms on a tree.
C#, Java, Ruby now safeguarded tight,
Snapshots and tests ensure each line is right.
With every check, security sings in delight,
A joyful rabbit hops forward into the night! 🐇💻


🪧 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.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @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 YAML-based AST security rules and tests for C#, Java, Ruby Mar 26, 2025
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: 0

🧹 Nitpick comments (13)
tests/java/weak-ssl-context-java-test.yml (1)

1-9: Clear Separation of Valid Scenarios
The valid section clearly differentiates secure SSL context instantiation examples by demonstrating protocols "TLSv1.2", "TLSv1.3", and a dynamic use via getSslContext(). This explicit separation helps ensure that only properly secured contexts are accepted.

rules/java/security/weak-ssl-context-java.yml (2)

16-35: Remove Unnecessary Commented Code
The commented-out rule configuration (lines 16–35) appears redundant given the active rule block below. Removing it (or moving it to a developer note) could help keep the file concise and easier to maintain.


36-80: AST Rule Configuration Complexity
The AST rule block is quite intricate and appears intended to flag insecure invocations of SSLContext.getInstance—by allowing only "TLSv1.2" and "TLSv1.3". Please verify that the use of nthChild: 3 for the argument list aligns with your AST’s node structure. Adding inline comments to explain the nested conditional logic (especially in lines 57–72) would benefit future maintainers in understanding the rationale behind these constraints.

tests/__snapshots__/weak-ssl-context-java-snapshot.yml (1)

1-158: Comprehensive Snapshot Documentation
This snapshot file meticulously captures various insecure SSLContext instantiations (using "SSL", "SSLv3", "TLS", "TLSv1", and "TLSv1.1") along with detailed labeling of each component (primary and secondary). Such granularity is highly useful for validating the AST rule’s performance. Please confirm that the repeated label entries (e.g. duplicate protocol labels) are intentional and are produced consistently by the AST tool; if not, consider streamlining these to ease future maintenance.

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

15-31: Insecure Examples Clearly Illustrate Hardcoded Secrets.
The “invalid” section intentionally includes hardcoded values (e.g. $pass = 'super secret', @pass1 = 'my secure pass phrase goes here', and the literal "secret" in the RSA instantiation) to trigger the detection rule. Consider adding inline comments or annotations within this block to document each vulnerability for clarity during testing.

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

18-42: Detailed Label Definitions for AST Matching.
The labels provide a granular breakdown of the code segments and their offsets. Please verify that the start and end positions accurately reflect the intended AST node boundaries. Additionally, if possible, consider consolidating overly granular labels to reduce redundancy.

rules/ruby/security/hardcoded-secret-rsa-passphrase-ruby.yml (4)

1-15: Metadata and Rule Configuration Look Correct.
The rule metadata is properly set up with an appropriate severity level and clear messaging. One minor nitpick: update “an hardcoded” to “a hardcoded” for grammatical accuracy.


44-74: Refine Regex in Call Chain Matching.
This segment for detecting OpenSSL::PKey::RSA.new(...).to_pem(..., '...') is well structured. However, the regex pattern ^to_pem|export$ (line 57) could be more precise if rewritten as ^(to_pem|export)$ to avoid any unintended matches.


106-137: Consistent Matching for Extended Method Chains.
The OpenSSL::PKey::RSA.new(...).to_pem(..., '...')_with_instance segment mirrors earlier patterns with an additional instance check. As with the previous block, consider updating the regex (line 117) to ^(to_pem|export)$ for clarity and accuracy.


171-204: Thorough Matching for $OPENSSL.to_pem Usage.
The segment captures method calls on $OPENSSL with a hardcoded secret ($ASSIGN) and validates corresponding assignments within the class. Similar to earlier notes, consider refining the regex for method identifiers for enhanced precision.

tests/__snapshots__/stacktrace-disclosure-csharp-snapshot.yml (1)

6-6: Trailing Whitespace Issue
YAMLlint has flagged trailing spaces on several lines (lines 6, 10, 12, 20, 22, 25, 30, 32, 35, and 37). Please remove these extraneous spaces to ensure a clean and consistent YAML format.

Also applies to: 10-10, 12-12, 20-20, 22-22, 25-25, 30-30, 32-32, 35-35, 37-37

tests/csharp/stacktrace-disclosure-csharp-test.yml (1)

6-6: Trailing Whitespace Issue
YAMLlint reports trailing spaces on several lines (lines 6, 10, 12, 20, 22, 25, 30, 32, 35, and 37). Please remove these trailing spaces to maintain consistency and prevent potential formatting issues.

Also applies to: 10-10, 12-12, 20-20, 22-22, 25-25, 30-30, 32-32, 35-35, 37-37

🧰 Tools
🪛 YAMLlint (1.35.1)

[error] 6-6: trailing spaces

(trailing-spaces)

rules/csharp/security/stacktrace-disclosure-csharp.yml (1)

6-6: Trailing Whitespace Issue
YAMLlint has identified trailing spaces on multiple lines (lines 6, 10, 12, 20, 22, 25, 30, 32, 35, and 37). Removing these will help ensure the YAML remains clean and compliant with formatting standards.

Also applies to: 10-10, 12-12, 20-20, 22-22, 25-25, 30-30, 32-32, 35-35, 37-37

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 620d83c and c2a58ce.

📒 Files selected for processing (9)
  • rules/csharp/security/stacktrace-disclosure-csharp.yml (1 hunks)
  • rules/java/security/weak-ssl-context-java.yml (1 hunks)
  • rules/ruby/security/hardcoded-secret-rsa-passphrase-ruby.yml (1 hunks)
  • tests/__snapshots__/hardcoded-secret-rsa-passphrase-ruby-snapshot.yml (1 hunks)
  • tests/__snapshots__/stacktrace-disclosure-csharp-snapshot.yml (1 hunks)
  • tests/__snapshots__/weak-ssl-context-java-snapshot.yml (1 hunks)
  • tests/csharp/stacktrace-disclosure-csharp-test.yml (1 hunks)
  • tests/java/weak-ssl-context-java-test.yml (1 hunks)
  • tests/ruby/hardcoded-secret-rsa-passphrase-ruby-test.yml (1 hunks)
🧰 Additional context used
🪛 YAMLlint (1.35.1)
tests/csharp/stacktrace-disclosure-csharp-test.yml

[error] 6-6: trailing spaces

(trailing-spaces)


[error] 10-10: trailing spaces

(trailing-spaces)


[error] 12-12: trailing spaces

(trailing-spaces)


[error] 20-20: trailing spaces

(trailing-spaces)


[error] 22-22: trailing spaces

(trailing-spaces)


[error] 25-25: trailing spaces

(trailing-spaces)


[error] 30-30: trailing spaces

(trailing-spaces)


[error] 32-32: trailing spaces

(trailing-spaces)


[error] 35-35: trailing spaces

(trailing-spaces)


[error] 37-37: trailing spaces

(trailing-spaces)

🔇 Additional comments (11)
tests/java/weak-ssl-context-java-test.yml (1)

9-19: Comprehensive Insecure Protocol Testing
The invalid section effectively enumerates deprecated or insecure protocols—"SSL", "TLS", "TLSv1", "SSLv3", and "TLSv1.1"—thus covering all the anticipated insecure instantiation scenarios for SSLContext. This should provide robust testing of the rule’s negative matches.

rules/java/security/weak-ssl-context-java.yml (1)

1-15: Metadata and Documentation Clarity
The metadata (id, language, severity, message, note) is clear and informative. Including references to [CWE-326] and RFC links strengthens the guidance provided by the rule message.

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

1-14: Valid Examples Adequately Demonstrate Secure Practices.
The examples in the “valid” section correctly use environment variables (e.g. ENV['SECRET_PASSPHRASE'] and ENV['SECURE_KEY']) for RSA key operations rather than hardcoding secrets. This aligns well with the security objectives.

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

1-17: Snapshot Captures Insecure Pattern Effectively.
The snapshot block reproduces the insecure code (with hardcoded passphrases and key values) as expected. This provides a good reference for what the rule should flag.

rules/ruby/security/hardcoded-secret-rsa-passphrase-ruby.yml (4)

17-43: AST Matching for RSA Instantiation is Thorough.
The first utils block targeting OpenSSL::PKey::RSA.new(..., '...') leverages detailed AST conditions. The use of nthChild: 2 to capture the hardcoded string is appropriate. No changes are needed here.


75-105: Instance-Based Detection Block is Appropriately Detailed.
The block for OpenSSL::PKey::RSA.new(..., '...')_with_instance correctly extends the matching by considering an instance context (including checking for an assignment of $SECRET). This helps pinpoint hardcoded secrets embedded in class structures.


138-170: Detection of $OPENSSL.export Calls is Clear and Structured.
This block accurately targets calls to $OPENSSL.export(...,'...') and also includes an inner check for assignment patterns. The logic is sound and aligns with the detection strategy.


205-235: Aggregated Rule Matches are Comprehensive.
The final match_call and rule sections effectively aggregate all individual patterns into a single rule. This ensures that various insecure patterns are detected. The design is robust and the use of the “any” matcher makes the rule flexible.

tests/__snapshots__/stacktrace-disclosure-csharp-snapshot.yml (1)

1-43: Snapshot Content Verification
The snapshot file clearly lists multiple usage scenarios for app.UseDeveloperExceptionPage(), covering diverse conditional invocations (e.g. checking for non-development environments, specific days, and even within a method). This comprehensive coverage will help ensure that the new AST-based rule is thoroughly validated.

tests/csharp/stacktrace-disclosure-csharp-test.yml (1)

1-39: Test Scenarios Are Well-Covered
The test file defines both valid and invalid blocks appropriately. The valid case properly uses app.UseExceptionHandler("/Error") when env.IsDevelopment() is true, while the invalid cases cover multiple misconfigurations where app.UseDeveloperExceptionPage() is used inappropriately. This robust set of scenarios should effectively validate the new security rule.

🧰 Tools
🪛 YAMLlint (1.35.1)

[error] 6-6: trailing spaces

(trailing-spaces)


[error] 10-10: trailing spaces

(trailing-spaces)


[error] 12-12: trailing spaces

(trailing-spaces)


[error] 20-20: trailing spaces

(trailing-spaces)


[error] 22-22: trailing spaces

(trailing-spaces)


[error] 25-25: trailing spaces

(trailing-spaces)


[error] 30-30: trailing spaces

(trailing-spaces)


[error] 32-32: trailing spaces

(trailing-spaces)


[error] 35-35: trailing spaces

(trailing-spaces)


[error] 37-37: trailing spaces

(trailing-spaces)

rules/csharp/security/stacktrace-disclosure-csharp.yml (1)

1-46: Security Rule Configuration Looks Solid
The rule is well structured and documented. It clearly defines the severity, language, message, and even provides helpful references (CWE-209 and OWASP guidelines). The AST pattern matching under the utils section is logically grouped and should effectively detect invocations of UseDeveloperExceptionPage outside the permitted contexts.

@ganeshpatro321 ganeshpatro321 merged commit d6616fc into coderabbitai:main Mar 26, 2025
1 of 2 checks passed
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.

3 participants