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 C++ and Go to prevent use-after-free and interface binding risks #130

Merged

Conversation

ESS-ENN
Copy link
Collaborator

@ESS-ENN ESS-ENN commented Jan 15, 2025

Summary by CodeRabbit

  • New Features

    • Added a new C++ rule to warn about returning pointers to string memory that may become invalid
    • Added a new Go rule to prevent binding network listeners to all interfaces
    • Introduced corresponding test cases and snapshots for both rules
  • Tests

    • Created test configurations for C++ string return type validation
    • Added test cases for Go network listener binding scenarios

Copy link

coderabbitai bot commented Jan 15, 2025

Walkthrough

This pull request introduces two new security-focused rules for C++ and Go programming languages. The first rule in rules/cpp/return-c-str-c.yml warns about potential use-after-free vulnerabilities when returning pointers from string objects. The second rule in rules/go/security/avoid-bind-to-all-interfaces-go.yml prevents network listeners from binding to all interfaces, which could expose servers to unintended public access. Accompanying test cases and snapshots have been added to validate these new rules.

Changes

File Change Summary
rules/cpp/return-c-str-c.yml Added new rule return-c-str-cpp to warn about returning pointers from string objects
rules/go/security/avoid-bind-to-all-interfaces-go.yml Added new rule avoid-bind-to-all-interfaces-go to prevent binding to all network interfaces
tests/__snapshots__/return-c-str-cpp-snapshot.yml Added snapshot for C++ string return scenarios
tests/__snapshots__/avoid-bind-to-all-interfaces-go-snapshot.yml Added snapshot for Go network listener configurations
tests/cpp/return-c-str-cpp-test.yml Added test configuration for C++ string return rule
tests/go/avoid-bind-to-all-interfaces-go-test.yml Added test configuration for Go network interface binding rule

Sequence Diagram

sequenceDiagram
    participant Dev as Developer
    participant Rule as Security Rule
    participant Code as Source Code
    
    Dev->>Code: Writes code with potential vulnerability
    Code->>Rule: Rule checks code pattern
    alt Vulnerable Pattern Detected
        Rule-->>Dev: Warns about potential security risk
    else Safe Pattern
        Rule->>Code: No warning issued
    end
Loading

Possibly related PRs

Suggested reviewers

  • ganeshpatro321

Poem

🐰 A rabbit's code review delight,
Catching vulnerabilities in sight!
No strings too tricky, no ports too wide,
Security rules, our faithful guide!
Hop along, code, safe and bright! 🛡️


🪧 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. (Beta)
  • @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 C++ and Go to prevent use-after-free and interface binding risks Jan 15, 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: 1

🧹 Nitpick comments (8)
rules/cpp/return-c-str-c.yml (1)

16-22: Consider adding pattern for string_view to prevent similar issues.

The patterns comprehensively cover std::string variants, but might want to consider adding patterns for std::string_view to prevent similar lifetime issues with .data().

Add these patterns:

  any:
    - pattern: return basic_string<$TYPE>($$$).$METHOD();
    - pattern: return std::basic_string<$TYPE>($$$).$METHOD();
    - pattern: return string($$$).$METHOD();
    - pattern: return std::string($$$).$METHOD();
    - pattern: return wstring($$$).$METHOD();
    - pattern: return std::wstring($$$).$METHOD();
+   - pattern: return string_view($$$).$METHOD();
+   - pattern: return std::string_view($$$).$METHOD();
tests/cpp/return-c-str-cpp-test.yml (1)

8-24: Enhance test coverage with additional edge cases.

While the current test cases cover the basic scenarios well, consider adding these additional test cases:

Add these test cases:

invalid:
  - |
    char *return_namespace_directly() {
      return std::string("foo").c_str();
    }
+  - |
+    const char *return_const_char() {
+      return std::string("foo").c_str();
+    }
+  - |
+    const char *return_from_temporary() {
+      return (std::string("foo") + "bar").c_str();
+    }
tests/go/avoid-bind-to-all-interfaces-go-test.yml (2)

2-4: Consider expanding valid test cases for better coverage.

The valid test cases could be enhanced to include more secure binding scenarios:

  • Localhost (127.0.0.1)
  • IPv6 addresses (::1)
  • Named interfaces (localhost)
 valid:
   - |
     l, err := net.Listen("tcp", "192.168.1.101:2000")
+  - |
+    l, err := net.Listen("tcp", "127.0.0.1:2000")
+  - |
+    l, err := net.Listen("tcp", "[::1]:2000")
+  - |
+    l, err := net.Listen("tcp", "localhost:2000")

