fix: bound the group-translate skip filter and scope the chatwoot reply mapping - #93
Merged
Conversation
The filter that skips messages with nothing to translate was one regular expression whose emoji branch
overlapped its URL branch: `\p{Emoji}` matches ASCII digits, `#` and `*`, because those are
keycap-sequence components, and so it also matched the `\S+` inside `https?://\S+`. Every additional
link-shaped token multiplied the backtracking space — a 145-character message took several seconds,
and `maxLength` allows 2000. Regex execution cannot be interrupted, so that time is spent holding the
worker's event loop, and any member of a group with translation on could send it.
Scanning token by token is linear and needs no backtracking at all. Measured on the same input: 5.5 s
before, 0.08 ms after, and flat at 3200 characters.
Switching the emoji test to `\p{Extended_Pictographic}` fixes a second consequence of the same
property: a message of `1` or `#5` was classed as emoji-only and silently left untranslated.
…tenants Chatwoot conversation ids are per-account autoincrement, so two accounts relayed by one gateway share one as a matter of course. Beside the tenant-scoped reverse mapping, an unscoped one was rewritten on every link and therefore held whichever session linked last. A delivery arriving without a session scope resolved through it — an agent reply for one customer sent to a different customer's number. Unlinking by conversation id deleted that shared mapping too, silently dropping the other tenant's replies until their next inbound message rebuilt it. The unscoped mapping is now claimed only while it is unclaimed, and marked unusable once a second session claims the same id: nothing in a scope-less delivery says which tenant it belongs to, so it is dropped and logged rather than routed to a guess. Unlink removes it only when it belongs to the session being unlinked. Mappings written before scoping keep resolving and single-tenant hosts are unaffected. One echo-loop test relied on the old guess and now supplies the scope it would carry in practice, which is what it meant to exercise; a new test pins the drop.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Two defects on the message path, both reachable from ordinary traffic and both reproduced before being fixed.
group-translate — a group message could stall the plugin worker
The filter that skips messages with nothing to translate was one regular expression:
\p{Emoji}matches ASCII digits,#and*— they are keycap-sequence components — so the emoji branch also matched the\S+inside the URL branch. Every additional link-shaped token multiplied the backtracking space:test()The identical payload written with letters instead of digits completes in 0.0 ms, which isolates the cause.
maxLengthdefaults to 2000 characters, JavaScript regex execution cannot be interrupted, and any member of a group with translation on could send such a message — so the time is spent holding the worker's event loop outright.The filter now splits on whitespace and checks each token, which is linear and needs no backtracking. Same input: 5 526 ms → 0.08 ms, and flat at 3200 characters.
Switching the emoji test to
\p{Extended_Pictographic}— the property that actually means "picture character" — fixes a second consequence of the same overlap: a message of1or#5was classed as emoji-only and silently left untranslated.chatwoot-adapter — an agent reply could reach the wrong customer
Chatwoot conversation ids are per-account autoincrement, so two accounts relayed by one gateway share one routinely. Beside the tenant-scoped reverse mapping (
wa:<session>:<id>), an unscoped one (wa:<id>) was rewritten on every link, so it held whichever session linked last. A delivery that arrived without a session scope resolved through it and was sent to that session's chat — one customer's agent reply delivered to another customer's number.Unlinking by conversation id, which the 404 recovery path does, deleted that shared mapping as well — silently dropping the other tenant's replies until their next inbound message rebuilt it.
The unscoped mapping is now claimed only while it is unclaimed, and marked unusable once a second session claims the same id. Nothing in a scope-less delivery identifies its tenant, so there is no right answer to guess: it is dropped and logged instead. Unlink removes the mapping only when it belongs to the session being unlinked.
Compatibility. Mappings written before scoping keep resolving, and a single-tenant host is unaffected — the unscoped key is still claimed and still used. One echo-loop test relied on the old guess; it now supplies the session scope such a delivery carries in practice, which is what it meant to exercise, and a new test pins the drop.
Verification