| 1 |
#!/usr/bin/env python3 |
| 2 |
# Copyright: 2026, Maximiliano Curia <[email protected]> |
| 3 |
# |
| 4 |
# License: ISC |
| 5 |
# Permission to use, copy, modify, and/or distribute this software for any |
| 6 |
# purpose with or without fee is hereby granted, provided that the above |
| 7 |
# copyright notice and this permission notice appear in all copies. |
| 8 |
# . |
| 9 |
# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH |
| 10 |
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY |
| 11 |
# AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, |
| 12 |
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM |
| 13 |
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE |
| 14 |
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 15 |
# PERFORMANCE OF THIS SOFTWARE. |
| 16 |
|
| 17 |
"""Image metadata fields reaching the output (Debian bug #1022330).""" |
| 18 |
|
| 19 |
import shutil |
| 20 |
import struct |
| 21 |
import subprocess |
| 22 |
import tempfile |
| 23 |
import unittest |
| 24 |
import zlib |
| 25 |
from pathlib import Path |
| 26 |
|
| 27 |
import xdg.Mime |
| 28 |
|
| 29 |
from decopy.fstree import parsers |
| 30 |
|
| 31 |
|
| 32 |
def _png() -> bytes: |
| 33 |
"""A valid 1x1 PNG, so a test can write metadata onto a real image.""" |
| 34 |
|
| 35 |
def chunk(tag: bytes, data: bytes) -> bytes: |
| 36 |
return ( |
| 37 |
struct.pack(">I", len(data)) |
| 38 |
+ tag |
| 39 |
+ data |
| 40 |
+ struct.pack(">I", zlib.crc32(tag + data)) |
| 41 |
) |
| 42 |
|
| 43 |
return ( |
| 44 |
b"\x89PNG\r\n\x1a\n" |
| 45 |
+ chunk(b"IHDR", struct.pack(">IIBBBBB", 1, 1, 8, 2, 0, 0, 0)) |
| 46 |
+ chunk(b"IDAT", zlib.compress(b"\x00\x00\x00\x00")) |
| 47 |
+ chunk(b"IEND", b"") |
| 48 |
) |
| 49 |
|
| 50 |
|
| 51 |
def _exif(*fields: str) -> str: |
| 52 |
"""Text shaped like exiftool's own output, one 'Label : value' per line.""" |
| 53 |
pairs = (f.split("=", 1) for f in fields) |
| 54 |
return "".join(f"{label:32}: {value}\n" for label, value in pairs) |
| 55 |
|
| 56 |
|
| 57 |
class TestExiftoolCopyrightField(unittest.TestCase): |
| 58 |
"""exiftool's Copyright label fires parse_holders whatever the value holds.""" |
| 59 |
|
| 60 |
@staticmethod |
| 61 |
def _parse(text: str) -> tuple[list[str], list[str]]: |
| 62 |
holders, licenses = parsers.process_exiftool_output(text) |
| 63 |
return [str(h) for h in holders], list(licenses) |
| 64 |
|
| 65 |
def test_a_licence_in_the_copyright_field_is_not_a_holder(self): |
| 66 |
text = _exif( |
| 67 |
"Copyright=CC Attribution-ShareAlike " |
| 68 |
"http://creativecommons.org/licenses/by-sa/3.0/", |
| 69 |
) |
| 70 |
holders, licenses = self._parse(text) |
| 71 |
self.assertEqual(holders, []) |
| 72 |
self.assertIn("CC-BY-SA-3", licenses) |
| 73 |
|
| 74 |
def test_every_licence_shaped_value_is_suppressed(self): |
| 75 |
values = ( |
| 76 |
"CC BY-SA 4.0", |
| 77 |
"Public Domain http://creativecommons.org/licenses/publicdomain/", |
| 78 |
"CC0 Public Domain Dedication " |
| 79 |
"http://creativecommons.org/publicdomain/zero/1.0/", |
| 80 |
"CC Attribution http://creativecommons.org/licenses/by/4.0/", |
| 81 |
) |
| 82 |
for value in values: |
| 83 |
with self.subTest(value=value): |
| 84 |
holders, licenses = self._parse(_exif(f"Copyright={value}")) |
| 85 |
self.assertEqual(holders, []) |
| 86 |
self.assertNotEqual(licenses, []) |
| 87 |
|
| 88 |
def test_a_real_copyright_is_kept(self): |
| 89 |
holders, _ = self._parse(_exif("Copyright=Copyright 2011 Lapo Calamandrei")) |
| 90 |
self.assertEqual(holders, ["2011, Lapo Calamandrei"]) |
| 91 |
|
| 92 |
def test_a_dated_name_is_kept(self): |
| 93 |
holders, _ = self._parse(_exif("Copyright=2011 Jakub Steiner")) |
| 94 |
self.assertEqual(holders, ["2011, Jakub Steiner"]) |
| 95 |
|
| 96 |
def test_a_plain_name_is_kept(self): |
| 97 |
holders, _ = self._parse(_exif("Copyright=Fonticons, Inc.")) |
| 98 |
self.assertEqual(holders, ["Fonticons, Inc"]) |
| 99 |
|
| 100 |
def test_suppression_is_per_line(self): |
| 101 |
# Only the line whose own value is a licence goes; a second Copyright |
| 102 |
# line naming a holder is untouched. |
| 103 |
text = _exif( |
| 104 |
"Copyright=CC Attribution-ShareAlike " |
| 105 |
"http://creativecommons.org/licenses/by-sa/3.0/", |
| 106 |
"Copyright=2011 Jakub Steiner", |
| 107 |
) |
| 108 |
holders, licenses = self._parse(text) |
| 109 |
self.assertEqual(holders, ["2011, Jakub Steiner"]) |
| 110 |
self.assertIn("CC-BY-SA-3", licenses) |
| 111 |
|
| 112 |
def test_a_rights_field_still_self_filters(self): |
| 113 |
kept, _ = self._parse(_exif("Rights=Copyright 2007 Niko Kiirala")) |
| 114 |
self.assertEqual(kept, ["2007, Niko Kiirala"]) |
| 115 |
dropped, _ = self._parse(_exif("Rights=CC BY-SA 4.0")) |
| 116 |
self.assertEqual(dropped, []) |
| 117 |
|
| 118 |
def test_other_fields_still_yield_their_licences(self): |
| 119 |
text = _exif( |
| 120 |
"Usage Terms=This work is licensed under a Creative Commons " |
| 121 |
"Attribution 4.0 International License.", |
| 122 |
) |
| 123 |
_, licenses = self._parse(text) |
| 124 |
self.assertIn("CC-BY-4", licenses) |
| 125 |
|
| 126 |
|
| 127 |
class TestExiftoolAuthorFields(unittest.TestCase): |
| 128 |
"""Author/Artist/Creator name a holder that parse_holders cannot see.""" |
| 129 |
|
| 130 |
@staticmethod |
| 131 |
def _parse(text: str) -> tuple[list[str], list[str]]: |
| 132 |
holders, licenses = parsers.process_exiftool_output(text) |
| 133 |
return [str(h) for h in holders], list(licenses) |
| 134 |
|
| 135 |
def test_author_becomes_a_holder(self): |
| 136 |
holders, _ = self._parse(_exif("Author=Lapo Calamandrei")) |
| 137 |
self.assertEqual(holders, ["Lapo Calamandrei"]) |
| 138 |
|
| 139 |
def test_every_author_field_is_read(self): |
| 140 |
for label in ("Author", "Artist", "Creator"): |
| 141 |
with self.subTest(label=label): |
| 142 |
holders, _ = self._parse(_exif(f"{label}=Jakub Steiner")) |
| 143 |
self.assertEqual(holders, ["Jakub Steiner"]) |
| 144 |
|
| 145 |
def test_a_real_notice_is_not_overridden(self): |
| 146 |
text = _exif("Copyright=2011 Sam Hewitt", "Author=Lapo Calamandrei") |
| 147 |
holders, _ = self._parse(text) |
| 148 |
self.assertEqual(holders, ["2011, Sam Hewitt"]) |
| 149 |
|
| 150 |
def test_a_licence_copyright_no_longer_blocks_the_author(self): |
| 151 |
# The 97 corpus files carrying both: the suppressed Copyright tag must |
| 152 |
# leave the gap open for the author to fill. |
| 153 |
text = _exif( |
| 154 |
"Copyright=CC Attribution-NonCommercial-ShareAlike " |
| 155 |
"http://creativecommons.org/licenses/by-nc-sa/3.0/", |
| 156 |
"Author=Lapo Calamandrei", |
| 157 |
) |
| 158 |
holders, licenses = self._parse(text) |
| 159 |
self.assertEqual(holders, ["Lapo Calamandrei"]) |
| 160 |
self.assertIn("CC-BY-NC-SA-3", licenses) |
| 161 |
|
| 162 |
def test_an_email_is_kept(self): |
| 163 |
holders, _ = self._parse(_exif("Author=Luca Ferretti <[email protected]>")) |
| 164 |
self.assertEqual(holders, ["Luca Ferretti <[email protected]>"]) |
| 165 |
|
| 166 |
def test_junk_values_are_rejected(self): |
| 167 |
for value in ( |
| 168 |
"(doofus-01)", |
| 169 |
"HASH(0x86d2470)", |
| 170 |
"x-default", |
| 171 |
"www.tonytextures.com", |
| 172 |
"Inkscape", |
| 173 |
"$committer", |
| 174 |
): |
| 175 |
with self.subTest(value=value): |
| 176 |
holders, _ = self._parse(_exif(f"Author={value}")) |
| 177 |
self.assertEqual(holders, []) |
| 178 |
|
| 179 |
def test_the_software_field_is_not_an_author(self): |
| 180 |
holders, _ = self._parse(_exif("Software=www.inkscape.org")) |
| 181 |
self.assertEqual(holders, []) |
| 182 |
|
| 183 |
def test_a_value_holding_a_colon_survives(self): |
| 184 |
# Labels never contain a colon, values often do. |
| 185 |
holders, _ = self._parse(_exif("Author=Ell", "Web Statement=http://x.invalid/")) |
| 186 |
self.assertEqual(holders, ["Ell"]) |
| 187 |
|
| 188 |
|
| 189 |
class TestExiftoolParserEndToEnd(unittest.TestCase): |
| 190 |
"""The suppression reaches a real image through the exiftool parser.""" |
| 191 |
|
| 192 |
@unittest.skipUnless(shutil.which("exiftool"), "exiftool not installed") |
| 193 |
def test_png_with_a_licence_in_its_copyright_tag(self): |
| 194 |
exiftool = shutil.which("exiftool") |
| 195 |
assert exiftool is not None |
| 196 |
with tempfile.TemporaryDirectory() as tmp: |
| 197 |
path = Path(tmp) / "icon.png" |
| 198 |
path.write_bytes(_png()) |
| 199 |
subprocess.run( # noqa: S603 |
| 200 |
[ |
| 201 |
exiftool, |
| 202 |
"-overwrite_original", |
| 203 |
"-Copyright=CC Attribution-ShareAlike " |
| 204 |
"http://creativecommons.org/licenses/by-sa/3.0/", |
| 205 |
str(path), |
| 206 |
], |
| 207 |
check=True, |
| 208 |
capture_output=True, |
| 209 |
) |
| 210 |
parser = parsers.lookup_mimetypes(xdg.Mime.get_type(str(path))) |
| 211 |
holders, licenses = parser(str(path)) |
| 212 |
self.assertEqual([str(h) for h in holders], []) |
| 213 |
self.assertIn("CC-BY-SA-3", list(licenses)) |
| 214 |
|
| 215 |
|
| 216 |
if __name__ == "__main__": |
| 217 |
unittest.main() |