Documentation Index
Fetch the complete documentation index at: https://mintlify.com/Eljakani/ward/llms.txt
Use this file to discover all available pages before exploring further.
Overview
Ward supports three types of pattern matching in security rules. Each type is optimized for different use cases and provides specific matching behavior. Source:internal/config/rules.go:27
Pattern Type Reference
regex
Matches lines using regular expressions (Go syntax).Regular expression pattern matching on a line-by-line basis.How it works:
- Ward reads each target file line by line
- Applies the regex pattern to each line
- Creates a finding for each line that matches
- Captures the line number and code snippet
- Detecting patterns with variations (e.g.,
password\s*=) - Matching structured formats (e.g., AWS keys:
AKIA[0-9A-Z]{16}) - Complex string patterns
- Use
\sfor whitespace - Use
\$to match literal$ - Use
[^)]for character class negation - Use
(?i)for case-insensitive matching
Examples
contains
Matches lines containing an exact substring.Simple substring matching for exact string presence.How it works:
- Ward checks if each line contains the exact pattern string
- Case-sensitive by default
- No special character escaping needed
- Faster than regex for simple strings
- Detecting specific function calls (e.g.,
dd(,eval() - Finding exact strings (e.g.,
APP_DEBUG=true) - Checking for required tokens with
negative: true
Examples
file-exists
Checks whether a file matching the target glob pattern exists.Checks for the presence or absence of files matching a glob pattern.How it works:
- Ward evaluates the
targetglob pattern - If any files match, a finding is created
- The
patternfield is not used (can be omitted or empty) - Useful with
negative: trueto require files
- Detecting sensitive files in version control (
.env) - Ensuring required security files exist
- Finding debug/test files in production code
Examples
Negative Patterns
All pattern types supportnegative: true to invert the match logic.
When
true, triggers a finding when the pattern is absent rather than present.Use cases:- Required security headers
- Mandatory CSRF tokens
- Must-have configuration
- Enforcing coding standards
Negative Pattern Examples
Exclude Patterns
Reduce false positives by excluding lines that match a secondary pattern.Lines matching this pattern are excluded even if they match the main pattern.Only supported for
regex and contains types (not file-exists).Use cases:- Filter out commented code
- Ignore test files
- Skip known safe patterns
Exclude Pattern Examples
Pattern Matching Workflow
For each rule, Ward follows this process:- Target Resolution - Expand the
targetto a list of files - Pattern Application - For each file:
- Read line by line
- Apply pattern based on
type - Check
exclude_patternif specified - Apply
negativelogic if enabled
- Finding Creation - If match (considering negative):
- Extract line number
- Capture code snippet (±2 lines context)
- Create finding with rule metadata
Performance Considerations
Use 'contains' for Simple Strings
contains is faster than regex when you don’t need pattern matching. Use it for exact function names or keywords.Narrow Your Targets
Use specific targets like
config-files instead of php-files to reduce scan time.Optimize Regex
Avoid overly complex regex patterns. Use anchors (
^, $) when possible.Use Exclude Patterns
Filter out false positives with
exclude_pattern rather than manual review.Best Practices
Choose the Right Pattern Type
Choose the Right Pattern Type
- Use
containsfor exact strings (fastest) - Use
regexwhen you need pattern flexibility - Use
file-existsfor presence/absence checks
Test Your Patterns
Test Your Patterns
Create a test file with known violations and run Ward to verify your rule catches them:
Document Complex Regex
Document Complex Regex
Add comments in your rule’s
description or remediation explaining what the regex matches:Start Broad, Then Narrow
Start Broad, Then Narrow
Begin with a simple pattern and add
exclude_pattern to filter false positives rather than creating a complex regex upfront.Related
- Rule Format - Complete YAML schema
- Target Types - File target reference
- CLI Options - Command-line flags