-
Notifications
You must be signed in to change notification settings - Fork 0
fix: team-scope workflow trigger uniqueness #165
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
michaelmcnees
merged 2 commits into
main
from
claude/office-assistant-mantle-feasibility-n8f4bk
Jul 2, 2026
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@mantle/engine": patch | ||
| --- | ||
|
|
||
| Make workflow trigger uniqueness multi-tenant-safe. The cron trigger uniqueness index is now scoped by team (`team_id, workflow_name, schedule`), so two teams can each register a workflow with the same name and schedule instead of the second team's apply failing with a unique-constraint violation. Webhook paths remain a global routing key (an inbound `POST /hooks/<path>` carries no team identity), and the webhook lookup is no longer team-scoped: it resolves by path and runs the workflow under the owning team from the matched trigger row, so webhooks registered by non-default teams now route correctly. Fixes #164. |
23 changes: 23 additions & 0 deletions
23
packages/engine/internal/db/migrations/022_team_scoped_trigger_uniqueness.sql
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,23 @@ | ||
| -- +goose Up | ||
| -- 022_team_scoped_trigger_uniqueness: cron trigger uniqueness must be per-team. | ||
| -- | ||
| -- Workflow names are team-scoped (workflow_definitions is UNIQUE(team_id, name, | ||
| -- version)), so two teams may legitimately own a workflow of the same name on | ||
| -- the same schedule. The original index on (workflow_name, schedule) was global, | ||
| -- so the second team's apply failed with a unique-constraint violation once | ||
| -- trigger registration went live. Scope the uniqueness by team. | ||
| -- | ||
| -- The webhook path index is intentionally left global: an inbound | ||
| -- POST /hooks/<path> request carries no team identity, so the path is the global | ||
| -- routing key and must be unique across all teams. Email triggers have no | ||
| -- uniqueness index and are unaffected. | ||
| DROP INDEX IF EXISTS idx_workflow_triggers_cron; | ||
| CREATE UNIQUE INDEX idx_workflow_triggers_cron | ||
| ON workflow_triggers (team_id, workflow_name, schedule) | ||
| WHERE type = 'cron'; | ||
|
|
||
| -- +goose Down | ||
| DROP INDEX IF EXISTS idx_workflow_triggers_cron; | ||
| CREATE UNIQUE INDEX idx_workflow_triggers_cron | ||
| ON workflow_triggers (workflow_name, schedule) | ||
| WHERE type = 'cron'; |
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,56 @@ | ||
| package server | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/dvflw/mantle/internal/auth" | ||
| ) | ||
|
|
||
| // TestLookupWebhookTrigger_GlobalPathReturnsOwningTeam verifies that a webhook | ||
| // registered by a non-default team is found by an unauthenticated inbound | ||
| // lookup (which carries no team identity) and reports the owning team, so the | ||
| // handler can run the workflow under the correct tenant. Previously the lookup | ||
| // was team-scoped to the default team, so non-default-team webhooks never | ||
| // routed. | ||
| func TestLookupWebhookTrigger_GlobalPathReturnsOwningTeam(t *testing.T) { | ||
| db, _ := setupWebhookTest(t) | ||
| ctx := context.Background() | ||
|
|
||
| teamB := "00000000-0000-0000-0000-0000000000b2" | ||
| if _, err := db.ExecContext(ctx, | ||
| `INSERT INTO teams (id, name) VALUES ($1, 'team-b')`, teamB); err != nil { | ||
| t.Fatalf("creating team-b: %v", err) | ||
| } | ||
| if _, err := db.ExecContext(ctx, | ||
| `INSERT INTO workflow_triggers (workflow_name, workflow_version, type, path, team_id) | ||
| VALUES ('wf', 1, 'webhook', '/hooks/tb', $1)`, teamB); err != nil { | ||
| t.Fatalf("seeding webhook trigger: %v", err) | ||
| } | ||
|
|
||
| // No team in context, as an inbound webhook request would be. | ||
| tr, err := LookupWebhookTrigger(ctx, db, "/hooks/tb") | ||
| if err != nil { | ||
| t.Fatalf("LookupWebhookTrigger: %v", err) | ||
| } | ||
| if tr == nil { | ||
| t.Fatal("expected trigger, got nil") | ||
| } | ||
| if tr.TeamID != teamB { | ||
| t.Errorf("TeamID = %q, want %q", tr.TeamID, teamB) | ||
| } | ||
| if tr.WorkflowName != "wf" { | ||
| t.Errorf("WorkflowName = %q, want wf", tr.WorkflowName) | ||
| } | ||
|
|
||
| // A default-team context must not change the result — the path is a global | ||
| // routing key, not team-scoped. | ||
| ctxDefault := auth.WithUser(ctx, &auth.User{TeamID: auth.DefaultTeamID}) | ||
| tr2, err := LookupWebhookTrigger(ctxDefault, db, "/hooks/tb") | ||
| if err != nil { | ||
| t.Fatalf("LookupWebhookTrigger (default ctx): %v", err) | ||
| } | ||
| if tr2 == nil || tr2.TeamID != teamB { | ||
| t.Errorf("global lookup should be team-agnostic; got %+v", tr2) | ||
| } | ||
| } |
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
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.
In the normal
mantle servepathsrv.AuthStoreis always set andStartwrapsapiMux(including/hooks/) inAuthMiddleware, so requests that reach this function already have a caller team inctx. This path-only lookup ignores that team and the handler then executes under the matched row'steam_id, which lets an API key from team A POST to any known webhook path owned by team B and run B's workflow/credentials; keep the team predicate for authenticated requests or explicitly bypass auth only for public webhook mode with separate authorization.Useful? React with 👍 / 👎.