Skip to content

Bulk asynchronous downloader for multiple file hosts

fix: PEM cert loading on p3.14

NTFSvolume8306a65bd8c3
diff --git a/cyberdrop_dl/clients/tcp.py b/cyberdrop_dl/clients/tcp.py
index b9e3c7b68..bb382e10b 100644
--- a/cyberdrop_dl/clients/tcp.py
+++ b/cyberdrop_dl/clients/tcp.py
@@ -1,5 +1,6 @@
from __future__ import annotations
+import base64
import logging
import platform
import ssl
@@ -7,6 +8,9 @@ from typing import TYPE_CHECKING
import aiohttp
import wassima
+import wassima.utils
+
+from cyberdrop_dl.utils import b64_pad
if TYPE_CHECKING:
import asyncio
@@ -84,7 +88,7 @@ def resolve_tls_version(name: str) -> ssl.TLSVersion:
def _load_certs(paths: Iterable[Path]) -> Generator[Path]:
def load(path: Path) -> Path:
- wassima.register_ca(path.read_text())
+ wassima.register_ca(PEM_cert_to_DER_cert(path.read_text()))
logger.debug("Loaded CA certificates from '%s'", path)
return path
@@ -96,3 +100,13 @@ def _load_certs(paths: Iterable[Path]) -> Generator[Path]:
continue
else:
yield load(path)
+
+
+def PEM_cert_to_DER_cert(pem_cert: str) -> bytes: # noqa: N802
+ cert = pem_cert.strip()
+ if not cert.startswith(wassima.utils.PEM_HEADER):
+ raise ValueError(f"Invalid PEM encoding; must start with {wassima.utils.PEM_HEADER}")
+ if not cert.strip().endswith(wassima.utils.PEM_FOOTER):
+ raise ValueError(f"Invalid PEM encoding; must end with {wassima.utils.PEM_FOOTER}")
+ content = cert.strip()[len(wassima.utils.PEM_HEADER) : -len(wassima.utils.PEM_FOOTER)]
+ return base64.decodebytes(b64_pad(content).encode("ascii", "strict"))
diff --git a/cyberdrop_dl/crawlers/_forum.py b/cyberdrop_dl/crawlers/_forum.py
index aad6ee7c5..ef656141f 100644
--- a/cyberdrop_dl/crawlers/_forum.py
+++ b/cyberdrop_dl/crawlers/_forum.py
@@ -22,7 +22,7 @@ from cyberdrop_dl import aio
from cyberdrop_dl.crawlers.crawler import Crawler
from cyberdrop_dl.exceptions import LoginError, MaxChildrenError, ScrapeError
from cyberdrop_dl.url_objects import AbsoluteHttpURL
-from cyberdrop_dl.utils import css, extr_text, is_blob_or_svg
+from cyberdrop_dl.utils import b64_pad, css, extr_text, is_blob_or_svg
from cyberdrop_dl.utils.errors import error_handling_wrapper
if TYPE_CHECKING:
@@ -529,8 +529,7 @@ class HTMLMessageBoardCrawler(MessageBoardCrawler, is_abc=True):
@error_handling_wrapper
async def resolve_confirmation_link(self, link: AbsoluteHttpURL) -> AbsoluteHttpURL | None:
if url := link.query.get("url") or link.query.get("to"):
- padding = -len(url) % 4
- url = base64.urlsafe_b64decode(url + "=" * padding).decode("utf-8")
+ url = base64.urlsafe_b64decode(b64_pad(url)).decode("utf-8")
if url.startswith("https://"):
return self.parse_url(url)
diff --git a/cyberdrop_dl/crawlers/voe_sx.py b/cyberdrop_dl/crawlers/voe_sx.py
index d9f0bf4b9..6f367629f 100644
--- a/cyberdrop_dl/crawlers/voe_sx.py
+++ b/cyberdrop_dl/crawlers/voe_sx.py
@@ -12,7 +12,7 @@ from cyberdrop_dl.crawlers.crawler import Crawler, SupportedDomains, SupportedPa
from cyberdrop_dl.exceptions import ScrapeError
from cyberdrop_dl.mediaprops import Resolution, Subtitle
from cyberdrop_dl.url_objects import AbsoluteHttpURL
-from cyberdrop_dl.utils import m3u8, open_graph, parse_url
+from cyberdrop_dl.utils import b64_pad, m3u8, open_graph, parse_url
from cyberdrop_dl.utils.errors import error_handling_wrapper
if TYPE_CHECKING:
@@ -189,10 +189,8 @@ def _load_json(json_content: str) -> Any:
def _decrypt_json(encrypted_json: str) -> Any:
def b64_decode(b64_string: str) -> str | None:
- if pad := len(b64_string) % 4:
- b64_string += "=" * (4 - pad)
try:
- return base64.b64decode(b64_string).decode("utf-8", errors="replace")
+ return base64.b64decode(b64_pad(b64_string)).decode("utf-8", errors="replace")
except ValueError:
return None
diff --git a/cyberdrop_dl/utils/__init__.py b/cyberdrop_dl/utils/__init__.py
index 98df8e37e..73df17995 100644
--- a/cyberdrop_dl/utils/__init__.py
+++ b/cyberdrop_dl/utils/__init__.py
@@ -153,3 +153,9 @@ def fast_cache[T, R](fn: Callable[[T], R]) -> Callable[[T], R]:
return val
return compute
+
+
+def b64_pad(b64_string: str) -> str:
+ if pad := len(b64_string) % 4:
+ return b64_string + "=" * (4 - pad)
+ return b64_string