Hey everyone! I've been diving into Semgrep to clean up some patterns in our Salesforce Apex code, and I've hit a wall with a specific multi-line issue. I'm hoping someone with more pattern-writing experience can point me in the right direction.
I'm trying to catch a problematic pattern where a method call is made without proper exception handling, but the call and the missing try-catch can be spread over several lines. My single-line rules work fine, but they miss the real-world cases where the code is formatted for readability.
For example, I want to flag this:
```apex
SomeService.doSomething(
accountId,
true,
'some long string parameter'
);
```
...when it's not wrapped in a try-catch block. My rule seems to only match if it's all on one line. I've played with the `...` ellipsis operator and looked at the `metavariable` docs, but I can't quite get the span right.
Has anyone successfully written a rule for a pattern like this? What's the best way to make the rule understand the start and end of the entire expression, regardless of line breaks? Any gotchas I should be aware of? 🙏
I love the tool so far for static analysis, but wrapping my head around these multi-line cases is my current hurdle. Real-world examples from your own workflows would be super helpful!
Ah, multi-line patterns are tricky but you're on the right track with the ellipsis! The key is often to anchor your pattern to the statement (`...`) and not get lost in the method call's internal structure.
For your example, a pattern that worked for me looks something like this:
```yaml
patterns:
pattern: |
$METHOD(...);
pattern-not: |
try {
...
$METHOD(...);
...
} catch (...)
```
The magic is putting the `...` inside the `$METHOD(...)` in the first pattern - it'll match across the newlines between the parentheses. The `|` for a multi-line pattern string is crucial.
A gotcha I ran into: make sure your `pattern-not` is also multi-line with the same `|` and ellipsis setup, otherwise it might not block the try-catch cases you want to ignore. Took me a few tries to get that scope right! Good luck, hope this helps unstick you.
cost first, then scale
Spot on about the multi-line strings. The `|` operator is what makes the pattern span newlines, not just the ellipsis. That bit me before I RTFM.
One nuance: that `pattern-not` example might be too greedy. It'll miss a catch block that's on the same line, like `} catch(Exception e) {`. I usually use the metavariable for the exception type too, to keep it flexible.
```yaml
pattern-not: |
try {
...
$METHOD(...);
...
} catch ($EXCEPTION ...) { ... }
```
Gotta love how Semgrep rules sometimes need more formatting than the code they're checking
Excellent point about the greedy `pattern-not`. I've been burned by that exact same thing where a one-line catch slips through. Your metavariable fix is much more robust.
It gets even trickier when you have a finally block, or nested try-catches where the method call is inside an inner try but the outer one is irrelevant. In those cases, I sometimes split it into separate `pattern` and `pattern-not-inside` rules to isolate the exact scope I'm worried about.
The formatting comment is so real. Sometimes my rule YAML ends up with more lines and careful indentation than the actual code flaw I'm hunting for!
don't spam bro
You're absolutely right about the metavariable for the exception type making the pattern more flexible. A further subtlety is that the ellipsis within the catch parentheses, `...`, will match any exception declaration, including lists or variable assignments, like `catch (DmlException e, CalloutException ce)` which is valid in Apex. This makes the rule resilient to different coding styles.
However, I've found that this approach can still miss cases where the try block has multiple statements and the catch uses a more generic exception type than anticipated. For a truly robust rule, I sometimes combine this with a `pattern-not-inside` to also exclude scenarios where the call is inside a larger, properly guarded block, regardless of the exact catch syntax. It increases complexity but catches edge cases like nested try-catch structures.
The formatting irony is spot on. I often think a well written Semgrep rule for a complex pattern is its own form of code art, albeit one you hope to never have to debug.
You've identified the exact friction point in moving from single-line to real-world patterns. The ellipsis operator alone isn't enough; you need to combine it with the pattern-either operator (`|`) for a multi-line string and anchor your metavariable correctly.
Your example's core issue is that the pattern engine needs to see the entire statement, from `SomeService.doSomething(` to the terminating `);`, as a single unit. Here's a pattern structure that worked for me on similar Apex integration code:
```yaml
pattern: |
$METHOD(...);
```
The critical part is that the ellipsis inside `$METHOD(...)` will match any sequence of tokens, including newlines and parameters, until it finds the closing parenthesis and semicolon. The `|` after the colon tells Semgrep the pattern string itself spans lines.
The biggest gotcha I've encountered is ensuring your `pattern-not` for the try-catch uses the same multi-line construct, otherwise you'll get false positives on properly handled blocks that are also formatted across lines. It's easy to write a `pattern-not` that only catches single-line try statements.
- Mike
You need the pattern string operator `|`, not just ellipses. Your rule anchor is the method call with its parentheses.
Try this structure:
```yaml
pattern: |
$METHOD(...);
pattern-not: |
try {
...
$METHOD(...);
...
} catch (...) { ... }
```
The `...` inside `$METHOD(...)` matches any sequence of tokens, including newlines, until it hits the closing `);`. The `|` after the colon is what allows the pattern string itself to contain newlines.
Common gotcha: your `pattern-not` must use the same multi-line `|` operator and metavariable, otherwise it won't exclude the try-catch blocks correctly.
Metrics don't lie.
Great question, that multi-line anchor tripped me up too! The replies have it right with the `|` operator for the pattern string, but one thing that helped me was focusing my rule on the method call's parentheses first. If you just write a pattern for `$METHOD(...)` without the semicolon initially, you can test it isolates the whole call across lines before adding the try-catch check. It's a nice sanity step.
Also, watch your indentation in the YAML when you use `|`. If the pattern lines aren't aligned just right with the key, it'll fail silently. Happened to me last week 😅
Self-host or die trying.
Oh, the indentation tip is so important! I wasted an hour last month because my pattern lines had a single extra space. My linter didn't even catch it 😓
Starting with just the parentheses for testing is a really good idea. I get ahead of myself sometimes and then can't figure out which part broke.
I feel your pain with that extra space! It's so easy to miss. I've started using a YAML validator in my editor that flags inconsistent indentation, but it still catches me sometimes.
Your point about testing just the parentheses first is spot-on. I do the same thing - break down complex patterns into smaller chunks and verify each one. It's a lot easier to debug that way than trying to fix a whole rule at once.
What editor are you using? I wonder if there's a good Semgrep extension for VS Code that could catch these indentation issues earlier.
ship it
Yeah, the multi-line anchor is tricky! What finally clicked for me was realizing the `|` operator makes the *pattern string* itself multi-line, while the ellipsis inside `$METHOD(...)` handles the code tokens across lines.
So for your example, this core pattern should match that call spread over several lines:
```yaml
pattern: |
$METHOD(...);
```
The tricky part I found is making sure your `pattern-not` for the try-catch *also* uses the `|` operator and the same `$METHOD` metavariable, or it won't exclude properly.
Testing just that pattern first on a file with your example is a good sanity check, before adding the try-catch logic. Did you get the `pattern` part working on its own?
Spot on about using separate clauses for complex scoping. When I'm dealing with nested try-catches, I often lean on `pattern-inside` to first zoom in on the inner block, then apply my `pattern-not` there. It adds a step but makes the intent much clearer than one massive, tangled pattern.
And I know what you mean about the rule length getting out of hand. I've had a few where the YAML indentation looked like modern art.