Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

fix(sentry): close credential redaction lifecycle gaps (#2688)

Call Whiteee76f0fe1ad6
diff --git a/pyproject.toml b/pyproject.toml
index a0f0bec4e..a0de70d83 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -66,7 +66,7 @@ dependencies = [
"fastapi>=0.110",
"lark-oapi>=1.4.0",
"requests>=2.31.0",
- "sentry-sdk>=2.0.0",
+ "sentry-sdk==2.56.0",
"python-socks[asyncio]>=2.0.0",
"aiohttp-socks>=0.8.0",
"httpx[socks]>=0.27",
diff --git a/tests/test_sentry_integration.py b/tests/test_sentry_integration.py
index 9e41debf8..e149d8a2d 100644
--- a/tests/test_sentry_integration.py
+++ b/tests/test_sentry_integration.py
@@ -7,6 +7,8 @@ import ast
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
+import pytest
+
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from config.v2_config import AgentsConfig, RuntimeConfig, SlackConfig, V2Config
@@ -23,6 +25,24 @@ def _config() -> V2Config:
)
[email protected](autouse=True)
+def _isolate_user_state(monkeypatch, tmp_path):
+ home = tmp_path / "home"
+ monkeypatch.setenv("HOME", str(home))
+ monkeypatch.setenv("XDG_CONFIG_HOME", str(home / "config"))
+ monkeypatch.setenv("XDG_DATA_HOME", str(home / "data"))
+ monkeypatch.setenv("XDG_CACHE_HOME", str(home / "cache"))
+ monkeypatch.setenv("XDG_STATE_HOME", str(home / "state"))
+ monkeypatch.setenv("AVIBE_HOME", str(tmp_path / "avibe"))
+ with sentry_integration._KNOWN_SECRETS_LOCK:
+ sentry_integration._known_secret_files_state.clear()
+ sentry_integration._known_secret_values.clear()
+ sentry_integration._known_secret_snapshot = (False, None, None)
+ sentry_integration._known_secret_collection_uncertain = False
+ sentry_integration._known_secret_collection_overflowed = False
+ assert sentry_integration._refresh_known_secret_state()
+
+
def test_resolve_sentry_options_prefers_environment(monkeypatch):
monkeypatch.setenv("VIBE_SENTRY_DSN", "https://[email protected]/2")
monkeypatch.setenv("VIBE_DEPLOYMENT_ENV", "regression")
@@ -51,12 +71,12 @@ def test_resolve_sentry_options_honors_empty_env_dsn_as_opt_out(monkeypatch):
assert sentry_integration.resolve_sentry_options() is None
-def test_detect_sentry_environment_defaults_to_local(monkeypatch):
+def test_detect_sentry_environment_defaults_to_local(monkeypatch, tmp_path):
monkeypatch.delenv("VIBE_SENTRY_ENVIRONMENT", raising=False)
monkeypatch.delenv("SENTRY_ENVIRONMENT", raising=False)
monkeypatch.delenv("VIBE_DEPLOYMENT_ENV", raising=False)
monkeypatch.delenv("E2E_TEST_MODE", raising=False)
- monkeypatch.setenv("AVIBE_HOME", "/tmp/vibe-remote-home")
+ monkeypatch.setenv("AVIBE_HOME", str(tmp_path / "avibe"))
monkeypatch.setattr(sentry_integration, "Path", lambda _: type("P", (), {"exists": staticmethod(lambda: False)})())
assert sentry_integration.detect_sentry_environment() == "local"
@@ -70,18 +90,18 @@ def test_detect_sentry_environment_uses_explicit_deployment_env(monkeypatch):
assert sentry_integration.detect_sentry_environment() == "production"
-def test_detect_sentry_environment_marks_integration_mode(monkeypatch):
+def test_detect_sentry_environment_marks_integration_mode(monkeypatch, tmp_path):
monkeypatch.delenv("VIBE_SENTRY_ENVIRONMENT", raising=False)
monkeypatch.delenv("SENTRY_ENVIRONMENT", raising=False)
monkeypatch.delenv("VIBE_DEPLOYMENT_ENV", raising=False)
monkeypatch.setenv("E2E_TEST_MODE", "true")
- monkeypatch.setenv("AVIBE_HOME", "/tmp/vibe-remote-home")
+ monkeypatch.setenv("AVIBE_HOME", str(tmp_path / "avibe"))
assert sentry_integration.detect_sentry_environment() == "integration"
-def test_build_sentry_contexts_contains_debug_metadata(monkeypatch):
- monkeypatch.setenv("AVIBE_HOME", "/tmp/vibe-remote-home")
+def test_build_sentry_contexts_contains_debug_metadata(monkeypatch, tmp_path):
+ monkeypatch.setenv("AVIBE_HOME", str(tmp_path / "avibe"))
contexts = sentry_integration.build_sentry_contexts(_config(), component="service", environment="regression")
@@ -138,6 +158,8 @@ def test_init_sentry_disables_client_discard_reports(monkeypatch):
assert sentry_integration.init_sentry(_config(), component="service") is True
assert init_kwargs["send_client_reports"] is False
+ assert init_kwargs["spotlight"] is False
+ assert init_kwargs["transport"] is sentry_integration._RedactingHttpTransport
def test_run_ui_server_skips_sentry_when_config_load_fails(monkeypatch):
@@ -335,23 +357,714 @@ def test_scrub_data_redacts_sensitive_values():
assert "[Filtered]" in scrubbed["message"]
-def test_before_send_throttles_repeated_noisy_slack_socket_events(monkeypatch):
- monkeypatch.delenv("VIBE_SENTRY_NOISE_FILTERS", raising=False)
- monkeypatch.delenv("VIBE_SENTRY_NOISE_TTL_SECONDS", raising=False)
+ ("text", "secret", "kept"),
+ [
+ (
+ "lark config rejected: LarkConfig(app_id='cli_x', app_secret='lark-s3cret', domain='feishu')",
+ "lark-s3cret",
+ "app_id='cli_x'",
+ ),
+ (
+ 'save failed: {"api_key": "sk-s3cret", "base_url": "https://api.example"}',
+ "sk-s3cret",
+ '"base_url": "https://api.example"',
+ ),
+ ("refresh failed client_secret=gw-s3cret status=401", "gw-s3cret", "status=401"),
+ # Credential names the code uses, in the spellings it uses them.
+ ({"provider": {"apiKey": "oc-s3cret"}, "model": "m1"}, "oc-s3cret", '"model": "m1"'),
+ ("request headers x-api-key: an-s3cret accept: json", "an-s3cret", "accept: json"),
+ ("engine health X-Management-Key: mk-s3cret status=503", "mk-s3cret", "status=503"),
+ ("media decrypt failed aes_key=aes-s3cret file=a.jpg", "aes-s3cret", "file=a.jpg"),
+ ('{"passphrase": "pp-s3cret", "vault": "v1"}', "pp-s3cret", '"vault": "v1"'),
+ ("Binding(private_key='pk-s3cret', agent='a1')", "pk-s3cret", "agent='a1'"),
+ # Generic names, as whole keys only: Model Hub validates ``{"key": ...}``.
+ ("probe failed {'vendor': 'openai', 'key': 'mh-s3cret'} cache_key=c1", "mh-s3cret", "cache_key=c1"),
+ ("upstream auth: Bearer au-s3cret author=me", "au-s3cret", "author=me"),
+ # Quoting: an escaped quote inside, a trailing backslash, JSON in a JSON string.
+ ('api_key="ab\\"esc-s3cret" retry=2', "esc-s3cret", "retry=2"),
+ ("cfg {'api_key': 'bs-s3cret\\\\'}", "bs-s3cret", "cfg {'api_key': "),
+ ('{"body": "{\\"api_key\\": \\"nest-s3cret\\", \\"n\\": 1}"}', "nest-s3cret", '\\"n\\": 1'),
+ # Any URL keeps only its origin.
+ (
+ "poll error url=https://api.telegram.org/bot123456:AAHs3cretS3cretS3cretS3cretS3cret/getUpdates",
+ "AAHs3cretS3cretS3cretS3cretS3cret",
+ "url=https://api.telegram.org/",
+ ),
+ ("409 url=https://hooks.slack.com/actions/T01/123/sl-s3cret", "sl-s3cret", "https://hooks.slack.com/"),
+ ("POST https://discord.com/api/v10/interactions/1/dc-s3cret/callback", "dc-s3cret", "https://discord.com/"),
+ ("GET https://cdn.weixin.qq.com/download?encrypted_query_param=wx-s3cret", "wx-s3cret", "https://cdn.weixin.qq.com/"),
+ ("proxy error http://alice:[email protected]:8080 timed out", "px-s3cret", "@proxy.local:8080 timed out"),
+ ("bad proxy http://bob:p/[email protected]/x", "w-s3cret", "bad proxy http://"),
+ # A quote or bracket in the password must not end the URL early.
+ ("proxy error http://alice:pa'[email protected]:8080 timed out", "sq-s3cret", "@proxy.local:8080 timed out"),
+ ('proxy error http://alice:pa"[email protected]:8080 timed out', "dq-s3cret", "@proxy.local:8080 timed out"),
+ ("proxy error http://alice:pa`[email protected]:8080 timed out", "bt-s3cret", "@proxy.local:8080 timed out"),
+ ("cfg {'proxy_url': 'http://alice:pa\\'[email protected]:8080'}", "rq-s3cret", "@proxy.local:8080'}"),
+ ("POST hooks.slack.com/actions/T01/123/ns-s3cret failed", "ns-s3cret", "POST hooks.slack.com/"),
+ # The same URLs spelled another way.
+ ('{"response_url":"https:\\/\\/hooks.slack.com\\/actions\\/T01\\/js-s3cret"}', "js-s3cret", "hooks.slack.com/"),
+ ("redirect=https%3A%2F%2Fhooks.slack.com%2Factions%2FT01%2Fpe-s3cret", "pe-s3cret", "hooks.slack.com/"),
+ ("next=https%253A%252F%252Fdiscord.com%252Fapi%252Fde-s3cret", "de-s3cret", "discord.com/"),
+ ("https:\\u002f\\u002fdiscord.com\\u002fapi\\u002fue-s3cret", "ue-s3cret", "discord.com/"),
+ ("https://discord.com/api/he-s3cret", "he-s3cret", "discord.com/"),
+ ("https:\\\\discord.com\\api\\bp-s3cret", "bp-s3cret", "discord.com/"),
+ ("fetch //discord.com/api/webhooks/1/sr-s3cret", "sr-s3cret", "fetch //discord.com/"),
+ ("GET 10.0.0.5:8080/download/ip-s3cret", "ip-s3cret", "GET 10.0.0.5:8080/"),
+ ("GET localhost:5123/api/media/lh-s3cret", "lh-s3cret", "GET localhost:5123/"),
+ ("GET [::1]:5123/api/media/v6-s3cret", "v6-s3cret", "GET [::1]:5123/"),
+ ("state=abc%26access_token%3Dqe-s3cret", "qe-s3cret", "state=abc&access_token="),
+ # A quoted value longer than any scan window is redacted to its end.
+ ('api_key="' + "x" * 9000 + 'lv-s3cret" next', "lv-s3cret", 'api_key="'),
+ ],
+)
+def test_scrub_data_redacts_secrets_inside_free_text(text, secret, kept):
+ """Log and exception messages carry secrets as text, not under a dict key."""
+ import json
+
+ scrubbed = sentry_integration.scrub_data({"logentry": {"params": [text]}})
+
+ (value,) = scrubbed["logentry"]["params"]
+ rendered = json.dumps(value) if isinstance(value, dict) else value
+ assert secret not in rendered
+ assert "[Filtered]" in rendered
+ assert kept in rendered
+ assert sentry_integration.scrub_data(value) == value
+
+
+ "text",
+ [
+ '{"url":"http://host:8080","email":"[email protected]"}',
+ "{'url':'http://host:8080','email':'[email protected]'}",
+ "path /Users/x/.avibe/config/config.json line 3, see ./scripts/run.sh",
+ "Retrying in 5s (attempt 2/3) status: 401 cache_key=c1 author=me",
+ ],
+)
+def test_scrub_data_keeps_ordinary_text(text):
+ """Over-redaction costs the debugging the events exist for."""
+ assert sentry_integration.scrub_data(text) == text
+
+
+def test_before_send_redacts_stored_credentials_in_any_text(monkeypatch, tmp_path):
+ """A credential Avibe stores leaves no trace however the text around it is shaped.
+
+ Prose such as ``auth failed for <secret>`` has no key for a pattern to find, a
+ secret can be escaped by JSON or repr or name a dict entry, and an unreadable
+ store must not keep a rotated credential from being covered.
+ """
+ import json
+
+ from config import paths
+ from config.v2_config import LarkConfig, TelegramConfig
+
+ monkeypatch.setenv("AVIBE_HOME", str(tmp_path))
+ sentry_integration._NOISY_EVENT_LAST_SEEN.clear()
+ sentry_integration._EVENT_RATE_STATE.clear()
+ config = _config()
+ config.lark = LarkConfig(
+ app_id="cli_x",
+ app_secret='lark"first\\s3cret',
+ proxy_url="http://bob:q'[email protected]:8080",
+ )
+ config.telegram = TelegramConfig(bot_token="s3cr7")
+ config.save()
+ state = paths.get_runtime_dir() / "model-hub" / "state"
+ (state / "credentials").mkdir(parents=True)
+ (state / "instances" / "i1").mkdir(parents=True)
+ (state / "credentials" / "cred_a.json").write_text(
+ json.dumps({"kind": "api_key", "vendor": "openai", "value": "mh-stored-s3cret"})
+ )
+ (state / "instances" / "i1" / "runtime-secrets.json").write_text(
+ json.dumps({"management_key": "mh-management-s3cret", "gateway_token": "mh-gateway-s3cret"})
+ )
+ assert sentry_integration._refresh_known_secret_state()
+
+ def send(message, extra=None):
+ event = {"logentry": {"message": message}, "breadcrumbs": {"values": [{"message": message}]}}
+ if extra is not None:
+ event["extra"] = extra
+ return json.dumps(sentry_integration.before_send(event, {}))
+
+ first = send(
+ "rejected mh-stored-s3cret and mh-management-s3cret (openai); short s3cr7! "
+ + json.dumps({"note": 'lark"first\\s3cret'})
+ + " "
+ + repr('lark"first\\s3cret')
+ + " proxy refused q'proxy-s3cret",
+ extra={"sessions": {"mh-gateway-s3cret": "active"}},
+ )
+ for secret in ("mh-stored-s3cret", "mh-management-s3cret", "mh-gateway-s3cret", "s3cr7", "first", "proxy-s3cret"):
+ assert secret not in first
+ assert "rejected [Filtered] and [Filtered] (openai); short [Filtered]!" in first
+
+ config.lark.app_secret = "lark-rotated-s3cret"
+ config.save()
+ assert sentry_integration._refresh_known_secret_state()
+ rotated = send("lark auth failed for lark-rotated-s3cret (cli_x)")
+ assert "lark-rotated-s3cret" not in rotated
+ assert "(cli_x)" in rotated
+
+
+def test_before_send_keeps_request_records_to_their_origin(monkeypatch, tmp_path):
+ """Request records keep method, origin and status, never paths, queries or bodies.
+
+ The shapes are the ones the SDK attaches: an HTTP client breadcrumb, a
+ subprocess breadcrumb and an incoming request.
+ """
+ import json
+
+ monkeypatch.setenv("AVIBE_HOME", str(tmp_path))
sentry_integration._NOISY_EVENT_LAST_SEEN.clear()
sentry_integration._EVENT_RATE_STATE.clear()
event = {
- "logger": "slack_sdk.socket_mode.aiohttp",
- "logentry": {
- "formatted": (
- "Failed to retrieve WSS URL: The request to the Slack API failed. "
- "(url: https://slack.com/api/apps.connections.open, status: 200)"
- )
+ "message": "request records",
+ "request": {
+ "method": "POST",
+ "url": "http://127.0.0.1:5123/api/media/md-s3cret",
+ "query_string": "state=st-s3cret",
+ "data": {"content": "body-s3cret"},
+ "cookies": "__Host-vibe_remote_session=ck-s3cret",
+ "headers": {"x-forwarded-for": "203.0.113.10"},
+ "env": {"REMOTE_ADDR": "203.0.113.10"},
+ },
+ "breadcrumbs": {
+ "values": [
+ {
+ "type": "http",
+ "category": "httplib",
+ "data": {
+ "http.method": "POST",
+ "url": "https://discord.com/api/webhooks/1/hk-s3cret",
+ "http.query": "wait=hq-s3cret",
+ "http.fragment": "fr-s3cret",
+ "http.response.status_code": 404,
+ },
+ },
+ {"type": "subprocess", "category": "subprocess", "message": "/usr/bin/git push https://x-access-token:[email protected]/o/r", "data": {"subprocess.cwd": "/srv/cw-s3cret"}},
+ ]
},
}
- assert sentry_integration.before_send(dict(event), {}) is not None
- assert sentry_integration.before_send(dict(event), {}) is None
+ sent = sentry_integration.before_send(event, {})
+ rendered = json.dumps(sent)
+
+ assert "s3cret" not in rendered
+ assert sent["request"] == {"method": "POST", "url": "http://127.0.0.1:5123/[Filtered]"}
+ http_crumb, subprocess_crumb = sent["breadcrumbs"]["values"]
+ assert http_crumb["data"]["url"] == "https://discord.com/[Filtered]"
+ assert http_crumb["data"]["http.response.status_code"] == 404
+ assert subprocess_crumb["message"] == "git"
+ assert subprocess_crumb["data"] == {}
+
+
+def test_known_secret_matching_survives_normalization_and_early_breadcrumbs(monkeypatch, tmp_path):
+ """A credential is protected before and after delimiter normalization on both hooks."""
+ import json
+
+ from config.v2_config import LarkConfig
+
+ monkeypatch.setenv("AVIBE_HOME", str(tmp_path / "avibe"))
+ monkeypatch.setenv("VIBE_SENTRY_EVENT_RATE_LIMIT_PER_WINDOW", "0")
+ config = _config()
+ config.lark = LarkConfig(app_secret="demo%3Acredential")
+ config.save()
+ assert sentry_integration._refresh_known_secret_state()
+
+ breadcrumb = sentry_integration.before_breadcrumb(
+ {"message": "early breadcrumb demo%3Acredential"}, {}
+ )
+ subprocess_breadcrumb = sentry_integration.before_breadcrumb(
+ {
+ "type": "subprocess",
+ "category": "subprocess",
+ "message": "/usr/bin/git push demo%3Acredential",
+ "data": {"credential": "demo%3Acredential"},
+ },
+ {},
+ )
+ event = {
+ "message": "event demo%3Acredential and normalized demo:credential",
+ "breadcrumbs": {"values": [breadcrumb]},
+ }
+ sent = sentry_integration.before_send(event, {})
+
+ rendered = json.dumps(sent)
+ assert "demo%3Acredential" not in rendered
+ assert "demo:credential" not in rendered
+ assert breadcrumb["message"] == "early breadcrumb [Filtered]"
+ assert subprocess_breadcrumb["message"] == "git"
+ assert subprocess_breadcrumb["data"] == {}
+
+
+def test_known_secret_redaction_preserves_protocol_keys_and_filters_user_maps(monkeypatch, tmp_path):
+ """Stored values cannot rewrite Sentry schema keys while custom maps stay protected."""
+ from config.v2_config import LarkConfig
+
+ monkeypatch.setenv("AVIBE_HOME", str(tmp_path / "avibe"))
+ monkeypatch.setenv("VIBE_SENTRY_EVENT_RATE_LIMIT_PER_WINDOW", "0")
+ config = _config()
+ config.lark = LarkConfig(app_secret="type")
+ config.save()
+ assert sentry_integration._refresh_known_secret_state()
+
+ sent = sentry_integration.before_send(
+ {
+ "message": "failure type",
+ "exception": {"values": [{"type": "ValueError", "value": "type"}]},
+ "threads": {"values": [{"type": "type", "stacktrace": {"frames": [{"type": "type"}]}}]},
+ "contexts": {
+ "trace": {"type": "type", "values": "type"},
+ "custom": {"type": "type"},
+ },
+ "spans": [{"op": "http.client", "data": {"type": "type"}}],
+ "extra": {"type": "type"},
+ },
+ {},
+ )
+
+ assert sent["message"] == "failure [Filtered]"
+ assert sent["exception"]["values"][0]["type"] == "ValueError"
+ assert sent["exception"]["values"][0]["value"] == "[Filtered]"
+ assert sent["threads"]["values"][0]["type"] == "[Filtered]"
+ assert sent["threads"]["values"][0]["stacktrace"]["frames"][0]["type"] == "[Filtered]"
+ assert sent["contexts"]["trace"]["type"] == "[Filtered]"
+ assert sent["contexts"]["trace"]["values"] == "[Filtered]"
+ assert sent["contexts"]["custom"]["[Filtered]"] == "[Filtered]"
+ assert sent["spans"][0]["data"] == {"[Filtered]": "[Filtered]"}
+
+ config.lark.app_secret = "threads"
+ config.save()
+ assert sentry_integration._refresh_known_secret_state()
+ shape = sentry_integration.before_send(
+ {"threads": {"values": [{"type": "threads"}]}, "extra": {"threads": "threads"}}, {}
+ )
+ assert "threads" in shape
+ assert shape["threads"]["values"][0]["type"] == "[Filtered]"
+ assert shape["extra"] == {"[Filtered]": "[Filtered]"}
+ assert sent["extra"] == {"[Filtered]": "[Filtered]"}
+
+
+def test_capture_hook_uses_published_snapshot_while_worker_refresh_blocks(monkeypatch, tmp_path):
+ import threading
+
+ from config.v2_config import LarkConfig
+
+ config = _config()
+ config.lark = LarkConfig(app_secret="hook-s3cret")
+ config.save()
+ assert sentry_integration._refresh_known_secret_state()
+ original_files = sentry_integration._known_secret_files
+ scan_entered = threading.Event()
+ release_scan = threading.Event()
+ hook_finished = threading.Event()
+
+ def blocked_files():
+ scan_entered.set()
+ assert release_scan.wait(2)
+ return original_files()
+
+ monkeypatch.setattr(sentry_integration, "_known_secret_files", blocked_files)
+ refresh = threading.Thread(target=sentry_integration._refresh_known_secret_state)
+ refresh.start()
+ assert scan_entered.wait(1)
+
+ def capture_breadcrumb():
+ result = sentry_integration.before_breadcrumb({"message": "ordinary warning"}, {})
+ assert result == {"message": "ordinary warning"}
+ hook_finished.set()
+
+ hook = threading.Thread(target=capture_breadcrumb)
+ hook.start()
+ try:
+ assert hook_finished.wait(1)
+ finally:
+ release_scan.set()
+ refresh.join(2)
+ hook.join(2)
+ assert hook_finished.is_set()
+
+
+def test_init_sentry_disables_spotlight_env_bypass(monkeypatch, tmp_path):
+ import sentry_sdk
+
+ monkeypatch.setenv("AVIBE_HOME", str(tmp_path))
+ monkeypatch.setenv("VIBE_SENTRY_DSN", "http://[email protected]:9/1")
+ monkeypatch.setenv("SENTRY_SPOTLIGHT", "true")
+ sent = []
+
+ class Response:
+ status = 200
+ headers = {}
+ data = b""
+ content = b""
+
+ def close(self):
+ return None
+
+ monkeypatch.setattr(
+ sentry_integration._RedactingHttpTransport,
+ "_request",
+ lambda _transport, _method, _endpoint_type, body, headers: sent.append(body) or Response(),
+ )
+ try:
+ assert sentry_integration.init_sentry(_config(), component="service") is True
+ client = sentry_sdk.get_client()
+ assert client.spotlight is None
+ sentry_sdk.capture_message("spotlight disabled")
+ sentry_sdk.flush(timeout=2)
+ assert sent
+ finally:
+ sentry_sdk.get_client().close(timeout=0)
+ sentry_sdk.get_global_scope().set_client(None)
+
+
+def test_performance_transactions_are_scrubbed_like_events(monkeypatch, tmp_path):
+ """Transactions skip ``before_send``; their spans must not carry what events may not."""
+ import gzip
+
+ import sentry_sdk
+ from sentry_sdk.envelope import Envelope
+
+ monkeypatch.setenv("AVIBE_HOME", str(tmp_path))
+ monkeypatch.setenv("VIBE_SENTRY_DSN", "http://[email protected]:9/1")
+ monkeypatch.setenv("VIBE_SENTRY_TRACES_SAMPLE_RATE", "1.0")
+ sent = []
+
+ class Response:
+ status = 200
+ headers = {}
+ data = b""
+ content = b""
+
+ def close(self):
+ return None
+
+ def request(_transport, _method, _endpoint_type, body, headers):
+ sent.append((body, headers))
+ return Response()
+
+ monkeypatch.setattr(sentry_integration._RedactingHttpTransport, "_request", request)
+ try:
+ assert sentry_integration.init_sentry(_config(), component="service") is True
+ with sentry_sdk.start_transaction(op="task", name="refresh"):
+ with sentry_sdk.start_span(op="http.client", name="POST https://discord.com/api/webhooks/1/tx-s3cret") as span:
+ span.set_data("url", "https://discord.com/api/webhooks/1/tx-s3cret")
+ span.set_data("http.query", "wait=tq-s3cret")
+ with sentry_sdk.start_span(op="subprocess", name="git push https://x:[email protected]/o/r"):
+ pass
+ with sentry_sdk.start_span(op="task", name="refresh lark app_secret=ts-s3cret"):
+ pass
+ sentry_sdk.flush(timeout=2)
+ finally:
+ sentry_sdk.get_client().close(timeout=0)
+ sentry_sdk.get_global_scope().set_client(None)
+
+ (body, headers), = sent
+ raw = gzip.decompress(body) if headers.get("Content-Encoding") == "gzip" else body
+ envelope = Envelope.deserialize(raw)
+ payload = next(item.get_bytes().decode("utf-8") for item in envelope.items if item.type == "transaction")
+ assert "s3cret" not in payload
+ assert "https://discord.com/[Filtered]" in payload
+
+
+def test_real_sdk_event_shapes_preserve_protocol_ownership(monkeypatch, tmp_path):
+ import gzip
+ import json
+
+ import sentry_sdk
+ from config.v2_config import LarkConfig, TelegramConfig
+ from sentry_sdk.envelope import Envelope
+
+ monkeypatch.setenv("AVIBE_HOME", str(tmp_path))
+ monkeypatch.setenv("VIBE_SENTRY_DSN", "http://[email protected]:9/1")
+ config = _config()
+ config.slack.signing_secret = "logentry"
+ config.lark = LarkConfig(app_secret="frames")
+ config.telegram = TelegramConfig(bot_token="type")
+ config.save()
+ sent = []
+
+ class Response:
+ status = 200
+ headers = {}
+ data = b""
+ content = b""
+
+ def close(self):
+ return None
+
+ monkeypatch.setattr(
+ sentry_integration._RedactingHttpTransport,
+ "_request",
+ lambda _transport, _method, _endpoint_type, body, headers: sent.append((body, headers)) or Response(),
+ )
+ try:
+ assert sentry_integration.init_sentry(config, component="service") is True
+ sentry_sdk.capture_event(
+ {
+ "message": "schema ownership",
+ "logentry": {"message": "failure"},
+ "exception": {
+ "values": [
+ {"type": "ValueError", "stacktrace": {"frames": [{"filename": "worker.py"}]}}
+ ]
+ },
+ "contexts": {
+ "trace": {"type": "trace", "trace_id": "a" * 32, "span_id": "b" * 16},
+ "custom": {"type": "type"},
+ },
+ }
+ )
+ sentry_sdk.flush(timeout=2)
+ finally:
+ sentry_sdk.get_client().close(timeout=0)
+ sentry_sdk.get_global_scope().set_client(None)
+
+ (body, headers), = sent
+ raw = gzip.decompress(body) if headers.get("Content-Encoding") == "gzip" else body
+ envelope = Envelope.deserialize(raw)
+ payload = json.loads(next(item.get_bytes() for item in envelope.items if item.type == "event"))
+ assert payload["logentry"]["message"] == "failure"
+ assert payload["exception"]["values"][0]["stacktrace"]["frames"][0]["filename"] == "worker.py"
+ assert payload["contexts"]["trace"]["type"] == "trace"
+ assert payload["contexts"]["custom"] == {"[Filtered]": "[Filtered]"}
+
+
+def test_worker_scrub_retains_rotated_secret_after_enqueue(monkeypatch, tmp_path):
+ """A value queued before rotation remains covered by the process-lifetime union."""
+ import gzip
+ import threading
+
+ import sentry_sdk
+ from config.v2_config import LarkConfig
+ from sentry_sdk.envelope import Envelope
+
+ monkeypatch.setenv("AVIBE_HOME", str(tmp_path))
+ config = _config()
+ config.lark = LarkConfig(app_secret="old-worker-s3cret")
+ config.save()
+ monkeypatch.setenv("VIBE_SENTRY_DSN", "http://[email protected]:9/1")
+ assert sentry_integration.init_sentry(config, component="service") is True
+ sent = []
+
+ class Response:
+ status = 200
+ headers = {}
+ data = b""
+ content = b""
+
+ def close(self):
+ return None
+
+ def request(_transport, _method, _endpoint_type, body, headers):
+ sent.append((body, headers))
+ return Response()
+
+ monkeypatch.setattr(sentry_integration._RedactingHttpTransport, "_request", request)
+ original_files = sentry_integration._known_secret_files
+ scan_entered = threading.Event()
+ release_scan = threading.Event()
+
+ def blocked_files():
+ scan_entered.set()
+ assert release_scan.wait(2)
+ return original_files()
+
+ monkeypatch.setattr(sentry_integration, "_known_secret_files", blocked_files)
+ transport = sentry_sdk.get_client().transport
+ envelope = Envelope()
+ envelope.add_event({"message": "old-worker-s3cret"})
+ transport.capture_envelope(envelope)
+ assert scan_entered.wait(2)
+
+ config.lark.app_secret = "new-worker-s3cret"
+ config.save()
+ release_scan.set()
+ sentry_sdk.flush(timeout=2)
+ assert transport._worker.is_alive
+ transport.kill()
+ assert not transport._worker.is_alive
+
+ try:
+ (body, headers), = sent
+ raw = gzip.decompress(body) if headers.get("Content-Encoding") == "gzip" else body
+ assert "old-worker-s3cret" not in raw.decode("utf-8")
+ finally:
+ sentry_sdk.get_client().close(timeout=0)
+ sentry_sdk.get_global_scope().set_client(None)
+
+
+def test_worker_drops_envelope_when_secret_read_is_uncertain(monkeypatch, tmp_path):
+ import sentry_sdk
+ from config import paths
+ from config.v2_config import LarkConfig
+ from sentry_sdk.envelope import Envelope
+
+ monkeypatch.setenv("AVIBE_HOME", str(tmp_path))
+ config = _config()
+ config.lark = LarkConfig(app_secret="uncertain-worker-s3cret")
+ config.save()
+ monkeypatch.setenv("VIBE_SENTRY_DSN", "http://[email protected]:9/1")
+ assert sentry_integration.init_sentry(config, component="service") is True
+ sent = []
+
+ class Response:
+ status = 200
+ headers = {}
+
+ def close(self):
+ return None
+
+ monkeypatch.setattr(
+ sentry_integration._RedactingHttpTransport,
+ "_request",
+ lambda _transport, _method, _endpoint_type, body, headers: sent.append(body) or Response(),
+ )
+ paths.get_config_path().write_text("{not json", encoding="utf-8")
+ envelope = Envelope()
+ envelope.add_event({"message": "uncertain-worker-s3cret"})
+ sentry_sdk.get_client().transport.capture_envelope(envelope)
+ sentry_sdk.flush(timeout=2)
+
+ try:
+ assert sent == []
+ finally:
+ sentry_sdk.get_client().close(timeout=0)
+ sentry_sdk.get_global_scope().set_client(None)
+
+
+def test_worker_scrub_drops_attachments_and_unknown_raw_items(monkeypatch, tmp_path):
+ from config.v2_config import LarkConfig
+ from sentry_sdk.envelope import Envelope, Item
+
+ monkeypatch.setenv("AVIBE_HOME", str(tmp_path))
+ config = _config()
+ config.lark = LarkConfig(app_secret="raw-item-s3cret")
+ config.save()
+ assert sentry_integration._refresh_known_secret_state()
+
+ envelope = Envelope(headers={"trace": {"trace_id": "trace-id"}, "diagnostic": "raw-item-s3cret"})
+ envelope.add_event({"message": "raw-item-s3cret"})
+ envelope.add_item(Item(payload=b"raw-item-s3cret", type="attachment"))
+ envelope.add_item(Item(payload=b"raw-item-s3cret", type="future_item"))
+
+ scrubbed = sentry_integration._scrub_envelope(envelope)
+ assert envelope.items[0].payload.json["message"] == "raw-item-s3cret"
+ assert [item.type for item in scrubbed.items] == ["event"]
+ assert "raw-item-s3cret" not in scrubbed.serialize().decode("utf-8")
+
+ only_attachment = Envelope(items=[Item(payload=b"raw-item-s3cret", type="attachment")])
+ with pytest.raises(sentry_integration._DropEnvelope):
+ sentry_integration._scrub_envelope(only_attachment)
+
+ unsafe_headers = Envelope(headers={"trace": {"secret": "raw-item-s3cret"}})
+ unsafe_headers.add_event({"message": "safe"})
+ with pytest.raises(sentry_integration._DropEnvelope):
+ sentry_integration._scrub_envelope(unsafe_headers)
+
+
+def test_worker_scrub_covers_default_structured_item_families(monkeypatch, tmp_path):
+ from config.v2_config import LarkConfig
+ from sentry_sdk.envelope import Envelope, Item, PayloadRef
+
+ monkeypatch.setenv("AVIBE_HOME", str(tmp_path))
+ config = _config()
+ config.lark = LarkConfig(app_secret="structured-item-s3cret")
+ config.save()
+ assert sentry_integration._refresh_known_secret_state()
+ payloads = {
+ "log": {"items": [{"body": "structured-item-s3cret", "attributes": {"secret": "structured-item-s3cret"}}]},
+ "trace_metric": {"items": [{"name": "structured-item-s3cret", "attributes": {"secret": "structured-item-s3cret"}}]},
+ "span": {"items": [{"name": "structured-item-s3cret", "attributes": {"secret": "structured-item-s3cret"}}]},
+ "session": {"sid": "structured-item-s3cret"},
+ "sessions": {"aggregates": [{"sid": "structured-item-s3cret"}]},
+ "client_report": {"discarded_events": [{"reason": "structured-item-s3cret"}]},
+ "check_in": {"check_in_id": "structured-item-s3cret"},
+ "profile": {"sample": "structured-item-s3cret"},
+ "profile_chunk": {"sample": "structured-item-s3cret"},
+ }
+
+ for item_type, payload in payloads.items():
+ envelope = Envelope(items=[Item(payload=PayloadRef(json=payload), type=item_type)])
+ scrubbed = sentry_integration._scrub_envelope(envelope)
+ assert scrubbed.items[0].type == item_type
+ assert "structured-item-s3cret" not in scrubbed.serialize().decode("utf-8")
+
+
+def test_worker_scrub_fails_closed_on_observation_overflow(monkeypatch, tmp_path):
+ from config.v2_config import LarkConfig
+ from sentry_sdk.envelope import Envelope
+
+ monkeypatch.setenv("AVIBE_HOME", str(tmp_path))
+ config = _config()
+ config.lark = LarkConfig(app_secret="overflow-worker-s3cret")
+ config.save()
+ monkeypatch.setattr(sentry_integration, "_KNOWN_SECRET_MAX_VALUES", 0)
+
+ assert not sentry_integration._refresh_known_secret_state()
+ assert sentry_integration._known_secret_files_state == {}
+ assert "overflow-worker-s3cret" not in sentry_integration._known_secret_values
+ config_path = sentry_integration.paths.get_config_path()
+ config_path.unlink()
+ assert not sentry_integration._refresh_known_secret_state()
+ assert sentry_integration._scrub_outgoing({"message": "overflow-worker-s3cret"}) is None
+ assert sentry_integration.before_send({"message": "overflow-worker-s3cret"}, {}) is None
+ envelope = Envelope()
+ envelope.add_event({"message": "overflow-worker-s3cret"})
+ with pytest.raises(sentry_integration._DropEnvelope):
+ sentry_integration._scrub_envelope(envelope)
+
+
+def test_worker_scrub_fails_closed_on_file_state_overflow(monkeypatch, tmp_path):
+ monkeypatch.setattr(sentry_integration, "_KNOWN_SECRET_MAX_FILES", 0)
+ config = _config()
+ config.save()
+
+ assert not sentry_integration._refresh_known_secret_state()
+ assert sentry_integration._known_secret_files_state == {}
+ assert not sentry_integration._known_secret_collection_is_complete()
+
+
[email protected]("unit", ["\\", "a.", "x-api-"])
+def test_scrub_data_stays_linear_on_pathological_text(unit):
+ """Scrubbing runs synchronously on every log call, so it must never backtrack."""
+ import time
+
+ text = unit * (40_000 // len(unit))
+ started = time.perf_counter()
+ sentry_integration.scrub_data(text)
+ assert time.perf_counter() - started < 1.0
+
+
+ ("first", "second"),
+ [
+ (
+ "Failed to retrieve WSS URL: The request to the Slack API failed. "
+ "(url: https://slack.com/api/apps.connections.open, status: 200)",
+ "Failed to retrieve WSS URL: The request to the Slack API failed. "
+ "(url: https://slack.com/api/apps.connections.open, status: 200)",
+ ),
+ # Two different reconnect failures share the filter's fingerprint only
+ # through the method name, which appears inside the URL.
+ (
+ "The request to the Slack API failed. (url: https://slack.com/api/apps.connections.open, status: 200)",
+ "The request to the Slack API failed. (url: https://slack.com/api/apps.connections.open, status: 503)",
+ ),
+ ],
+)
+def test_before_send_throttles_repeated_noisy_slack_socket_events(monkeypatch, first, second):
+ monkeypatch.delenv("VIBE_SENTRY_NOISE_FILTERS", raising=False)
+ monkeypatch.delenv("VIBE_SENTRY_NOISE_TTL_SECONDS", raising=False)
+ sentry_integration._NOISY_EVENT_LAST_SEEN.clear()
+ sentry_integration._EVENT_RATE_STATE.clear()
+
+ def event(formatted):
+ return {"logger": "slack_sdk.socket_mode.aiohttp", "logentry": {"formatted": formatted}}
+
+ assert sentry_integration.before_send(event(first), {}) is not None
+ assert sentry_integration.before_send(event(second), {}) is None
def test_before_send_throttles_repeated_normal_errors(monkeypatch):
@@ -368,17 +1081,31 @@ def test_before_send_throttles_repeated_normal_errors(monkeypatch):
assert sentry_integration.before_send(dict(event), {}) is None
-def test_before_send_allows_distinct_normal_errors(monkeypatch):
+ ("first", "second"),
+ [
+ (
+ "Failed to send result message: unexpected application bug",
+ "Failed to send result message: different application bug",
+ ),
+ # Errors on one host differ only by path, which the sent event no longer carries.
+ (
+ "Upload failed: 404 url=https://api.example.com/v1/files",
+ "Upload failed: 404 url=https://api.example.com/v1/messages",
+ ),
+ ],
+)
+def test_before_send_allows_distinct_normal_errors(monkeypatch, first, second):
monkeypatch.delenv("VIBE_SENTRY_EVENT_RATE_LIMIT_PER_WINDOW", raising=False)
sentry_integration._NOISY_EVENT_LAST_SEEN.clear()
sentry_integration._EVENT_RATE_STATE.clear()
first_event = {
"logger": "core.message_dispatcher",
- "logentry": {"formatted": "Failed to send result message: unexpected application bug"},
+ "logentry": {"formatted": first},
}
second_event = {
"logger": "core.message_dispatcher",
- "logentry": {"formatted": "Failed to send result message: different application bug"},
+ "logentry": {"formatted": second},
}
assert sentry_integration.before_send(dict(first_event), {}) is not None
diff --git a/uv.lock b/uv.lock
index 245c8c324..32888f944 100644
--- a/uv.lock
+++ b/uv.lock
@@ -375,7 +375,7 @@ requires-dist = [
{ name = "pywebpush", specifier = ">=2.0.3" },
{ name = "pyyaml", specifier = ">=6.0" },
{ name = "requests", specifier = ">=2.31.0" },
- { name = "sentry-sdk", specifier = ">=2.0.0" },
+ { name = "sentry-sdk", specifier = "==2.56.0" },
{ name = "slack-sdk", specifier = ">=3.26.0" },
{ name = "sqlalchemy", specifier = ">=2.0.0" },
{ name = "tomli", marker = "python_full_version < '3.11'", specifier = ">=2.0.1" },
diff --git a/vibe/sentry_integration.py b/vibe/sentry_integration.py
index b9f97c903..87b4be87a 100644
--- a/vibe/sentry_integration.py
+++ b/vibe/sentry_integration.py
@@ -1,5 +1,6 @@
from __future__ import annotations
+import json
import logging
import os
import platform
@@ -10,10 +11,16 @@ import threading
import time
from pathlib import Path
from typing import Any, Optional
+from urllib.parse import quote, urlsplit
from config.v2_config import V2Config
from config import paths
+try:
+ from sentry_sdk.transport import HttpTransport as _SentryHttpTransport
+except Exception: # pragma: no cover - the dependency is required in packaged builds
+ _SentryHttpTransport = None
+
logger = logging.getLogger(__name__)
# Fill this with the real project DSN to make Sentry default-on across deployments.
@@ -24,28 +31,199 @@ DEPLOYMENT_ENV_VAR = "VIBE_DEPLOYMENT_ENV"
_SENSITIVE_KEY_PARTS = (
"access_key",
+ "aes_key",
"api_key",
"app_token",
"authorization",
"bot_token",
"client_secret",
"cookie",
+ "credential",
"dsn",
+ "management_key",
+ "passphrase",
"password",
+ "private_key",
"secret",
"signing_secret",
"token",
"webhook",
"workspace_token",
)
+# Generic names a credential also travels under, matched only as the whole key so
+# ``cache_key`` or ``author`` stay readable. Model Hub sends ``{"key": ...}`` to
+# validate a credential before it is stored anywhere.
+_SENSITIVE_EXACT_KEYS = ("auth", "key")
+# Fields that carry a request's query string; dropped whole.
+_DROPPED_KEYS = frozenset({"http.query", "http.fragment", "query_string"})
_REDACTED = "[Filtered]"
+
+
+def _normalized_key(key: str) -> str:
+ # ``api_key``, ``apiKey``, ``x-api-key`` and ``API-KEY`` name the same credential.
+ return re.sub(r"[^a-z0-9]", "", key.lower())
+
+
+_SENSITIVE_KEY_TOKENS = tuple(_normalized_key(part) for part in _SENSITIVE_KEY_PARTS)
+
+
+def _text_key_pattern(token: str) -> str:
+ # A normalized key as it may be written in text: any case, with an optional
+ # ``_``, ``-`` or ``.`` between its characters.
+ return "[_.-]?".join(re.escape(char) for char in token)
+
+
+# A sensitive key rendered into free text: a dataclass repr, a header line,
+# ``key=value``, or JSON, including JSON nested inside a JSON string. The key
+# starts at a word boundary, and the key and its escaping depth are bounded so
+# long runs of word characters or backslashes stay linear.
+# A quoted value ends at its first unescaped closing delimiter; when none is
+# found, everything after the opening delimiter is redacted rather than kept. An
+# unquoted value led by an auth scheme (``Bearer <token>``) includes the token.
+_SENSITIVE_KEY_VALUE_RE = re.compile(
+ r"(?i)(?<![\w.-])(?P<kq>\\{0,4}[\"']?)"
+ r"(?:[\w.-]{0,40}(?:" + "|".join(_text_key_pattern(token) for token in _SENSITIVE_KEY_TOKENS) + r")[\w.-]{0,40}"
+ r"|" + "|".join(_text_key_pattern(key) for key in _SENSITIVE_EXACT_KEYS) + r")"
+ r"(?P=kq)\s*[:=]\s*"
+ r"(?:(?P<vq>\\{0,4}[\"'])(?P<quoted>.*?)(?:(?<!\\)(?P=vq)|\Z)"
+ r"|(?P<bare>(?:(?:basic|bearer|bot|digest|token)\s+)?(?:[^\s\"'\\]|\\(?![\"']))+))",
+ re.DOTALL,
+)
+
+
+def _redact_key_value(match: re.Match[str]) -> str:
+ group = "quoted" if match.group("quoted") is not None else "bare"
+ start, end = match.span(group)
+ offset = match.start()
+ text = match.group(0)
+ return text[: start - offset] + _REDACTED + text[end - offset :]
+
+
+# Any URL keeps only its origin. A path, query, fragment or userinfo carries
+# capabilities no key name announces: a Slack response_url, a Discord
+# interaction token, WeChat CDN parameters, a Telegram bot token, a proxy
+# password. An authority that is not a plain host[:port] is dropped whole. The
+# scheme is length-bounded so runs like ``a.a.a`` stay linear, and a backslash
+# stands for a slash as browsers read it; it ends the URL only before a quote.
+# A ``user:pass`` whose password holds a quote or bracket would end the URL
+# early, so a colon-bearing authority cut by one of them, with an ``@`` before
+# the next space, is read as userinfo running to that ``@`` - unless the cut is
+# compact JSON structure (``","`` or ``":"``), as after ``"http://host:8080"``.
+_URL_RE = re.compile(
+ r"(?i)\b(?P<scheme>[a-z][a-z0-9+.-]{0,30}:[/\\]{2})"
+ r"(?:(?P<userinfo>[^\s/\\?#\"'<>`@]*:[^\s/\\?#\"'<>`@]*"
+ r"(?![\"'][,:][\"'])\\{0,4}[\"'<>`](?:(?![\"'][,:][\"'])[^\s@]){0,256})@)?"
+ r"(?P<authority>[^\s/\\?#\"'<>`]*)"
+ r"(?P<rest>(?:[^\s\"'<>`\\]|\\(?![\"']))*)"
+)
+# The same for a URL written without its scheme: ``hooks.slack.com/actions/...``,
+# ``//host/path``, ``10.0.0.5:8080/path``, ``localhost:5123/path``.
+_SCHEMELESS_URL_RE = re.compile(
+ r"(?i)(?:(?<=//)|(?<![\w.@/-]))"
+ r"(?P<host>(?:(?:[a-z0-9-]{1,63}\.){1,10}[a-z]{2,24}|\d{1,3}(?:\.\d{1,3}){3}|localhost|\[[0-9a-f:.]{2,45}\])"
+ r"(?::\d{1,5})?)(?P<rest>/(?:[^\s\"'<>`\\]|\\(?![\"']))*)"
+)
+# URL delimiters spelled another way: percent-encoded (any depth of ``%25``),
+# JSON ``\u00XX`` or ``\/``, or an HTML character reference. They are read as
+# the delimiter itself before URLs and keys are matched.
+_DELIMITER_CHARACTERS = {
+ "3a": ":", "2f": "/", "5c": "\\", "3f": "?", "23": "#", "40": "@", "3d": "=", "26": "&",
+}
+_DELIMITER_DECIMALS = {"58": ":", "47": "/", "92": "\\", "63": "?", "35": "#", "64": "@", "61": "=", "38": "&"}
+_DELIMITER_NAMES = {
+ "colon": ":", "sol": "/", "bsol": "\\", "quest": "?", "num": "#", "commat": "@", "equals": "=", "amp": "&",
+}
+_ENCODED_DELIMITER_RE = re.compile(
+ r"(?i)%(?:25)*(?P<pct>3a|2f|5c|3f|23|40|3d|26)"
+ r"|\\u00(?P<uni>3a|2f|5c|3f|23|40|3d|26)"
+ r"|&#x0*(?P<hex>3a|2f|5c|3f|23|40|3d|26);?"
+ r"|&#0*(?P<dec>58|47|92|63|35|64|61|38);?"
+ r"|&(?P<name>colon|sol|bsol|quest|num|commat|equals|amp);"
+ r"|\\(?P<slash>/)"
+)
+
+
+def _decoded_delimiter(match: re.Match[str]) -> str:
+ if match.group("slash"):
+ return "/"
+ if match.group("dec"):
+ return _DELIMITER_DECIMALS[match.group("dec")]
+ if match.group("name"):
+ return _DELIMITER_NAMES[match.group("name").lower()]
+ code = match.group("pct") or match.group("uni") or match.group("hex")
+ return _DELIMITER_CHARACTERS[code.lower()]
+
+
+_URL_HOST_RE = re.compile(r"(?i)(?:[a-z0-9.-]+|\[[0-9a-f:.]+\])(?::\d{1,5})?")
+_URL_TRAILING_PUNCTUATION = ".,;:!?)]}"
+
+
+def _reduced_url(origin: str, rest: str) -> str:
+ marker = "/" + _REDACTED
+ if rest.startswith(marker) and not rest[len(marker) :].rstrip(_URL_TRAILING_PUNCTUATION):
+ # Already reduced: scrubbing runs on a breadcrumb and again on its event.
+ return origin + rest
+ path = rest.rstrip(_URL_TRAILING_PUNCTUATION)
+ return origin + (marker if path else "") + rest[len(path) :]
+
+
+def _url_origin(match: re.Match[str]) -> str:
+ _, at, host = match.group("authority").rpartition("@")
+ if not _URL_HOST_RE.fullmatch(host):
+ return match.group("scheme") + _REDACTED
+ has_userinfo = bool(at or match.group("userinfo") is not None)
+ return _reduced_url(match.group("scheme") + (_REDACTED + "@" if has_userinfo else "") + host, match.group("rest"))
+
+
+def _schemeless_url_origin(match: re.Match[str]) -> str:
+ return _reduced_url(match.group("host"), match.group("rest"))
+
+
+_URL_REPLACEMENTS = (
+ (_URL_RE, _url_origin),
+ (_SCHEMELESS_URL_RE, _schemeless_url_origin),
+)
_STRING_REPLACEMENTS = (
+ (_ENCODED_DELIMITER_RE, _decoded_delimiter),
+ *_URL_REPLACEMENTS,
(re.compile(r"(?i)(authorization[:=]\s*)(bearer\s+)?\S+"), r"\1" + _REDACTED),
(re.compile(r"(?i)(token=)[^&\s]+"), r"\1" + _REDACTED),
(re.compile(r"(?i)(cookie[:=]\s*)\S+"), r"\1" + _REDACTED),
(re.compile(r"\bxox[baprs]-[A-Za-z0-9-]+\b"), _REDACTED),
(re.compile(r"\bxapp-[A-Za-z0-9-]+\b"), _REDACTED),
+ (_SENSITIVE_KEY_VALUE_RE, _redact_key_value),
+ # A Telegram bot token outside a URL (``bot<id>:<secret>``).
+ (re.compile(r"\b(bot)?\d{5,}:[A-Za-z0-9_-]{30,}"), r"\1" + _REDACTED),
)
+# Credential values Avibe stores. Values shorter than this are not secrets worth
+# the noise; short ones (below the boundary length) match only as a whole word.
+_KNOWN_SECRET_MIN_LENGTH = 4
+_KNOWN_SECRET_BOUNDARY_LENGTH = 12
+# These caps bound the process-lifetime observation set and the compiled matcher.
+# They are deliberately fail-closed: once a cap is exceeded, old observations are
+# retained and telemetry is dropped rather than evicting a still-usable secret.
+_KNOWN_SECRET_MAX_VALUES = 4096
+_KNOWN_SECRET_MAX_VALUE_BYTES = 256 * 1024
+_KNOWN_SECRET_MAX_PATTERN_BYTES = 512 * 1024
+_KNOWN_SECRET_MAX_FILE_BYTES = 1024 * 1024
+_KNOWN_SECRET_MAX_FILES = 1024
+_KNOWN_SECRETS_LOCK = threading.RLock()
+# path -> file signature; secret values live only in the process-lifetime union below.
+_known_secret_files_state: dict[str, tuple[int, int, int, int]] = {}
+_known_secret_values: set[str] = set()
+# (ready, retained values at publication, compiled matcher). Hooks read this one
+# immutable pointer; refresh publishes only after the worker has completed its scan.
+_known_secret_snapshot: tuple[bool, frozenset[str] | None, re.Pattern[str] | None] = (False, None, None)
+_known_secret_collection_uncertain = False
+_known_secret_collection_overflowed = False
+
+
+class _DropEnvelope(Exception):
+ """Private, secret-free signal used to fail closed before HTTP serialization."""
+
+ pass
+
+
_ONE_DAY_SECONDS = 60 * 60 * 24
_NOISY_EVENT_TTL_SECONDS = _ONE_DAY_SECONDS
_NOISY_EVENT_CACHE_LIMIT = 128
@@ -66,33 +244,363 @@ _NETWORK_EXCEPTION_TYPES = {
}
-def _redact_string(value: str) -> str:
+def _redact_string(value: str, *, minimize_urls: bool = True) -> str:
redacted = value
for pattern, replacement in _STRING_REPLACEMENTS:
+ if not minimize_urls and (pattern, replacement) in _URL_REPLACEMENTS:
+ continue
redacted = pattern.sub(replacement, redacted)
return redacted
def _is_sensitive_key(key: str) -> bool:
- lowered = key.lower()
- return any(part in lowered for part in _SENSITIVE_KEY_PARTS)
+ normalized = _normalized_key(key)
+ return normalized in _SENSITIVE_EXACT_KEYS or any(token in normalized for token in _SENSITIVE_KEY_TOKENS)
-def scrub_data(value: Any) -> Any:
+def scrub_data(value: Any, *, minimize_urls: bool = True) -> Any:
if isinstance(value, dict):
scrubbed: dict[Any, Any] = {}
for key, item in value.items():
- if isinstance(key, str) and _is_sensitive_key(key):
- scrubbed[key] = _REDACTED
+ # A key can itself be a secret, such as a token naming a cache entry.
+ out_key = _redact_string(key, minimize_urls=minimize_urls) if isinstance(key, str) else key
+ if isinstance(key, str) and (key in _DROPPED_KEYS or _is_sensitive_key(key)):
+ scrubbed[out_key] = _REDACTED
else:
- scrubbed[key] = scrub_data(item)
+ scrubbed[out_key] = scrub_data(item, minimize_urls=minimize_urls)
return scrubbed
if isinstance(value, list):
- return [scrub_data(item) for item in value]
+ return [scrub_data(item, minimize_urls=minimize_urls) for item in value]
if isinstance(value, tuple):
- return tuple(scrub_data(item) for item in value)
+ return tuple(scrub_data(item, minimize_urls=minimize_urls) for item in value)
if isinstance(value, str):
- return _redact_string(value)
+ return _redact_string(value, minimize_urls=minimize_urls)
+ return value
+
+
+def _program_name(command: Any) -> str | None:
+ # A command line carries whatever its caller passed; keep the program name.
+ words = command.split() if isinstance(command, str) else []
+ return os.path.basename(words[0]) if words else None
+
+
+def _minimize_breadcrumb(crumb: dict[str, Any]) -> dict[str, Any]:
+ if crumb.get("type") != "subprocess" and crumb.get("category") != "subprocess":
+ return crumb
+ return {**crumb, "message": _program_name(crumb.get("message")), "data": {}}
+
+
+def _minimize_span(span: dict[str, Any]) -> dict[str, Any]:
+ operation = str(span.get("op") or span.get("name") or "")
+ if not operation.startswith("subprocess"):
+ return span
+ minimized = dict(span)
+ for key in ("description", "name"):
+ if key in span:
+ minimized[key] = _program_name(span.get(key))
+ for key in ("data", "attributes"):
+ if key in span:
+ minimized[key] = {}
+ return minimized
+
+
+def _minimize_event(event: dict[str, Any]) -> dict[str, Any]:
+ minimized = dict(event)
+ request = event.get("request")
+ if isinstance(request, dict):
+ # An incoming request's query, body, cookies, headers and environment
+ # stay local; the transaction already names the route.
+ minimized["request"] = {key: request[key] for key in ("method", "url") if key in request}
+ breadcrumbs = event.get("breadcrumbs")
+ if isinstance(breadcrumbs, dict) and isinstance(breadcrumbs.get("values"), list):
+ minimized["breadcrumbs"] = {
+ **breadcrumbs,
+ "values": [
+ _minimize_breadcrumb(crumb) if isinstance(crumb, dict) else crumb for crumb in breadcrumbs["values"]
+ ],
+ }
+ spans = event.get("spans")
+ if isinstance(spans, list):
+ minimized["spans"] = [_minimize_span(span) if isinstance(span, dict) else span for span in spans]
+ return minimized
+
+
+def _collect_secret_values(
+ node: Any, found: set[str], *, value_keys: frozenset[str] = frozenset(), sensitive: bool = False
+) -> None:
+ if isinstance(node, dict):
+ for key, item in node.items():
+ item_sensitive = sensitive or (isinstance(key, str) and (key in value_keys or _is_sensitive_key(key)))
+ _collect_secret_values(item, found, value_keys=value_keys, sensitive=item_sensitive)
+ elif isinstance(node, list):
+ for item in node:
+ _collect_secret_values(item, found, value_keys=value_keys, sensitive=sensitive)
+ elif isinstance(node, str):
+ if sensitive and len(node) >= _KNOWN_SECRET_MIN_LENGTH:
+ found.add(node)
+ # A stored URL's userinfo is a credential whatever its key is called,
+ # such as a platform ``proxy_url`` of ``http://user:pass@host``.
+ if "@" in node and "://" in node:
+ try:
+ parts = urlsplit(node.strip())
+ except ValueError:
+ return
+ if parts.password and len(parts.password) >= _KNOWN_SECRET_MIN_LENGTH:
+ found.add(parts.password)
+ # A username is a secret when it carries the credential alone
+ # (``https://<token>@host``) or is token-length; ``admin`` is not.
+ username = parts.username or ""
+ if username and (not parts.password or len(username) >= _KNOWN_SECRET_BOUNDARY_LENGTH):
+ if len(username) >= _KNOWN_SECRET_MIN_LENGTH:
+ found.add(username)
+
+
+def _known_secret_files() -> list[tuple[Path, frozenset[str]]]:
+ """Files where Avibe stores credentials, with any extra keys naming a secret."""
+
+ model_hub_state = paths.get_runtime_dir() / "model-hub" / "state"
+ files = [(paths.get_config_path(), frozenset())]
+ for pattern in ("instances/*/runtime-secrets.json", "auth/*.json", "oauth-staging/*.json"):
+ files.extend((path, frozenset()) for path in model_hub_state.glob(pattern))
+ # An API-key credential keeps its secret under ``value``.
+ files.extend((path, frozenset({"value"})) for path in model_hub_state.glob("credentials/*.json"))
+ return files
+
+
+def _secret_spellings(value: str) -> set[str]:
+ # The value as it appears raw, URL-encoded, escaped inside JSON or a repr, and
+ # with either quote backslash-escaped as a repr picks its quote. Every spelling
+ # also gets the same delimiter normalization that the outbound scrubber applies,
+ # so an early breadcrumb cannot turn a credential into a new unmatched value.
+ backslashed = value.replace("\\", "\\\\")
+ base_spellings = {
+ value,
+ quote(value, safe=""),
+ json.dumps(value)[1:-1],
+ repr(value)[1:-1],
+ backslashed.replace("'", "\\'"),
+ backslashed.replace('"', '\\"'),
+ }
+ spellings: set[str] = set()
+ for spelling in base_spellings:
+ normalized = spelling
+ for _ in range(4):
+ decoded = _ENCODED_DELIMITER_RE.sub(_decoded_delimiter, normalized)
+ spellings.add(decoded)
+ if decoded == normalized:
+ break
+ normalized = decoded
+ return spellings
+
+
+def _secret_alternative(spelling: str) -> str:
+ escaped = re.escape(spelling)
+ if len(spelling) < _KNOWN_SECRET_BOUNDARY_LENGTH:
+ return r"(?<![A-Za-z0-9])" + escaped + r"(?![A-Za-z0-9])"
+ return escaped
+
+
+def _read_secret_values(
+ path: Path, value_keys: frozenset[str], signature: tuple[int, int, int, int]
+) -> tuple[frozenset[str], bool]:
+ cached = _known_secret_files_state.get(str(path))
+ if cached == signature:
+ return frozenset(), False
+ if signature[2] > _KNOWN_SECRET_MAX_FILE_BYTES:
+ return frozenset(), True
+ try:
+ raw = path.read_bytes()
+ after = path.stat()
+ except OSError:
+ return frozenset(), True
+ after_signature = (after.st_ino, after.st_mtime_ns, after.st_size, after.st_mode & 0o7777)
+ if len(raw) != signature[2] or after_signature != signature:
+ return frozenset(), True
+ try:
+ data = json.loads(raw.decode("utf-8"))
+ except (UnicodeDecodeError, ValueError):
+ return frozenset(), True
+ found: set[str] = set()
+ _collect_secret_values(data, found, value_keys=value_keys)
+ return frozenset(found), False
+
+
+def _compile_secret_pattern(values: frozenset[str]) -> tuple[re.Pattern[str] | None, bool]:
+ spellings = {spelling for value in values for spelling in _secret_spellings(value)}
+ source = "|".join(_secret_alternative(spelling) for spelling in sorted(spellings, key=len, reverse=True))
+ if len(source.encode("utf-8")) > _KNOWN_SECRET_MAX_PATTERN_BYTES:
+ return None, True
+ try:
+ return (re.compile(source) if source else None), False
+ except re.error:
+ return None, True
+
+
+def _refresh_known_secret_state() -> bool:
+ """Refresh observed stores without ever evicting process-lifetime values."""
+
+ global _known_secret_snapshot, _known_secret_collection_uncertain, _known_secret_collection_overflowed
+ with _KNOWN_SECRETS_LOCK:
+ if _known_secret_collection_overflowed:
+ _known_secret_collection_uncertain = True
+ _known_secret_snapshot = (False, _known_secret_snapshot[1], _known_secret_snapshot[2])
+ return False
+
+ uncertain = False
+ present: set[str] = set()
+ try:
+ files = _known_secret_files()
+ except OSError:
+ files = []
+ uncertain = True
+ if len(files) > _KNOWN_SECRET_MAX_FILES:
+ _known_secret_collection_overflowed = True
+ _known_secret_collection_uncertain = True
+ _known_secret_snapshot = (False, _known_secret_snapshot[1], _known_secret_snapshot[2])
+ return False
+ for path, value_keys in files:
+ try:
+ stat = path.stat()
+ except FileNotFoundError:
+ continue
+ except OSError:
+ uncertain = True
+ continue
+ path_key = str(path)
+ present.add(path_key)
+ signature = (stat.st_ino, stat.st_mtime_ns, stat.st_size, stat.st_mode & 0o7777)
+ values, read_uncertain = _read_secret_values(path, value_keys, signature)
+ uncertain |= read_uncertain
+ if read_uncertain:
+ continue
+ new_values = values - _known_secret_values
+ new_value_bytes = sum(len(value.encode("utf-8")) for value in new_values)
+ current_value_bytes = sum(len(value.encode("utf-8")) for value in _known_secret_values)
+ if (
+ len(_known_secret_values) + len(new_values) > _KNOWN_SECRET_MAX_VALUES
+ or current_value_bytes + new_value_bytes > _KNOWN_SECRET_MAX_VALUE_BYTES
+ ):
+ _known_secret_collection_overflowed = True
+ _known_secret_collection_uncertain = True
+ return False
+ _known_secret_values.update(new_values)
+ _known_secret_files_state[path_key] = signature
+
+ if len(_known_secret_files_state) > _KNOWN_SECRET_MAX_FILES:
+ _known_secret_collection_overflowed = True
+ _known_secret_collection_uncertain = True
+ _known_secret_snapshot = (False, _known_secret_snapshot[1], _known_secret_snapshot[2])
+ return False
+
+ for stale in set(_known_secret_files_state) - present:
+ del _known_secret_files_state[stale]
+
+ if not uncertain and _known_secret_snapshot[1] == frozenset(_known_secret_values):
+ _known_secret_collection_uncertain = False
+ _known_secret_snapshot = (True, _known_secret_snapshot[1], _known_secret_snapshot[2])
+ return True
+
+ pattern, pattern_uncertain = _compile_secret_pattern(frozenset(_known_secret_values))
+ if pattern_uncertain:
+ _known_secret_collection_overflowed = True
+ uncertain = True
+ if not uncertain:
+ retained = frozenset(_known_secret_values)
+ _known_secret_snapshot = (True, retained, pattern)
+ else:
+ _known_secret_snapshot = (False, _known_secret_snapshot[1], _known_secret_snapshot[2])
+ _known_secret_collection_uncertain = uncertain
+ return not uncertain
+
+
+def _published_secret_snapshot() -> tuple[bool, re.Pattern[str] | None]:
+ snapshot = _known_secret_snapshot
+ return snapshot[0], snapshot[2]
+
+
+def _known_secret_pattern() -> re.Pattern[str] | None:
+ """Read the immutable published matcher without waiting for worker refresh."""
+
+ return _published_secret_snapshot()[1]
+
+
+def _known_secret_collection_is_complete() -> bool:
+ return _published_secret_snapshot()[0]
+
+
+_ROOT_USER_MAPS = frozenset({"extra", "tags"})
+_USER_MAP_BOUNDARIES = frozenset({"attributes", "cookies", "data", "env", "headers", "params", "vars"})
+_TYPED_CONTEXT_NAMES = frozenset(
+ {
+ "app",
+ "browser",
+ "cloud_resource",
+ "culture",
+ "device",
+ "gpu",
+ "os",
+ "profile",
+ "replay",
+ "runtime",
+ "trace",
+ }
+)
+
+
+def _redaction_child_mode(owner: str, path: tuple[str, ...], key: str) -> str:
+ """Choose ownership from the containing Sentry shape, not from key names alone."""
+
+ if owner == "user":
+ return "user"
+ if owner == "contexts":
+ return "typed_context" if key in _TYPED_CONTEXT_NAMES else "user"
+ if owner == "typed_context":
+ return "user" if key in _USER_MAP_BOUNDARIES else "protocol"
+ if owner == "user_object":
+ return "user" if key == "data" else "protocol"
+ if owner == "request":
+ return "user" if key in {"body", "cookies", "data", "env", "headers"} else "protocol"
+ if owner == "protocol":
+ if not path:
+ if key in _ROOT_USER_MAPS:
+ return "user"
+ if key == "contexts":
+ return "contexts"
+ if key == "request":
+ return "request"
+ if key == "user":
+ return "user_object"
+ if key in _USER_MAP_BOUNDARIES:
+ return "user"
+ return "protocol"
+ return "user"
+
+
+def _redact_matches(
+ value: Any,
+ pattern: re.Pattern[str],
+ *,
+ mode: str = "protocol",
+ path: tuple[str, ...] = (),
+) -> Any:
+ if isinstance(value, dict):
+ redacted: dict[Any, Any] = {}
+ for key, item in value.items():
+ if isinstance(key, str) and mode in {"user", "contexts"} and (
+ mode == "user" or key not in _TYPED_CONTEXT_NAMES
+ ):
+ out_key = pattern.sub(_REDACTED, key)
+ else:
+ out_key = key
+ child_mode = _redaction_child_mode(mode, path, key) if isinstance(key, str) else mode
+ redacted[out_key] = _redact_matches(item, pattern, mode=child_mode, path=path + (key,))
+ return redacted
+ if isinstance(value, list):
+ return [_redact_matches(item, pattern, mode=mode, path=path) for item in value]
+ if isinstance(value, tuple):
+ return tuple(_redact_matches(item, pattern, mode=mode, path=path) for item in value)
+ if isinstance(value, str):
+ return pattern.sub(_REDACTED, value)
return value
@@ -413,23 +921,215 @@ def _should_drop_repeated_event(event: dict[str, Any]) -> bool:
def before_send(event: dict[str, Any], hint: dict[str, Any]) -> Optional[dict[str, Any]]:
del hint
- scrubbed = scrub_data(event)
- if _should_drop_noisy_event(scrubbed):
+ if not _known_secret_collection_is_complete():
return None
- if _should_drop_repeated_event(scrubbed):
+ # Drop decisions stay in process and match on URL paths (Slack's
+ # ``apps.connections.open``) and tell errors on one host apart, so they see
+ # the text before URLs are reduced to their origin.
+ fingerprinted = scrub_data(event, minimize_urls=False)
+ if _should_drop_noisy_event(fingerprinted):
return None
+ if _should_drop_repeated_event(fingerprinted):
+ return None
+ return _scrub_outgoing(event)
+
+
+def before_send_transaction(event: dict[str, Any], hint: dict[str, Any]) -> Optional[dict[str, Any]]:
+ """Performance transactions skip ``before_send``; their spans get the same scrub."""
+
+ del hint
+ if not _known_secret_collection_is_complete():
+ return None
+ return _scrub_outgoing(event)
+
+
+def _scrub_outgoing(event: dict[str, Any], *, breadcrumb: bool = False) -> Optional[dict[str, Any]]:
+ ready, known_secrets = _published_secret_snapshot()
+ if not ready:
+ return None
+ # Match stored credentials before normalization, then once again after it.
+ # The second pass covers values that arrived through before_breadcrumb or were
+ # transformed by URL/delimiter minimization into another spelling.
+ protected: Any = event
+ if known_secrets is not None:
+ protected = _redact_matches(protected, known_secrets)
+ minimized = _minimize_breadcrumb(protected) if breadcrumb else _minimize_event(protected)
+ scrubbed = scrub_data(minimized)
+ if known_secrets is not None:
+ scrubbed = _redact_matches(scrubbed, known_secrets)
return scrubbed
-def before_breadcrumb(crumb: dict[str, Any], hint: dict[str, Any]) -> dict[str, Any]:
+def before_breadcrumb(crumb: dict[str, Any], hint: dict[str, Any]) -> Optional[dict[str, Any]]:
del hint
- return scrub_data(crumb)
+ return _scrub_outgoing(crumb, breadcrumb=True)
+
+
+_STRUCTURED_ITEM_TYPES = frozenset(
+ {
+ "event",
+ "transaction",
+ "log",
+ "trace_metric",
+ "span",
+ "session",
+ "sessions",
+ "client_report",
+ "check_in",
+ "profile",
+ "profile_chunk",
+ }
+)
+_DROPPED_ITEM_TYPES = frozenset({"attachment"})
+_HEADER_PROTOCOL_KEYS = frozenset(
+ {
+ "baggage",
+ "content_type",
+ "dsn",
+ "event_id",
+ "host",
+ "item_count",
+ "length",
+ "platform",
+ "sample_rates",
+ "sdk",
+ "section",
+ "sent_at",
+ "trace",
+ "type",
+ }
+)
+
+
+def _immutable_json_copy(value: Any) -> Any:
+ """Copy only JSON-shaped values so unsupported payloads fail closed."""
+
+ if value is None or isinstance(value, (bool, int, float, str)):
+ return value
+ if isinstance(value, dict):
+ if any(not isinstance(key, str) for key in value):
+ raise _DropEnvelope
+ return {key: _immutable_json_copy(item) for key, item in value.items()}
+ if isinstance(value, (list, tuple)):
+ return [_immutable_json_copy(item) for item in value]
+ raise _DropEnvelope
+
+
+def _header_contains_secret(value: Any, pattern: re.Pattern[str] | None) -> bool:
+ if pattern is None:
+ return False
+ if isinstance(value, dict):
+ return any(_header_contains_secret(item, pattern) for item in value.values())
+ if isinstance(value, list):
+ return any(_header_contains_secret(item, pattern) for item in value)
+ return isinstance(value, str) and pattern.search(value) is not None
+
+
+def _scrub_header_value(value: Any, pattern: re.Pattern[str] | None) -> Any:
+ if isinstance(value, dict):
+ scrubbed: dict[str, Any] = {}
+ for key, item in value.items():
+ if not isinstance(key, str):
+ raise _DropEnvelope
+ if key in _HEADER_PROTOCOL_KEYS:
+ copied = _immutable_json_copy(item)
+ if _header_contains_secret(copied, pattern):
+ raise _DropEnvelope
+ scrubbed[key] = copied
+ elif key in _DROPPED_KEYS or _is_sensitive_key(key):
+ scrubbed[key] = _REDACTED
+ else:
+ scrubbed[key] = _scrub_header_value(item, pattern)
+ return scrubbed
+ if isinstance(value, list):
+ return [_scrub_header_value(item, pattern) for item in value]
+ if isinstance(value, str):
+ scrubbed = _redact_string(value)
+ return pattern.sub(_REDACTED, scrubbed) if pattern is not None else scrubbed
+ if value is None or isinstance(value, (bool, int, float)):
+ return value
+ raise _DropEnvelope
+
+
+def _scrub_item_payload(payload: Any, item_type: str, pattern: re.Pattern[str] | None) -> Any:
+ copied = _immutable_json_copy(payload)
+ if pattern is not None:
+ copied = _redact_matches(copied, pattern)
+ if item_type in {"event", "transaction"}:
+ if not isinstance(copied, dict):
+ raise _DropEnvelope
+ copied = _minimize_event(copied)
+ elif item_type == "span" and isinstance(copied, dict) and isinstance(copied.get("items"), list):
+ copied = {**copied, "items": [_minimize_span(item) if isinstance(item, dict) else item for item in copied["items"]]}
+ copied = scrub_data(copied)
+ if pattern is not None:
+ copied = _redact_matches(copied, pattern)
+ return _immutable_json_copy(copied)
+
+
+def _scrub_envelope(envelope: Any) -> Any:
+ """Return a new envelope, or refuse it before the SDK serializer sees it."""
+
+ if not _refresh_known_secret_state():
+ raise _DropEnvelope
+ ready, pattern = _published_secret_snapshot()
+ if not ready:
+ raise _DropEnvelope
+ try:
+ from sentry_sdk.envelope import Envelope, Item, PayloadRef
+
+ headers = _scrub_header_value(envelope.headers, pattern)
+ items = []
+ for item in envelope.items:
+ item_type = item.type
+ if item_type in _DROPPED_ITEM_TYPES:
+ continue
+ if item_type not in _STRUCTURED_ITEM_TYPES:
+ continue
+ payload = item.payload.json
+ if payload is None:
+ continue
+ scrubbed_payload = _scrub_item_payload(payload, item_type, pattern)
+ scrubbed_headers = _scrub_header_value(item.headers, pattern)
+ items.append(Item(payload=PayloadRef(json=scrubbed_payload), headers=scrubbed_headers))
+ if not items:
+ raise _DropEnvelope
+ return Envelope(headers=headers, items=items)
+ except _DropEnvelope:
+ raise
+ except Exception as exc:
+ del exc
+ raise _DropEnvelope from None
+
+
+if _SentryHttpTransport is not None:
+
+ class _RedactingHttpTransport(_SentryHttpTransport):
+ """Pinned SDK HTTP transport with final worker-side envelope scrubbing."""
+
+ def __init__(self, options: dict[str, Any]) -> None:
+ import sentry_sdk
+
+ if sentry_sdk.VERSION != "2.56.0" or not hasattr(_SentryHttpTransport, "_serialize_envelope"):
+ raise RuntimeError("unsupported sentry-sdk transport serialization seam")
+ super().__init__(options)
+
+ def _serialize_envelope(self, envelope: Any) -> tuple[Optional[str], Any]:
+ scrubbed = _scrub_envelope(envelope)
+ return super()._serialize_envelope(scrubbed)
+
+else: # pragma: no cover - only reachable in an incomplete installation
+ _RedactingHttpTransport = None
def init_sentry(config: V2Config, component: str, enable_fastapi: bool = False) -> bool:
options = resolve_sentry_options()
if not options:
return False
+ if _RedactingHttpTransport is None:
+ logger.warning("Sentry initialization skipped: supported transport seam unavailable")
+ return False
+ _refresh_known_secret_state()
try:
import sentry_sdk
@@ -463,6 +1163,7 @@ def init_sentry(config: V2Config, component: str, enable_fastapi: bool = False)
release=f"avibe@{__version__}",
integrations=integrations,
before_send=before_send,
+ before_send_transaction=before_send_transaction,
before_breadcrumb=before_breadcrumb,
attach_stacktrace=True,
# Frame locals hold user data the scrubber cannot see into: an Agent's tool
@@ -474,6 +1175,8 @@ def init_sentry(config: V2Config, component: str, enable_fastapi: bool = False)
traces_sample_rate=options["traces_sample_rate"],
profiles_sample_rate=options["profiles_sample_rate"],
send_client_reports=False,
+ spotlight=False,
+ transport=_RedactingHttpTransport,
send_default_pii=True,
server_name=socket.gethostname(),
)