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 SSL Configuration and Hard-Coded Secrets in Ruby #106

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

Summary by CodeRabbit

  • New Features

    • Introduced security rules to identify potential vulnerabilities related to SSL settings and hard-coded secrets in Ruby applications.
    • Added validation tests for SSL enforcement and authentication methods in Faraday and Redis.
  • Bug Fixes

    • Enhanced detection of hard-coded secrets in various authentication scenarios for Faraday and Redis.
  • Tests

    • Added new test cases and snapshots to validate the implementation of security rules and configurations.

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

Walkthrough

This pull request introduces new security rules for Ruby applications, focusing on two main areas: SSL configuration and hard-coded secrets. A rule is added to check if force_ssl is set to false, which could expose applications to security risks. Additionally, two new rules target hard-coded secrets in applications using the Faraday HTTP client and Redis, emphasizing the use of secure practices for managing sensitive information. Corresponding test cases and snapshots for each rule are also included to validate the configurations and identify potential vulnerabilities.

Changes

File Path Change Summary
rules/ruby/security/force-ssl-false-ruby.yml New rule force-ssl-false-ruby added to check config.force_ssl set to false.
rules/ruby/security/ruby-faraday-hardcoded-secret-ruby.yml New rule ruby-faraday-hardcoded-secret-ruby added to identify hard-coded secrets in Faraday usage.
rules/ruby/security/ruby-redis-hardcoded-secret-ruby.yml New rule ruby-redis-hardcoded-secret-ruby added to identify hard-coded secrets in Redis usage.
tests/__snapshots__/force-ssl-false-ruby-snapshot.yml New snapshot entry for force-ssl-false-ruby, including a method bad_ssl with config.force_ssl set to false.
tests/__snapshots__/ruby-faraday-hardcoded-secret-ruby-snapshot.yml New snapshot file for testing various authentication methods in Faraday.
tests/__snapshots__/ruby-redis-hardcoded-secret-ruby-snapshot.yml New snapshot file for testing Redis connection configurations with sensitive information.
tests/ruby/force-ssl-false-ruby-test.yml New test case for force-ssl-false-ruby with valid and invalid configurations for SSL settings.
tests/ruby/ruby-faraday-hardcoded-secret-ruby-test.yml New test cases for validating authentication methods in Faraday, categorized as valid and invalid.
tests/ruby/ruby-redis-hardcoded-secret-ruby-test.yml New test cases for validating Redis connection configurations, highlighting valid and invalid practices.

Possibly related PRs

Suggested reviewers

  • rohit121

Poem

In the land of Ruby, so bright and fair,
New rules for secrets, we now declare.
With SSL shining, no more to hide,
Secure your code, let safety abide.
Faraday and Redis, take heed, my friend,
For hard-coded secrets, we must amend! 🐇✨


🪧 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 Security Rules for SSL Configuration and Hard-Coded Secrets in Ruby Dec 11, 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)
rules/ruby/security/ruby-redis-hardcoded-secret-ruby.yml (1)

1-13: Consider enhancing the security message with specific recommendations

The message effectively warns about hard-coded secrets but could be more actionable by:

  1. Mentioning specific secure alternatives (e.g., Rails credentials, encrypted credentials)
  2. Adding links to Ruby-specific secure configuration guides
 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).
+  retrieve them from a secure vault or Hardware Security Module (HSM).
+  Consider using Rails.application.credentials, Rails.application.secrets,
+  or encrypted credentials in Rails applications.
 note: >-
   [CWE-798] Use of Hard-coded Credentials.
   [REFERENCES]
       - https://cheatsheetseries.owasp.org/cheatsheets/Secrets_Management_Cheat_Sheet.html
+      - https://guides.rubyonrails.org/security.html#custom-credentials
tests/ruby/ruby-redis-hardcoded-secret-ruby-test.yml (2)

2-4: Enhance test coverage with additional valid cases

Consider adding more valid test cases to demonstrate recommended approaches:

 valid:
   - |
      redis_ok1 = Redis.new(username: 'myname', password: ENV["PASS"])
+  - |
+      redis_ok2 = Redis.new(password: Rails.application.credentials.redis_password)
+  - |
+      redis_ok3 = Redis.new(password: Rails.application.secrets.redis_password)

5-15: Add edge cases to invalid test cases

Consider adding edge cases to ensure comprehensive coverage:

 invalid:
   - |
      require "redis"
      redis = Redis.new(password: "mysecret")
   - |
      require "redis"
      redis1 = Redis.new(username: 'myname', password: 'mysecret')
   - |
      require "redis"
      pass = 'password'
      redis1 = Redis.new(username: 'myname', password: pass)
+  - |
+      require "redis"
+      redis = Redis.new(password: '')  # Empty password
+  - |
+      require "redis"
+      redis = Redis.new(password: nil)  # Nil password
rules/ruby/security/force-ssl-false-ruby.yml (1)

13-13: Enhance pattern matching for better coverage

The current pattern might miss variations in SSL configuration. Consider expanding it to catch more cases:

