Skip to content

fix(comment-automation): validate flowId exists before saving a reply - #1235

Merged
realcodesiman merged 4 commits into
ChatbotXIO:mainfrom
rawdaymx:fix/comment-automation-flow-validation
Oct 2, 2026
Merged

realcodesiman merged 4 commits into
ChatbotXIO:mainfrom
rawdaymx:fix/comment-automation-flow-validation

Conversation

@rawdaymx

Copy link
Copy Markdown
Contributor

Fixes #1228.

None of CommentAutomation's 8 write methods (Messenger, Instagram, Threads, TikTok — create/update) validated that privateReply.value or publicReply.value pointed at a real flow when the reply type is "flow". AutomatedResponse (Keywords) already does this via flowService.exists before saving (automated-response/service.ts); this table never did, for either reply field or any of its write methods.

A stale or mistyped flowId saves silently and the automation looks active in the UI, but every trigger fails at delivery time with a bare FlowVersion not found (worker's detectFlowVersion, apps/worker/src/lib/db.ts) — that failure is never surfaced back to the person who configured the automation.

Not a tenant-isolation issue

We checked this specifically before treating it as a normal bug: detectFlowVersion scopes its lookup to the triggering conversation's own workspaceId in every case, for both privateReply (private-reply.ts) and publicReply (public-reply.ts). A foreign flowId can never execute cross-tenant — it just fails closed with "not found." That's why this is a validation gap, not a security report.

Change

Adds a private assertFlowReplyExists(workspaceId, field, reply, tx?) helper, mirroring the check AutomatedResponse already runs, and wires it into all 8 write methods:

  • createMessenger / updateMessenger — privateReply + publicReply
  • createInstagram / updateInstagram — privateReply + publicReply
  • createThreadsAutomation / updateThreadsAutomation — publicReply only
  • createTiktokAutomation / updateTiktokAutomation — publicReply only

(Threads and TikTok fix privateReply to {type: "none"} and don't let the caller set it, so only their publicReply needs covering.)

Verification

packages/business/__tests__/comment-automation-write-methods.test.ts, 7 new cases added to the existing suite:

Test Files  1 passed (1)
     Tests  17 passed (17)

ultracite check and check-types are clean.

@github-actions github-actions Bot added the bug Something isn't working as expected label Sep 19, 2026
rawdaymx and others added 3 commits October 2, 2026 08:23
None of CommentAutomation's 8 write methods (Messenger, Instagram,
Threads, TikTok create/update) validated that privateReply.value or
publicReply.value pointed at a real flow when their reply type is
"flow" — AutomatedResponse (Keywords) already does this via
flowService.exists before saving. A stale or mistyped flowId saves
silently and the automation looks active in the UI, but every trigger
fails at delivery time with a bare "FlowVersion not found" (worker's
detectFlowVersion, apps/worker/src/lib/db.ts) — never surfaced back to
the user who configured it.

detectFlowVersion scopes its lookup to the triggering conversation's
own workspaceId in all cases, so a foreign flowId can never execute
cross-tenant — this is a validation gap, not a tenant-isolation
defect.

Adds assertFlowReplyExists, mirroring the check Keywords already runs,
wired into all 8 write methods for privateReply and (where the
channel lets the caller set it) publicReply.
service.ts now calls flowService.exists() before saving a flow reply
(previous commit). Importing the real flowService transitively pulls
in botFieldService -> the full contact-filter query builder, which
these two suites fully replace @chatbotx.io/database/partials and
@chatbotx.io/database/schema without, so the real chain broke both
with "No X export is defined on the mock".

Neither suite is testing flow validation, so flowService.exists is
stubbed to always resolve true instead of chasing the transitive mock
surface deeper.
… missed-comments test mocks

Rebasing fix/comment-automation-flow-validation onto main wires a new
`flowService` import into comment-automation/service.ts, pulling in
`../bot-field/service`, which transitively reaches
`@chatbotx.io/database/queries/contact-filter` at module load time.
That module needs `operatorTypes` (from partials) and
`contactsToTagsModel` (from schema), neither of which this test's
hand-picked partials/schema mocks exposed. Switched both mocks to
spread the real module (`vi.importActual`) and override only the
fields this test actually fakes, instead of hand-listing every export
a transitive import might need.
@realcodesiman
realcodesiman force-pushed the fix/comment-automation-flow-validation branch from 6319a18 to 532f67e Compare October 2, 2026 01:40
…test drift

Collapse the 12 near-identical assertFlowReplyExists call blocks across
the 8 write methods into one assertReplyFlowsExist(workspaceId, replies,
tx?) call per method, checking privateReply before publicReply so the
rejected field stays deterministic. Shrink the worker-narrative docblock
to why the check exists.

Test fixes from review:
- missed-comments test: revert the partials/schema mocks back to the
  hand-picked stubs (pre-532f67e41), stub ../src/flow/service instead of
  importing the real schema module, which can open a DB connection via
  the sharding client.
- write-methods test: fix the "both reply flows exist" case to actually
  set both replies and assert flowExists runs in order; add rejection
  coverage for updateMessenger, createInstagram, updateTiktokAutomation;
  pin the privateReply-first error order when both flows are missing.
- list/service test mocks: drop the "now calls" wording that goes stale.
@realcodesiman
realcodesiman merged commit 38b1d3d into ChatbotXIO:main Oct 2, 2026
3 checks passed
hello-jackytruong referenced this pull request in hello-jackytruong/ChatWorkAI Nov 22, 2026
… (#1235)

* fix(comment-automation): validate flowId exists before saving a reply

None of CommentAutomation's 8 write methods (Messenger, Instagram,
Threads, TikTok create/update) validated that privateReply.value or
publicReply.value pointed at a real flow when their reply type is
"flow" — AutomatedResponse (Keywords) already does this via
flowService.exists before saving. A stale or mistyped flowId saves
silently and the automation looks active in the UI, but every trigger
fails at delivery time with a bare "FlowVersion not found" (worker's
detectFlowVersion, apps/worker/src/lib/db.ts) — never surfaced back to
the user who configured it.

detectFlowVersion scopes its lookup to the triggering conversation's
own workspaceId in all cases, so a foreign flowId can never execute
cross-tenant — this is a validation gap, not a tenant-isolation
defect.

Adds assertFlowReplyExists, mirroring the check Keywords already runs,
wired into all 8 write methods for privateReply and (where the
channel lets the caller set it) publicReply.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Comment automations never validate a reply's flow id — Keywords does, comment triggers don't, so a bad reference silently dead-ends at delivery time

2 participants