5-9: Consider additional invalid test cases for comprehensive coverage.

The invalid test cases could include more variations of problematic bindings:

  • IPv6 all interfaces (::)
  • Alternative notations for 0.0.0.0
 invalid:
   - |
     l, err := net.Listen("tcp", "0.0.0.0:2000")
   - |
     l, err := net.Listen("tcp", ":2000")
+  - |
+    l, err := net.Listen("tcp", "[::]:2000")
+  - |
+    l, err := net.Listen("tcp", "0:2000")
rules/go/security/avoid-bind-to-all-interfaces-go.yml (4)

4-8: Enhance the message with more actionable guidance.

The current message could be more specific about recommended secure alternatives.

 message: >-
   "Detected a network listener listening on 0.0.0.0 or an empty string.
       This could unexpectedly expose the server publicly as it binds to all
-      available interfaces. Instead, specify another IP address that is not
-      0.0.0.0 nor the empty string."
+      available interfaces. Use '127.0.0.1' or 'localhost' for local-only access,
+      or specify a specific interface IP address for controlled network access."

9-12: Add more specific security references.

Consider adding more relevant CWE references for network exposure:

  • CWE-1327: Binding to an Unrestricted IP Address
  • CWE-668: Exposure of Resource to Wrong Sphere
 note: >-
   [CWE-200] Exposure of Sensitive Information to an Unauthorized Actor
+  [CWE-1327] Binding to an Unrestricted IP Address
+  [CWE-668] Exposure of Resource to Wrong Sphere
   [REFERENCES]
       -  https://owasp.org/Top10/A01_2021-Broken_Access_Control
+      -  https://cwe.mitre.org/data/definitions/1327.html

19-21: Consider expanding pattern matching for comprehensive coverage.

The rule could be enhanced to catch more network binding scenarios:

 any:
   - pattern: tls.Listen($NETWORK, $IP $$$)
   - pattern: net.Listen($NETWORK, $IP $$$)
+  - pattern: http.ListenAndServe($IP $$$)
+  - pattern: http.ListenAndServeTLS($IP $$$)
+  - pattern: grpc.NewServer().Serve(net.Listen($NETWORK, $IP $$$))

26-29: Strengthen regex patterns for IP matching.

The current regex patterns could be enhanced to catch more edge cases:

       - kind: interpreted_string_literal
-        regex: ^"0.0.0.0:.*"$|^":.*"$|^'0.0.0.0:.*'$|^':.*'$
+        regex: ^"(0\.0\.0\.0|0|::|):.*"$|^":.*"$|^'(0\.0\.0\.0|0|::|):.*'$|^':.*'$
       - kind: raw_string_literal
-        regex: ^`0.0.0.0:.*`$|^`:.*`$
+        regex: ^`(0\.0\.0\.0|0|::|):.*`$|^`:.*`$
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 670f26f and 45e72ac.

📒 Files selected for processing (7)
  • rules/cpp/return-c-str-c.yml (1 hunks)
  • rules/go/security/avoid-bind-to-all-interfaces-go.yml (1 hunks)
  • tests/__snapshots__/avoid-bind-to-all-interfaces-go-snapshot.yml (1 hunks)
  • tests/__snapshots__/return-c-str-cpp-snapshot.yml (1 hunks)
  • tests/__snapshots__/sizeof-this-cpp-snapshot.yml (1 hunks)
  • tests/cpp/return-c-str-cpp-test.yml (1 hunks)
  • tests/go/avoid-bind-to-all-interfaces-go-test.yml (1 hunks)
✅ Files skipped from review due to trivial changes (2)
  • tests/snapshots/avoid-bind-to-all-interfaces-go-snapshot.yml
  • tests/snapshots/return-c-str-cpp-snapshot.yml
🔇 Additional comments (2)
rules/cpp/return-c-str-c.yml (1)

4-12: LGTM! Well-documented security rule with proper references.

The warning message is clear and the documentation includes relevant CWE and SEI CERT references. Good practice linking to both C and C++ specific guidelines.

tests/__snapshots__/sizeof-this-cpp-snapshot.yml (1)

2-13: LGTM! Well-structured snapshot with proper label positioning.

The snapshot correctly captures the sizeof(this) expression with appropriate primary and secondary labels for syntax highlighting.

@ganeshpatro321 ganeshpatro321 merged commit 56e9aa5 into coderabbitai:main Jan 16, 2025
1 check 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.

2 participants