From 1daf8d6c1e11c7c2b87da789cba046969d91155a Mon Sep 17 00:00:00 2001
From: Sai Pranav <rajasaipranav0@gmail.com>
Date: Fri, 20 Nov 2026 15:21:28 +0000
Subject: [PATCH v1] pg_waldump: accept archive WAL members only at top level
 or in pg_wal/

member_is_wal_file() was meant to skip WAL files in subdirectories
other than pg_wal/, but it only checked that the member path starts
with "pg_wal".  So a file such as pg_wal.old/000000010000000000000038
was read as WAL.  If that file came first in the archive, the real
pg_wal/ copy was then skipped as a duplicate.

This matters for pg_verifybackup, which runs pg_waldump on base.tar
when the backup has no pg_wal.tar.  A data directory with a
pg_wal.old/ directory holding a segment of the same name from another
cluster made pg_verifybackup check that file instead of the backup's
own WAL, and report a backup with corrupted WAL as verified.

Accept a WAL file only at the top level of the archive or directly in
pg_wal/, and add a test.

Backpatch to 19, where reading WAL from tar archives was added.
---
 src/bin/pg_waldump/archive_waldump.c | 11 ++++++---
 src/bin/pg_waldump/t/001_basic.pl    | 34 ++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+), 3 deletions(-)

diff --git a/src/bin/pg_waldump/archive_waldump.c b/src/bin/pg_waldump/archive_waldump.c
index e0c3aa4d25..fe1809b898 100644
--- a/src/bin/pg_waldump/archive_waldump.c
+++ b/src/bin/pg_waldump/archive_waldump.c
@@ -829,9 +829,14 @@ member_is_wal_file(astreamer_waldump *mystreamer, astreamer_member *member,
 	canonicalize_path(pathname);
 	pathlen = strlen(pathname);
 
-	/* Skip files in subdirectories other than pg_wal/ */
-	if (pathlen > XLOG_FNAME_LEN &&
-		strncmp(pathname, XLOGDIR, strlen(XLOGDIR)) != 0)
+	/*
+	 * Accept only a WAL file at the top level of the archive or directly in
+	 * pg_wal/.  Checking just the prefix would also accept a directory such
+	 * as pg_wal.old/ or pg_wal/archive/ that merely starts with "pg_wal".
+	 */
+	if (pathlen != XLOG_FNAME_LEN &&
+		(pathlen != strlen(XLOGDIR) + 1 + XLOG_FNAME_LEN ||
+		 strncmp(pathname, XLOGDIR "/", strlen(XLOGDIR) + 1) != 0))
 		return false;
 
 	/* WAL file may appear with a full path (e.g., pg_wal/<name>) */
diff --git a/src/bin/pg_waldump/t/001_basic.pl b/src/bin/pg_waldump/t/001_basic.pl
index 7b33efc629..244fdd10a3 100644
--- a/src/bin/pg_waldump/t/001_basic.pl
+++ b/src/bin/pg_waldump/t/001_basic.pl
@@ -509,4 +509,38 @@ for my $scenario (@scenarios)
 	}
 }
 
+# A WAL file in a directory whose name only starts with "pg_wal" is not the
+# server's WAL and must not be read from an archive.
+SKIP:
+{
+	skip "tar command is not available", 1
+	  if !defined $tar || $tar eq '';
+
+	my $lookalike_dir = "$tmp_dir/lookalike";
+	mkdir $lookalike_dir;
+	mkdir "$lookalike_dir/pg_wal.old";
+	copy($node->data_dir . '/pg_wal/' . $start_walfile,
+		"$lookalike_dir/pg_wal.old/$start_walfile")
+	  || die "copy: $!";
+
+	my $cwd = getcwd;
+	chdir($lookalike_dir) || die "chdir: $!";
+	command_ok(
+		[
+			$tar, @tar_p_flags, '-cf', "$tmp_dir/lookalike.tar",
+			"pg_wal.old/$start_walfile"
+		]);
+	chdir($cwd) || die "chdir: $!";
+
+	command_fails_like(
+		[
+			'pg_waldump',
+			'--path' => "$tmp_dir/lookalike.tar",
+			'--start' => $start_lsn,
+			'--limit' => 1,
+		],
+		qr/error: could not find WAL/,
+		'WAL file outside pg_wal/ in archive is ignored');
+}
+
 done_testing();
-- 
2.50.1 (Apple Git-155)

