Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(backend/library): let a trigger preset's webhook cross the database-manager RPC (#15816)

Reinier van der Leerfbf6718be919
diff --git a/autogpt_platform/backend/backend/api/features/library/model.py b/autogpt_platform/backend/backend/api/features/library/model.py
index 0e4670e4b0..fcb9953e54 100644
--- a/autogpt_platform/backend/backend/api/features/library/model.py
+++ b/autogpt_platform/backend/backend/api/features/library/model.py
@@ -16,7 +16,7 @@ from backend.util.json import loads as json_loads
from backend.util.models import Pagination
if TYPE_CHECKING:
- from backend.data.integrations import Webhook
+ from backend.data.integrations import WebhookWithoutSecrets
class LibraryAgentStatus(str, Enum):
@@ -545,27 +545,15 @@ class LibraryAgentPreset(LibraryAgentPresetCreatable):
# Read-only in responses; set server-side via the setup-trigger flow only.
webhook_id: Optional[str] = None
- webhook: "Webhook | None"
+ webhook: "WebhookWithoutSecrets | None"
# Expert attribution, resolved server-side at creation; every run this
# preset fires inherits it.
expert_id: Optional[str] = None
- @pydantic.field_serializer("webhook")
- def _redact_webhook_signing_material(
- self, webhook: "Webhook | None", info: pydantic.FieldSerializationInfo
- ):
- # Never expose a webhook's signing secret / provider webhook id to API
- # clients; callers only need the ingress URL and non-sensitive metadata.
- if webhook is None:
- return None
- return webhook.model_dump(
- mode=info.mode, exclude={"secret", "provider_webhook_id"}
- )
-
@classmethod
def from_db(cls, preset: prisma.models.AgentPreset) -> "LibraryAgentPreset":
- from backend.data.integrations import Webhook
+ from backend.data.integrations import WebhookWithoutSecrets
if preset.InputPresets is None:
raise ValueError("InputPresets must be included in AgentPreset query")
@@ -600,7 +588,11 @@ class LibraryAgentPreset(LibraryAgentPresetCreatable):
organization_id=preset.organizationId,
team_id=preset.teamId,
webhook_id=preset.webhookId,
- webhook=Webhook.from_db(preset.Webhook) if preset.Webhook else None,
+ webhook=(
+ WebhookWithoutSecrets.from_db(preset.Webhook)
+ if preset.Webhook
+ else None
+ ),
expert_id=preset.expertId,
)
diff --git a/autogpt_platform/backend/backend/api/features/library/model_test.py b/autogpt_platform/backend/backend/api/features/library/model_test.py
index d23ecc0e18..e57d9888b5 100644
--- a/autogpt_platform/backend/backend/api/features/library/model_test.py
+++ b/autogpt_platform/backend/backend/api/features/library/model_test.py
@@ -5,7 +5,9 @@ import prisma.enums
import prisma.models
import pytest
+from backend.api.features.library.triggers import setup_triggered_preset
from backend.blocks.io import AgentGoogleDriveFileInputBlock
+from backend.util.service import _build_return_adapter
from . import model as library_model
@@ -208,41 +210,49 @@ async def test_agent_preset_from_db(test_user_id: str):
assert agent.inputs == {"input1": {"type": "string", "value": "test value"}}
-def test_preset_serialization_redacts_webhook_signing_material():
- """A serialized preset must never leak the webhook's signing secret or
- provider webhook id (GHSA-4m2w-qfr5-9f3v), while still exposing the ingress
- URL and non-sensitive metadata the frontend needs."""
- from backend.data.integrations import Webhook
- from backend.integrations.providers import ProviderName
-
- webhook = Webhook(
- id="wh-1",
- user_id="u1",
- provider=ProviderName.GITHUB,
- credentials_id="cred-1",
- webhook_type="repo",
- resource="owner/repo",
- events=["pull_request"],
- config={},
- secret="super-secret-signing-key",
- provider_webhook_id="gh-provider-123",
- )
- preset = library_model.LibraryAgentPreset(
+def _preset_row_with_webhook() -> prisma.models.AgentPreset:
+ """A trigger preset as the library loads it: webhook row included, signing
+ material and all."""
+ return prisma.models.AgentPreset(
+ deactivatedByExpertArchive=False,
id="preset-1",
- user_id="u1",
- created_at=datetime.datetime.now(),
- updated_at=datetime.datetime.now(),
- graph_id="g1",
- graph_version=1,
- inputs={},
- credentials={},
+ createdAt=datetime.datetime.now(),
+ updatedAt=datetime.datetime.now(),
+ agentGraphId="g1",
+ agentGraphVersion=1,
name="p",
description="",
- is_active=True,
- webhook_id="wh-1",
- webhook=webhook,
+ isActive=True,
+ userId="u1",
+ isDeleted=False,
+ visibility=prisma.enums.ResourceVisibility.PRIVATE,
+ InputPresets=[],
+ webhookId="wh-1",
+ Webhook=prisma.models.IntegrationWebhook.model_validate(
+ {
+ "id": "wh-1",
+ "createdAt": datetime.datetime.now(),
+ "userId": "u1",
+ "provider": "github",
+ "credentialsId": "cred-1",
+ "webhookType": "repo",
+ "resource": "owner/repo",
+ "events": ["pull_request"],
+ "config": "{}",
+ "secret": "super-secret-signing-key",
+ "providerWebhookId": "gh-provider-123",
+ "visibility": prisma.enums.ResourceVisibility.PRIVATE,
+ }
+ ),
)
+
+def test_preset_serialization_redacts_webhook_signing_material():
+ """A serialized preset must never leak the webhook's signing secret or
+ provider webhook id (GHSA-4m2w-qfr5-9f3v), while still exposing the ingress
+ URL and non-sensitive metadata the frontend needs."""
+ preset = library_model.LibraryAgentPreset.from_db(_preset_row_with_webhook())
+
dumped = preset.model_dump(mode="json")
assert dumped["webhook"] is not None
assert "secret" not in dumped["webhook"]
@@ -255,3 +265,25 @@ def test_preset_serialization_redacts_webhook_signing_material():
serialized = preset.model_dump_json()
assert "super-secret-signing-key" not in serialized
assert "gh-provider-123" not in serialized
+
+
+def test_preset_with_webhook_survives_the_rpc_round_trip():
+ """The copilot executor gets presets from the database manager as JSON and
+ rebuilds them with the RPC client's validator. A preset whose webhook cannot
+ make that trip reaches the copilot tools as a dict, and every read of
+ ``.webhook`` on it fails."""
+ preset = library_model.LibraryAgentPreset.from_db(_preset_row_with_webhook())
+ assert preset.webhook is not None
+ adapter = _build_return_adapter(
+ setup_triggered_preset,
+ library_model.LibraryAgentPreset,
+ "setup_triggered_preset",
+ )
+ assert adapter is not None
+
+ delivered = adapter.validate_python(json.loads(preset.model_dump_json()))
+
+ assert isinstance(delivered, library_model.LibraryAgentPreset)
+ assert delivered.webhook is not None
+ assert delivered.webhook.url == preset.webhook.url
+ assert delivered.webhook.provider == preset.webhook.provider
diff --git a/autogpt_platform/backend/backend/copilot/tools/setup_agent_webhook_trigger_test.py b/autogpt_platform/backend/backend/copilot/tools/setup_agent_webhook_trigger_test.py
index 9bef55b26f..9dadb3eef8 100644
--- a/autogpt_platform/backend/backend/copilot/tools/setup_agent_webhook_trigger_test.py
+++ b/autogpt_platform/backend/backend/copilot/tools/setup_agent_webhook_trigger_test.py
@@ -1,10 +1,14 @@
"""Tests for SetupAgentWebhookTriggerTool."""
+import datetime
+import json
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from pydantic import SecretStr
+from backend.api.features.library.model import LibraryAgentPreset
+from backend.api.features.library.triggers import setup_triggered_preset
from backend.blocks._base import BlockType
from backend.copilot.tools.models import ErrorResponse, SetupRequirementsResponse
from backend.copilot.tools.setup_agent_webhook_trigger import (
@@ -12,6 +16,7 @@ from backend.copilot.tools.setup_agent_webhook_trigger import (
TriggerConfigRequiredResponse,
TriggerSetupResponse,
)
+from backend.data.integrations import WebhookWithoutSecrets
from backend.data.model import (
APIKeyCredentials,
CredentialsFieldInfo,
@@ -24,6 +29,7 @@ from backend.util.exceptions import (
NotFoundError,
WebhookRegistrationError,
)
+from backend.util.service import _build_return_adapter
from ._test_data import make_session
@@ -91,14 +97,40 @@ def _make_graph(
return graph
-def _make_preset(*, provider: str, url: str):
- preset = MagicMock()
- preset.id = "preset-1"
- preset.name = "My Trigger"
- preset.is_active = True
- preset.webhook.url = url
- preset.webhook.provider = provider
- return preset
+def _make_preset(*, provider: str, webhook_id: str) -> LibraryAgentPreset:
+ """The preset as the tool receives it: built by the library, sent by the
+ database manager as JSON and rebuilt by the RPC client's validator."""
+ now = datetime.datetime.now(datetime.timezone.utc)
+ preset = LibraryAgentPreset(
+ id="preset-1",
+ user_id=_USER,
+ created_at=now,
+ updated_at=now,
+ graph_id="graph-1",
+ graph_version=1,
+ name="My Trigger",
+ description="",
+ is_active=True,
+ inputs={},
+ credentials={},
+ webhook_id=webhook_id,
+ webhook=WebhookWithoutSecrets(
+ id=webhook_id,
+ user_id=_USER,
+ provider=ProviderName(provider),
+ credentials_id="cred-gh-1",
+ webhook_type="repo",
+ resource="owner/repo",
+ events=[],
+ secret="signing-secret",
+ provider_webhook_id="provider-webhook-1",
+ ),
+ )
+ adapter = _build_return_adapter(
+ setup_triggered_preset, LibraryAgentPreset, "setup_triggered_preset"
+ )
+ assert adapter is not None
+ return adapter.validate_python(json.loads(preset.model_dump_json()))
def _patches(graph, *, matched=None, available=None, preset=None, graph_db_mock=None):
@@ -161,10 +193,7 @@ async def test_graph_is_loaded_with_subgraphs(tool, session):
"""Without sub-graphs, a sub-agent's credentials are missing from the card
and the registered trigger fails on every firing."""
graph = _make_graph(manual=True, regular_credentials={})
- preset = _make_preset(
- provider="generic_webhook",
- url="https://backend.agpt.co/api/integrations/generic_webhook/webhooks/wh-1/ingress",
- )
+ preset = _make_preset(provider="generic_webhook", webhook_id="wh-1")
graph_db_mock = MagicMock()
ctxs, _ = _patches(graph, preset=preset, graph_db_mock=graph_db_mock)
with ctxs[0], ctxs[1], ctxs[2], ctxs[3], ctxs[4]:
@@ -179,10 +208,7 @@ async def test_graph_is_loaded_with_subgraphs(tool, session):
async def test_manual_webhook_no_creds_proceeds(tool, session):
"""Manual webhook with no required credentials: create the preset, return URL."""
graph = _make_graph(manual=True, regular_credentials={})
- preset = _make_preset(
- provider="generic_webhook",
- url="https://backend.agpt.co/api/integrations/generic_webhook/webhooks/wh-1/ingress",
- )
+ preset = _make_preset(provider="generic_webhook", webhook_id="wh-1")
ctxs, setup_mock = _patches(graph, preset=preset)
with ctxs[0], ctxs[1], ctxs[2], ctxs[3], ctxs[4]:
result = await tool._execute(
@@ -190,7 +216,9 @@ async def test_manual_webhook_no_creds_proceeds(tool, session):
)
assert isinstance(result, TriggerSetupResponse)
assert result.manual_setup_required is True
- assert result.webhook_url and "backend.agpt.co" in result.webhook_url
+ assert preset.webhook is not None
+ assert result.webhook_url == preset.webhook.url
+ assert result.webhook_url.endswith("/webhooks/wh-1/ingress")
# The URL lives on webhook_url (rendered in the card's copy box) and must
# NOT be duplicated inline in the user-facing message.
assert result.webhook_url not in result.message
@@ -204,10 +232,7 @@ async def test_expert_session_passes_expert_scope_to_trigger_setup(
):
session.expert_id = "expert-1"
graph = _make_graph(manual=True, regular_credentials={})
- preset = _make_preset(
- provider="generic_webhook",
- url="https://backend.agpt.co/api/integrations/generic_webhook/webhooks/wh-1/ingress",
- )
+ preset = _make_preset(provider="generic_webhook", webhook_id="wh-1")
ctxs, setup_mock = _patches(graph, preset=preset)
installed = patch(
"backend.copilot.tools.setup_agent_webhook_trigger.require_installed_workflow",
@@ -230,7 +255,7 @@ async def test_expert_session_attributes_trigger_to_expert(tool, request):
to preset creation, so its webhook fires count as the expert's work."""
session = make_session(_USER, expert_id="expert-1")
graph = _make_graph(manual=True, regular_credentials={})
- preset = _make_preset(provider="generic_webhook", url="https://x/ingress")
+ preset = _make_preset(provider="generic_webhook", webhook_id="wh-1")
ctxs, setup_mock = _patches(graph, preset=preset)
installed = patch(
"backend.copilot.tools.setup_agent_webhook_trigger.require_installed_workflow",
@@ -251,7 +276,7 @@ async def test_plain_session_does_not_attribute_trigger(tool, session):
"""A plain (non-expert) chat forwards no expert; setup_triggered_preset's
graph-level fallback still applies but the tool passes None."""
graph = _make_graph(manual=True, regular_credentials={})
- preset = _make_preset(provider="generic_webhook", url="https://x/ingress")
+ preset = _make_preset(provider="generic_webhook", webhook_id="wh-1")
ctxs, setup_mock = _patches(graph, preset=preset)
with ctxs[0], ctxs[1], ctxs[2], ctxs[3], ctxs[4]:
await tool._execute(
@@ -323,10 +348,7 @@ async def test_provider_webhook_explicit_selection_proceeds(tool, session):
)
},
)
- preset = _make_preset(
- provider="github",
- url="https://backend.agpt.co/api/integrations/github/webhooks/wh-2/ingress",
- )
+ preset = _make_preset(provider="github", webhook_id="wh-2")
ctxs, setup_mock = _patches(
graph,
matched={"github_credentials": _github_meta()},
@@ -395,10 +417,7 @@ async def test_provider_webhook_config_and_creds_provided_proceeds(tool, session
"required": ["repo"],
},
)
- preset = _make_preset(
- provider="github",
- url="https://backend.agpt.co/api/integrations/github/webhooks/wh-3/ingress",
- )
+ preset = _make_preset(provider="github", webhook_id="wh-3")
ctxs, setup_mock = _patches(
graph,
matched={"github_credentials": _github_meta()},
@@ -497,10 +516,7 @@ async def test_setup_failure_expert_workspace_unavailable(tool, session):
async def test_library_agent_id_resolves_and_proceeds(tool, session):
"""Passing library_agent_id resolves the graph via get_library_agent."""
graph = _make_graph(manual=True, regular_credentials={})
- preset = _make_preset(
- provider="generic_webhook",
- url="https://backend.agpt.co/api/integrations/generic_webhook/webhooks/wh-1/ingress",
- )
+ preset = _make_preset(provider="generic_webhook", webhook_id="wh-1")
ctxs, setup_mock = _patches(graph, preset=preset)
mock_lib_db = MagicMock()
mock_lib_db.get_library_agent = AsyncMock(
diff --git a/autogpt_platform/backend/backend/data/integrations.py b/autogpt_platform/backend/backend/data/integrations.py
index 59ddc2d161..3733f0aa35 100644
--- a/autogpt_platform/backend/backend/data/integrations.py
+++ b/autogpt_platform/backend/backend/data/integrations.py
@@ -49,9 +49,9 @@ class Webhook(BaseDbModel):
def url(self) -> str:
return webhook_ingress_url(self.provider, self.id)
- @staticmethod
- def from_db(webhook: IntegrationWebhook):
- return Webhook(
+ @classmethod
+ def from_db(cls, webhook: IntegrationWebhook):
+ return cls(
id=webhook.id,
user_id=webhook.userId,
provider=ProviderName(webhook.provider),
@@ -67,9 +67,22 @@ class Webhook(BaseDbModel):
)
-# LibraryAgentPreset import must be after Webhook definition to avoid
-# broken circular import:
-# integrations.py → library/model.py → integrations.py (for Webhook)
+class WebhookWithoutSecrets(Webhook):
+ """
+ Webhook variant that excludes the signing secret and the provider's webhook
+ id from serialization (GHSA-4m2w-qfr5-9f3v).
+
+ Used where a webhook leaves the backend: API responses and the
+ database-manager RPC. The defaults let the payload validate back on the
+ other side, so a preset survives the trip instead of arriving as a dict.
+ """
+
+ secret: str = Field(default="", exclude=True)
+ provider_webhook_id: str = Field(default="", exclude=True)
+
+
+# LibraryAgentPreset import must be after the webhook models to avoid a
+# circular import: integrations.py → library/model.py → integrations.py
from backend.api.features.library.model import LibraryAgentPreset # noqa: E402
# Resolve forward refs
@@ -80,15 +93,15 @@ class WebhookWithRelations(Webhook):
triggered_nodes: list[NodeModel]
triggered_presets: list[LibraryAgentPreset]
- @staticmethod
- def from_db(webhook: IntegrationWebhook):
+ @classmethod
+ def from_db(cls, webhook: IntegrationWebhook):
if webhook.AgentNodes is None or webhook.AgentPresets is None:
raise ValueError(
"AgentNodes and AgentPresets must be included in "
"IntegrationWebhook query with relations"
)
- return WebhookWithRelations(
+ return cls(
**Webhook.from_db(webhook).model_dump(),
triggered_nodes=[NodeModel.from_db(node) for node in webhook.AgentNodes],
triggered_presets=[
diff --git a/autogpt_platform/frontend/src/app/api/openapi.json b/autogpt_platform/frontend/src/app/api/openapi.json
index 9f10e49652..a8e8c858c7 100644
--- a/autogpt_platform/frontend/src/app/api/openapi.json
+++ b/autogpt_platform/frontend/src/app/api/openapi.json
@@ -28482,7 +28482,7 @@
},
"webhook": {
"anyOf": [
- { "$ref": "#/components/schemas/Webhook" },
+ { "$ref": "#/components/schemas/WebhookWithoutSecrets" },
{ "type": "null" }
]
},
@@ -38044,7 +38044,7 @@
"title": "VoiceSample",
"description": "A short writing sample in a persona's own voice, offered as a pick in\nthe hire or raise flow. The first sample is choice \"a\", the second choice\n\"b\"."
},
- "Webhook": {
+ "WebhookWithoutSecrets": {
"properties": {
"id": { "type": "string", "title": "Id" },
"user_id": { "type": "string", "title": "User Id" },
@@ -38066,7 +38066,6 @@
"type": "object",
"title": "Config"
},
- "secret": { "type": "string", "title": "Secret" },
"organization_id": {
"anyOf": [{ "type": "string" }, { "type": "null" }],
"title": "Organization Id"
@@ -38075,10 +38074,6 @@
"anyOf": [{ "type": "string" }, { "type": "null" }],
"title": "Team Id"
},
- "provider_webhook_id": {
- "type": "string",
- "title": "Provider Webhook Id"
- },
"url": { "type": "string", "title": "Url", "readOnly": true }
},
"type": "object",
@@ -38089,11 +38084,10 @@
"webhook_type",
"resource",
"events",
- "secret",
- "provider_webhook_id",
"url"
],
- "title": "Webhook"
+ "title": "WebhookWithoutSecrets",
+ "description": "Webhook variant that excludes the signing secret and the provider's webhook\nid from serialization (GHSA-4m2w-qfr5-9f3v).\n\nUsed where a webhook leaves the backend: API responses and the\ndatabase-manager RPC. The defaults let the payload validate back on the\nother side, so a preset survives the trip instead of arriving as a dict."
},
"WorkspaceFile": {
"properties": {