Skip to content

Commit

Permalink
Modify rule S5144: Add HTTPX support (APPSEC-1247) (#3365)
Browse files Browse the repository at this point in the history
## Review

A dedicated reviewer checked the rule description successfully for:

- [ ] logical errors and incorrect information
- [ ] information gaps and missing content
- [ ] text style and tone
- [ ] PR summary and labels follow [the
guidelines](https://github.com/SonarSource/rspec/#to-modify-an-existing-rule)
  • Loading branch information
egon-okerman-sonarsource authored Oct 27, 2023
1 parent effc297 commit 75e4b48
Show file tree
Hide file tree
Showing 3 changed files with 55 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 @@ -87,6 +87,7 @@
* Python Standard Library
* PyYAML
* Requests
* HTTPX
* SQLAlchemy
* Amazon DynamoDB
* python-ldap
Expand Down
52 changes: 52 additions & 0 deletions rules/S5144/python/how-to-fix-it/httpx.adoc
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[]
2 changes: 2 additions & 0 deletions rules/S5144/python/rule.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ include::how-to-fix-it/python.adoc[]

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

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

== Resources

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

0 comments on commit 75e4b48

Please sign in to comment.