From 9d59a48ae26cbe59d8d905be5316e67c14f826a5 Mon Sep 17 00:00:00 2001 From: Christian Kreibich Date: Fri, 13 May 2022 14:18:22 -0700 Subject: [PATCH] Expand build_path() function to handle empty dir arguments gracefully When passing an empty string as a directory, the function would produce filenames starting with a slash even when the given file_name is not an absolute path. Defaulting to the root directory is likely never intended and might conveivably be dangerous. The middle "/" is now skipped also if dir is an empty string. --- scripts/base/utils/paths.zeek | 8 +++++--- testing/btest/Baseline/scripts.base.utils.paths/output | 7 +++++-- testing/btest/scripts/base/utils/paths.test | 6 ++++-- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/scripts/base/utils/paths.zeek b/scripts/base/utils/paths.zeek index 76b7061ec6..9bf1f56f20 100644 --- a/scripts/base/utils/paths.zeek +++ b/scripts/base/utils/paths.zeek @@ -28,11 +28,13 @@ function extract_path(input: string): string ## file_name: the name of the file. ## ## Returns: the concatenation of the directory path and file name, or just -## the file name if it's already an absolute path. +## the file name if it's already an absolute path or dir is empty. function build_path(dir: string, file_name: string): string { - return (file_name == absolute_path_pat) ? - file_name : cat(dir, "/", file_name); + # Avoid introducing "//" into the result: + local sep = ends_with(dir, "/") ? "" : "/"; + return (file_name == absolute_path_pat || dir == "") ? + file_name : cat(dir, sep, file_name); } ## Returns a compressed path to a file given a directory and file name. diff --git a/testing/btest/Baseline/scripts.base.utils.paths/output b/testing/btest/Baseline/scripts.base.utils.paths/output index a75108acf6..8c5497a770 100644 --- a/testing/btest/Baseline/scripts.base.utils.paths/output +++ b/testing/btest/Baseline/scripts.base.utils.paths/output @@ -79,7 +79,10 @@ test build_path_compressed() /usr/local/bro/share/bro/somefile.zeek /usr/local/bro/somefile.zeek =============================== -test build_full_path() +test build_path() =============================== -/home/bro//policy/somefile.zeek +/home/bro/policy/somefile.zeek /usr/local/bro/share/bro/somefile.zeek +policy/somefile.zeek +/usr/local/bro/share/bro/somefile.zeek +/policy/somefile.zeek diff --git a/testing/btest/scripts/base/utils/paths.test b/testing/btest/scripts/base/utils/paths.test index 17b579fefc..44224e1685 100644 --- a/testing/btest/scripts/base/utils/paths.test +++ b/testing/btest/scripts/base/utils/paths.test @@ -50,8 +50,10 @@ print build_path_compressed("/home/bro/", "/usr/local/bro/share/bro/somefile.zee print build_path_compressed("/home/bro/", "/usr/local/bro/share/../../bro/somefile.zeek"); print "==============================="; -print "test build_full_path()"; +print "test build_path()"; print "==============================="; print build_path("/home/bro/", "policy/somefile.zeek"); print build_path("/home/bro/", "/usr/local/bro/share/bro/somefile.zeek"); - +print build_path("", "policy/somefile.zeek"); +print build_path("", "/usr/local/bro/share/bro/somefile.zeek"); +print build_path("/", "policy/somefile.zeek");