[BugFix] openbb-core: Build combined certificate path from the file name (#7818)

combine_certificates split the full path on the first ".", so a dot in a
parent directory produced an invalid output path (the CA bundle was then
silently dropped) and a certificate without an extension raised IndexError.
Use the pathlib stem and suffix instead.

Signed-off-by: Lucas Barros <lucasbarros.kam@gmail.com>
Co-authored-by: dlee-bq <dlee@brightquery.com>
This commit is contained in:
Lucas Barros
2026-11-28 23:00:43 +00:00
committed by GitHub
parent 862fe94408
commit e61a902240
2 changed files with 41 additions and 3 deletions

View File

@@ -478,13 +478,17 @@ def combine_certificates(cert: str, bundle: str | None = None) -> str:
from pathlib import Path
from warnings import warn
if not Path(cert).exists():
cert_path = Path(cert)
if not cert_path.exists():
raise FileNotFoundError(f"Certificate file '{cert}' not found")
if cert.split(".", maxsplit=1)[0].endswith("_combined"):
if cert_path.stem.endswith("_combined"):
return cert
combined_cert = cert.split(".", maxsplit=1)[0] + "_combined." + cert.split(".")[1]
combined_cert = str(
cert_path.with_name(f"{cert_path.stem}_combined{cert_path.suffix}")
)
if Path(combined_cert).exists():
return combined_cert

View File

@@ -740,6 +740,40 @@ def test_combine_certificates_returns_existing_combined_file(tmp_path):
assert combine_certificates(str(cert)) == str(combined)
def test_combine_certificates_dotted_directory(tmp_path):
"""A dot in a parent directory name must not be taken as the file extension."""
from openbb_core.provider.utils.helpers import combine_certificates
cert_dir = tmp_path / ".certs"
cert_dir.mkdir()
cert = cert_dir / "ca.pem"
cert.write_text("CERT")
bundle = tmp_path / "bundle.pem"
bundle.write_text("BUNDLE")
out = Path(combine_certificates(str(cert), str(bundle)))
assert out == cert_dir / "ca_combined.pem"
assert out.read_text() == "BUNDLECERT"
out.unlink()
def test_combine_certificates_without_extension(tmp_path):
"""A certificate file without an extension is combined instead of raising."""
from openbb_core.provider.utils.helpers import combine_certificates
cert = tmp_path / "ca"
cert.write_text("CERT")
bundle = tmp_path / "bundle.pem"
bundle.write_text("BUNDLE")
out = Path(combine_certificates(str(cert), str(bundle)))
assert out == tmp_path / "ca_combined"
assert out.read_text() == "BUNDLECERT"
out.unlink()
def test_combine_certificates_cleanup_tolerates_missing_file(tmp_path, monkeypatch):
"""The registered atexit cleanup must not raise if the file is already gone."""
import atexit