-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modify rule S5144: Add HTTPX support (APPSEC-1247) (#3410)
* Add HTTPX * Enhance compliant code sample * Keep samples consistent * Simplify compliant example somewhat
- Loading branch information
1 parent
4ed4c84
commit a3fd54b
Showing
3 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -88,6 +88,7 @@ | |
* Python Standard Library | ||
* PyYAML | ||
* Requests | ||
* HTTPX | ||
* SQLAlchemy | ||
* Amazon DynamoDB | ||
* python-ldap | ||
|
This file contains 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,52 @@ | ||
== How to fix it in HTTPX | ||
|
||
=== Code examples | ||
|
||
include::../../common/fix/code-rationale.adoc[] | ||
|
||
==== Noncompliant code example | ||
|
||
[source,python,diff-id=21,diff-type=noncompliant] | ||
---- | ||
from fastapi import FastAPI | ||
import httpx | ||
app = FastAPI() | ||
@app.get('/example') | ||
def example(url: str): | ||
r = httpx.get(url) # Noncompliant | ||
return {"response": r.text} | ||
---- | ||
|
||
==== Compliant solution | ||
|
||
[source,python,diff-id=21,diff-type=compliant] | ||
---- | ||
from fastapi import FastAPI | ||
from fastapi.responses import JSONResponse | ||
import httpx | ||
from urllib.parse import urlparse | ||
DOMAINS_ALLOWLIST = ['trusted1.example.com', 'trusted2.example.com'] | ||
app = FastAPI() | ||
@app.get('/example') | ||
def example(url: str): | ||
if not urlparse(url).hostname in DOMAINS_ALLOWLIST: | ||
return JSONResponse({"error": f"URL {url} is not whitelisted."}, 400) | ||
r = httpx.get(url) | ||
return {"response": r.text} | ||
---- | ||
|
||
=== How does this work? | ||
|
||
include::../../common/fix/pre-approved-list.adoc[] | ||
|
||
The compliant code example uses such an approach. | ||
HTTPX implicitly validates the scheme as it only allows `http` and `https` by default. | ||
|
||
=== Pitfalls | ||
|
||
include::../../common/pitfalls/starts-with.adoc[] |
This file contains 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