Files
coreutils/tests/misc/tty-quoting.sh
T
Pádraig Brady 67b6801ad3 cksum: use more defensive file name escaping
Similarly to commit v9.10-115-gb3fe24213, which applied
the same quoting mechanism to --check operation,
also quote file names when generating checksums to tty.
This change gives these advantages:

  1. Avoids dumping arbitrary data to vulnerable terminals
  2. Avoids visual deception with ansi codes with unknown file names
  3. More secure if users copy and paste file names from output

* doc/coreutils.texi (sum invocation): Mention the tty quoting
and reference the details from ...
(cksum output modes): Describe the two forms of quoting used.
* tests/misc/tty-quoting.sh: Add test cases.
* NEWS: Mention the improvement.

Discovered by Anthropic's OSS Scanner, as bug ANT-2026-Z25BP6MW.
2026-11-06 23:55:00 +00:00

85 lines
2.8 KiB
Bash
Executable File

#!/bin/sh
# Test quoting output when standard output is a terminal.
# Copyright (C) 2026 Free Software Foundation, Inc.
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
print_ver_ basename cksum dirname du ls readlink realpath printf test
require_strace_ ioctl
touch 'b ar' || framework_failure_
ln -s 'b ar' 'f oo' || framework_failure_
# Induce terminal probes to return true.
# Also ensure a single ioctl(TCGETS) is intercepted.
run_tty_ ()
{
strace --quiet=all -o run_tty.strace -e trace=ioctl \
-e inject=ioctl:retval=0 "$@" &&
# Return status of these also so commands' tty detection
# is correlated with that of the initial 'test -t 1' probe
test "$(wc -l < run_tty.strace)" = 1 &&
grep 'ioctl.*TCGETS' run_tty.strace >/dev/null
}
run_tty_ test -t 1 ||
skip_ 'strace cannot make standard output appear to be a terminal'
# We're redirecting to file so this doesn't happen on glibc
# but be defensive on systems that might probe in this case.
run_tty_ printf foo >printf.t &&
skip_ 'libc buffering induced a tty probe'
for cmd in basename du dirname 'ls -w0' readlink 'realpath --relative-to=.' \
printenv; do
test "$cmd" = 'du' && field=2 || field=1
case "$cmd" in
dirname) file='f oo/.' ;;
printenv) file=TEST_ENV1 ;;
*) file='f oo' ;;
esac
TEST_ENV1='f oo' run_tty_ $cmd "$file" >quoted.t || fail=1
cut -f$field- quoted.t >quoted || framework_failure_
# Note ls theoretically doesn't need isatty() for a specified QUOTING_STYLE
# but it does need it to determine appropriate output format.
TEST_ENV1='f oo' QUOTING_STYLE=literal run_tty_ $cmd "$file" \
>unquoted.t || fail=1
cut -f$field- unquoted.t >unquoted || framework_failure_
env printf '%q\n' "$(cat unquoted)" >printf_quoted || framework_failure_
compare quoted printf_quoted || fail=1
done
for cmd in 'cksum -a sha1 --untagged' 'cksum -a sha1'; do
file='f oo'
run_tty_ $cmd "$file" >full_quoted || fail=1
# Ensure shell quoting found
cut -d "'" -f2 full_quoted >unquoted || framework_failure_
test "$file" = "$(cat unquoted)" || fail=1
# Ensure unquoted doesn't match quoted
QUOTING_STYLE=literal run_tty_ $cmd "$file" >full_unquoted || fail=1
compare full_quoted full_unquoted && fail=1
done
Exit $fail