Pattern Matching
MergeGuide uses a dual-layer detection engine: regex for fast known-pattern matching, and Semgrep for AST-based taint analysis and data flow tracking. Built-in policies use both layers. Custom policies can use regex patterns (and, in some configurations, Semgrep rules).Detection Engine Layers
| Layer | Technology | Best For |
|---|---|---|
| Layer 1 | Regex | Known patterns, string matching, fast scanning |
| Layer 2 | Semgrep AST taint analysis | Data flow, injection vulnerabilities, language-aware detection |
type: regex, you’re writing a Layer 1 pattern. The built-in policies for injection vulnerabilities (SQL, XSS, command injection) leverage Semgrep’s taint analysis in Layer 2.
MergeGuide supports multiple pattern types for different use cases.
Regex Patterns
Regular expressions are the most common pattern type.Basic Syntax
Capturing Groups
Use groups to provide context in messages:Common Patterns
Hardcoded Secrets
Security Issues
Code Quality
Regex Flags
| Flag | Description |
|---|---|
i | Case insensitive |
m | Multiline (^ and $ match line boundaries) |
s | Dot matches newline |
Negative Patterns
Exclude certain contexts using negative lookahead:AST Patterns
Abstract Syntax Tree patterns understand code structure.JavaScript/TypeScript AST
Python AST
AST Query Syntax
MergeGuide uses a CSS-like selector syntax for AST queries:AST Benefits
- Structure-aware: Won’t match code in strings or comments
- Language-specific: Understands language semantics
- Precise: Can target specific code constructs
AST Limitations
- Requires parsing (slower than regex)
- Language-specific patterns needed
- More complex to write
Semantic Patterns
High-level patterns that detect code behaviors.Available Semantic Patterns
Semantic Pattern List
| Pattern | Description |
|---|---|
sql-string-concatenation | SQL built with string operations |
hardcoded-credential | Secrets in source code |
insecure-random | Math.random for security |
missing-input-validation | Unvalidated user input |
unsafe-deserialization | Deserializing untrusted data |
path-traversal | File path from user input |
command-injection | Shell commands with user input |
open-redirect | Redirect URL from user input |
Multi-Pattern Policies
Combine multiple patterns:Pattern Context
Line Context
Include surrounding lines for context:File Context
Apply patterns based on file location:Performance Tips
- Order matters: Put fast regex patterns before slow AST patterns
- Be specific: Narrow file patterns reduce scanning
- Avoid backtracking: Use atomic groups in complex regex
- Cache results: Patterns are cached per file
Regex Optimization
Testing Patterns
Test Mode
Pattern Playground
Use the dashboard pattern tester:- Go to Policies > Create Policy
- Enter pattern in the Test tab
- Paste sample code
- See matches highlighted in real-time