From 6a025d4b30145479a024ba0e8c26b98a4e61142a Mon Sep 17 00:00:00 2001 From: Raja Boujbel Date: Fri, 24 Mar 2023 15:36:49 +0100 Subject: [PATCH] filename: add might_escape function --- master_changes.md | 1 + src/core/opamFilename.ml | 10 ++++++++++ src/core/opamFilename.mli | 3 +++ 3 files changed, 14 insertions(+) diff --git a/master_changes.md b/master_changes.md index 21bc114fe8b..c566d454d37 100644 --- a/master_changes.md +++ b/master_changes.md @@ -246,3 +246,4 @@ users) ## opam-core * `OpamStd.Env`: add `env_string_list` for parsing string list environment variables (comma separated) [#5682 @desumn] * `OpamHash`: export `compare_kind` [#5561 @rjbou] + * `OpamFilename`: add `might_escape` to check if a path is escapable, ie contains `..` [#5561 @rjbou] diff --git a/src/core/opamFilename.ml b/src/core/opamFilename.ml index 27c5dbf59bf..e94fbbaa497 100644 --- a/src/core/opamFilename.ml +++ b/src/core/opamFilename.ml @@ -9,6 +9,16 @@ (* *) (**************************************************************************) +let might_escape ~sep path = + let sep = + match sep with + | `Unix -> Re.char '/' + | `Windows -> Re.alt Re.[ char '\\'; char '/' ] + | `Unspecified -> Re.str Filename.dir_sep + in + List.exists (String.equal Filename.parent_dir_name) + Re.(split (compile sep) path) + module Base = struct include OpamStd.AbstractString diff --git a/src/core/opamFilename.mli b/src/core/opamFilename.mli index 44b9e3a467c..f27f832360a 100644 --- a/src/core/opamFilename.mli +++ b/src/core/opamFilename.mli @@ -12,6 +12,9 @@ (** Higher level file and directory name manipulation AND file operations, wrappers on OpamSystem using the filename type *) +(* Returns [true] if string contains '..' between directory separators *) +val might_escape: sep:[`Unix | `Windows | `Unspecified ] -> string -> bool + (** Basenames *) module Base: sig include OpamStd.ABSTRACT