8000 [3.6] bpo-42103: Improve validation of Plist files. (GH-22882) by serhiy-storchaka · Pull Request #23118 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

[3.6] bpo-42103: Improve validation of Plist files. (GH-22882) #23118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 10, 2020
Merged
Show file tree
Hide file tree
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
34 changes: 21 additions & 13 deletions Lib/plistlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ def parse(self, fp):
return self._read_object(top_object)

except (OSError, IndexError, struct.error, OverflowError,
UnicodeDecodeError):
ValueError):
raise InvalidFileException()

def _get_size(self, tokenL):
Expand All @@ -649,7 +649,7 @@ def _get_size(self, tokenL):
def _read_ints(self, n, size):
data = self._fp.read(size * n)
if size in _BINARY_FORMAT:
return struct.unpack('>' + _BINARY_FORMAT[size] * n, data)
return struct.unpack(f'>{n}{_BINARY_FORMAT[size]}', data)
else:
if not size or len(data) != size * n:
raise InvalidFileException()
Expand Down Expand Up @@ -708,19 +708,25 @@ def _read_object(self, ref):

elif tokenH == 0x40: # data
s = self._get_size(tokenL)
if self._use_builtin_types:
result = self._fp.read(s)
else:
result = Data(self._fp.read(s))
result = self._fp.read(s)
if len(result) != s:
raise InvalidFileException()
if not self._use_builtin_types:
result = Data(result)

elif tokenH == 0x50: # ascii string
s = self._get_size(tokenL)
result = self._fp.read(s).decode('ascii')
result = result
data = self._fp.read(s)
if len(data) != s:
raise InvalidFileException()
result = data.decode('ascii')

elif tokenH == 0x60: # unicode string
s = self._get_size(tokenL)
result = self._fp.read(s * 2).decode('utf-16be')
s = self._get_size(tokenL) * 2
data = self._fp.read(s)
if len(data) != s:
raise InvalidFileException()
result = data.decode('utf-16be')

# tokenH == 0x80 is documented as 'UID' and appears to be used for
# keyed-archiving, not in plists.
Expand All @@ -744,9 +750,11 @@ def _read_object(self, ref):
obj_refs = self._read_refs(s)
result = self._dict_type()
self._objects[ref] = result
for k, o in zip(key_refs, obj_refs):
result[self._read_object(k)] = self._read_object(o)

try:
for k, o in zip(key_refs, obj_refs):
result[self._read_object(k)] = self._read_object(o)
except TypeError:
raise InvalidFileException()
else:
raise InvalidFileException()

Expand Down
Loading
0