From 8644eaa8467992f475c2f48a2819d0fb5bb1a4a8 Mon Sep 17 00:00:00 2001 From: Stephen Sherratt Date: Wed, 26 Oct 2022 15:33:47 +1100 Subject: [PATCH] Prevent crash on absolute path in install stanza and glob_files_rec These cases now result in a user error being raised instead of crashing dune. Signed-off-by: Stephen Sherratt --- CHANGES.md | 3 ++ doc/concepts.rst | 3 ++ doc/dune-files.rst | 3 ++ src/dune_rules/dep_conf_eval.ml | 3 ++ src/dune_rules/dune_file.ml | 14 ++++++- .../test-cases/glob_files_rec.t | 21 ++++++++++ .../test-cases/install-absolute-path-error.t | 41 +++++++++++++++++++ 7 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 test/blackbox-tests/test-cases/install-absolute-path-error.t diff --git a/CHANGES.md b/CHANGES.md index ccf5bccfbcb..43950fa21fd 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -24,6 +24,9 @@ Unreleased - Create a fake socket file `_build/.rpc/dune` on windows to allow rpc clients to connect using the build directory. (#6329, @rgrinberg) +- Prevent crash if absolute paths are used in the install stanza and in + recursive globs. These cases now result in a user error. (#6331, @gridbugs) + 3.5.0 (2022-10-19) ------------------ diff --git a/doc/concepts.rst b/doc/concepts.rst index 7c42e333cb7..6719ed80ce7 100644 --- a/doc/concepts.rst +++ b/doc/concepts.rst @@ -662,6 +662,9 @@ Dune supports globbing files in a single directory via ``(glob_files - anything after the last ``/``, or everything if the glob contains no ``/``, is interpreted using the glob syntax +Absolute paths are permitted in the ``(glob_files ...)`` term only. It's an error to pass +an absolute path (i.e., a path beginning with a ``/``) to ``(glob_files_rec ...)```. + The glob syntax is interpreted as follows: - ``\`` matches exactly ````, even if it's a special character diff --git a/doc/dune-files.rst b/doc/dune-files.rst index d2bee50e07b..161da9a2595 100644 --- a/doc/dune-files.rst +++ b/doc/dune-files.rst @@ -1416,6 +1416,9 @@ installed in. If the section above is documented as "with the executable bit set", they are installed with mode ``0o755`` (``rwxr-xr-x``); otherwise they are installed with mode ``0o644`` (``rw-r--r--``). +Note that all files in the install stanza must be specified by relative paths only. +It is an error to specify files by absolute paths. + Including Files in the Install Stanza ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/src/dune_rules/dep_conf_eval.ml b/src/dune_rules/dep_conf_eval.ml index cc0bad4483e..59ab7b9a204 100644 --- a/src/dune_rules/dep_conf_eval.ml +++ b/src/dune_rules/dep_conf_eval.ml @@ -140,6 +140,9 @@ let rec dep expander = function Other (let loc = String_with_vars.loc s in let* path = Expander.expand_path expander s in + if recursive && not (Path.is_managed path) then + User_error.raise ~loc + [ Pp.textf "Absolute paths in recursive globs are not supported." ]; let files_in = let glob = Path.basename path |> Glob.of_string_exn loc in fun dir -> diff --git a/src/dune_rules/dune_file.ml b/src/dune_rules/dune_file.ml index 33207a5395c..81d533bd23b 100644 --- a/src/dune_rules/dune_file.ml +++ b/src/dune_rules/dune_file.ml @@ -1076,6 +1076,16 @@ module Plugin = struct end module Install_conf = struct + (* Expands a [String_with_vars.t] with a given function, returning the result + unless the result is an absolute path in which case a user error is raised. *) + let expand_str_with_check_for_local_path ~expand_str sw = + Memo.map (expand_str sw) ~f:(fun str -> + (if not (Filename.is_relative str) then + let loc = String_with_vars.loc sw in + User_error.raise ~loc + [ Pp.textf "Absolute paths are not allowed in the install stanza." ]); + str) + module File_entry = struct include Recursive_include.Make @@ -1097,7 +1107,9 @@ module Install_conf = struct let open Memo.O in let* unexpanded = expand_include t ~expand_str ~dir in Memo.List.map unexpanded - ~f:(File_binding.Unexpanded.expand ~dir ~f:expand_str) + ~f: + (File_binding.Unexpanded.expand ~dir + ~f:(expand_str_with_check_for_local_path ~expand_str)) let expand_multi ts ~expand_str ~dir = Memo.List.concat_map ts ~f:(expand ~expand_str ~dir) diff --git a/test/blackbox-tests/test-cases/glob_files_rec.t b/test/blackbox-tests/test-cases/glob_files_rec.t index f7e5f9cb88d..3e51a6ef5be 100644 --- a/test/blackbox-tests/test-cases/glob_files_rec.t +++ b/test/blackbox-tests/test-cases/glob_files_rec.t @@ -82,3 +82,24 @@ Check that generated directories are ignored $ dune build @x +Check that we get a nice error message if we pass and absolute path to `glob_files_rec` +--------------------------------------------------------------------------------------- + +Put $PWD in a file that can be read with the %{read:...} pform, so the underline +in the error message is of consistent length on different systems. + $ printf $PWD > pwd + + $ cat > dune < (rule + > (alias x) + > (deps (glob_files_rec %{read:pwd}/*)) + > (action (echo %{deps}))) + > EOF + + $ dune build @x + File "dune", line 3, characters 23-36: + 3 | (deps (glob_files_rec %{read:pwd}/*)) + ^^^^^^^^^^^^^ + Error: Absolute paths in recursive globs are not supported. + [1] + diff --git a/test/blackbox-tests/test-cases/install-absolute-path-error.t b/test/blackbox-tests/test-cases/install-absolute-path-error.t new file mode 100644 index 00000000000..08728204d07 --- /dev/null +++ b/test/blackbox-tests/test-cases/install-absolute-path-error.t @@ -0,0 +1,41 @@ +Report an error when absolute paths appear in the install stanza + + $ cat >dune-project < (lang dune 3.6) + > (package (name foo)) + > EOF + +Put $PWD in a file that can be read with the %{read:...} pform, so the underline +in the error message is of consistent length on different systems. + $ printf $PWD > pwd + + $ touch foo.txt + + $ cat >dune < (install + > (files %{read:pwd}/foo.txt) + > (section share)) + > EOF + + $ dune build @install + File "dune", line 2, characters 8-27: + 2 | (files %{read:pwd}/foo.txt) + ^^^^^^^^^^^^^^^^^^^ + Error: Absolute paths are not allowed in the install stanza. + [1] + + $ mkdir -p bar + $ touch bar/bar.txt + + $ cat >dune < (install + > (dirs %{read:pwd}/bar) + > (section share)) + > EOF + + $ dune build @install + File "dune", line 2, characters 7-22: + 2 | (dirs %{read:pwd}/bar) + ^^^^^^^^^^^^^^^ + Error: Absolute paths are not allowed in the install stanza. + [1]