Skip to content

Commit 685c6a9

Browse files
committed
update
1 parent 0d66976 commit 685c6a9

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

security/openfile.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ def get_file_safe(user_input: str, safe_directory: str = "data_folder") -> str:
5757

5858
Neither `open()` nor `pathlib` automatically “jails” execution to a specific directory.
5959

60+
Overview:
61+
62+
| Feature | open(path) | Path(path).read_text() |
63+
|-----------------------|--------------------------------|----------------------------------|
64+
| Path Traversal Risk | High | High |
65+
| Encoding Handling | Implicit (OS-dependent) | **Explicit (secure default)** |
66+
| Logic Errors | **Easy to forget `f.close()`** | Handles closing automatically |
67+
| Input Sanitisation | None | None |
68+
69+
70+
6071
`pathlib.Path` is generally considered more secure in a broad sense because it encourages better coding practices that reduce accidental errors—though not solely because of path security. Using `pathlib` is the **modern approach** to file handling in Python.
6172

6273
Key advantages include:

0 commit comments

Comments
 (0)