Skip to content
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

Remove zero-byte end padding when parsing any XMP data #8171

Merged
merged 1 commit into from
Jun 27, 2024
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
22 changes: 22 additions & 0 deletions Tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import tempfile
import warnings
from pathlib import Path
from types import ModuleType
from typing import IO, Any

import pytest
Expand Down Expand Up @@ -35,6 +36,12 @@
skip_unless_feature,
)

ElementTree: ModuleType | None
try:
from defusedxml import ElementTree
except ImportError:
ElementTree = None


# Deprecation helper
def helper_image_new(mode: str, size: tuple[int, int]) -> Image.Image:
Expand Down Expand Up @@ -921,6 +928,21 @@
with Image.open("Tests/images/hopper.gif") as im:
assert im.getxmp() == {}

def test_getxmp_padded(self) -> None:
im = Image.new("RGB", (1, 1))
im.info["xmp"] = (
b'<?xpacket begin="\xef\xbb\xbf" id="W5M0MpCehiHzreSzNTczkc9d"?>\n'
b'<x:xmpmeta xmlns:x="adobe:ns:meta/" />\n<?xpacket end="w"?>\x00\x00'
)
if ElementTree is None:
with pytest.warns(

Check warning on line 938 in Tests/test_image.py

View check run for this annotation

Codecov / codecov/patch

Tests/test_image.py#L938

Added line #L938 was not covered by tests
UserWarning,
match="XMP data cannot be read without defusedxml dependency",
):
assert im.getxmp() == {}

Check warning on line 942 in Tests/test_image.py

View check run for this annotation

Codecov / codecov/patch

Tests/test_image.py#L942

Added line #L942 was not covered by tests
else:
assert im.getxmp() == {"xmpmeta": None}

@pytest.mark.parametrize("size", ((1, 0), (0, 1), (0, 0)))
def test_zero_tobytes(self, size: tuple[int, int]) -> None:
im = Image.new("RGB", size)
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1511,7 +1511,7 @@ def get_value(element):
return {}
if "xmp" not in self.info:
return {}
root = ElementTree.fromstring(self.info["xmp"])
root = ElementTree.fromstring(self.info["xmp"].rstrip(b"\x00"))
return {get_name(root.tag): get_value(root)}

def getexif(self) -> Exif:
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/JpegImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def APP(self, marker):
self.info["exif"] = s
self._exif_offset = self.fp.tell() - n + 6
elif marker == 0xFFE1 and s[:29] == b"http://ns.adobe.com/xap/1.0/\x00":
self.info["xmp"] = s.split(b"\x00")[1]
self.info["xmp"] = s.split(b"\x00", 1)[1]
elif marker == 0xFFE2 and s[:5] == b"FPXR\0":
# extract FlashPix information (incomplete)
self.info["flashpix"] = s # FIXME: value will change
Expand Down
Loading