fix get_file() checking relative paths (#3386)

* fix get_file() handling of relative paths

* aglib get_file test

* fix typo

* lint formatting
pull/3389/head
Will Rosenberg 1 month ago committed by GitHub
parent 39083a134c
commit 3029957148
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -82,10 +82,6 @@ def get_file(path: str, try_local_path: bool = False) -> str:
The local path to the file
"""
has_target_prefix = path.startswith("target:")
has_good_prefix = path.startswith(("/", "./", "../")) or has_target_prefix
if not has_good_prefix:
raise OSError("get_file called with incorrect path", errno.ENOENT)
if has_target_prefix:
path = path[7:] # len('target:') == 7

@ -0,0 +1,19 @@
from __future__ import annotations
import os
import tempfile
from pwndbg.aglib.file import get
def test_get():
# Test different relative and absolute file path prefixes
cwd = os.getcwd()
test_file_prefixes = ["/", "./", f"../{os.path.basename(cwd)}/", ""]
with tempfile.TemporaryDirectory(dir=cwd) as tempdir:
path = os.path.join(tempdir, "test_file")
with open(path, "w") as f:
f.write("test")
for test_prefix in test_file_prefixes:
test_path = path if test_prefix == "/" else test_prefix + os.path.relpath(path)
assert get(test_path) == b"test"
Loading…
Cancel
Save