Protect against decoding errors with subprocess text mode

All these are calling subprocess in 'text mode', where it will
try to decode stdout/stderr using the default encoding (utf-8
for us). If it doesn't decode, subprocess will raise an exception
and kobo doesn't handle it, it just passes it along to us, so
things blow up - see https://pagure.io/releng/issue/12474 . To
avoid this, let's set `errors="replace"`, which tells the decoder
to replace invalid data with ? characters. This way we should get
as much of the output as can be read, and no crashes.

We also replace `universal_newlines=True` with `text=True` as
the latter is shorter, clearer, and what Python 3 subprocess
wants us to use, it considers `universal_newlines` to just be
a backwards-compatibility thing - "The universal_newlines argument
is equivalent to text and is provided for backwards compatibility"

Signed-off-by: Adam Williamson <awilliam@redhat.com>
Merges: https://pagure.io/pungi/pull-request/1812
This commit is contained in:
Adam Williamson 2025-01-15 11:00:33 -08:00 committed by Lubomír Sedlář
parent 98e3b3f8c4
commit 2d16a3af00
17 changed files with 132 additions and 70 deletions

View file

@ -25,7 +25,8 @@ class TestGitRefResolver(unittest.TestCase):
self.assertEqual(url, "https://git.example.com/repo.git?somedir#CAFEBABE")
run.assert_called_once_with(
["git", "ls-remote", "https://git.example.com/repo.git", "HEAD"],
universal_newlines=True,
text=True,
errors="replace",
)
@mock.patch("pungi.util.run")
@ -39,7 +40,8 @@ class TestGitRefResolver(unittest.TestCase):
self.assertEqual(url, "https://git.example.com/repo.git?somedir#CAFEBABE")
run.assert_called_once_with(
GIT_WITH_CREDS + ["ls-remote", "https://git.example.com/repo.git", "HEAD"],
universal_newlines=True,
text=True,
errors="replace",
)
@mock.patch("pungi.util.run")
@ -53,7 +55,8 @@ class TestGitRefResolver(unittest.TestCase):
self.assertEqual(url, "https://git.example.com/repo.git?somedir#CAFEBABE")
run.assert_called_once_with(
["git", "ls-remote", "https://git.example.com/repo.git", "refs/heads/f24"],
universal_newlines=True,
text=True,
errors="replace",
)
def test_resolve_ref_with_commit_id(self):
@ -72,7 +75,8 @@ class TestGitRefResolver(unittest.TestCase):
self.assertEqual(ref, "CAFEBABE")
run.assert_called_once_with(
["git", "ls-remote", "https://git.example.com/repo.git", "master"],
universal_newlines=True,
text=True,
errors="replace",
)
@mock.patch("pungi.util.run")
@ -84,7 +88,8 @@ class TestGitRefResolver(unittest.TestCase):
self.assertEqual(ref, "CAFEBABE")
run.assert_called_once_with(
["git", "ls-remote", "https://git.example.com/repo.git", "HEAD"],
universal_newlines=True,
text=True,
errors="replace",
)
@mock.patch("pungi.util.run")
@ -110,7 +115,8 @@ class TestGitRefResolver(unittest.TestCase):
run.assert_called_once_with(
["git", "ls-remote", "https://git.example.com/repo.git", "HEAD"],
universal_newlines=True,
text=True,
errors="replace",
)
@mock.patch("pungi.util.run")
@ -121,7 +127,8 @@ class TestGitRefResolver(unittest.TestCase):
run.assert_called_once_with(
["git", "ls-remote", "https://git.example.com/repo.git", "HEAD"],
universal_newlines=True,
text=True,
errors="replace",
)
self.assertEqual(url, "https://git.example.com/repo.git?#CAFEBABE")
@ -133,7 +140,8 @@ class TestGitRefResolver(unittest.TestCase):
run.assert_called_once_with(
["git", "ls-remote", "https://git.example.com/repo.git", "HEAD"],
universal_newlines=True,
text=True,
errors="replace",
)
self.assertEqual(url, "git+https://git.example.com/repo.git#CAFEBABE")
@ -153,7 +161,8 @@ class TestGitRefResolver(unittest.TestCase):
"https://git.example.com/repo.git",
"refs/heads/my-branch",
],
universal_newlines=True,
text=True,
errors="replace",
)
self.assertIn("ref does not exist in remote repo", str(ctx.exception))
@ -171,7 +180,8 @@ class TestGitRefResolver(unittest.TestCase):
[
mock.call(
["git", "ls-remote", "https://git.example.com/repo.git", "HEAD"],
universal_newlines=True,
text=True,
errors="replace",
)
]
* 2,
@ -600,7 +610,8 @@ class TestUnmountCmd(unittest.TestCase):
cmd,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
universal_newlines=True,
text=True,
errors="replace",
)
],
)
@ -621,7 +632,8 @@ class TestUnmountCmd(unittest.TestCase):
cmd,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
universal_newlines=True,
text=True,
errors="replace",
)
],
)
@ -643,7 +655,8 @@ class TestUnmountCmd(unittest.TestCase):
cmd,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
universal_newlines=True,
text=True,
errors="replace",
)
]
* 3,
@ -668,7 +681,8 @@ class TestUnmountCmd(unittest.TestCase):
cmd,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
universal_newlines=True,
text=True,
errors="replace",
)
]
* 3,
@ -707,37 +721,43 @@ class TestUnmountCmd(unittest.TestCase):
cmd,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
universal_newlines=True,
text=True,
errors="replace",
),
mock.call(
cmd,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
universal_newlines=True,
text=True,
errors="replace",
),
mock.call(
cmd,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
universal_newlines=True,
text=True,
errors="replace",
),
mock.call(
["ls", "-lA", "/path"],
stderr=subprocess.STDOUT,
stdout=subprocess.PIPE,
universal_newlines=True,
text=True,
errors="replace",
),
mock.call(
["fuser", "-vm", "/path"],
stderr=subprocess.STDOUT,
stdout=subprocess.PIPE,
universal_newlines=True,
text=True,
errors="replace",
),
mock.call(
["lsof", "+D", "/path"],
stderr=subprocess.STDOUT,
stdout=subprocess.PIPE,
universal_newlines=True,
text=True,
errors="replace",
),
]
self.assertEqual(mockPopen.call_args_list, expected)