-
Notifications
You must be signed in to change notification settings - Fork 205
feat(scanner): add FastAPI and Flask HTTP route matchers #52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
divyamagrawal06
wants to merge
2
commits into
vercel-labs:main
from
divyamagrawal06:feat/scanner-python-route-matchers
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| from fastapi import APIRouter, FastAPI | ||
|
|
||
| app = FastAPI() | ||
| router = APIRouter() | ||
|
|
||
|
|
||
| @app.get("/status") | ||
| async def status(): | ||
| return {"ok": True} | ||
|
|
||
|
|
||
| @router.post("/users") | ||
| def create_user(payload: dict): | ||
| return payload | ||
|
|
||
|
|
||
| @router.api_route("/reports", methods=["GET", "POST"]) | ||
| async def reports(): | ||
| return [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| from flask import Blueprint, Flask, request | ||
|
|
||
| app = Flask(__name__) | ||
| admin = Blueprint("admin", __name__) | ||
|
|
||
|
|
||
| @app.route("/healthz") | ||
| def healthz(): | ||
| return "ok" | ||
|
|
||
|
|
||
| @admin.post("/users") | ||
| def create_user(): | ||
| return request.json |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import type { CandidateMatch } from "@deepsec/core"; | ||
| import type { MatcherPlugin } from "../types.js"; | ||
| import { isPythonTestFile } from "./python-utils.js"; | ||
|
|
||
| const FASTAPI_CONTEXT_RE = | ||
| /(?:^|\n)\s*(?:from\s+fastapi\s+import|import\s+fastapi)\b|\b(?:FastAPI|APIRouter)\s*\(/; | ||
|
|
||
| const FASTAPI_ROUTE_DECORATOR_RE = | ||
| /^\s*@[\w.]+\.(get|post|put|patch|delete|options|head|trace|websocket|api_route)\s*\(/; | ||
|
|
||
| export const fastapiRouteMatcher: MatcherPlugin = { | ||
| noiseTier: "noisy" as const, | ||
| slug: "fastapi-route", | ||
| description: | ||
| "FastAPI route decorators — Python web entry point coverage for AI review (weak candidate)", | ||
| filePatterns: ["**/*.py"], | ||
| match(content, filePath) { | ||
| if (isPythonTestFile(filePath)) return []; | ||
| if (!FASTAPI_CONTEXT_RE.test(content)) return []; | ||
|
|
||
| const matches: CandidateMatch[] = []; | ||
| const lines = content.split("\n"); | ||
|
|
||
| for (let i = 0; i < lines.length; i++) { | ||
| const methodMatch = lines[i].match(FASTAPI_ROUTE_DECORATOR_RE); | ||
| if (!methodMatch) continue; | ||
|
|
||
| const method = methodMatch[1].toUpperCase(); | ||
| const start = Math.max(0, i - 2); | ||
| const end = Math.min(lines.length, i + 3); | ||
| matches.push({ | ||
| vulnSlug: "fastapi-route", | ||
| lineNumbers: [i + 1], | ||
| snippet: lines.slice(start, end).join("\n"), | ||
| matchedPattern: | ||
| method === "API_ROUTE" | ||
| ? "FastAPI API route decorator — Python HTTP entry point (weak candidate)" | ||
| : `FastAPI ${method} route decorator — Python HTTP entry point (weak candidate)`, | ||
| }); | ||
| } | ||
|
|
||
| return matches; | ||
| }, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import type { CandidateMatch } from "@deepsec/core"; | ||
| import type { MatcherPlugin } from "../types.js"; | ||
| import { isPythonTestFile } from "./python-utils.js"; | ||
|
|
||
| const FLASK_CONTEXT_RE = | ||
| /(?:^|\n)\s*(?:from\s+flask\s+import|import\s+flask)\b|\b(?:Flask|Blueprint)\s*\(/; | ||
|
|
||
| const FLASK_ROUTE_DECORATOR_RE = /^\s*@[\w.]+\.(route|get|post|put|patch|delete|options|head)\s*\(/; | ||
|
|
||
| export const flaskRouteMatcher: MatcherPlugin = { | ||
| noiseTier: "noisy" as const, | ||
| slug: "flask-route", | ||
| description: | ||
| "Flask route decorators — Python web entry point coverage for AI review (weak candidate)", | ||
| filePatterns: ["**/*.py"], | ||
| match(content, filePath) { | ||
| if (isPythonTestFile(filePath)) return []; | ||
| if (!FLASK_CONTEXT_RE.test(content)) return []; | ||
|
|
||
| const matches: CandidateMatch[] = []; | ||
| const lines = content.split("\n"); | ||
|
|
||
| for (let i = 0; i < lines.length; i++) { | ||
| const methodMatch = lines[i].match(FLASK_ROUTE_DECORATOR_RE); | ||
| if (!methodMatch) continue; | ||
|
|
||
| const method = methodMatch[1].toUpperCase(); | ||
| const start = Math.max(0, i - 2); | ||
| const end = Math.min(lines.length, i + 3); | ||
| matches.push({ | ||
| vulnSlug: "flask-route", | ||
| lineNumbers: [i + 1], | ||
| snippet: lines.slice(start, end).join("\n"), | ||
| matchedPattern: | ||
| method === "ROUTE" | ||
| ? "Flask ROUTE decorator — Python HTTP entry point (weak candidate)" | ||
| : `Flask ${method} route decorator — Python HTTP entry point (weak candidate)`, | ||
| }); | ||
| } | ||
|
|
||
| return matches; | ||
| }, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| const PYTHON_TEST_FILE_RE = | ||
| /(?:^|\/)(?:tests?|__tests__)\/|(?:^|\/)(?:test_.*|.*_(?:test|spec))\.py$/i; | ||
|
|
||
| export function isPythonTestFile(filePath: string): boolean { | ||
| return PYTHON_TEST_FILE_RE.test(filePath); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Duplicate FastAPI and Flask matchers registered in the default registry produce redundant candidate matches on every Python file using those frameworks.