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: harden remaining CodeQL alerts (log-injection, url-redirection)

Beenu Arorad8a87c2dd297
diff --git a/services/api/app/api/v1/endpoints/connectors.py b/services/api/app/api/v1/endpoints/connectors.py
index b9be82354..b21d6f20f 100644
--- a/services/api/app/api/v1/endpoints/connectors.py
+++ b/services/api/app/api/v1/endpoints/connectors.py
@@ -786,17 +786,19 @@ async def update_connector_capabilities(
await db.commit()
await db.refresh(connector)
+ # Audit log: only emit fields with provably bounded shape.
+ # ``request.allowed_capabilities`` is a list of caller-supplied strings
+ # and would otherwise carry taint into the log record (CodeQL
+ # py/log-injection). The capability *count* is sufficient for ops
+ # forensics — the full set is already persisted on the row above and
+ # can be recovered from the DB, which is the source of truth anyway.
logger.info(
"connector.capabilities.updated",
extra={
"tenant_id": current_user.tenant_id,
"connector_id": str(connector_id),
- # _safe_connector_type reconstructs from a whitelist regex
- # (negative-class sub) — that's a taint-breaker CodeQL
- # actually recognises, unlike _safe_log_val's strip-only
- # cleanup which it (correctly) treats as cosmetic.
"connector_type": _safe_connector_type(connector.connector_type),
- "allowed": request.allowed_capabilities,
+ "allowed_count": len(request.allowed_capabilities),
},
)
@@ -1111,14 +1113,19 @@ async def troubleshoot_connection(
detail="connector_type contains invalid characters",
)
safe_type = _safe_connector_type(request.connector_type)
+ # Diagnostics log: caller-supplied ``request.auth_config_keys`` is a
+ # list of arbitrary strings and would carry taint into the log
+ # record (CodeQL py/log-injection). For triage we only need the
+ # *count* — the actual key names on a misconfigured connector are
+ # available via the schema endpoint and don't add forensic value
+ # here. ``safe_type`` is already reconstructed via
+ # ``_safe_connector_type`` (whitelist regex) so it's a recognised
+ # taint break. ``err_len`` is an int, not a string.
logger.info(
"connectors.troubleshoot",
extra={
- # safe_type is already taint-broken by _safe_connector_type
- # above; wrapping it in _safe_log_val just re-introduced a
- # function CodeQL doesn't model as a sanitizer.
"type": safe_type,
- "keys": sorted(request.auth_config_keys)[:8], # cap to avoid log spam
+ "keys_count": len(request.auth_config_keys),
"err_len": len(request.error),
},
)
diff --git a/services/api/app/api/v1/endpoints/oauth.py b/services/api/app/api/v1/endpoints/oauth.py
index ef7c9a3ea..8ad2d7d85 100644
--- a/services/api/app/api/v1/endpoints/oauth.py
+++ b/services/api/app/api/v1/endpoints/oauth.py
@@ -65,7 +65,7 @@ import secrets
import uuid
from datetime import UTC, datetime, timedelta
from typing import Annotated, Any
-from urllib.parse import urlencode, urlparse
+from urllib.parse import quote, urlencode, urlparse, urlsplit, urlunsplit
import httpx
from fastapi import APIRouter, Depends, HTTPException, Query, status
@@ -294,17 +294,66 @@ def _validate_return_to(return_to: str | None) -> str:
only:
* Pure paths starting with ``/`` (e.g. ``/onboarding``).
* Absolute URLs whose origin matches ``CONSOLE_PUBLIC_BASE_URL``.
+
+ Every accepted branch *reconstructs* the URL from validated
+ components via ``urlunsplit`` + ``quote`` rather than returning
+ the caller's string verbatim. That's the canonical taint break
+ CodeQL recognises for ``py/url-redirection`` — it cannot reason
+ about ``startswith`` allowlists alone, but it does know that
+ rebuilding a URL from individually-quoted parts removes the
+ open-redirect class entirely.
"""
default = "/onboarding"
if not return_to:
return default
- if return_to.startswith("/") and not return_to.startswith("//"):
- return return_to
+
+ parsed = urlsplit(return_to)
+ # Path-only (relative) navigation inside the console.
+ if (
+ not parsed.scheme
+ and not parsed.netloc
+ and parsed.path.startswith("/")
+ and not parsed.path.startswith("//")
+ ):
+ # ``safe`` keeps URL-meaningful chars; everything else gets
+ # percent-encoded, which both hardens the value and gives
+ # CodeQL a sanitizer it tracks.
+ safe_path = quote(parsed.path, safe="/-._~")
+ safe_query = quote(parsed.query, safe="=&-._~")
+ safe_fragment = quote(parsed.fragment, safe="-._~")
+ return urlunsplit(("", "", safe_path, safe_query, safe_fragment))
+
base = _resolve_console_base()
- if base and return_to.startswith(base + "/"):
- return return_to
- if base and return_to == base:
- return return_to
+ if base:
+ base_parsed = urlsplit(base)
+ # Same-origin absolute URL: rebuild from the base scheme/host
+ # we trust, plus the path/query/fragment of the input — never
+ # echo the raw string. The host check below also guarantees
+ # the origin matches before we reconstruct.
+ if (
+ parsed.scheme == base_parsed.scheme
+ and parsed.netloc == base_parsed.netloc
+ and (
+ parsed.path == ""
+ or (
+ parsed.path.startswith("/")
+ and not parsed.path.startswith("//")
+ )
+ )
+ ):
+ safe_path = quote(parsed.path or "/", safe="/-._~")
+ safe_query = quote(parsed.query, safe="=&-._~")
+ safe_fragment = quote(parsed.fragment, safe="-._~")
+ return urlunsplit(
+ (
+ base_parsed.scheme,
+ base_parsed.netloc,
+ safe_path,
+ safe_query,
+ safe_fragment,
+ )
+ )
+
logger.info(
"oauth.return_to.rejected",
extra={