Skip to content

Commit ba36a39

Browse files
committed
applying requested changes
Signed-off-by: Helge Wehder <[email protected]>
1 parent 1ec0689 commit ba36a39

File tree

2 files changed

+16
-25
lines changed

2 files changed

+16
-25
lines changed

docs/Secure-Coding-Guide-for-Python/CWE-693/CWE-184/README.md

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Avoid Incomplete 'deny lists' that can lead to security vulnerabilities such as
66

77
The `noncompliant01.py` code demonstrates the difficult handling of exclusion lists in a multi language support use case. `UTF-8` has __1,112,064__ mappings between `8-32` bit values and printable characters such as `` known as "code points".
88

9-
The `noncompliant01.py` `filterString()` method attempts to search for disallowed input is leading to an incomplete list of disallowed input due to the non-English character `` in `<script生>`.
9+
The `noncompliant01.py` `filterString()` method attempts to search for disallowed inputs and fails to find the `script` tag due to the non-English character `` in `<script生>`.
1010

1111
*[noncompliant01.py](noncompliant01.py):*
1212

@@ -48,22 +48,12 @@ names = [
4848
for name in names:
4949
print(name)
5050
filter_string(name)
51-
```
52-
53-
The `noncompliant01.py` code will print all lines of strings including the one `<script生>`. Different ways and sequencing of canonicalizing or normalizing the user provided data as explained in [CWE-180: Incorrect Behavior Order: Validate Before Canonicalize](https://github.com/ossf/wg-best-practices-os-developers/tree/main/docs/Secure-Coding-Guide-for-Python/CWE-707/CWE-180), can turn `<script生>` into `<script>` either in the backend, or on the clients front-end browser.
5451

55-
__Example `noncompliant01.py` output:__
56-
57-
```bash
58-
YES 毛泽东先生
59-
YES dash-
60-
NOK <script﷯>
61-
NOK <script生>
6252
```
6353

6454
## Compliant Solution
6555

66-
The `compliant01.py` uses an allow list instead of deny list and prevents the use of unwanted characters by raising an exception even without canonicalize. The missing canonicalize in `compliant01.py` according to [CWE-180: Incorrect Behavior Order: Validate Before Canonicalize](https://github.com/ossf/wg-best-practices-os-developers/tree/main/docs/Secure-Coding-Guide-for-Python/CWE-707/CWE-180), and must be added in order to make logging or displaying them safe!
56+
The `compliant01.py` uses an allow list instead of a deny list and prevents the use of unwanted characters by raising an exception even without canonicalization. The missing canonicalization in `compliant01.py` according to [CWE-180: Incorrect Behavior Order: Validate Before Canonicalize](https://github.com/ossf/wg-best-practices-os-developers/tree/main/docs/Secure-Coding-Guide-for-Python/CWE-707/CWE-180) must be added in order to make logging or displaying them safe!
6757

6858
*[compliant01.py](compliant01.py):*
6959

@@ -98,10 +88,10 @@ def filter_string(input_string: str):
9888
# attempting to exploit above code example
9989
#####################
10090
names = [
101-
"毛泽东先生",
102-
"dash-",
103-
"<script" + "\ufdef" + ">",
104-
"<script生>",
91+
"YES 毛泽东先生",
92+
"YES dash-",
93+
"NOK <script" + "\ufdef" + ">",
94+
"NOK <script生>",
10595
]
10696
for name in names:
10797
print(name)
@@ -115,13 +105,14 @@ __Example compliant01.py output:__
115105

116106
```bash
117107
/wg-best-practices-os-developers/docs/Secure-Coding-Guide-for-Python/CWE-693/CWE-184/compliant01.py
118-
毛泽东先生
119-
dash-
120-
<script﷯>
108+
$ python3 compliant01.py
109+
YES 毛泽东先生
110+
YES dash-
111+
NOK <script﷯>
121112
Traceback (most recent call last):
122-
File "/wg-best-practices-os-developers/docs/Secure-Coding-Guide-for-Python/CWE-693/CWE-184/compliant01.py", line 38, in <module>
113+
File "/workspace/wg-best-practices-os-developers/docs/Secure-Coding-Guide-for-Python/CWE-693/CWE-184/compliant01.py", line 38, in <module>
123114
filter_string(name)
124-
File "/wg-best-practices-os-developers/docs/Secure-Coding-Guide-for-Python/CWE-693/CWE-184/compliant01.py", line 23, in filter_string
115+
File "/workspace/wg-best-practices-os-developers/docs/Secure-Coding-Guide-for-Python/CWE-693/CWE-184/compliant01.py", line 23, in filter_string
125116
raise ValueError("Invalid input tag")
126117
ValueError: Invalid input tag
127118

docs/Secure-Coding-Guide-for-Python/CWE-693/CWE-184/compliant01.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ def filter_string(input_string: str):
2828
# attempting to exploit above code example
2929
#####################
3030
names = [
31-
"毛泽东先生",
32-
"dash-",
33-
"<script" + "\ufdef" + ">",
34-
"<script生>",
31+
"YES 毛泽东先生",
32+
"YES dash-",
33+
"NOK <script" + "\ufdef" + ">",
34+
"NOK <script生>",
3535
]
3636
for name in names:
3737
print(name)

0 commit comments

Comments
 (0)