diff --git a/src/extract_favicon/loader.py b/src/extract_favicon/loader.py index 4a47342..cc46aff 100644 --- a/src/extract_favicon/loader.py +++ b/src/extract_favicon/loader.py @@ -145,18 +145,19 @@ def _load_base64_img(favicon: Favicon, force: bool = False) -> Favicon: if suffix == "svg+xml": suffix = "svg" - bytes_content = base64.b64decode(data_img[1]) - img, is_valid = _open_and_verify_image(bytes_content) - width, height, img_format = _get_meta_image(img) - - favicon = favicon._replace( - width=width, - height=height, - format=img_format, - valid=is_valid, - image=img, - reachable=True, - ) + if len(data_img) > 1: + bytes_content = base64.b64decode(data_img[1]) + img, is_valid = _open_and_verify_image(bytes_content) + width, height, img_format = _get_meta_image(img) + + favicon = favicon._replace( + width=width, + height=height, + format=img_format, + valid=is_valid, + image=img, + reachable=len(data_img[1]) > 0, + ) return favicon diff --git a/test/test_download.py b/test/test_download.py index a568172..6c4220b 100644 --- a/test/test_download.py +++ b/test/test_download.py @@ -75,6 +75,23 @@ async def test_base64_async(base64_img): assert favicons[0].height == 1 +@pytest.mark.parametrize( + "url,is_valid", + [("data:image/png;base64,", False), ("data:;base64,", False), ("data:", None)], + ids=["No img data", "No format data", "Only data"], +) +def test_base64_wrong(url, is_valid): + fav = Favicon(url, format=None, width=0, height=0) + fav = extract_favicon.loader._load_base64_img(fav) + assert fav.url == fav.url + assert fav.format is None + assert fav.http is None + assert fav.valid is is_valid + assert fav.image is None + assert fav.width == 0 + assert fav.height == 0 + + def test_svg(svg_url): fav = Favicon(svg_url, format=None, width=0, height=0) favicons = extract_favicon.download([fav])