Skip to content
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

New Published Rules - khanhldt.flask-route-decorator #3238

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions khanhldt/flask-route-decorator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from flask import Blueprint

blueprint = Blueprint()


# FP: Only 1 decorator
@blueprint.route("HAI")
def f():
pass

# FP: Correct order
@blueprint.route("HAI")
@another.func("HAI")
def f():
pass

# FP: "func" not "route"
@another.func("HAI")
@blueprint.func("HAI")
def f():
pass

# FP: Correct order with app
@app.route("HAI")
@another.func("HAI")
def f():
pass

@blueprint.route("HAI")
@app.route("HAI")
def f():
pass

@another.func("HAI")
@app.route("HAI")
@app.route("HAI")
def f():
pass

@app.route("ABC")
@app.route("HAI")
@another.func("HAI")
@app.route("BCD")
@app.route("HAI")
def f():
pass
30 changes: 30 additions & 0 deletions khanhldt/flask-route-decorator.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
rules:
- id: flask-route-decorator
patterns:
- pattern-either:
- pattern-inside: |
import flask
...
- pattern-inside: |
import flask_protobuf
...
- pattern-inside: |
@$TARGET_DECORATOR(...)
def $F(...):
...
- metavariable-pattern:
metavariable: $TARGET_DECORATOR
patterns:
- pattern-regex: ^(.*\.)?route$
- pattern-regex: \@((?!route\().)*\n@(.*\.)?route\(
message: In Flask, the route() decorator has to be the outermost. Everything outside
of route() will take no effect.
languages:
- python
severity: ERROR
metadata:
category: correctness
technology:
- flask
references:
- https://flask.palletsprojects.com/en/2.3.x/patterns/viewdecorators/#login-required-decorator
Loading