4

I have a Semgrep rule:

rules:
  - id: create-chat-client
    patterns:
      - pattern: var $X = GrpcChannel.ForAddress(...); 
      - pattern: var $Y = new ChatService.ChatServiceClient($X);
    languages: 
      - csharp
    message: <pass>
    severity: INFO

And I am trying to match this code:

using Grpc.Net.Client;
using GrpcChat.ProtoBuf;

var channel = GrpcChannel.ForAddress("https://localhost:8888");
var client = new ChatService.ChatServiceClient(channel);

These match separately, but the 'patterns' should be a "AND" match and it fails. I must be missing something obvious. Anyone see anything?

1 Answer 1

4

Using pattern-inside works:

rules:
  - id: chat
    patterns:
      - pattern-inside: |
          var $X = GrpcChannel.ForAddress(...); 
          ...
      - pattern: var $Y = new ChatService.ChatServiceClient($X);
    languages: 
      - csharp
    message: <pass>
    severity: INFO

with this test case:

using Grpc.Net.Client;
using GrpcChat.ProtoBuf;

var channel = GrpcChannel.ForAddress("https://localhost:8888");
// ruleid: chat
var client = new ChatService.ChatServiceClient(channel);

when I run a test:

% semgrep --test rules/
✓ All tests passed!
1
  • 1
    Why use pattern-inside here? In the docs under "patterns operator evaluation strategy" it says that patterns are "intersected", and that I understand as an "AND".
    – adamczi
    Commented Sep 5, 2023 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.