Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbbc1bbd

Browse files
samcarroll42miss-islington
authored andcommitted
pythongh-99889: Fix directory traversal security flaw in uu.decode() (pythonGH-104096)
* Fix directory traversal security flaw in uu.decode()* also check absolute paths and os.altsep* Add a regression test.---------(cherry picked from commit0aeda29)Co-authored-by: Sam Carroll <70000253+samcarroll42@users.noreply.github.com>Co-authored-by: Gregory P. Smith <greg@krypto.org> [Google]
1 parent7522ff7 commitbbc1bbd

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

‎Lib/test/test_uu.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,34 @@ def test_newlines_escaped(self):
145145
uu.encode(inp,out,filename)
146146
self.assertIn(safefilename,out.getvalue())
147147

148+
deftest_no_directory_traversal(self):
149+
relative_bad=b"""\
150+
begin 644 ../../../../../../../../tmp/test1
151+
$86)C"@``
152+
`
153+
end
154+
"""
155+
withself.assertRaisesRegex(uu.Error,'directory'):
156+
uu.decode(io.BytesIO(relative_bad))
157+
ifos.altsep:
158+
relative_bad_bs=relative_bad.replace(b'/',b'\\')
159+
withself.assertRaisesRegex(uu.Error,'directory'):
160+
uu.decode(io.BytesIO(relative_bad_bs))
161+
162+
absolute_bad=b"""\
163+
begin 644 /tmp/test2
164+
$86)C"@``
165+
`
166+
end
167+
"""
168+
withself.assertRaisesRegex(uu.Error,'directory'):
169+
uu.decode(io.BytesIO(absolute_bad))
170+
ifos.altsep:
171+
absolute_bad_bs=absolute_bad.replace(b'/',b'\\')
172+
withself.assertRaisesRegex(uu.Error,'directory'):
173+
uu.decode(io.BytesIO(absolute_bad_bs))
174+
175+
148176
classUUStdIOTest(unittest.TestCase):
149177

150178
defsetUp(self):

‎Lib/uu.py‎

100755100644
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,14 @@ def decode(in_file, out_file=None, mode=None, quiet=False):
130130
# If the filename isn't ASCII, what's up with that?!?
131131
out_file=hdrfields[2].rstrip(b'\t\r\n\f').decode("ascii")
132132
ifos.path.exists(out_file):
133-
raiseError('Cannot overwrite existing file: %s'%out_file)
133+
raiseError(f'Cannot overwrite existing file:{out_file}')
134+
if (out_file.startswith(os.sep)or
135+
f'..{os.sep}'inout_fileor (
136+
os.altsepand
137+
(out_file.startswith(os.altsep)or
138+
f'..{os.altsep}'inout_file))
139+
):
140+
raiseError(f'Refusing to write to{out_file} due to directory traversal')
134141
ifmodeisNone:
135142
mode=int(hdrfields[1],8)
136143
#
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed a security in flaw in:func:`uu.decode` that could allow for
2+
directory traversal based on the input if no ``out_file`` was specified.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp