-
Notifications
You must be signed in to change notification settings - Fork 5
feat(notifications): outbound webhook (#560) #594
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7dd20e8
feat(notifications): outbound webhook for batch/blocker/PR events (#560)
6d7f1fc
fix(notifications): address review feedback on #560
77adedb
fix(notifications): defence-in-depth + corrupt JSON handling on #560
97989f9
fix(notifications): round-3 review polish on #560
3c022c5
fix(notifications): redact URL in logs + fix stale pr_v2 docstring (#…
2044c7c
fix(notifications): round-5 nits — task callback + types + docstring …
77459ef
fix(notifications): basic-auth log leak + clean async cancel (#560)
3279f17
fix(notifications): guard parsed.port from raising on malformed URLs …
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| """Per-workspace outbound webhook notification config (issue #560). | ||
|
|
||
| Stored alongside other workspace UI settings under ``.codeframe/`` as a JSON | ||
| file. Headless — no FastAPI or HTTP imports. | ||
|
|
||
| Schema (``.codeframe/notifications_config.json``): | ||
|
|
||
| { | ||
| "webhook_url": "https://hooks.example.com/...", // or null | ||
| "webhook_enabled": true | ||
| } | ||
|
|
||
| Defense-in-depth: ``is_webhook_active`` re-validates the URL scheme/host | ||
| because the JSON file can be hand-edited or migrated from an older | ||
| deployment that didn't enforce validation at write time. The router PUT | ||
| endpoint validates too — never trust just one layer. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| import logging | ||
| import os | ||
| import tempfile | ||
| from pathlib import Path | ||
| from typing import Optional, TypedDict | ||
|
|
||
| from codeframe.core.workspace import Workspace | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| NOTIFICATIONS_CONFIG_FILENAME = "notifications_config.json" | ||
|
|
||
| # Intentionally duplicated with codeframe/ui/routers/settings_v2.py's | ||
| # ``_ALLOWED_WEBHOOK_SCHEMES``: core cannot import from the UI layer | ||
| # (architecture rule #1 — core must be headless). Keep both values in | ||
| # sync if extended. | ||
| _ALLOWED_SCHEMES = frozenset({"http", "https"}) | ||
|
|
||
|
|
||
| class NotificationsConfig(TypedDict): | ||
| webhook_url: Optional[str] | ||
| webhook_enabled: bool | ||
|
|
||
|
|
||
| _DEFAULT: NotificationsConfig = {"webhook_url": None, "webhook_enabled": False} | ||
|
|
||
|
|
||
| def _config_path(workspace: Workspace) -> Path: | ||
| return workspace.state_dir / NOTIFICATIONS_CONFIG_FILENAME | ||
|
|
||
|
|
||
| def _is_safe_webhook_url(url: str) -> bool: | ||
| """Defence-in-depth: scheme/host check on a stored URL before we POST. | ||
|
|
||
| The router PUT validates too, but a hand-edited or pre-migration JSON | ||
| file could carry a ``file://`` or schemeless URL. Used by | ||
| ``is_webhook_active`` to fail-safe to ``None``. | ||
|
|
||
| TODO: This does NOT block RFC-1918 (10/8, 172.16/12, 192.168/16) or | ||
| loopback (127/8) addresses. For a self-hosted single-user tool that is | ||
| intentional — users want to point at local receivers. If CodeFRAME ever | ||
| runs as a shared / multi-tenant service, add a socket-level check here | ||
| that resolves the host and rejects private/loopback ranges. | ||
| """ | ||
| from urllib.parse import urlparse | ||
|
|
||
| try: | ||
| parsed = urlparse(url) | ||
| except (TypeError, ValueError): | ||
| return False | ||
| return parsed.scheme.lower() in _ALLOWED_SCHEMES and bool(parsed.netloc) | ||
|
|
||
|
|
||
| def load_notifications_config(workspace: Workspace) -> NotificationsConfig: | ||
| """Read notifications config, returning defaults on missing/corrupt file. | ||
|
|
||
| Never raises — a broken config should not break the triggering operation. | ||
| Non-object JSON (``[]``, ``null``, a bare integer) is treated as corrupt | ||
| and falls back to defaults rather than raising ``AttributeError`` from | ||
| a downstream ``.get()`` call. | ||
| """ | ||
| path = _config_path(workspace) | ||
| if not path.exists(): | ||
| return dict(_DEFAULT) # type: ignore[return-value] | ||
| try: | ||
| data = json.loads(path.read_text()) | ||
| if not isinstance(data, dict): | ||
| raise ValueError( | ||
| f"Expected JSON object, got {type(data).__name__}" | ||
| ) | ||
| return { | ||
| "webhook_url": data.get("webhook_url") or None, | ||
| "webhook_enabled": bool(data.get("webhook_enabled", False)), | ||
| } | ||
| except (OSError, json.JSONDecodeError, ValueError) as e: | ||
| logger.warning( | ||
| "Invalid notifications_config.json — falling back to defaults: %s", e | ||
| ) | ||
| return dict(_DEFAULT) # type: ignore[return-value] | ||
|
|
||
|
|
||
| def save_notifications_config( | ||
| workspace: Workspace, config: NotificationsConfig | ||
| ) -> None: | ||
| """Atomically persist notifications config to disk.""" | ||
| path = _config_path(workspace) | ||
| payload = { | ||
| "webhook_url": config.get("webhook_url") or None, | ||
| "webhook_enabled": bool(config.get("webhook_enabled", False)), | ||
| } | ||
| path.parent.mkdir(parents=True, exist_ok=True) | ||
| fd, tmp_name = tempfile.mkstemp( | ||
| prefix=f".{path.name}.", suffix=".tmp", dir=path.parent | ||
| ) | ||
| try: | ||
| with os.fdopen(fd, "w") as f: | ||
| f.write(json.dumps(payload, indent=2)) | ||
| os.replace(tmp_name, path) | ||
| except Exception: | ||
| try: | ||
| os.unlink(tmp_name) | ||
| except OSError: | ||
| pass | ||
| raise | ||
|
|
||
|
|
||
| def is_webhook_active(workspace: Workspace) -> Optional[str]: | ||
| """Return the webhook URL if URL is set, enabled flag is on, AND the URL | ||
| passes basic safety checks (``http(s)`` scheme + non-empty host). | ||
|
|
||
| Returns ``None`` otherwise — callers should short-circuit on ``None`` to | ||
| avoid instantiating the webhook service for nothing. The safety check is | ||
| intentionally redundant with the PUT-endpoint validation: it protects | ||
| against hand-edited config files and pre-migration data. | ||
| """ | ||
| cfg = load_notifications_config(workspace) | ||
| url = (cfg["webhook_url"] or "").strip() | ||
| if not url or not cfg["webhook_enabled"]: | ||
| return None | ||
| if not _is_safe_webhook_url(url): | ||
| logger.warning( | ||
| "Refusing to dispatch webhook to unsafe URL: %s", | ||
| _redact_url_for_log(url), | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| ) | ||
|
frankbria marked this conversation as resolved.
|
||
| return None | ||
| return url | ||
|
|
||
|
|
||
| def _redact_url_for_log(url: str) -> str: | ||
| """Return a logging-safe representation of a webhook URL. | ||
|
|
||
| Slack/Discord/GitHub-style webhook URLs commonly embed secrets in: | ||
|
|
||
| * the path or query (Slack's ``T*/B*/...`` token, signed Zapier hooks) | ||
| * basic-auth credentials in ``user:password@host`` form | ||
|
|
||
| ``parsed.netloc`` preserves the userinfo segment, so we use | ||
| ``parsed.hostname`` (which strips auth and port) and re-attach the | ||
| port explicitly. Returns ``scheme://host[:port]`` when parsable, else | ||
| ``"<unparseable>"``. | ||
| """ | ||
| from urllib.parse import urlparse | ||
|
|
||
| try: | ||
| parsed = urlparse(url) | ||
| except (TypeError, ValueError): | ||
| return "<unparseable>" | ||
| if not parsed.scheme or not parsed.hostname: | ||
| return "<unparseable>" | ||
| host = parsed.hostname | ||
| # ``parsed.port`` raises ValueError for malformed ports (e.g. | ||
| # ``file://host:abc/x``). Without this guard the "fail-safe" branch | ||
| # in ``is_webhook_active`` could end up raising instead of returning | ||
| # None, which would bubble through every dispatch site's broad | ||
| # ``except Exception``. | ||
| try: | ||
| port = parsed.port | ||
| except ValueError: | ||
| return "<unparseable>" | ||
| if port is not None: | ||
| host = f"{host}:{port}" | ||
| return f"{parsed.scheme}://{host}" | ||
|
frankbria marked this conversation as resolved.
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.