Add a regression test for the hard link counterpart of the
--no-absolute-filenames handling.  A tar member of type hard link whose
target is an absolute path must have its leading '/' stripped on
extraction, like member names, otherwise the link can escape the
extraction directory.  The symbolic link side is already covered by
symlink-tar-abs.

The test builds a ustar archive with cpio -o from an absolute hard link
target and checks that cpio -itv --no-absolute-filenames reports the
target being stripped.  It passes with the fix in place and fails
without it.

Signed-off-by: Pratik Farkase <[email protected]>
---
 tests/Makefile.am                  |  1 +
 tests/no-abs-filenames-hardlink.at | 44 ++++++++++++++++++++++++++++++
 tests/testsuite.at                 |  1 +
 3 files changed, 46 insertions(+)
 create mode 100644 tests/no-abs-filenames-hardlink.at

diff --git a/tests/Makefile.am b/tests/Makefile.am
index e5096e5..ed7dcc3 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -56,6 +56,7 @@ TESTSUITE_AT = \
  big-block-size.at\
  CVE-2015-1197.at\
  CVE-2019-14866.at\
+ no-abs-filenames-hardlink.at\
  linktime.at\
  linktime01.at\
  symlink-tar-abs.at
diff --git a/tests/no-abs-filenames-hardlink.at 
b/tests/no-abs-filenames-hardlink.at
new file mode 100644
index 0000000..a700e59
--- /dev/null
+++ b/tests/no-abs-filenames-hardlink.at
@@ -0,0 +1,44 @@
+# Process this file with autom4te to create testsuite.  -*- Autotest -*-
+# 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, 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 <http://www.gnu.org/licenses/>.
+
+# --no-absolute-filenames must strip a leading `/' from hard link
+# targets, not only from member names.  A hard link target names another
+# member of the same archive, so an absolute target left intact lets the
+# link escape the extraction directory.  This is the hard link
+# counterpart of the symbolic link behaviour checked in symlink-tar-abs.
+
+AT_SETUP([--no-absolute-filenames strips hard link targets (tar format)])
+AT_KEYWORDS([no-abs-filenames-hardlink])
+
+AT_CHECK([
+mkdir dir
+cd dir
+echo content > file
+ln file hardlink
+cd ..
+
+echo "$(pwd)/dir/file" > filelist
+echo "$(pwd)/dir/hardlink" >> filelist
+cpio -o -H ustar < filelist > test.tar 2>/dev/null
+
+LC_ALL=C cpio -itv --no-absolute-filenames < test.tar 2>&1 \
+    | grep -c "hard link targets"
+],
+[0],
+[1
+])
+
+AT_CLEANUP
diff --git a/tests/testsuite.at b/tests/testsuite.at
index 099c1e9..0619b4b 100644
--- a/tests/testsuite.at
+++ b/tests/testsuite.at
@@ -43,6 +43,7 @@ m4_include([big-block-size.at])
 
 m4_include([CVE-2015-1197.at])
 m4_include([CVE-2019-14866.at])
+m4_include([no-abs-filenames-hardlink.at])
 
 m4_include([linktime.at])
 m4_include([linktime01.at])
-- 
2.43.0


Reply via email to