-
Notifications
You must be signed in to change notification settings - Fork 510
feat(automations): test draft webhooks before save #3986
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
10 commits
Select commit
Hold shift + click to select a range
c3e0f67
feat(automations): test draft webhooks before save
mmabrouk 6612466
feat(automations): add delivery logs to the drawer
mmabrouk 19023e2
fix(frontend): redesign delivery logs tab — simplify detail to raw JS…
mmabrouk 178d6c0
fix(api,frontend): redact sensitive headers from webhook delivery rec…
mmabrouk a7f850b
fix(frontend): keep automation logs scrolling contained
mmabrouk 63f91df
Merge branch 'fix/shady-webhooks' into fix/automations-draft-testing
mmabrouk 5aa7945
fix(api): remove broken merge code from webhook task
mmabrouk 2462142
fix(api): restore webhook header redaction in shared delivery helper
mmabrouk 2c2c519
Merge remote-tracking branch 'origin/fix/automations-draft-testing' i…
mmabrouk d9c0582
Merge pull request #3987 from Agenta-AI/fix/automations-delivery-logs
mmabrouk 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,219 @@ | ||
| import hashlib | ||
| import hmac | ||
| import json | ||
| from dataclasses import dataclass | ||
| from datetime import datetime, timezone | ||
| from typing import Any, Dict, Optional | ||
| from uuid import UUID | ||
|
|
||
| import httpx | ||
|
|
||
| from agenta.sdk.workflows.handlers import resolve_json_selector | ||
|
|
||
| from oss.src.core.webhooks.types import ( | ||
| EVENT_CONTEXT_FIELDS, | ||
| SUBSCRIPTION_CONTEXT_FIELDS, | ||
| WEBHOOK_TIMEOUT, | ||
| WebhookDeliveryData, | ||
| WebhookEventType, | ||
| ) | ||
| from oss.src.core.webhooks.utils import validate_webhook_url | ||
| from oss.src.utils.crypting import decrypt | ||
| from oss.src.utils.logging import get_module_logger | ||
|
|
||
| log = get_module_logger(__name__) | ||
|
|
||
| MAX_RESOLVE_DEPTH = 10 | ||
|
|
||
| NON_OVERRIDABLE_HEADERS = { | ||
| "content-type", | ||
| "content-length", | ||
| "host", | ||
| "user-agent", | ||
| "x-agenta-event-type", | ||
| "x-agenta-delivery-id", | ||
| "x-agenta-event-id", | ||
| "x-agenta-signature", | ||
| "idempotency-key", | ||
| "authorization", | ||
| } | ||
|
|
||
| REDACTED_HEADERS = { | ||
| "authorization", | ||
| "x-agenta-signature", | ||
| } | ||
|
|
||
| REDACTED_VALUE = "[REDACTED]" | ||
|
|
||
|
|
||
| @dataclass | ||
| class PreparedWebhookRequest: | ||
| typed_event_type: Optional[WebhookEventType] | ||
| data: WebhookDeliveryData | ||
| payload_json: str | ||
| request_headers: dict[str, str] | ||
|
|
||
|
|
||
| class PreparedWebhookRequestError(ValueError): | ||
| def __init__(self, message: str, *, data: WebhookDeliveryData): | ||
| super().__init__(message) | ||
| self.data = data | ||
|
|
||
|
|
||
| def _redact_headers(headers: dict[str, str]) -> dict[str, str]: | ||
| return { | ||
| key: (REDACTED_VALUE if key.lower() in REDACTED_HEADERS else value) | ||
| for key, value in headers.items() | ||
| } | ||
|
|
||
|
|
||
| def _merge_headers( | ||
| *, | ||
| user_headers: Optional[dict], | ||
| system_headers: dict[str, str], | ||
| ) -> dict[str, str]: | ||
| merged: dict[str, str] = {} | ||
| dropped: list[str] = [] | ||
|
|
||
| for key, value in (user_headers or {}).items(): | ||
| key_str = str(key) | ||
| if key_str.lower() in NON_OVERRIDABLE_HEADERS: | ||
| dropped.append(key_str) | ||
| continue | ||
| merged[key_str] = str(value) | ||
|
|
||
| if dropped: | ||
| log.warning( | ||
| "[WEBHOOKS DELIVERY] Dropped non-overwritable user headers: %s", | ||
| ", ".join(sorted(set(dropped))), | ||
| ) | ||
|
|
||
| merged.update(system_headers) | ||
| return merged | ||
|
|
||
|
|
||
| def resolve_payload_fields( | ||
| fields: Any, | ||
| context: Dict[str, Any], | ||
| *, | ||
| _depth: int = 0, | ||
| ) -> Any: | ||
| if _depth > MAX_RESOLVE_DEPTH: | ||
| return None | ||
| if isinstance(fields, dict): | ||
| return { | ||
| k: resolve_payload_fields(v, context, _depth=_depth + 1) | ||
| for k, v in fields.items() | ||
| } | ||
| if isinstance(fields, list): | ||
| return [ | ||
| resolve_payload_fields(item, context, _depth=_depth + 1) for item in fields | ||
| ] | ||
| try: | ||
| return resolve_json_selector(fields, context) | ||
| except Exception: | ||
| return None | ||
|
|
||
|
|
||
| def prepare_webhook_request( | ||
| *, | ||
| project_id: UUID, | ||
| delivery_id: UUID, | ||
| event_id: UUID, | ||
| event_type: str, | ||
| url: str, | ||
| headers: dict, | ||
| payload_fields: Optional[Dict[str, Any]], | ||
| auth_mode: Optional[str], | ||
| event: Dict[str, Any], | ||
| subscription: Dict[str, Any], | ||
| encrypted_secret: str, | ||
| ) -> PreparedWebhookRequest: | ||
| try: | ||
| typed_event_type = WebhookEventType(event_type) | ||
| except ValueError: | ||
| log.warning( | ||
| "[WEBHOOKS DELIVERY] Unrecognized event_type %r — storing None in delivery data", | ||
| event_type, | ||
| ) | ||
| typed_event_type = None | ||
|
|
||
| context = { | ||
| "event": {k: v for k, v in event.items() if k in EVENT_CONTEXT_FIELDS}, | ||
| "subscription": { | ||
| k: v for k, v in subscription.items() if k in SUBSCRIPTION_CONTEXT_FIELDS | ||
| }, | ||
| "scope": {"project_id": str(project_id)}, | ||
| } | ||
|
|
||
| resolved_fields = payload_fields if payload_fields is not None else "$" | ||
| payload = resolve_payload_fields(resolved_fields, context) | ||
|
|
||
| base_data = WebhookDeliveryData( | ||
| event_type=typed_event_type, | ||
| url=url, | ||
| payload=payload, | ||
| ) | ||
|
|
||
| try: | ||
| validate_webhook_url(url) | ||
| except ValueError as exc: | ||
| raise PreparedWebhookRequestError(str(exc), data=base_data) from exc | ||
|
|
||
| signing_secret = decrypt(encrypted_secret) | ||
| resolved_auth_mode = auth_mode or "signature" | ||
| payload_json = json.dumps(payload, sort_keys=True, separators=(",", ":")) | ||
| timestamp = str(int(datetime.now(timezone.utc).timestamp())) | ||
|
|
||
| if resolved_auth_mode == "authorization": | ||
| system_headers = { | ||
| "Content-Type": "application/json", | ||
| "User-Agent": "Agenta-Webhook/1.0", | ||
| "X-Agenta-Event-Type": event_type, | ||
| "X-Agenta-Delivery-Id": str(delivery_id), | ||
| "X-Agenta-Event-Id": str(event_id), | ||
| "Idempotency-Key": str(delivery_id), | ||
| "Authorization": signing_secret, | ||
| } | ||
| else: | ||
| to_sign = f"{timestamp}.{payload_json}" | ||
| signature = hmac.new( | ||
| key=signing_secret.encode("utf-8"), | ||
| msg=to_sign.encode("utf-8"), | ||
| digestmod=hashlib.sha256, | ||
| ).hexdigest() | ||
| system_headers = { | ||
| "Content-Type": "application/json", | ||
| "User-Agent": "Agenta-Webhook/1.0", | ||
| "X-Agenta-Event-Type": event_type, | ||
| "X-Agenta-Delivery-Id": str(delivery_id), | ||
| "X-Agenta-Event-Id": str(event_id), | ||
| "Idempotency-Key": str(delivery_id), | ||
| "X-Agenta-Signature": f"t={timestamp},v1={signature}", | ||
| } | ||
|
|
||
| request_headers = _merge_headers( | ||
| user_headers=headers, | ||
| system_headers=system_headers, | ||
| ) | ||
|
|
||
| return PreparedWebhookRequest( | ||
| typed_event_type=typed_event_type, | ||
| data=base_data.model_copy(update={"headers": _redact_headers(request_headers)}), | ||
| payload_json=payload_json, | ||
| request_headers=request_headers, | ||
| ) | ||
|
|
||
|
|
||
| async def send_webhook_request( | ||
| *, | ||
| url: str, | ||
| payload_json: str, | ||
| headers: dict[str, str], | ||
| ) -> httpx.Response: | ||
| async with httpx.AsyncClient(timeout=WEBHOOK_TIMEOUT) as client: | ||
| return await client.post( | ||
| url, | ||
| content=payload_json, | ||
| headers=headers, | ||
| ) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Pydantic Union always resolves to WebhookSubscriptionEdit, making WebhookSubscriptionCreate unreachable
In
WebhookSubscriptionDraftTestRequest, the fieldsubscription: Union[WebhookSubscriptionEdit, WebhookSubscriptionCreate]will always deserialize any payload asWebhookSubscriptionEdit, because all of its extra fields (fromIdentifier:id: Optional[UUID] = None, fromLifecycle:created_at: Optional[datetime] = None, etc.) haveOptionaldefaults. Pydantic v2 tries left-to-right andWebhookSubscriptionEditsucceeds for every validWebhookSubscriptionCreatepayload, so theWebhookSubscriptionCreatebranch is dead code.In practice, the service code at
api/oss/src/core/webhooks/service.py:178usesgetattr(subscription, "id", None)which returnsNonefor create payloads (sinceIdentifier.iddefaults toNone), so runtime behavior is correct by coincidence. However, this also means create-style draft test payloads now silently acceptLifecyclefields (created_at,deleted_at,deleted_by_id, etc.) that should not be part of a create request, weakening input validation.Prompt for agents
Was this helpful? React with 👍 or 👎 to provide feedback.