Skip to content

Commit

Permalink
Modify rule S6287: Add FastAPI support (APPSEC-1252) (#3390)
Browse files Browse the repository at this point in the history
  • Loading branch information
pierre-loup-tristant-sonarsource authored Dec 18, 2023
1 parent 8d5e029 commit 241c6bb
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/header_names/allowed_framework_names.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
* pyDes
* PyJWT
* python-jwt
* FastAPI
* python-jose
* ssl
// Docker
Expand Down
39 changes: 39 additions & 0 deletions rules/S6287/python/how-to-fix-it/fastapi.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
== How to fix it in FastAPI

=== Code examples

include::../../common/fix/code-rationale.adoc[]

==== Noncompliant code example

[source,python,diff-id=11,diff-type=noncompliant]
----
from fastapi import APIRouter
from fastapi.responses import Response, RedirectResponse
from fastapi.requests import Request
router = APIRouter()
@router.get('/check-cookie')
async def check_cookie(request: Request, response: Response, cookie: str | None = None):
if "session" not in request.cookies.keys():
response.set_cookie("session", cookie) # Noncompliant
----

==== Compliant solution

[source,python,diff-id=11,diff-type=compliant]
----
from fastapi import APIRouter
from fastapi.responses import Response, RedirectResponse
from fastapi.requests import Request
router = APIRouter()
@router.get('/check-cookie')
async def check_cookie(request: Request):
if "session" not in request.cookies.keys():
return RedirectResponse("/getcookie")
----

include::../../common/fix/how-does-this-work.adoc[]
2 changes: 2 additions & 0 deletions rules/S6287/python/rule.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ include::../impact.adoc[]

include::how-to-fix-it/django.adoc[]

include::how-to-fix-it/fastapi.adoc[]

== Resources

include::../common/resources/standards.adoc[]
Expand Down

0 comments on commit 241c6bb

Please sign in to comment.