Repository navigation
fix(trigger): store webhook uploads under the trigger end user - #44995
Conversation
Multipart and octet-stream webhook uploads were saved as ToolFiles owned by the account that created the webhook, but the async run executes as the app's trigger end user, and an end user's file access scope only loads the tool files it owns. The worker then failed with "ToolFile <id> not found" and downstream nodes never received the file. Pass the injected end-user provisioner into request extraction and make the trigger end user the owner of uploaded files. Uploads resolve it once per request through the same helper that trigger_workflow_execution uses for the run identity.
Stream29
left a comment
There was a problem hiding this comment.
[P2] Reuse the upload-resolved EndUser for dispatch
The upload path and trigger_workflow_execution each looked the trigger end user up with get-or-create. end_users has no unique constraint, so a concurrent first request to a new app can create a second trigger end user in between, and the run was then queued under an end user that does not own this request's uploads. Keep the trigger end user resolved in a request in request-local context (flask.g, keyed by tenant and app) and reuse it for the run. Outside a request context nothing is cached.
|
Thanks, good catch. Done in 8ec0bdb.
Regression for your interleaving:
|
Pyrefly Diffbase → PR--- /tmp/pyrefly_base.txt 2026-11-09 09:07:32.336533247 +0000
+++ /tmp/pyrefly_pr.txt 2026-11-09 09:07:17.756374507 +0000
@@ -2701,7 +2701,7 @@
ERROR Object of class `FromClause` has no attribute `drop` [missing-attribute]
--> tests/unit_tests/controllers/trigger/test_trigger.py:46:5
ERROR `_RequestStub` is not assignable to attribute `request` with type `Request` [bad-assignment]
- --> tests/unit_tests/controllers/trigger/test_webhook.py:47:22
+ --> tests/unit_tests/controllers/trigger/test_webhook.py:70:22
ERROR Argument `None` is not assignable to parameter `type` with type `EndUserType | SQLCoreOperations[EndUserType]` in function `models.model.EndUser.__init__` [bad-argument-type]
--> tests/unit_tests/controllers/web/test_wraps.py:387:81
ERROR Expected 1 more positional argument [bad-argument-count]
@@ -5909,41 +5909,41 @@
ERROR Argument `dict[str, str | dict[str, str]]` is not assignable to parameter `node_config` with type `NodeConfigDict` in function `services.trigger.schedule_service.ScheduleService.to_schedule_config` [bad-argument-type]
--> tests/unit_tests/services/trigger/test_schedule_service.py:592:56
ERROR Argument `dict[str, dict[Unknown, Unknown]]` is not assignable to parameter `node_config` with type `NodeConfigDict` in function `services.trigger.webhook_service.WebhookService.generate_webhook_response` [bad-argument-type]
- --> tests/unit_tests/services/trigger/test_webhook_service.py:507:83
+ --> tests/unit_tests/services/trigger/test_webhook_service.py:589:83
ERROR Argument `dict[str, dict[str, int | str]]` is not assignable to parameter `node_config` with type `NodeConfigDict` in function `services.trigger.webhook_service.WebhookService.generate_webhook_response` [bad-argument-type]
- --> tests/unit_tests/services/trigger/test_webhook_service.py:517:83
+ --> tests/unit_tests/services/trigger/test_webhook_service.py:599:83
ERROR Argument `dict[str, dict[str, int | str]]` is not assignable to parameter `node_config` with type `NodeConfigDict` in function `services.trigger.webhook_service.WebhookService.generate_webhook_response` [bad-argument-type]
- --> tests/unit_tests/services/trigger/test_webhook_service.py:527:83
+ --> tests/unit_tests/services/trigger/test_webhook_service.py:609:83
ERROR Argument `dict[str, dict[str, int | str]]` is not assignable to parameter `node_config` with type `NodeConfigDict` in function `services.trigger.webhook_service.WebhookService.generate_webhook_response` [bad-argument-type]
- --> tests/unit_tests/services/trigger/test_webhook_service.py:536:83
+ --> tests/unit_tests/services/trigger/test_webhook_service.py:618:83
ERROR Argument `dict[str, dict[str, int | str]]` is not assignable to parameter `node_config` with type `NodeConfigDict` in function `services.trigger.webhook_service.WebhookService.generate_webhook_response` [bad-argument-type]
- --> tests/unit_tests/services/trigger/test_webhook_service.py:545:83
+ --> tests/unit_tests/services/trigger/test_webhook_service.py:627:83
ERROR Argument `dict[str, dict[str, int | str]]` is not assignable to parameter `node_config` with type `NodeConfigDict` in function `services.trigger.webhook_service.WebhookService.generate_webhook_response` [bad-argument-type]
- --> tests/unit_tests/services/trigger/test_webhook_service.py:555:83
+ --> tests/unit_tests/services/trigger/test_webhook_service.py:637:83
ERROR Argument `dict[str, dict[str, list[dict[str, bool | str]] | str]]` is not assignable to parameter `node_config` with type `NodeConfigDict` in function `services.trigger.webhook_service.WebhookService.extract_and_validate_webhook_data` [bad-argument-type]
- --> tests/unit_tests/services/trigger/test_webhook_service.py:807:92
+ --> tests/unit_tests/services/trigger/test_webhook_service.py:895:38
ERROR Argument `dict[str, dict[str, str]]` is not assignable to parameter `node_config` with type `NodeConfigDict` in function `services.trigger.webhook_service.WebhookService.extract_and_validate_webhook_data` [bad-argument-type]
- --> tests/unit_tests/services/trigger/test_webhook_service.py:834:87
+ --> tests/unit_tests/services/trigger/test_webhook_service.py:924:42
ERROR Argument `dict[str, dict[str, str]]` is not assignable to parameter `node_config` with type `NodeConfigDict` in function `services.trigger.webhook_service.WebhookService.extract_and_validate_webhook_data` [bad-argument-type]
- --> tests/unit_tests/services/trigger/test_webhook_service.py:854:87
+ --> tests/unit_tests/services/trigger/test_webhook_service.py:946:42
ERROR Argument `dict[str, dict[str, list[dict[str, bool | str]] | str]]` is not assignable to parameter `node_config` with type `NodeConfigDict` in function `services.trigger.webhook_service.WebhookService.extract_and_validate_webhook_data` [bad-argument-type]
- --> tests/unit_tests/services/trigger/test_webhook_service.py:872:90
+ --> tests/unit_tests/services/trigger/test_webhook_service.py:966:45
ERROR Argument `dict[str, dict[str, list[dict[str, bool | str]] | str]]` is not assignable to parameter `node_config` with type `NodeConfigDict` in function `services.trigger.webhook_service.WebhookService.extract_and_validate_webhook_data` [bad-argument-type]
- --> tests/unit_tests/services/trigger/test_webhook_service.py:891:95
+ --> tests/unit_tests/services/trigger/test_webhook_service.py:987:41
ERROR Argument `dict[str, dict[str, list[dict[str, bool | str]] | str]]` is not assignable to parameter `node_config` with type `NodeConfigDict` in function `services.trigger.webhook_service.WebhookService.extract_and_validate_webhook_data` [bad-argument-type]
- --> tests/unit_tests/services/trigger/test_webhook_service.py:913:90
+ --> tests/unit_tests/services/trigger/test_webhook_service.py:1011:45
ERROR Argument `dict[str, dict[str, list[dict[str, bool | str]] | str]]` is not assignable to parameter `node_config` with type `NodeConfigDict` in function `services.trigger.webhook_service.WebhookService.extract_and_validate_webhook_data` [bad-argument-type]
- --> tests/unit_tests/services/trigger/test_webhook_service.py:932:90
+ --> tests/unit_tests/services/trigger/test_webhook_service.py:1032:45
ERROR Argument `dict[str, dict[str, list[dict[str, bool | str]] | str]]` is not assignable to parameter `node_config` with type `NodeConfigDict` in function `services.trigger.webhook_service.WebhookService.extract_and_validate_webhook_data` [bad-argument-type]
- --> tests/unit_tests/services/trigger/test_webhook_service.py:951:90
+ --> tests/unit_tests/services/trigger/test_webhook_service.py:1053:45
ERROR Argument `dict[str, dict[str, list[dict[str, bool | str]] | str]]` is not assignable to parameter `node_config` with type `NodeConfigDict` in function `services.trigger.webhook_service.WebhookService.extract_and_validate_webhook_data` [bad-argument-type]
- --> tests/unit_tests/services/trigger/test_webhook_service.py:969:95
+ --> tests/unit_tests/services/trigger/test_webhook_service.py:1073:41
ERROR Argument `dict[str, str | dict[str, str]]` is not assignable to parameter `webhook_data` with type `RawWebhookDataDict` in function `services.trigger.webhook_service.WebhookService._validate_http_metadata` [bad-argument-type]
- --> tests/unit_tests/services/trigger/test_webhook_service.py:1249:61
+ --> tests/unit_tests/services/trigger/test_webhook_service.py:1356:61
ERROR Argument `dict[str, dict[str, int] | dict[str, str]]` is not assignable to parameter `webhook_data` with type `RawWebhookDataDict` in function `services.trigger.webhook_service.WebhookService.build_workflow_inputs` [bad-argument-type]
- --> tests/unit_tests/services/trigger/test_webhook_service.py:1261:59
+ --> tests/unit_tests/services/trigger/test_webhook_service.py:1368:59
ERROR Argument `dict[str, dict[str, int | str]]` is not assignable to parameter `node_config` with type `NodeConfigDict` in function `services.trigger.webhook_service.WebhookService.generate_webhook_response` [bad-argument-type]
- --> tests/unit_tests/services/trigger/test_webhook_service.py:1272:69
+ --> tests/unit_tests/services/trigger/test_webhook_service.py:1379:69
ERROR Cannot find module `dify_graph.enums` [missing-import]
--> tests/unit_tests/services/workflow/test_workflow_converter.py:36:5
ERROR Cannot find module `dify_graph.model_runtime.entities.llm_entities` [missing-import]
|
Pyrefly Type Coverage
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #44995 +/- ##
==========================================
- Coverage 90.09% 90.09% -0.01%
==========================================
Files 6269 6269
Lines 391992 392005 +13
Branches 78442 78444 +2
==========================================
+ Hits 353153 353164 +11
- Misses 32740 32744 +4
+ Partials 6099 6097 -2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Merged
|
|
Merged |
Important
Fixes #<issue number>.Summary
Fixes #42987
A Webhook Trigger that receives a file returns 200, but the published (async) run cannot load the file. The worker logs
ToolFile <id> not foundandFailed to build FileVariable for webhook file parameter ..., and downstream nodes then fail with... is not an ArrayFileSegment.Cause.
WebhookService._create_file_from_binary()saved every upload as a ToolFile owned bywebhook_trigger.created_by, the app creator's Account (sync_webhook_relationshipscopies it fromapp.created_by). The async run executes as the app's Trigger EndUser, the user thattrigger_workflow_executionpasses toAsyncWorkflowService.trigger_workflow_async.BaseAppGenerator._bind_file_access_scopetherefore binds anEND_USERfile access scope, andDatabaseFileAccessController.apply_tool_file_filtersonly returns tool files owned by that end user. The upload is never found.Change.
_prepare_webhook_executionpassesapplication_services().app_scoped_end_users.commandstoextract_and_validate_webhook_data. This is the same provisionerhandle_webhookalready givestrigger_workflow_execution. It is threaded throughextract_webhook_datato the multipart and octet-stream extractors.get_or_create_end_user_by_type(type=TRIGGER, tenant_id, app_id, user_id=None)call._create_file_from_binarynow takes that end user and stores the ToolFile under its id. This covers both multipart file fields andapplication/octet-streambodies._resolve_trigger_end_userhelper, whichtrigger_workflow_executionnow uses too. Within a request it keeps the end user it resolves in request-local context (flask.g, keyed by tenant and app, likeagent_bindingincontrollers/common/rbac/locators.py) and returns it again on later calls. So the run is queued under the same Trigger EndUser that owns the request's uploads, without a second lookup. Outside a request context nothing is cached.The change keeps the provisioner injection from #41398 instead of reaching for the composition root inside the service. It does what the reporter's production workaround does. #44796 fixed the schedule-trigger path; this PR fixes the webhook path.
Behaviour to be aware of:
/webhook-debug/...shares the extraction code, so a debug request with a file now provisions the app's Trigger EndUser, as production webhook runs already do, and its upload is owned by that end user. Debug runs execute as the console Account, whose file access scope is only filtered by tenant, so they still load the file.end_usershas no unique constraint, so concurrent first requests to a new app can create two default Trigger EndUser rows. Because the run reuses the end user the upload resolved, a request's run always owns its own uploads, even if another request creates a second row between the upload and the dispatch. Which row later requests get is unchanged; the duplicate-creation race itself is pre-existing._cleanup_tool_files) removes conversation-less ToolFiles owned by the app's end users. Before this change these uploads belonged to the Account and were kept.Tests
New regression test
TestPrepareWebhookExecution::test_prepare_stores_uploads_for_the_trigger_end_user_runintests/unit_tests/controllers/trigger/test_webhook.py. It sends a real multipart request through_prepare_webhook_execution. The realToolFileManagerwrites to the SQLite unit-test database, with onlystorage.savestubbed. The test then repeats the worker's lookup: it binds the Trigger EndUser'sEND_USERscope and rebuilds the file withfile_factory.build_from_mappingandDatabaseFileAccessController.Production code from
mainwith the new test: fails with the error from the issue.With this change:
1 passed.Two new service tests cover the request-local reuse:
test_trigger_workflow_execution_reuses_the_end_user_that_owns_the_uploads: one request sends anapplication/octet-streambody throughextract_webhook_dataand thentrigger_workflow_execution. The provisioner returns end user B for the first lookup and A for any later one, the interleaving from the review. The upload and the queued run both use B, and the provisioner is called once. On the previous commit (80e7b3d) the run is queued under A:test_trigger_end_user_is_looked_up_again_in_the_next_request: within one request the end user is looked up once; a second request looks it up again, so nothing is shared across requests. It fails on 80e7b3d too, where every call looks the end user up.The service tests now check the file owner:
(TRIGGER, tenant, app, None)._create_file_from_binarypasses the end user's id tocreate_file_by_raw.tests/unit_tests/services/trigger,tests/unit_tests/controllers/triggerandtests/unit_tests/core/workflow/nodes/trigger_webhook: 255 passed and 1 skipped onmain, 258 passed and 1 skipped with this change. The skipped test is the python-magic test, which skips here because libmagic isn't available.These checks all pass:
ruff formatandruff checklint-imports(75 contracts kept)dev/pyrefly-check-localon the changed modulesdev/lint_response_contracts.pycheck_no_new_test_doubles,check_no_spec_mock,check_no_new_getattr,check_no_new_session_mock,check_no_new_controller_sqlalchemyandcheck_no_new_base_modelguardspyrefly reports no new diagnostics in the changed test files. These checks were re-run on the follow-up commit; the guards were run with
--base-revset to this branch's base.Screenshots
ToolFile <id> not foundChecklist
make lint && make type-check(backend) andvp staged(frontend) to appease the lint godsFrom Claude Code