diff --git a/changelog.d/+doctor-foreign-relative-entries.bugfix.md b/changelog.d/+doctor-foreign-relative-entries.bugfix.md new file mode 100644 index 0000000..76a70d6 --- /dev/null +++ b/changelog.d/+doctor-foreign-relative-entries.bugfix.md @@ -0,0 +1,3 @@ +`eb-local doctor` and `build.clear_foreign_prefixes` treat every +relative path-list entry and every empty one as outside the approved +prefixes. An empty entry is reported as `''`. diff --git a/docs/orgmode/reference/cli.org b/docs/orgmode/reference/cli.org index bcf885e..b9e31dd 100644 --- a/docs/orgmode/reference/cli.org +++ b/docs/orgmode/reference/cli.org @@ -390,6 +390,7 @@ With =--workspace PATH=, it also creates, writes, renames, executes and removes It reports the file-system type, whether the storage is node-local or shared, free bytes and inodes, a =noexec= mount, and whether the effective =XDG_CACHE_HOME= (or =$HOME/.cache= when unset) is inside the workspace. The check does not create a build workspace and never starts a build. It names an entry of =PERL5LIB=, =PYTHONPATH=, =R_LIBS*=, =CMAKE_PREFIX_PATH= or =PKG_CONFIG_PATH=, and a compiler-wrapper variable (=CC=, =CXX=, =FC=, =F77=, =F90=, =F95=, =CPP=, the =OMPI_=, =MPICH_= and =I_MPI_= forms, =RUSTC_WRAPPER=, =CCACHE_*=, =SCCACHE_*= and =CMAKE_*_COMPILER_LAUNCHER=), that resolves outside the approved prefixes. +A relative entry and an empty entry in those lists are foreign too, because the approved prefixes are absolute paths. =doctor= shows an empty entry as =''=. A build keeps that entry unless =build.clear_foreign_prefixes= is set. It reports the path =/etc/localtime= resolves to, warns when that file is its own bind mount, and warns about a host library directory a container runtime injected, including =/.singularity.d/libs= and a directory named by =APPTAINER_CONTAINLIBS= or =SINGULARITY_CONTAINLIBS=. With =--cvmfs=, it also checks the CernVM-FS repository named by =EESSI_CVMFS_REPO=, or =/cvmfs/software.eessi.io=. diff --git a/docs/orgmode/reference/environment.org b/docs/orgmode/reference/environment.org index 64a407b..0bc3167 100644 --- a/docs/orgmode/reference/environment.org +++ b/docs/orgmode/reference/environment.org @@ -89,7 +89,7 @@ The script a build runs gets eb-local's environment without the variables =env= With =build.clear_foreign_prefixes= or =EB_LOCAL_BUILD_CLEAR_FOREIGN_PREFIXES= set, it also drops entries of =PERL5LIB=, =PYTHONPATH=, =R_LIBS*=, =CMAKE_PREFIX_PATH= and =PKG_CONFIG_PATH= that lie outside the approved prefixes, and unsets a compiler-wrapper variable that resolves outside them. Off, which is the default, a build keeps those variables. The approved prefixes are the EESSI repository, the install prefix, the four EESSI install variables, =doctor.approved_prefixes= and eb-local's RPATH wrapper directory. -=eb-local doctor= names each foreign entry either way. +A relative entry and an empty entry in those lists count as outside the approved prefixes. =eb-local doctor= names each foreign entry either way. It sets these, where =W= is the workspace the build makes in its job's node-local mount: | Variable | Value | diff --git a/src/foreign.zig b/src/foreign.zig index 77d425a..1145f06 100644 --- a/src/foreign.zig +++ b/src/foreign.zig @@ -166,12 +166,19 @@ fn commandPath(value: []const u8) []const u8 { const Split = struct { kept: []const []const u8, foreign: []const []const u8 }; +/// A path-list entry is approved only when it is absolute and lies inside an +/// approved prefix. A relative entry, and an empty one, is foreign. The site +/// file's approved prefixes are absolute paths, and a build runs in its own +/// directory, so these entries stay unresolved. fn splitList(a: Allocator, value: []const u8, prefixes: []const []const u8) Allocator.Error!Split { var kept: std.ArrayList([]const u8) = .empty; var foreign: std.ArrayList([]const u8) = .empty; var it = std.mem.splitScalar(u8, value, ':'); while (it.next()) |entry| { - if (entry.len == 0) continue; + if (entry.len == 0 or entry[0] != '/') { + try foreign.append(a, entry); + continue; + } if (inside(a, entry, prefixes)) try kept.append(a, entry) else try foreign.append(a, entry); } return .{ .kept = kept.items, .foreign = foreign.items }; @@ -245,7 +252,12 @@ fn appendList(a: Allocator, out: *std.ArrayList(Finding), name: []const u8, valu const split = try splitList(a, value, prefixes); if (split.foreign.len == 0) return false; const code: Code = if (clear) .list_drop else .list_keep; - for (split.foreign) |entry| try out.append(a, .{ .code = code, .name = name, .path = entry }); + for (split.foreign) |entry| { + // The locked report template prints the entry as-is. An empty entry + // is shown as '', the same quotes a shell uses for an empty word. + const shown = if (entry.len == 0) "''" else entry; + try out.append(a, .{ .code = code, .name = name, .path = shown }); + } return true; } diff --git a/tests/test_foreign_cli.py b/tests/test_foreign_cli.py index 5e2ea6a..56e6ffe 100644 --- a/tests/test_foreign_cli.py +++ b/tests/test_foreign_cli.py @@ -22,9 +22,9 @@ def setUp(self): "R_LIBS", "R_LIBS_USER", "R_LIBS_SITE", "APPTAINER_CONTAINLIBS", "SINGULARITY_CONTAINLIBS"): self.env.pop(name, None) - def run_tool(self, args, **extra): + def run_tool(self, args, cwd=None, **extra): argv, env = binary.command(self.script, args, dict(self.env, **extra)) - return __import__("subprocess").run(argv, env=env, cwd=self.root, capture_output=True, text=True) + return __import__("subprocess").run(argv, env=env, cwd=cwd or self.root, capture_output=True, text=True) def test_doctor_names_a_foreign_entry_and_keeps_an_approved_one(self): run = self.run_tool(["doctor"], PERL5LIB=f"/opt/host/perl5:{self.prefix}/lib", @@ -172,6 +172,34 @@ def test_symlinked_approved_prefix_is_kept_and_escape_symlink_is_flagged_and_scr seen = json.loads((self.root / "eb.json").read_text()) self.assertEqual(seen["paths"]["PYTHONPATH"], f"{sym_lib}:{real_lib}") + def test_a_relative_or_empty_path_list_entry_is_foreign(self): + self.prefix.mkdir(parents=True, exist_ok=True) + locale = {"LC_ALL": "C.UTF-8", "LANG": "C.UTF-8"} + warn = ("warn PYTHONPATH entry {0} is outside the approved prefixes; " + "a build keeps it until build.clear_foreign_prefixes is set") + + # doctor is started in the prefix. The build runs in its own directory, + # so the relative entry stays foreign. + inside = self.run_tool(["doctor"], cwd=self.prefix, PYTHONPATH="lib", **locale) + self.assertEqual(inside.returncode, 0, inside.stdout + inside.stderr) + self.assertIn(warn.format("lib"), inside.stdout) + + empty = self.run_tool(["doctor"], cwd=self.root, PYTHONPATH=":", **locale) + self.assertEqual(empty.returncode, 0, empty.stdout + empty.stderr) + self.assertIn(warn.format("''"), empty.stdout) + + climbed = self.run_tool(["doctor"], cwd=self.prefix, PYTHONPATH="lib/../lib", **locale) + self.assertEqual(climbed.returncode, 0, climbed.stdout + climbed.stderr) + self.assertIn(warn.format("lib/../lib"), climbed.stdout) + + self.env["PYTHONPATH"] = f"{self.prefix}/lib:" + self.env["CONTROL_PATH_NAMES"] = "PYTHONPATH" + self.env.update(locale) + cleared = self.run_wrapper(["recipe.eb"], EB_LOCAL_BUILD_CLEAR_FOREIGN_PREFIXES="true") + self.assertEqual(cleared.returncode, 0, cleared.stdout + cleared.stderr) + seen = json.loads((self.root / "eb.json").read_text()) + self.assertEqual(seen["paths"]["PYTHONPATH"], f"{self.prefix}/lib") + if __name__ == "__main__": unittest.main()