Skip to content

armgit / AiSOC

Public Imported from GitHub
0

Open-source AI Security Operations Center: alert fusion, LLM-agent triage, MITRE ATT&CK investigation, and a replayable decision ledger for every agent step. Self-hostable, runs with no API keys, MIT licensed. Ships an MCP server for Claude, Cursor and Continue.

security(codeql): fix py/log-injection in alert_explain + py/unused-global in vendor sync (#98)

Beenu Arorab383da37fd41
diff --git a/scripts/sync_vendored_nl_query.py b/scripts/sync_vendored_nl_query.py
index 76fcf8bbf..f04d42dc0 100755
--- a/scripts/sync_vendored_nl_query.py
+++ b/scripts/sync_vendored_nl_query.py
@@ -77,6 +77,10 @@ def _check() -> int:
if not filecmp.cmp(src, dst, shallow=False):
drift.append(f" - out of date: {name}")
+ for name in VENDORED_ONLY:
+ if not (VENDORED_DIR / name).is_file():
+ drift.append(f" - missing vendored-only file: {name}")
+
# Make sure no stray extra .py files snuck into the vendored tree.
vendored_pyfiles = {p.name for p in VENDORED_DIR.iterdir() if p.is_file() and p.suffix == ".py"}
extra_py = vendored_pyfiles - set(SYNCED_FILES)
@@ -123,8 +127,7 @@ def _sync() -> int:
print(f"removing stale {path.relative_to(REPO_ROOT)}")
path.unlink()
- print("\nDone. Don't forget to commit the changes under "
- "services/api/app/_vendor/nl_query/.")
+ print("\nDone. Don't forget to commit the changes under services/api/app/_vendor/nl_query/.")
return 0
diff --git a/services/api/app/api/v1/endpoints/alert_explain.py b/services/api/app/api/v1/endpoints/alert_explain.py
index 649de5f05..793e15674 100644
--- a/services/api/app/api/v1/endpoints/alert_explain.py
+++ b/services/api/app/api/v1/endpoints/alert_explain.py
@@ -43,6 +43,7 @@ from fastapi import APIRouter, Depends, HTTPException, Request, Response, status
from sqlalchemy import select
from app.api.v1.deps import AuthUser, require_permission
+from app.core.logging import safe_log_value
from app.db.rls import TenantDBSession
from app.models.alert import Alert
from app.services.alert_explain import (
@@ -93,7 +94,7 @@ async def _acquire_or_429(
# error envelope so retry-after semantics survive the 429.
logger.info(
"explain.rate_limited tenant_id=%s cost=%.2f remaining=%.2f retry_after=%.2fs",
- tenant_id,
+ safe_log_value(tenant_id),
cost,
decision.remaining,
decision.retry_after_seconds,
@@ -174,11 +175,15 @@ async def explain_alert(
# errors, etc. — so reaching this branch means something
# unexpected (e.g. database read failure mid-explain) blew up.
# Log loudly and return a 502 so the client can retry; we do
- # *not* expose the exception text to the caller.
+ # *not* expose the exception text to the caller. Tenant/alert
+ # IDs are UUIDs in practice but they originate from the request,
+ # so we route them through ``safe_log_value`` to neutralise any
+ # CR/LF injection attempt before they hit the log stream
+ # (CWE-117 / CodeQL ``py/log-injection``).
logger.exception(
"explain.failed tenant=%s alert=%s",
- current_user.tenant_id,
- alert_id,
+ safe_log_value(current_user.tenant_id),
+ safe_log_value(alert_id),
)
raise HTTPException(
status_code=status.HTTP_502_BAD_GATEWAY,
@@ -215,11 +220,16 @@ async def explain_alert(
# Audit-log failures must never break the explain response.
# We log a warning so ops can investigate, but we do not
# propagate — the user already got their answer.
+ # Sanitise every interpolated value: tenant_id/alert_id come
+ # from the request and ``exc`` may carry arbitrary text
+ # (e.g. database constraint messages). ``safe_log_value``
+ # escapes CR/LF/NUL/ESC and truncates, defending against
+ # CWE-117 (CodeQL ``py/log-injection``).
logger.warning(
"explain.audit_failed tenant=%s alert=%s error=%s",
- current_user.tenant_id,
- alert_id,
- exc,
+ safe_log_value(current_user.tenant_id),
+ safe_log_value(alert_id),
+ safe_log_value(exc),
)
return _explanation_to_payload(explanation)