A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/python/cpython/commit/0a4f650347fdcfd82d094ab2134ca89584f4e877 below:

Make mailcap refuse to match unsafe filenames/types/p… · python/cpython@0a4f650 · GitHub

File tree Expand file treeCollapse file tree 4 files changed

+46

-4

lines changed

Filter options

Expand file treeCollapse file tree 4 files changed

+46

-4

lines changed Original file line number Diff line number Diff line change

@@ -54,6 +54,18 @@ standard. However, mailcap files are supported on most Unix systems.

54 54

use) to determine whether or not the mailcap line applies. :func:`findmatch`

55 55

will automatically check such conditions and skip the entry if the check fails.

56 56 57 +

.. versionchanged:: 3.11

58 + 59 +

To prevent security issues with shell metacharacters (symbols that have

60 +

special effects in a shell command line), ``findmatch`` will refuse

61 +

to inject ASCII characters other than alphanumerics and ``@+=:,./-_``

62 +

into the returned command line.

63 + 64 +

If a disallowed character appears in *filename*, ``findmatch`` will always

65 +

return ``(None, None)`` as if no entry was found.

66 +

If such a character appears elsewhere (a value in *plist* or in *MIMEtype*),

67 +

``findmatch`` will ignore all mailcap entries which use that value.

68 +

A :mod:`warning <warnings>` will be raised in either case.

57 69 58 70

.. function:: getcaps()

59 71 Original file line number Diff line number Diff line change

@@ -2,6 +2,7 @@

2 2 3 3

import os

4 4

import warnings

5 +

import re

5 6 6 7

__all__ = ["getcaps","findmatch"]

7 8

@@ -13,6 +14,11 @@ def lineno_sort_key(entry):

13 14

else:

14 15

return 1, 0

15 16 17 +

_find_unsafe = re.compile(r'[^\xa1-\U0010FFFF\w@+=:,./-]').search

18 + 19 +

class UnsafeMailcapInput(Warning):

20 +

"""Warning raised when refusing unsafe input"""

21 + 16 22 17 23

# Part 1: top-level interface.

18 24

@@ -165,15 +171,22 @@ def findmatch(caps, MIMEtype, key='view', filename="/dev/null", plist=[]):

165 171

entry to use.

166 172 167 173

"""

174 +

if _find_unsafe(filename):

175 +

msg = "Refusing to use mailcap with filename %r. Use a safe temporary filename." % (filename,)

176 +

warnings.warn(msg, UnsafeMailcapInput)

177 +

return None, None

168 178

entries = lookup(caps, MIMEtype, key)

169 179

# XXX This code should somehow check for the needsterminal flag.

170 180

for e in entries:

171 181

if 'test' in e:

172 182

test = subst(e['test'], filename, plist)

183 +

if test is None:

184 +

continue

173 185

if test and os.system(test) != 0:

174 186

continue

175 187

command = subst(e[key], MIMEtype, filename, plist)

176 -

return command, e

188 +

if command is not None:

189 +

return command, e

177 190

return None, None

178 191 179 192

def lookup(caps, MIMEtype, key=None):

@@ -206,14 +219,23 @@ def subst(field, MIMEtype, filename, plist=[]):

206 219

elif c == 's':

207 220

res = res + filename

208 221

elif c == 't':

222 +

if _find_unsafe(MIMEtype):

223 +

msg = "Refusing to substitute MIME type %r into a shell command." % (MIMEtype,)

224 +

warnings.warn(msg, UnsafeMailcapInput)

225 +

return None

209 226

res = res + MIMEtype

210 227

elif c == '{':

211 228

start = i

212 229

while i < n and field[i] != '}':

213 230

i = i+1

214 231

name = field[start:i]

215 232

i = i+1

216 -

res = res + findparam(name, plist)

233 +

param = findparam(name, plist)

234 +

if _find_unsafe(param):

235 +

msg = "Refusing to substitute parameter %r (%s) into a shell command" % (param, name)

236 +

warnings.warn(msg, UnsafeMailcapInput)

237 +

return None

238 +

res = res + param

217 239

# XXX To do:

218 240

# %n == number of parts if type is multipart/*

219 241

# %F == list of alternating type and filename for parts

Original file line number Diff line number Diff line change

@@ -121,7 +121,8 @@ def test_subst(self):

121 121

(["", "audio/*", "foo.txt"], ""),

122 122

(["echo foo", "audio/*", "foo.txt"], "echo foo"),

123 123

(["echo %s", "audio/*", "foo.txt"], "echo foo.txt"),

124 -

(["echo %t", "audio/*", "foo.txt"], "echo audio/*"),

124 +

(["echo %t", "audio/*", "foo.txt"], None),

125 +

(["echo %t", "audio/wav", "foo.txt"], "echo audio/wav"),

125 126

(["echo \\%t", "audio/*", "foo.txt"], "echo %t"),

126 127

(["echo foo", "audio/*", "foo.txt", plist], "echo foo"),

127 128

(["echo %{total}", "audio/*", "foo.txt", plist], "echo 3")

@@ -205,7 +206,10 @@ def test_findmatch(self):

205 206

('"An audio fragment"', audio_basic_entry)),

206 207

([c, "audio/*"],

207 208

{"filename": fname},

208 -

("/usr/local/bin/showaudio audio/*", audio_entry)),

209 +

(None, None)),

210 +

([c, "audio/wav"],

211 +

{"filename": fname},

212 +

("/usr/local/bin/showaudio audio/wav", audio_entry)),

209 213

([c, "message/external-body"],

210 214

{"plist": plist},

211 215

("showexternal /dev/null default john python.org /tmp foo bar", message_entry))

Original file line number Diff line number Diff line change

@@ -0,0 +1,4 @@

1 +

The deprecated mailcap module now refuses to inject unsafe text (filenames,

2 +

MIME types, parameters) into shell commands. Instead of using such text, it

3 +

will warn and act as if a match was not found (or for test commands, as if

4 +

the test failed).

You can’t perform that action at this time.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4