Lab 02 — Source Code Vulnerability Scanner
Operation Cedar Lattice, Phase 11.
Meridian Freight International runs a custom logistics management application. FIN-LATTICE has identified weaknesses in the codebase through a combination of phishing-delivered source files and a misconfigured internal Git server. Your job: scan the source code for exploitable patterns, classify each finding by CWE, and produce a prioritized vulnerability report. This lab implements a lightweight static analysis engine covering the three most common web application vulnerability classes.
Safety. This lab is a static pattern scanner over synthetic code strings. There is no code execution, no exploit payload generation, and no network access. The input is a list of code snippet dicts; the output is a structured FINDINGS report with CWE identifiers and severity levels.
What You Will Build
Five functions that implement the Cedar Lattice Source Code Triage Pipeline:
| Function | Input | Output |
|---|---|---|
sql_injection_patterns(code) | source code string | list[dict] — line-level findings |
command_injection_patterns(code) | source code string | list[dict] — line-level findings |
path_traversal_patterns(code) | source code string | list[dict] — line-level findings |
scan_snippet(snippet) | code snippet dict | list[dict] — CWE-enriched findings |
severity_rank(findings) | findings list | sorted findings list |
Snippet Schema
snippet = {
"language": "python", # language hint (informational)
"code": str, # source code to scan
"context": str, # human-readable context (module name, purpose)
}
Function Specifications
sql_injection_patterns(code)
Scan line by line. A line is flagged if it contains both:
- A SQL keyword:
SELECT,INSERT,UPDATE,DELETE, orWHERE(case-insensitive) - A string-interpolation marker:
" + "," % ",.format(, or an f-string variable marker{
Return {line_hint: int, pattern_type: "sql_string_concat", severity: "CRITICAL"} for
each matching line. line_hint is 1-indexed.
command_injection_patterns(code)
Scan line by line. Flag each line matching:
| Pattern | Detection Rule | Severity |
|---|---|---|
os_system | line contains "os.system(" | HIGH |
subprocess_shell_true | line contains "shell=True" and is not a comment | HIGH |
eval_call | line contains "eval(" | HIGH |
Return {line_hint: int, pattern_type: str, severity: "HIGH"} for each match.
path_traversal_patterns(code)
Scan line by line. Flag each line matching:
| Pattern | Detection Rule | Severity |
|---|---|---|
open_string_concat | line contains "open(" AND ("+" OR ".format(" OR "{") | MEDIUM |
path_join_user_input | line contains "os.path.join(" AND ("request" OR "input" OR "param" OR "args") | MEDIUM |
Return {line_hint: int, pattern_type: str, severity: "MEDIUM"} for each match.
scan_snippet(snippet)
Run all three pattern scanners on snippet["code"]. Enrich each finding with CWE info:
| pattern_type | CWE | name |
|---|---|---|
sql_string_concat | 89 | "SQL Injection" |
subprocess_shell_true, os_system, eval_call | 78 | "Command Injection" |
open_string_concat, path_join_user_input | 22 | "Path Traversal" |
Return combined list sorted by severity (CRITICAL first, then HIGH, then MEDIUM)
then by line_hint ascending.
severity_rank(findings)
Sort findings by severity: CRITICAL first, HIGH second, MEDIUM third, LOW last.
Within the same severity, sort by line_hint ascending.
Files
| File | Purpose |
|---|---|
lab.py | Your implementation — stubs with NotImplementedError |
solution.py | Complete reference solution |
test_lab.py | 12 adversarial pytest tests |
requirements.txt | pytest>=7.0 |
Running the Tests
# Run against your stubs (expect NotImplementedError)
pytest -q
# Run against reference solution (all 12 should pass)
LAB_MODULE=solution pytest -q
Learning Objectives
After completing this lab you should be able to:
- Identify SQL injection patterns from string concatenation and f-string interpolation.
- Distinguish dangerous subprocess / shell invocation patterns from safe alternatives.
- Explain why
os.path.join()with user-supplied input requiresos.path.realpath()validation. - Assign CWE identifiers to common vulnerability classes.
- Prioritize findings by severity for efficient remediation triage.