Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
gh-103186: assert in tests that UnsafeMailcapInput warnings are provi…
…ded (GH-103217)

(cherry picked from commit 1724553)

Co-authored-by: Ijtaba Hussain <ijtabahussain@live.com>
  • Loading branch information
TabLand authored and miss-islington committed Sep 2, 2023
commit 915d0ed4a435f3646ba4ebdb9932c63415b327eb
28 changes: 24 additions & 4 deletions Lib/test/test_mailcap.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ def test_subst(self):
(["", "audio/*", "foo.txt"], ""),
(["echo foo", "audio/*", "foo.txt"], "echo foo"),
(["echo %s", "audio/*", "foo.txt"], "echo foo.txt"),
(["echo %t", "audio/*", "foo.txt"], None),
(["echo %t", "audio/wav", "foo.txt"], "echo audio/wav"),
(["echo \\%t", "audio/*", "foo.txt"], "echo %t"),
(["echo foo", "audio/*", "foo.txt", plist], "echo foo"),
Expand Down Expand Up @@ -211,9 +210,6 @@ def test_findmatch(self):
([c, "audio/basic"],
{"key": "description", "filename": fname},
('"An audio fragment"', audio_basic_entry)),
([c, "audio/*"],
{"filename": fname},
(None, None)),
([c, "audio/wav"],
{"filename": fname},
("/usr/local/bin/showaudio audio/wav", audio_entry)),
Expand Down Expand Up @@ -246,6 +242,30 @@ def test_test(self):
]
self._run_cases(cases)

def test_unsafe_mailcap_input(self):
with self.assertWarnsRegex(mailcap.UnsafeMailcapInput,
'Refusing to substitute parameter.*'
'into a shell command'):
unsafe_param = mailcap.subst("echo %{total}",
"audio/wav",
"foo.txt",
["total=*"])
self.assertEqual(unsafe_param, None)

with self.assertWarnsRegex(mailcap.UnsafeMailcapInput,
'Refusing to substitute MIME type'
'.*into a shell'):
unsafe_mimetype = mailcap.subst("echo %t", "audio/*", "foo.txt")
self.assertEqual(unsafe_mimetype, None)

with self.assertWarnsRegex(mailcap.UnsafeMailcapInput,
'Refusing to use mailcap with filename.*'
'Use a safe temporary filename.'):
unsafe_filename = mailcap.findmatch(MAILCAPDICT,
"audio/wav",
filename="foo*.txt")
self.assertEqual(unsafe_filename, (None, None))

def _run_cases(self, cases):
for c in cases:
self.assertEqual(mailcap.findmatch(*c[0], **c[1]), c[2])
Expand Down