bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
From: Bernhard Voelker <mail@bernhard-voelker.de>
To: Paul Eggert <eggert@cs.ucla.edu>, Kamil Dudka <kdudka@redhat.com>
Cc: bug-gnulib <bug-gnulib@gnu.org>
Subject: Re: tests: dis/allow '.' in PATH?
Date: Fri, 26 Nov 2021 01:10:01 +0100	[thread overview]
Message-ID: <cb359cb9-9fae-7a64-2887-d686c34c02c5@bernhard-voelker.de> (raw)
In-Reply-To: <4340739c-a574-65e4-ec60-d27df4f961f8@cs.ucla.edu>

[-- Attachment #1: Type: text/plain, Size: 555 bytes --]

On 11/25/21 00:54, Paul Eggert wrote:
> On 11/24/21 15:03, Bernhard Voelker wrote:
> Something like the following untested code. This removes all relative 
> names from PATH, not just '.'.

Good idea.  Looking at some code from coreutils, I also suggest to
test if the entries exist.

> saved_IFS=$IFS
> IFS=:
> new_PATH=
> for dir in $PATH; do
>    case $dir in
>      /*) new_PATH=$new_PATH${new_PATH:-:}$dir;;
_______________________________________^^
This operator doesn't do what we need here.

PFA the revised patch.

Thanks & have a nice day,
Berny

[-- Attachment #2: 0001-test-framework-sh-remove-unsafe-entries-from-PATH.patch --]
[-- Type: text/x-patch, Size: 1971 bytes --]

From d50912b6c60732476bb2955d947bacb73aaa2d59 Mon Sep 17 00:00:00 2001
From: Bernhard Voelker <mail@bernhard-voelker.de>
Date: Wed, 24 Nov 2021 23:59:00 +0100
Subject: [PATCH] test-framework-sh: remove unsafe entries from PATH

Running tests with '.' in the PATH may yield unspecified results,
and is deemed unsafe per se.  This includes empty entries as well
which are treated like a '.' entry as per POSIX.

* tests/init.sh (setup_): Add snippet to remove relative and non-
accessible entries from the PATH environment variable.
---
 ChangeLog     |  9 +++++++++
 tests/init.sh | 17 +++++++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/ChangeLog b/ChangeLog
index 3e752b238..efbe6c888 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2021-11-25  Bernhard Voelker  <mail@bernhard-voelker.de>
+
+	test-framework-sh: remove unsafe entries from PATH
+	Running tests with '.' in the PATH may yield unspecified results,
+	and is deemed unsafe per se.  This includes empty entries as well
+	which are treated like a '.' entry as per POSIX.
+	* tests/init.sh (setup_): Add snippet to remove relative and non-
+	accessible entries from the PATH environment variable.
+
 2021-11-24  Paul Eggert  <eggert@cs.ucla.edu>
 
 	regex: merge from glibc
diff --git a/tests/init.sh b/tests/init.sh
index 9ef834888..a975592ff 100644
--- a/tests/init.sh
+++ b/tests/init.sh
@@ -426,6 +426,23 @@ setup_ ()
   for sig_ in 1 2 3 13 15; do
     eval "trap 'Exit $(expr $sig_ + 128)' $sig_"
   done
+
+  # Remove relative and non-accessible directories from PATH, including '.'
+  # and Zero-length entries.
+  saved_IFS="$IFS"
+  IFS=:
+  new_PATH=
+  sep_=
+  for dir in $PATH; do
+    case "$dir" in
+      /*) test -d "$dir/." || continue
+          new_PATH="${new_PATH}${sep_}${dir}"
+          sep_=':';;
+    esac
+  done
+  IFS="$saved_IFS"
+  PATH="$new_PATH"
+  export PATH
 }
 
 # This is a stub function that is run upon trap (upon regular exit and
-- 
2.34.0


      reply	other threads:[~2021-11-26  0:10 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-23 22:19 tests: dis/allow '.' in PATH? Bernhard Voelker
2021-11-24  0:10 ` Paul Eggert
2021-11-24  7:24 ` Kamil Dudka
2021-11-24 23:03   ` Bernhard Voelker
2021-11-24 23:54     ` Paul Eggert
2021-11-26  0:10       ` Bernhard Voelker [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=cb359cb9-9fae-7a64-2887-d686c34c02c5@bernhard-voelker.de \
    --to=mail@bernhard-voelker.de \
    --cc=bug-gnulib@gnu.org \
    --cc=eggert@cs.ucla.edu \
    --cc=kdudka@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).