Getting Started
Write your first security rule in 5 minutes. This hands-on guide will have you detecting vulnerabilities immediately.
Prerequisites
Before writing rules, make sure you have Code Pathfinder installed:
Verify the installation:
Your First Rule
Let's write a rule to detect hardcoded passwords. Create a file called my_first_rule.py:
How this works:
@rule(...)- Decorator that marks this as a security ruleid- Unique identifier for the ruleseverity- How serious this issue is (critical/high/medium/low)cwe- Maps to Common Weakness Enumerationcalls("connect", ...)- Find calls to any function named "connect"match_name={"password": "*"}- Where password argument has any value
Running the Rule
Create a test file to scan. Save this as test_app.py:
Now run Code Pathfinder with your rule:
Understanding the Output
You'll see output like this:
[high] hardcoded-password
CWE: CWE-798
Detects hardcoded passwords in database connections
→ test_app.py:4
Found call to 'connect' with password argument
The rule correctly identified the hardcoded password on line 4!
Making It Better
Let's improve the rule to catch more patterns. Update my_first_rule.py:
Improvements made:
Or(...)- Matches if ANY pattern is found*.connect- Wildcard matches any module (psycopg2.connect, mysql.connect, etc.)match_position- Matches positional arguments (second argument in this case)- Multiple keyword names - Catches password, passwd, and pwd arguments
Next Steps
Congratulations! You've written and run your first security rule. Continue learning: