Skip to content

fix(trigger): store webhook uploads under the trigger end user - #44995

Merged
Stream29 merged 4 commits into
langgenius:mainfrom
tanishqbaweja:fix/webhook-upload-trigger-end-user
Nov 9, 2026
Merged

Stream29 merged 4 commits into
langgenius:mainfrom
tanishqbaweja:fix/webhook-upload-trigger-end-user

Conversation

@tanishqbaweja

@tanishqbaweja tanishqbaweja commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Important

  1. Make sure you have read our contribution guidelines
  2. Ensure there is an associated issue
  3. Use the correct syntax to link this PR: 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 found and Failed 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 by webhook_trigger.created_by, the app creator's Account (sync_webhook_relationships copies it from app.created_by). The async run executes as the app's Trigger EndUser, the user that trigger_workflow_execution passes to AsyncWorkflowService.trigger_workflow_async. BaseAppGenerator._bind_file_access_scope therefore binds an END_USER file access scope, and DatabaseFileAccessController.apply_tool_file_filters only returns tool files owned by that end user. The upload is never found.

Change.

  • _prepare_webhook_execution passes application_services().app_scoped_end_users.commands to extract_and_validate_webhook_data. This is the same provisioner handle_webhook already gives trigger_workflow_execution. It is threaded through extract_webhook_data to the multipart and octet-stream extractors.
  • The upload path resolves the Trigger EndUser once per request with the same get_or_create_end_user_by_type(type=TRIGGER, tenant_id, app_id, user_id=None) call. _create_file_from_binary now takes that end user and stores the ToolFile under its id. This covers both multipart file fields and application/octet-stream bodies.
  • The lookup lives in a small _resolve_trigger_end_user helper, which trigger_workflow_execution now uses too. Within a request it keeps the end user it resolves in request-local context (flask.g, keyed by tenant and app, like agent_binding in controllers/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_users has 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.
  • Webhook uploads are now app-scoped. App deletion cleanup (_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_run in tests/unit_tests/controllers/trigger/test_webhook.py. It sends a real multipart request through _prepare_webhook_execution. The real ToolFileManager writes to the SQLite unit-test database, with only storage.save stubbed. The test then repeats the worker's lookup: it binds the Trigger EndUser's END_USER scope and rebuilds the file with file_factory.build_from_mapping and DatabaseFileAccessController.

    • Production code from main with the new test: fails with the error from the issue.

      E   ValueError: ToolFile 0461b0aa-f175-4976-bcf0-81d4bcd81c44 not found
      FAILED tests/unit_tests/controllers/trigger/test_webhook.py::TestPrepareWebhookExecution::test_prepare_stores_uploads_for_the_trigger_end_user_run
      1 failed
      
    • 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 an application/octet-stream body through extract_webhook_data and then trigger_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:

      AssertionError: assert EndUser(... id='end-user-a' ...) is EndUser(... id='end-user-b' ...)
      
    • 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:

    • Two multipart files are both created with the Trigger EndUser's id, and the provisioner is called once with (TRIGGER, tenant, app, None).
    • An octet-stream body gets the same owner.
    • _create_file_from_binary passes the end user's id to create_file_by_raw.
    • Existing tests were updated for the new parameter.
  • tests/unit_tests/services/trigger, tests/unit_tests/controllers/trigger and tests/unit_tests/core/workflow/nodes/trigger_webhook: 255 passed and 1 skipped on main, 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 format and ruff check
    • lint-imports (75 contracts kept)
    • dev/pyrefly-check-local on the changed modules
    • dev/lint_response_contracts.py
    • the check_no_new_test_doubles, check_no_spec_mock, check_no_new_getattr, check_no_new_session_mock, check_no_new_controller_sqlalchemy and check_no_new_base_model guards

    pyrefly 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-rev set to this branch's base.

Screenshots

Before After
Upload saved as a ToolFile owned by the app creator's Account; the async run logs ToolFile <id> not found Upload saved under the app's Trigger EndUser; the async run loads it

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've verified the change and added or updated tests where meaningful regression risk justifies coverage.
  • I've updated the documentation accordingly.
  • I ran make lint && make type-check (backend) and vp staged (frontend) to appease the lint gods

From Claude Code

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.
@github-actions github-actions Bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Oct 28, 2026

@Stream29 Stream29 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Reuse the upload-resolved EndUser for dispatch

Comment thread api/services/trigger/webhook_service.py Resolved
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.
@tanishqbaweja

Copy link
Copy Markdown
Contributor Author

Thanks, good catch. Done in 8ec0bdb.

  • _resolve_trigger_end_user now keeps the Trigger EndUser it resolves in request-local context (flask.g, keyed by tenant and app, the same pattern as agent_binding in controllers/common/rbac/locators.py). Later calls in the same request reuse it.
  • The multipart/octet-stream upload resolves it first, so trigger_workflow_execution now queues the run under the end user that owns the request's files, without a second lookup.
  • Outside a request context nothing is cached.
  • A request without files still resolves it once, at dispatch, as before. /webhook-debug only does the upload part.

Regression for your interleaving: test_trigger_workflow_execution_reuses_the_end_user_that_owns_the_uploads.

  • The provisioner returns B for the first lookup and A for any later one (R1 created B, another request committed A).
  • One request sends an octet-stream body through extract_webhook_data and then trigger_workflow_execution.
  • On 80e7b3d the run is queued under A. With this change, both the upload and the run use B, and the provisioner is called once.

test_trigger_end_user_is_looked_up_again_in_the_next_request checks that a new request resolves the end user again, so nothing leaks across requests.

tests/unit_tests/services/trigger, controllers/trigger and core/workflow/nodes/trigger_webhook: 258 passed, 1 skipped. ruff, pyrefly, lint-imports, the response-contract lint and the six guard scripts pass on the new commit.

@github-actions

github-actions Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Pyrefly Diff

base → 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]

@github-actions

github-actions Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 84.53% 84.53% +0.00%
Strict coverage 84.19% 84.19% +0.00%
Typed symbols 88,744 88,753 +9
Untyped symbols 16,304 16,305 +1
Modules 4442 4442 0

@codecov

codecov Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.09%. Comparing base (e37e727) to head (e5a18b4).
⚠️ Report is 6 commits behind head on main.

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     
Flag Coverage Δ
api 89.93% <100.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Stream29
Stream29 previously approved these changes Nov 4, 2026

@Stream29 Stream29 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Stream29
Stream29 added this pull request to the merge queue Nov 4, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to no response for status checks Nov 4, 2026
@tanishqbaweja

Copy link
Copy Markdown
Contributor Author

Merged main to resolve the conflict with #45188 and #45626. Only the imports in tests/unit_tests/controllers/trigger/test_webhook.py conflicted. After the merge, three tests needed the new required arguments: this PR's two tests now pass trigger_status (from #45188), and #45626's test_extract_and_validate_webhook_request_rejects_boolean_for_number_body_param passes end_users, which extract_and_validate_webhook_data() takes since this PR. No change to the non-test code.

tests/unit_tests/controllers/trigger/test_webhook.py and tests/unit_tests/services/trigger/test_webhook_service.py: 105 passed, 1 skipped. ruff format and ruff check are clean.

@tanishqbaweja

Copy link
Copy Markdown
Contributor Author

Merged main again for #45195. The only conflict was two helpers added next to each other in the service tests. This PR's upload-owner test now injects workflow_dispatch like the other tests and checks that the run's actor is the end user that owns the uploads. The two webhook test files: 105 passed, 1 skipped; ruff is clean.

@Stream29 Stream29 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Stream29
Stream29 added this pull request to the merge queue Nov 9, 2026
Merged via the queue into langgenius:main with commit 99683fe Nov 9, 2026
51 checks passed
@wylswz wylswz mentioned this pull request Nov 17, 2026
1 task done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Webhook file upload fails in async workflow because ToolFile owner differs from Trigger EndUser

2 participants