-  pattern: config.force_ssl = false
+  patterns:
+    - pattern: config.force_ssl = false
+    - pattern: config.ssl_options = { :secure => false }
+    - pattern: config.ssl_options = { secure: false }
tests/ruby/force-ssl-false-ruby-test.yml (1)

1-11: Enhance test coverage with additional scenarios

While the basic test cases are good, consider adding more scenarios to improve coverage:

 id: force-ssl-false-ruby
 valid:
   - |
     def bad_ssl
     config.force_ssl = true
     end
+  - |
+    def ssl_options
+      config.ssl_options = { secure: true }
+    end
 invalid:
   - |
     def bad_ssl
     config.force_ssl = false
     end
+  - |
+    def multiple_settings
+      config.force_ssl = true
+      config.force_ssl = false  # Should catch even if set to true first
+    end
+  - |
+    def ssl_options
+      config.ssl_options = { secure: false }
+    end
rules/ruby/security/ruby-faraday-hardcoded-secret-ruby.yml (1)

120-122: Fix typo in pattern name comment

The comment doesn't match the pattern name. It should reflect that it's for a variable $PSD instead of a string literal.

-    # $X.request :authorization, $BEARER, "..."
+    # $X.request :authorization, $BEARER, $PSD
tests/ruby/ruby-faraday-hardcoded-secret-ruby-test.yml (1)

9-30: Consider adding test case for non-hardcoded token auth with options

The test suite would benefit from a valid case showing token auth with options but using an environment variable instead of a hardcoded token.

Add this test case to the valid section:

 valid:
+  - |
+     require "faraday"
+     token = ENV['API_TOKEN']
+     conn.request :token_auth, token, **options
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between d3972fa and 91fec18.

📒 Files selected for processing (9)
  • rules/ruby/security/force-ssl-false-ruby.yml (1 hunks)
  • rules/ruby/security/ruby-faraday-hardcoded-secret-ruby.yml (1 hunks)
  • rules/ruby/security/ruby-redis-hardcoded-secret-ruby.yml (1 hunks)
  • tests/__snapshots__/force-ssl-false-ruby-snapshot.yml (1 hunks)
  • tests/__snapshots__/ruby-faraday-hardcoded-secret-ruby-snapshot.yml (1 hunks)
  • tests/__snapshots__/ruby-redis-hardcoded-secret-ruby-snapshot.yml (1 hunks)
  • tests/ruby/force-ssl-false-ruby-test.yml (1 hunks)
  • tests/ruby/ruby-faraday-hardcoded-secret-ruby-test.yml (1 hunks)
  • tests/ruby/ruby-redis-hardcoded-secret-ruby-test.yml (1 hunks)
🧰 Additional context used
🪛 yamllint (1.35.1)
rules/ruby/security/ruby-redis-hardcoded-secret-ruby.yml

[warning] 17-17: wrong indentation: expected 4 but found 3

(indentation)


[warning] 19-19: wrong indentation: expected 5 but found 4

(indentation)


[warning] 20-20: wrong indentation: expected 8 but found 7

(indentation)


[warning] 45-45: wrong indentation: expected 18 but found 20

(indentation)


[error] 50-50: trailing spaces

(trailing-spaces)


[warning] 56-56: wrong indentation: expected 4 but found 3

(indentation)


[warning] 58-58: wrong indentation: expected 5 but found 4

(indentation)


[warning] 59-59: wrong indentation: expected 8 but found 7

(indentation)


[error] 94-94: trailing spaces

(trailing-spaces)

🔇 Additional comments (4)
tests/__snapshots__/ruby-redis-hardcoded-secret-ruby-snapshot.yml (1)

1-158: LGTM! Snapshots are well-structured and accurate

The snapshots provide precise source mapping for each test case, which will help with accurate error reporting.

tests/__snapshots__/force-ssl-false-ruby-snapshot.yml (1)

1-11: Ensure snapshot maintenance aligns with test cases

The snapshot looks good for the current test case, but remember to update it when adding the suggested additional test scenarios. The snapshot should maintain parity with all test cases.

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

4-8: Verify rule patterns for test parameter handling

The valid test cases include a test parameter, but the rule patterns in ruby-faraday-hardcoded-secret-ruby.yml don't explicitly handle this case. This might cause false positives.

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

74-80: Verify source positions for multi-line snippets

The source positions for multi-line code blocks might need adjustment. For example, in this block, the end position (90) seems to include newlines, while other blocks handle newlines differently.

✅ Verification successful

Let me gather more information about the specific file and its end positions to verify the consistency.


Source positions are correctly calculated

After analyzing the file, the end positions are consistently calculated including newlines and are correct for all snippets. For example:

  • Single-line snippets like require "faraday" have end: 17
  • Multi-line snippets have end positions that account for all characters including newlines
  • The specific snippet in question (end: 90) correctly accounts for three lines of code including newlines
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check consistency of end positions in multi-line snippets
rg "end: \d+" tests/__snapshots__/ -A 1 | grep -B 1 "source: |"

Length of output: 2753


Script:

#!/bin/bash
# Get all source blocks with their end positions from the specific file
rg "source: \||-|end: \d+" tests/__snapshots__/ruby-faraday-hardcoded-secret-ruby-snapshot.yml -B 1 -A 1

Length of output: 2906

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