|
| 1 | +# CWE-184: Incomplete List of Disallowed Input |
| 2 | + |
| 3 | +Avoid Incomplete 'deny lists' that can lead to security vulnerabilities such as cross-site scripting (XSS) by using 'allow lists' instead. |
| 4 | + |
| 5 | +## Non-Compliant Code Example |
| 6 | + |
| 7 | +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". |
| 8 | + |
| 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生>`. |
| 10 | + |
| 11 | +*[noncompliant01.py](noncompliant01.py):* |
| 12 | + |
| 13 | +```python |
| 14 | +# SPDX-FileCopyrightText: OpenSSF project contributors |
| 15 | +# SPDX-License-Identifier: MIT |
| 16 | +"""Compliant Code Example""" |
| 17 | + |
| 18 | +import re |
| 19 | +import sys |
| 20 | + |
| 21 | +if sys.stdout.encoding.lower() != "utf-8": |
| 22 | + sys.stdout.reconfigure(encoding="UTF-8") |
| 23 | + |
| 24 | + |
| 25 | +def filter_string(input_string: str): |
| 26 | + """Normalize and validate untrusted string |
| 27 | +
|
| 28 | + Parameters: |
| 29 | + input_string(string): String to validate |
| 30 | + """ |
| 31 | + # TODO Canonicalize (normalize) before Validating |
| 32 | + |
| 33 | + # validate, exclude dangerous tags: |
| 34 | + for tag in re.findall("<[^>]*>", input_string): |
| 35 | + if tag in ["<script>", "<img", "<a href"]: |
| 36 | + raise ValueError("Invalid input tag") |
| 37 | + |
| 38 | + |
| 39 | +##################### |
| 40 | +# attempting to exploit above code example |
| 41 | +##################### |
| 42 | +names = [ |
| 43 | + "YES 毛泽东先生", |
| 44 | + "YES dash-", |
| 45 | + "NOK <script" + "\ufdef" + ">", |
| 46 | + "NOK <script生>", |
| 47 | +] |
| 48 | +for name in names: |
| 49 | + print(name) |
| 50 | + filter_string(name) |
| 51 | + |
| 52 | +``` |
| 53 | + |
| 54 | +## Compliant Solution |
| 55 | + |
| 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! |
| 57 | + |
| 58 | +*[compliant01.py](compliant01.py):* |
| 59 | + |
| 60 | +```python |
| 61 | +# SPDX-FileCopyrightText: OpenSSF project contributors |
| 62 | +# SPDX-License-Identifier: MIT |
| 63 | +"""Compliant Code Example""" |
| 64 | + |
| 65 | +import re |
| 66 | +import sys |
| 67 | + |
| 68 | +if sys.stdout.encoding.lower() != "utf-8": |
| 69 | + sys.stdout.reconfigure(encoding="UTF-8") |
| 70 | + |
| 71 | + |
| 72 | +def filter_string(input_string: str): |
| 73 | + """Normalize and validate untrusted string |
| 74 | +
|
| 75 | + Parameters: |
| 76 | + input_string(string): String to validate |
| 77 | + """ |
| 78 | + # TODO Canonicalize (normalize) before Validating |
| 79 | + |
| 80 | + # validate, only allow harmless tags |
| 81 | + for tag in re.findall("<[^>]*>", input_string): |
| 82 | + if tag not in ["<b>", "<p>", "</p>"]: |
| 83 | + raise ValueError("Invalid input tag") |
| 84 | + # TODO handle exception |
| 85 | + |
| 86 | + |
| 87 | +##################### |
| 88 | +# attempting to exploit above code example |
| 89 | +##################### |
| 90 | +names = [ |
| 91 | + "YES 毛泽东先生", |
| 92 | + "YES dash-", |
| 93 | + "NOK <script" + "\ufdef" + ">", |
| 94 | + "NOK <script生>", |
| 95 | +] |
| 96 | +for name in names: |
| 97 | + print(name) |
| 98 | + filter_string(name) |
| 99 | + |
| 100 | +``` |
| 101 | + |
| 102 | +The `compliant01.py` detects the unallowed character correctly and throws a `ValueError` exception. An actual production solution would also need to canonicalize and handle the exception correctly. |
| 103 | + |
| 104 | +__Example compliant01.py output:__ |
| 105 | + |
| 106 | +```bash |
| 107 | +/wg-best-practices-os-developers/docs/Secure-Coding-Guide-for-Python/CWE-693/CWE-184/compliant01.py |
| 108 | +$ python3 compliant01.py |
| 109 | +YES 毛泽东先生 |
| 110 | +YES dash- |
| 111 | +NOK <script> |
| 112 | +Traceback (most recent call last): |
| 113 | + File "/workspace/wg-best-practices-os-developers/docs/Secure-Coding-Guide-for-Python/CWE-693/CWE-184/compliant01.py", line 38, in <module> |
| 114 | + filter_string(name) |
| 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 |
| 116 | + raise ValueError("Invalid input tag") |
| 117 | +ValueError: Invalid input tag |
| 118 | + |
| 119 | +``` |
| 120 | + |
| 121 | +According to *Unicode Technical Report #36, Unicode Security Considerations [Davis 2008b]*, `\uFFFD` is usually unproblematic, as a replacement for unwanted or dangerous characters. That is, `\uFFFD` will typically just cause a failure in parsing. Where the output character set is not Unicode, though, this character may not be available. |
| 122 | + |
| 123 | +## Automated Detection |
| 124 | + |
| 125 | +|Tool|Version|Checker|Description| |
| 126 | +|:---|:---|:---|:---| |
| 127 | +|Bandit|1.7.4 on Python 3.10.4|Not Available|| |
| 128 | +|Flake8|8-4.0.1 on Python 3.10.4|Not Available|| |
| 129 | + |
| 130 | +## Related Guidelines |
| 131 | + |
| 132 | +||| |
| 133 | +|:---|:---| |
| 134 | +|[MITRE CWE](http://cwe.mitre.org/)|Pillar: [CWE-693: CWE-693: Protection Mechanism Failure (mitre.org)](https://cwe.mitre.org/data/definitions/693.html)| |
| 135 | +|[MITRE CWE](http://cwe.mitre.org/)|Base : [CWE-184, Incomplete List of Disallowed Inputs (4.13) (mitre.org)](https://cwe.mitre.org/data/definitions/184.html)| |
| 136 | +|[SEI CERT Coding Standard for Java](https://wiki.sei.cmu.edu/confluence/display/java/SEI+CERT+Oracle+Coding+Standard+for+Java)|[IDS11-J. Perform any string modifications before validation](https://wiki.sei.cmu.edu/confluence/display/java/IDS11-J.+Perform+any+string+modifications+before+validation)| |
| 137 | + |
| 138 | +## Bibliography |
| 139 | + |
| 140 | +||| |
| 141 | +|:---|:---| |
| 142 | +|[Unicode 2024]|Unicode 16.0.0 [online]. Available from: [https://www.unicode.org/versions/Unicode16.0.0/](https://www.unicode.org/versions/Unicode16.0.0/) [accessed 20 March 2025] | |
| 143 | +|[Davis 2008b]|Unicode Technical Report #36, Unicode Security Considerations, Section 3.5 "Deletion of Code Points" [online]. Available from: [https://www.unicode.org/reports/tr36/](https://www.unicode.org/reports/tr36/) [accessed 20 March 2025] | |
| 144 | +|[Davis 2008b]|Unicode Technical Report #36, Unicode Security Considerations, Section 3.5 "Deletion of Code Points" [online]. Available from: [https://www.unicode.org/reports/tr36/](https://www.unicode.org/reports/tr36/) [accessed 20 March 2025] | |
0 commit comments