Files
learningcircuit--local-deep…/tests/journal_quality/test_downloader_exception_sanitization.py
wehub-resource-sync 7a0da7932b
OSV-Scanner (Scheduled) / scan-scheduled (push) Failing after 0s
Create Release / test-gate (push) Has been cancelled
Create Release / release-gate (push) Has been cancelled
Create Release / ci-gate (push) Has been cancelled
Create Release / version-check (push) Has been cancelled
Create Release / e2e-test-gate (push) Has been cancelled
Create Release / responsive-test-gate (push) Has been cancelled
Create Release / compat-test-gate (push) Has been cancelled
Create Release / compose-integration-gate (push) Has been cancelled
Create Release / vulture-gate (push) Has been cancelled
Create Release / build (push) Has been cancelled
Create Release / provenance (push) Has been cancelled
Create Release / prerelease-docker (push) Has been cancelled
Create Release / publish-docker (push) Has been cancelled
Create Release / create-release (push) Has been cancelled
Create Release / cleanup-changelog (push) Has been cancelled
Create Release / trigger-pypi (push) Has been cancelled
Create Release / monitor-pypi (push) Has been cancelled
Create Release / Clean up orphan prerelease tags and signatures (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [research-form] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [research-metrics] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [research-workflow] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [settings-core] (push) Has been cancelled
CodeQL Advanced / Analyze (javascript-typescript) (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [history-news] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [library] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [link-analytics] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [chat-core] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [chat-lifecycle] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [error-benchmark] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [settings-pages] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) (push) Has been cancelled
Docker Tests (Consolidated) / Accessibility Tests (push) Has been cancelled
Docker Tests (Consolidated) / LLM Unit Tests (push) Has been cancelled
Docker Tests (Consolidated) / LLM Example Tests (push) Has been cancelled
Docker Tests (Consolidated) / Production Image Smoke Test (push) Has been cancelled
Docker Tests (Consolidated) / Infrastructure Tests (push) Has been cancelled
OSSF Scorecard / OSSF Security Scorecard Analysis (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [mobile] (push) Has been cancelled
Backwards Compatibility / Verify Encryption Constants (push) Has been cancelled
Backwards Compatibility / PyPI Version Compatibility (push) Has been cancelled
Backwards Compatibility / Database Migration Tests (push) Has been cancelled
CodeQL Advanced / Analyze (python) (push) Has been cancelled
Docker Tests (Consolidated) / detect-changes (push) Has been cancelled
Docker Tests (Consolidated) / Build Test Image (push) Has been cancelled
Docker Tests (Consolidated) / All Pytest Tests + Coverage (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [accessibility] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [api-crud] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [auth-login] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [auth-pages] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [auth-register] (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:08:55 +08:00

147 lines
4.2 KiB
Python

"""Exception text must not leak into the downloader's return message.
CodeQL alerts 7650 and 7684 flagged `str(exc)` flowing from the
build_db failure path through `download_journal_data` into the
HTTP response. The fix sanitizes at the source — only the exception
*class name* survives. The log line (server-side only) still carries
the full traceback.
"""
from unittest.mock import patch
import pytest
from local_deep_research.journal_quality.downloader import (
JOURNAL_DATA_VERSION,
download_journal_data,
)
@pytest.fixture
def tmp_data_dir(tmp_path):
with patch(
"local_deep_research.journal_quality.downloader._get_data_dir",
return_value=tmp_path,
):
yield tmp_path
def _fake_disk_usage(free_bytes):
class _Usage:
total = 10 * 1024**3
used = total - free_bytes
free = free_bytes
return _Usage()
SENSITIVE_SUBSTRINGS = [
"/home/",
"Traceback",
'File "',
"line ",
"sqlalchemy.",
"SELECT ",
"INSERT ",
"near SYNTAX",
]
def test_build_db_exception_text_does_not_leak_into_message(tmp_data_dir):
"""Ensure str(exc) from build_db failure never reaches the caller."""
# Craft an exception whose str() would leak system information.
leaking_exc = RuntimeError(
"SELECT * FROM journals WHERE h_index > 5 "
'-- File "/home/user/.ldr/secret.db" line 42'
)
with (
patch(
"shutil.disk_usage",
return_value=_fake_disk_usage(10 * 1024**3),
),
patch(
"local_deep_research.journal_quality.downloader._fetch_openalex_sources",
return_value=1,
),
patch(
"local_deep_research.journal_quality.downloader._fetch_doaj_journals",
return_value=1,
),
patch(
"local_deep_research.journal_quality.data_sources.institutions."
"InstitutionSource.fetch",
return_value=0,
),
patch(
"local_deep_research.journal_quality.data_sources.jabref."
"JabRefSource.fetch",
return_value=0,
),
patch(
"local_deep_research.journal_quality.data_sources.predatory."
"PredatorySource.fetch",
return_value=0,
),
patch(
"local_deep_research.journal_quality.db.build_db",
side_effect=leaking_exc,
),
):
success, message = download_journal_data(force=True)
assert success is False
# The class name is fine — callers can show "RuntimeError" safely.
assert "RuntimeError" in message
# None of the sensitive content must appear.
for needle in SENSITIVE_SUBSTRINGS:
assert needle not in message, (
f"Sanitization regression: {needle!r} leaked into {message!r}"
)
def test_healthy_success_message_has_no_exception_artifacts(tmp_data_dir):
"""On the happy path, success message is count+elapsed only."""
with (
patch(
"shutil.disk_usage",
return_value=_fake_disk_usage(10 * 1024**3),
),
patch(
"local_deep_research.journal_quality.downloader._fetch_openalex_sources",
return_value=42,
),
patch(
"local_deep_research.journal_quality.downloader._fetch_doaj_journals",
return_value=7,
),
patch(
"local_deep_research.journal_quality.data_sources.institutions."
"InstitutionSource.fetch",
return_value=0,
),
patch(
"local_deep_research.journal_quality.data_sources.jabref."
"JabRefSource.fetch",
return_value=0,
),
patch(
"local_deep_research.journal_quality.data_sources.predatory."
"PredatorySource.fetch",
return_value=0,
),
patch(
"local_deep_research.journal_quality.db.build_db",
return_value=None,
),
):
success, message = download_journal_data(force=True)
assert success is True
assert "42" in message # openalex count
assert JOURNAL_DATA_VERSION # sanity
for needle in SENSITIVE_SUBSTRINGS:
assert needle not in message