diff --git a/CHANGES b/CHANGES index c386379ced..e71f1fc678 100644 --- a/CHANGES +++ b/CHANGES @@ -1,4 +1,8 @@ +3.2.0-dev.862 | 2020-07-06 20:40:44 -0700 + + * GH-1041: Move compress_path to a bif that uses normalize_path (Tim Wojtulewicz, Corelight) + 3.2.0-dev.859 | 2020-07-06 14:55:00 +0000 * Add backtrace() and print_backtrace() BIF functions. (Jon Siwek, diff --git a/VERSION b/VERSION index 826e3cdbc7..1be69263b7 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -3.2.0-dev.859 +3.2.0-dev.862 diff --git a/doc b/doc index 0eb33dbac5..d2380ce104 160000 --- a/doc +++ b/doc @@ -1 +1 @@ -Subproject commit 0eb33dbac55bf5351a72dc8c204ed059fc1bd1e2 +Subproject commit d2380ce1047b4f097e1f2602bfc007d9610236bf diff --git a/scripts/base/utils/paths.zeek b/scripts/base/utils/paths.zeek index fdc9bd5d3d..76b7061ec6 100644 --- a/scripts/base/utils/paths.zeek +++ b/scripts/base/utils/paths.zeek @@ -21,45 +21,6 @@ function extract_path(input: string): string return parts[1]; } -## Compresses a given path by removing '..'s and the parent directory it -## references and also removing dual '/'s and extraneous '/./'s. -## -## dir: a path string, either relative or absolute. -## -## Returns: a compressed version of the input path. -function compress_path(dir: string): string - { - const cdup_sep = /((\/)*([^\/]|\\\/)+)?((\/)+\.\.(\/)*)/; - - local parts = split_string_n(dir, cdup_sep, T, 1); - if ( |parts| > 1 ) - { - # reaching a point with two parent dir references back-to-back means - # we don't know about anything higher in the tree to pop off - if ( parts[1] == "../.." ) - return join_string_vec(parts, ""); - if ( sub_bytes(parts[1], 0, 1) == "/" ) - parts[1] = "/"; - else - parts[1] = ""; - dir = join_string_vec(parts, ""); - return compress_path(dir); - } - - const multislash_sep = /(\/\.?){2,}/; - parts = split_string_all(dir, multislash_sep); - for ( i in parts ) - if ( i % 2 == 1 ) - parts[i] = "/"; - dir = join_string_vec(parts, ""); - - # remove trailing slashes from path - if ( |dir| > 1 && sub_bytes(dir, |dir|, 1) == "/" ) - dir = sub_bytes(dir, 0, |dir| - 1); - - return dir; - } - ## Constructs a path to a file given a directory and a file name. ## ## dir: the directory in which the file lives. diff --git a/src/zeek.bif b/src/zeek.bif index 79ded154ba..d374698a8e 100644 --- a/src/zeek.bif +++ b/src/zeek.bif @@ -5157,3 +5157,14 @@ function to_json%(val: any, only_loggable: bool &default=F, field_escape_pattern %{ return val->ToJSON(only_loggable, field_escape_pattern); %} + +## Compresses a given path by removing '..'s and the parent directory it +## references and also removing dual '/'s and extraneous '/./'s. +## +## dir: a path string, either relative or absolute. +## +## Returns: a compressed version of the input path. +function compress_path%(dir: string%): string + %{ + return zeek::make_intrusive(normalize_path(dir->ToStdString())); + %} \ No newline at end of file diff --git a/testing/btest/Baseline/bifs.compress_path/out b/testing/btest/Baseline/bifs.compress_path/out new file mode 100644 index 0000000000..ad5fdd8dc0 --- /dev/null +++ b/testing/btest/Baseline/bifs.compress_path/out @@ -0,0 +1,2 @@ +../foo +../foo diff --git a/testing/btest/bifs/compress_path.zeek b/testing/btest/bifs/compress_path.zeek new file mode 100644 index 0000000000..a21c7349e7 --- /dev/null +++ b/testing/btest/bifs/compress_path.zeek @@ -0,0 +1,9 @@ +# +# @TEST-EXEC: zeek -b %INPUT >out +# @TEST-EXEC: btest-diff out + +event zeek_init() + { + print compress_path("./../foo"); + print compress_path("././../foo"); + }