Skip to content

Commit ef595d3

Browse files
authored
adding CWE-209 (#945)
Adding documentation as part #531 Fixed linter errors Fixed the grammatical error pointed out @dwiley258 --------- Signed-off-by: edanhub <[email protected]> Signed-off-by: Hubert Daniszewski <[email protected]>
1 parent c8e13db commit ef595d3

File tree

6 files changed

+536
-1
lines changed

6 files changed

+536
-1
lines changed

docs/Secure-Coding-Guide-for-Python/CWE-664/CWE-209/README.md

Lines changed: 412 additions & 1 deletion
Large diffs are not rendered by default.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# SPDX-FileCopyrightText: OpenSSF project contributors
2+
# SPDX-License-Identifier: MIT
3+
""" Compliant Code Example """
4+
5+
from pathlib import Path
6+
import random
7+
import logging
8+
import os
9+
10+
11+
def file_reader(args: list):
12+
"""
13+
Compliant example demonstrates split and filter error to the user
14+
It will not go into details on:
15+
- Proper logging
16+
- Proper exception handling for each exception scenario.
17+
"""
18+
filepath = Path(Path.home(), args[0])
19+
# To avoid path traversal attacks,
20+
# use the realpath method
21+
filepath = Path(os.path.realpath(filepath))
22+
# TODO: follow CWE-180: Incorrect Behavior Order: Validate Before Canonicalize.
23+
# Depending on the use case, it can include removing special characters
24+
# from the filename, ensuring it adheres to a predefined regex, etc.
25+
try:
26+
# Restrict provided filepath to a chosen directory
27+
# and throw an exception if user attempt to access confidential areas
28+
if Path.home() not in filepath.parents:
29+
raise PermissionError("Invalid file")
30+
_ = filepath.read_text(encoding='utf8')
31+
except (PermissionError, IsADirectoryError):
32+
error_id = f"{random.getrandbits(64):16x}"
33+
34+
print("***** Backend server-side logging: *****")
35+
logging.exception("ERROR %s", error_id)
36+
37+
# TODO: handle the exception in accordance with
38+
# - CWE-390: Detection of Error Condition without Action
39+
# TODO: log the error with a unique error_id and apply:
40+
# - CWE-117: Improper Output Neutralization for Logs
41+
# - CWE-532: Insertion of Sensitive Information into Log File
42+
43+
# Present a simplified error to the client
44+
print("\n***** Frontend 'client' error: *****")
45+
print(f"ERROR {error_id}: Unable to retrieve file '{filepath.stem}'")
46+
47+
48+
#####################
49+
# Exploiting above code example
50+
#####################
51+
file_reader(["Documents"])
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# SPDX-FileCopyrightText: OpenSSF project contributors
2+
# SPDX-License-Identifier: MIT
3+
""" Non-compliant Code Example """
4+
5+
from pathlib import Path
6+
7+
8+
class FileReader:
9+
""" Class that reads files"""
10+
def __init__(self, args: list[str]):
11+
path = Path(Path.home(), args[0])
12+
fh = open(path, 'r', encoding="utf-8")
13+
fh.readlines()
14+
15+
16+
#####################
17+
# exploiting above code example
18+
#####################
19+
fr = FileReader(["Documents"])
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# SPDX-FileCopyrightText: OpenSSF project contributors
2+
# SPDX-License-Identifier: MIT
3+
""" Non-compliant Code Example """
4+
5+
from pathlib import Path
6+
import sys
7+
8+
9+
class FileReader:
10+
""" Class that reads files"""
11+
def __init__(self, args: list):
12+
path = Path(Path.home(), args[0])
13+
try:
14+
fh = open(path, 'r', encoding="utf-8")
15+
fh.readlines()
16+
except OSError as e:
17+
# TODO: log the original exception
18+
# For more details, check CWE-693/CWE-778: Insufficient Logging
19+
20+
# Throw a generic exception instead
21+
sys.tracebacklimit = 0
22+
raise Exception("Unable to retrieve file " + str(e.filename)) from None
23+
24+
25+
#####################
26+
# exploiting above code example
27+
#####################
28+
fr = FileReader(["Documents"])
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# SPDX-FileCopyrightText: OpenSSF project contributors
2+
# SPDX-License-Identifier: MIT
3+
""" Non-compliant Code Example """
4+
5+
from pathlib import Path
6+
import sys
7+
8+
9+
class FileReader:
10+
""" Class that reads files"""
11+
def __init__(self, args: list):
12+
path = Path(Path.home(), args[0])
13+
try:
14+
file_handle = open(path, 'r', encoding="utf-8")
15+
file_handle.readlines()
16+
except (PermissionError, FileNotFoundError, IsADirectoryError):
17+
# Re-throw exception without details
18+
sys.tracebacklimit = 0
19+
raise BaseException() from None
20+
21+
22+
#####################
23+
# exploiting above code example
24+
#####################
25+
fr = FileReader(["Documents"])

docs/Secure-Coding-Guide-for-Python/readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ It is __not production code__ and requires code-style or python best practices t
5151
|[CWE-134: Use of Externally-Controlled Format String](CWE-664/CWE-134/README.md)|[CVE-2022-27177](https://www.cvedetails.com/cve/CVE-2022-27177/),<br/>CVSSv3.1: __9.8__,<br/>EPSS: __00.37__ (01.12.2023)|
5252
|[CWE-197: Numeric Truncation Error](CWE-664/CWE-197/README.md)||
5353
|[CWE-197: Control rounding when converting to less precise numbers](CWE-664/CWE-197/01/README.md)||
54+
|[CWE-209: Generation of Error Message Containing Sensitive Information](CWE-664/CWE-209/README.md)|[CVE-2013-0773](https://www.cvedetails.com/cve/CVE-2013-0773/),<br/>CVSSv3.1:__3.3__,<br/>EPSS: __00.95__ (23.11.2023)|
5455
|[CWE-400: Uncontrolled Resource Consumption](CWE-664/CWE-400/README.md)||
5556
|[CWE-409: Improper Handling of Highly Compressed Data (Data Amplification)](CWE-664/CWE-409/README.md)||
5657
|[CWE-410: Insufficient Resource Pool](CWE-664/CWE-410/README.md)||

0 commit comments

Comments
 (0)