Skip to content

Commit

Permalink
Tests: Solve TODO about better path checks.
Browse files Browse the repository at this point in the history
  • Loading branch information
kayhayen committed Sep 20, 2021
1 parent 438908e commit d44548a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 61 deletions.
112 changes: 52 additions & 60 deletions nuitka/tools/testing/Common.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
getFileContentByLine,
getFileContents,
getFileList,
isPathBelow,
isPathBelowOrSameAs,
makePath,
removeDirectory,
)
Expand Down Expand Up @@ -495,7 +495,7 @@ def displayRuntimeTraces(logger, path):

def checkRuntimeLoadedFilesForOutsideAccesses(loaded_filenames, white_list):
# A lot of special white listing is required.
# pylint: disable=too-many-branches,too-many-statements
# pylint: disable=too-many-branches

result = []

Expand All @@ -522,44 +522,36 @@ def checkRuntimeLoadedFilesForOutsideAccesses(loaded_filenames, white_list):
if ok:
continue

if loaded_filename.startswith(("/etc/", "/usr/etc", "/usr/local/etc")):
continue

if loaded_filename.startswith("/proc/") or loaded_filename == "/proc":
continue

if loaded_filename.startswith("/dev/"):
continue

# TODO: Use this for all.
if isPathBelow("/tmp", loaded_filename):
continue

if loaded_filename.startswith("/run/"):
continue

if loaded_filename.startswith("/sys/"):
continue

if loaded_filename.startswith("/usr/lib/locale/"):
continue

if loaded_filename.startswith("/usr/share/locale/"):
continue

if loaded_filename.startswith("/usr/share/X11/locale/"):
ignore = True
for ignored_dir in (
# System configuration is OK
"/etc",
"/usr/etc",
"/usr/local/etc",
# Runtime user state and kernel information is OK.
"/proc",
"/dev",
"/run",
"/sys",
"/tmp",
# Locals may of course be loaded.
"/usr/lib/locale",
"/usr/share/locale",
"/usr/share/X11/locale",
# Themes may of course be loaded.
"/usr/share/themes",
# Terminal info files are OK too.
"/lib/terminfo",
):
if isPathBelowOrSameAs(ignored_dir, loaded_filename):
ignore = False
break
if not ignore:
continue

# Themes may of course be loaded.
if loaded_filename.startswith("/usr/share/themes"):
continue
if "gtk" in loaded_filename and "/engines/" in loaded_filename:
continue

# Terminal info files are OK too.
if loaded_filename.startswith("/lib/terminfo/"):
continue

# System C libraries are to be expected.
if loaded_basename.startswith(
(
Expand Down Expand Up @@ -1501,31 +1493,31 @@ def checkLoadedFileAccesses(loaded_filenames, current_dir):
if loaded_filename.startswith(current_dir_ext):
continue

if loaded_filename.startswith("/etc/"):
continue

if loaded_filename.startswith("/usr/etc/"):
continue

if loaded_filename.startswith("/proc/") or loaded_filename == "/proc":
continue

if loaded_filename.startswith("/dev/"):
continue

if loaded_filename.startswith("/tmp/") or loaded_filename == "/proc":
continue

if loaded_filename.startswith("/run/"):
continue

if loaded_filename.startswith("/usr/lib/locale/"):
continue

if loaded_filename.startswith("/usr/share/locale/"):
continue

if loaded_filename.startswith("/usr/share/X11/locale/"):
ignore = True
for ignored_dir in (
# System configuration is OK
"/etc",
"/usr/etc",
"/usr/local/etc",
# Runtime user state and kernel information is OK.
"/proc",
"/dev",
"/run",
"/sys",
"/tmp",
# Locals may of course be loaded.
"/usr/lib/locale",
"/usr/share/locale",
"/usr/share/X11/locale",
# Themes may of course be loaded.
"/usr/share/themes",
# Terminal info files are OK too.
"/lib/terminfo",
):
if isPathBelowOrSameAs(ignored_dir, loaded_filename):
ignore = False
break
if not ignore:
continue

# Themes may of course be loaded.
Expand Down
6 changes: 6 additions & 0 deletions nuitka/utils/FileOperations.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,7 @@ def getWindowsDrive(path):


def isPathBelow(path, filename):
"""Is a path inside of a given directory path."""
path = os.path.abspath(path)
filename = os.path.abspath(filename)

Expand All @@ -577,6 +578,11 @@ def isPathBelow(path, filename):
return os.path.relpath(filename, path).split(os.path.sep)[0] != ".."


def isPathBelowOrSameAs(path, filename):
"""Is a path inside of a given directory path or the same path as that directory."""
return isPathBelow(path, filename) or areSamePaths(path, filename)


def getWindowsShortPathName(filename):
"""Gets the short path name of a given long path.
Expand Down
2 changes: 1 addition & 1 deletion tests/onefile/run_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def main():
displayRuntimeTraces(test_logger, binary_filename)

test_logger.warning(
"Should not access these file(s): '%r'." % illegal_accesses
"Should not access these file(s): '%s'." % ",".join(illegal_accesses)
)

search_mode.onErrorDetected(1)
Expand Down

0 comments on commit d44548a

Please sign in to comment.