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
156 lines
4.3 KiB
Python
Executable File
156 lines
4.3 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""
|
|
Pre-commit hook to prevent stdlib printf-style formatting in direct loguru calls.
|
|
|
|
loguru uses brace formatting (`{}`), not stdlib logging placeholders like
|
|
`%s` or `%d`. Mixing the two leaves placeholders unrendered in runtime logs.
|
|
|
|
Applies to files importing either raw loguru or the project's diagnose-gated
|
|
``security.secure_logging`` wrapper (which delegates formatting to loguru).
|
|
The pytest guardian ``tests/utilities/test_loguru_placeholder_formatting.py``
|
|
imports this module so hook and guardian cannot diverge.
|
|
"""
|
|
|
|
import ast
|
|
from pathlib import Path
|
|
import re
|
|
import sys
|
|
|
|
|
|
PRINTF_PLACEHOLDER_RE = re.compile(
|
|
r"%(?:\([^)]+\))?[#0 +\-]*\d*(?:\.\d+)?[sdfr]"
|
|
)
|
|
LOGURU_METHODS = {
|
|
"trace",
|
|
"debug",
|
|
"info",
|
|
"success",
|
|
"warning",
|
|
"error",
|
|
"critical",
|
|
"exception",
|
|
"log",
|
|
}
|
|
|
|
# The secure_logging wrapper import, matched exactly (module + level) like
|
|
# check-sensitive-logging.py does — never by suffix.
|
|
WRAPPER_MODULE_RELATIVE = "security.secure_logging"
|
|
WRAPPER_MODULE_ABSOLUTE = "local_deep_research.security.secure_logging"
|
|
|
|
# SecureLogger.bind()/.patch() re-wrap and keep loguru brace formatting, so
|
|
# chained calls need the same placeholder check as direct ones.
|
|
WRAPPER_CHAIN_METHODS = {"bind", "patch"}
|
|
|
|
|
|
def imports_project_logger(tree: ast.AST) -> bool:
|
|
"""True if the module imports ``logger`` from loguru or the wrapper.
|
|
|
|
Walks the whole tree so function-local imports are detected too.
|
|
"""
|
|
for node in ast.walk(tree):
|
|
if not isinstance(node, ast.ImportFrom):
|
|
continue
|
|
if not any(alias.name == "logger" for alias in node.names):
|
|
continue
|
|
if node.level == 0 and node.module in (
|
|
"loguru",
|
|
WRAPPER_MODULE_ABSOLUTE,
|
|
):
|
|
return True
|
|
if node.level > 0 and node.module == WRAPPER_MODULE_RELATIVE:
|
|
return True
|
|
return False
|
|
|
|
|
|
def _is_logger_receiver(expr: ast.AST) -> bool:
|
|
"""True for ``logger`` or a bind()/patch() chain rooted at ``logger``."""
|
|
while (
|
|
isinstance(expr, ast.Call)
|
|
and isinstance(expr.func, ast.Attribute)
|
|
and expr.func.attr in WRAPPER_CHAIN_METHODS
|
|
):
|
|
expr = expr.func.value
|
|
return isinstance(expr, ast.Name) and expr.id == "logger"
|
|
|
|
|
|
def find_printf_violations(tree: ast.AST) -> list[tuple[int, str]]:
|
|
"""Return (lineno, message) for logger calls using printf placeholders."""
|
|
violations = []
|
|
for node in ast.walk(tree):
|
|
if not (
|
|
isinstance(node, ast.Call)
|
|
and isinstance(node.func, ast.Attribute)
|
|
and node.func.attr in LOGURU_METHODS
|
|
and _is_logger_receiver(node.func.value)
|
|
):
|
|
continue
|
|
|
|
method_name = node.func.attr
|
|
message_index = 1 if method_name == "log" else 0
|
|
min_args = 3 if method_name == "log" else 2
|
|
if len(node.args) < min_args:
|
|
continue
|
|
|
|
message_arg = node.args[message_index]
|
|
if not (
|
|
isinstance(message_arg, ast.Constant)
|
|
and isinstance(message_arg.value, str)
|
|
and PRINTF_PLACEHOLDER_RE.search(message_arg.value)
|
|
):
|
|
continue
|
|
|
|
violations.append(
|
|
(
|
|
node.lineno,
|
|
message_arg.value,
|
|
)
|
|
)
|
|
|
|
return violations
|
|
|
|
|
|
def check_file(file_path: str) -> list[tuple[int, str]]:
|
|
path = Path(file_path)
|
|
if path.suffix != ".py":
|
|
return []
|
|
|
|
try:
|
|
source = path.read_text(encoding="utf-8")
|
|
except Exception as exc:
|
|
return [(0, f"failed to read file: {exc}")]
|
|
|
|
try:
|
|
tree = ast.parse(source, filename=file_path)
|
|
except SyntaxError:
|
|
return []
|
|
|
|
if not imports_project_logger(tree):
|
|
return []
|
|
|
|
return find_printf_violations(tree)
|
|
|
|
|
|
def main() -> int:
|
|
exit_code = 0
|
|
|
|
for file_path in sys.argv[1:]:
|
|
violations = check_file(file_path)
|
|
for lineno, message in violations:
|
|
print(
|
|
f"{file_path}:{lineno}: loguru logger call uses printf-style placeholders"
|
|
)
|
|
print(f" {message!r}")
|
|
exit_code = 1
|
|
|
|
if exit_code:
|
|
print()
|
|
print(
|
|
"Hint: use loguru brace formatting, e.g. logger.info('value: {}', x)"
|
|
)
|
|
|
|
return exit_code
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|