-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into rule/add-RSPEC-S7192
- Loading branch information
Showing
23 changed files
with
611 additions
and
33 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
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
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,2 @@ | ||
{ | ||
} |
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,71 @@ | ||
include::../summary.adoc[] | ||
|
||
== Why is this an issue? | ||
|
||
include::../rationale.adoc[] | ||
|
||
include::../impact.adoc[] | ||
|
||
include::../exceptions.adoc[] | ||
|
||
// How to fix it section | ||
|
||
== How to fix it | ||
|
||
=== Code examples | ||
|
||
include::../common/fix/code-rationale.adoc[] | ||
|
||
==== Noncompliant code example | ||
|
||
[source,go,diff-id=1,diff-type=noncompliant] | ||
---- | ||
import ( | ||
"crypto/sha256" | ||
"golang.org/x/crypto/pbkdf2" | ||
) | ||
func example(password []byte) { | ||
pbkdf2.Key(password, []byte("fixedSalt"), 4096, 32, sha256.New) // Noncompliant | ||
} | ||
---- | ||
|
||
==== Compliant solution | ||
|
||
[source,go,diff-id=1,diff-type=compliant] | ||
---- | ||
import ( | ||
"crypto/rand" | ||
"crypto/sha256" | ||
"golang.org/x/crypto/pbkdf2" | ||
) | ||
func example(password []byte) { | ||
randomSalt := make([]byte, 32) | ||
rand.Read(randomSalt) | ||
pbkdf2.Key(password, randomSalt, 4096, 32, sha256.New) | ||
} | ||
---- | ||
|
||
=== How does this work? | ||
|
||
include::../common/fix/salt.adoc[] | ||
|
||
Here, the compliant code example ensures the salt is random and has a sufficient | ||
length by calling the `crypto.rand.Read` function. This function internally | ||
uses a cryptographically secure pseudo-random number generator. | ||
|
||
|
||
== Resources | ||
|
||
include::../common/resources/standards.adoc[] | ||
|
||
ifdef::env-github,rspecator-view[] | ||
|
||
''' | ||
== Implementation Specification | ||
(visible only on this page) | ||
|
||
include::../message.adoc[] | ||
|
||
endif::env-github,rspecator-view[] |
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
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
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
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,50 @@ | ||
== How to fix it in aiohttp | ||
|
||
=== Code examples | ||
|
||
include::../../common/fix/code-rationale.adoc[] | ||
|
||
==== Noncompliant code example | ||
|
||
[source,python,diff-id=31,diff-type=noncompliant] | ||
---- | ||
from fastapi import FastAPI | ||
import aiohttp | ||
app = FastAPI() | ||
@app.get('/example') | ||
async def example(url: str): | ||
async with aiohttp.request('GET', url) as response: # Noncompliant | ||
return {"response": await response.text()} | ||
---- | ||
|
||
==== Compliant solution | ||
|
||
[source,python,diff-id=31,diff-type=compliant] | ||
---- | ||
from fastapi import FastAPI | ||
from fastapi.responses import JSONResponse | ||
import aiohttp | ||
from urllib.parse import urlparse | ||
DOMAINS_ALLOWLIST = ['trusted1.example.com', 'trusted2.example.com']; | ||
app = FastAPI() | ||
@app.get('/example') | ||
async def example(url: str): | ||
if urlparse(url).hostname not in DOMAINS_ALLOWLIST: | ||
return JSONResponse({"error": f"URL {url} is not whitelisted."}, 400) | ||
async with aiohttp.request('GET', url.unicode_string()) as response: | ||
return {"response": await response.text()} | ||
---- | ||
|
||
=== How does this work? | ||
|
||
include::../../common/fix/pre-approved-list.adoc[] | ||
|
||
The compliant code example uses such an approach. | ||
|
||
=== 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
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,2 @@ | ||
{ | ||
} |
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,65 @@ | ||
include::../description.adoc[] | ||
|
||
include::../ask-yourself.adoc[] | ||
|
||
include::../recommended.adoc[] | ||
|
||
== Sensitive Code Example | ||
|
||
Examples of sensitive file creation: | ||
|
||
[source,go] | ||
---- | ||
file, _ = os.Create("/tmp/tempfile.txt") // Sensitive | ||
file, _ = os.Create(os.TempDir()+"/tempfile.txt") // Sensitive | ||
file, _ := os.OpenFile("/tmp/tempfile.txt", os.O_CREATE, 0755) // Sensitive | ||
os.WriteFile("/tmp/tempfile.txt", []byte{"sensitive"}, 0755) // Sensitive | ||
---- | ||
|
||
Example of sensitive directory creation: | ||
|
||
[source,go] | ||
---- | ||
tempdir := "/tmp/tempdir/" | ||
os.Mkdir(tempdir, 0755) // Sensitive | ||
file, _ := os.Create("/tmp/tempdir/tempfile.txt") | ||
---- | ||
|
||
== Compliant Solution | ||
|
||
Compliant temporary file creation: | ||
|
||
[source,go] | ||
---- | ||
file, _ := os.CreateTemp("", "example-pattern") | ||
---- | ||
|
||
Compliant temporary directory creation: | ||
|
||
[source,go] | ||
---- | ||
dir, _ := os.MkdirTemp("", "example-directory") | ||
filename := filepath.Join(dir, "tempfile.txt") | ||
file, _ := os.Create(filename) | ||
---- | ||
|
||
include::../see.adoc[] | ||
|
||
ifdef::env-github,rspecator-view[] | ||
|
||
''' | ||
== Implementation Specification | ||
(visible only on this page) | ||
|
||
include::../message.adoc[] | ||
|
||
''' | ||
== Comments And Links | ||
(visible only on this page) | ||
|
||
include::../comments-and-links.adoc[] | ||
|
||
endif::env-github,rspecator-view[] |
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,2 @@ | ||
{ | ||
} |
Oops, something went wrong.