Skip to content

Commit 1eec55f

Browse files
committed
Fix a DOS vulnerability in posixpath regarding string slicing
1 parent ebf6d13 commit 1eec55f

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

Lib/posixpath.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ def expandvars(path):
302302
start = b'{'
303303
end = b'}'
304304
environ = getattr(os, 'environb', None)
305+
join = b''.join
305306
else:
306307
if '$' not in path:
307308
return path
@@ -312,12 +313,16 @@ def expandvars(path):
312313
start = '{'
313314
end = '}'
314315
environ = os.environ
315-
i = 0
316+
join = ''.join
317+
318+
result = []
319+
last = 0
316320
while True:
317-
m = search(path, i)
321+
m = search(path, last)
318322
if not m:
319323
break
320324
i, j = m.span(0)
325+
result.append(path[last:i])
321326
name = m.group(1)
322327
if name.startswith(start) and name.endswith(end):
323328
name = name[1:-1]
@@ -327,13 +332,12 @@ def expandvars(path):
327332
else:
328333
value = environ[name]
329334
except KeyError:
330-
i = j
335+
result.append(path[i:j])
331336
else:
332-
tail = path[j:]
333-
path = path[:i] + value
334-
i = len(path)
335-
path += tail
336-
return path
337+
result.append(value)
338+
last = j
339+
result.append(path[last:])
340+
return join(result)
337341

338342

339343
# Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a DOS vulnerability in :mod:`posixpath` regarding string slicing.

0 commit comments

Comments
 (0)