Skip to content

Commit

Permalink
strict name object
Browse files Browse the repository at this point in the history
  • Loading branch information
francois committed Apr 17, 2024
1 parent 3169286 commit cba799b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
2 changes: 2 additions & 0 deletions pypdf/generic/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,8 @@ def read_from_stream(stream: StreamType, pdf: Any) -> "NameObject": # PdfReader
if name != NameObject.surfix:
raise PdfReadError("name read error")
name += read_until_regex(stream, NameObject.delimiter_pattern)
if pdf and pdf.strict and not name.isascii():
raise PdfReadError(f"Illegal character in NameObject ({name})")
name = NameObject.unnumber(name)
try:
name = NameObject(name.decode(NameObject.encoding))
Expand Down
23 changes: 9 additions & 14 deletions tests/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1076,20 +1076,15 @@ def test_checkboxradiobuttonattributes_opt():


def test_name_object_invalid_decode():
charsets = deepcopy(NameObject.CHARSETS)
try:
NameObject.CHARSETS = ("utf-8",)
stream = BytesIO(b"/\x80\x02\x03")
# strict:
with pytest.raises(PdfReadError) as exc:
NameObject.read_from_stream(stream, ReaderDummy(strict=True))
assert "Illegal character in NameObject " in exc.value.args[0]

# non-strict:
stream.seek(0)
NameObject.read_from_stream(stream, ReaderDummy(strict=False))
finally:
NameObject.CHARSETS = charsets
stream = BytesIO(b"/\x80\x02\x03")
# strict:
with pytest.raises(PdfReadError) as exc:
NameObject.read_from_stream(stream, ReaderDummy(strict=True))
assert "Illegal character in NameObject " in exc.value.args[0]

# non-strict:
stream.seek(0)
NameObject.read_from_stream(stream, ReaderDummy(strict=False))


def test_indirect_object_invalid_read():
Expand Down

0 comments on commit cba799b

Please sign in to comment.