fix(msteams): handle invalid JSON escape sequences in Bot Framework activities#34581
fix(msteams): handle invalid JSON escape sequences in Bot Framework activities#34581hddevteam wants to merge 5 commits intoopenclaw:mainfrom
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 5b2a2fb988
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
Greptile SummaryThis PR replaces Key changes:
Potential issue: Confidence Score: 4/5
Last reviewed commit: 5b2a2fb |
There was a problem hiding this comment.
Pull request overview
Updates the MS Teams provider’s Express webhook handling to tolerate Bot Framework activity payloads containing invalid JSON escape sequences (e.g., \p) so the endpoint avoids returning non-200 responses that trigger Azure Bot Service exponential backoff.
Changes:
- Replace
express.json()withexpress.raw()and a custom two-pass JSON parse/repair step. - Add error handling to acknowledge unrecoverable JSON parse errors with HTTP 200 while logging details.
- Tighten the 413 (“Payload too large”) error guard type check.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 88468fab95
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
|
Thanks for the thorough reviews! All three issues have been addressed in the latest commits: Regex edge case (Greptile + chatgpt-codex-connector feedback) A dedicated regression test (
All changes are in commit |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: ef2f5fbdeb
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
|
Fixed CI formatting failure in commit The |
|
Hi @hddevteam — thanks for the submission. I’m the new Microsoft Teams maintainer for OpenClaw. Please give me a day or two to work through the open Teams backlog. Also, join the Twitter community for daily MS Teams feedback + updates: https://x.com/i/communities/2031170403607974228 |
1 similar comment
|
Hi @hddevteam — thanks for the submission. I’m the new Microsoft Teams maintainer for OpenClaw. Please give me a day or two to work through the open Teams backlog. Also, join the Twitter community for daily MS Teams feedback + updates: https://x.com/i/communities/2031170403607974228 |
|
Codex review: needs maintainer review before merge. What this changes: The PR changes the MS Teams webhook middleware from Maintainer follow-up before merge: This is an open implementation PR for a valid MS Teams webhook bug, and the remaining action is maintainer review or a small test-quality request rather than an automated replacement fix. Security review: Security review cleared: The diff keeps JWT validation before body parsing, preserves the body-size limit, adds no dependency or workflow changes, and does not expose a concrete supply-chain issue. Review detailsBest possible solution: Review and land this MS Teams-owned webhook parsing fix, ideally with a small production-middleware regression test or an exported local helper test so the repair logic is covered directly. No core/plugin API change appears necessary. Do we have a high-confidence way to reproduce the issue? Yes. Current main and Is this the best way to solve the issue? Yes, broadly. Parsing raw bytes after JWT validation preserves the existing pre-parse auth hardening and gives the plugin a narrow place to repair Teams payloads; the safer pre-merge refinement is stronger production-path test coverage, not a different architecture. Acceptance criteria:
What I checked:
Likely related people:
Remaining risk / open question:
Codex review notes: model gpt-5.5, reasoning high; reviewed against 554b32feeac0. |
|
This assigned pull request has been marked as stale after being open for 27 days. |
|
This assigned pull request has been automatically marked as stale after being open for 27 days. |
|
This assigned pull request has been automatically marked as stale after being open for 27 days. |
d14ee15 to
2e72f13
Compare
…ctivities Some Bot Framework clients (e.g. certain MS Teams clients) send activity payloads that contain invalid JSON escape sequences such as bare backslashes followed by characters not defined in RFC 8259 (e.g. \p, \q, \c). The previous `express.json()` middleware uses body-parser's strict JSON parser which throws `SyntaxError: Bad escaped character in JSON` on such payloads. This causes the webhook to return a non-200 response, putting Azure Bot Service into exponential backoff and dropping subsequent messages. This fix replaces `express.json()` with `express.raw()` (raw Buffer body), then adds a two-pass JSON parse middleware: 1. First tries standard JSON.parse on the raw body. 2. If that fails (SyntaxError), attempts to repair the payload by double-escaping bare backslashes: /\\([^"\\/bfnrtu])/g → \\\\$1 3. If the repaired payload parses successfully, logs a warning and continues normally so the activity is processed. 4. If the payload cannot be repaired, responds with HTTP 200 to prevent Azure Bot Service backoff, and logs the error for investigation. Fixes: SyntaxError: Bad escaped character in JSON at position N Tested: conversationUpdate and message activities from MS Teams clients
…iddleware Covers the bug where express.json()'s strict parser throws SyntaxError on Bot Framework activities containing invalid escape sequences (e.g. \\p, \\q), causing Azure Bot Service to enter exponential backoff and drop messages. Tests verify: 1. Original JSON.parse() fails on bad-escape payloads (proving the bug) 2. Two-pass repair middleware recovers such payloads 3. Normal well-formed payloads still work (no regression) 4. Valid escape sequences (\\n \\t \\\\ etc.) are not double-escaped 5. Completely unrecoverable JSON is re-thrown (caller returns HTTP 200)"
Copilot Reviewer identified that the express mock in monitor.lifecycle.test.ts exported json() but not raw(). Since monitor.ts now uses express.raw() instead of express.json(), the test suite would throw 'express.raw is not a function' at startup. Add raw() as a no-op middleware factory alongside json() in the mock, matching the pattern already used for json().
CI 'Check types and lint and oxfmt' step failed because our new files were not formatted with oxfmt. Run oxfmt --write on both files to fix.
2e72f13 to
7155816
Compare
|
This assigned pull request has been automatically marked as stale after being open for 27 days. |
|
This assigned pull request has been automatically marked as stale after being open for 27 days. |
|
Closing due to inactivity. |
Problem
Some Bot Framework clients (e.g. certain MS Teams desktop/mobile clients) send
activity payloads that contain invalid JSON escape sequences — bare backslashes
followed by characters not defined in RFC 8259
(e.g.
\p,\q,\c).The existing
express.json()middleware uses body-parser's strict JSON parserwhich throws:
This causes the webhook handler to return a non-200 response to Azure Bot
Service. Azure Bot Service interprets non-200 responses as delivery failures and
enters exponential backoff — dropping all subsequent user messages until the
backoff window expires (which can be minutes to hours).
Observed Symptom
conversationUpdateon channel create) triggers theSyntaxError, msteams provider replies with 4xx/5xxthe local endpoint)
Fix
Replace
express.json()with a two-pass raw body parsing approach:Bufferviaexpress.raw({ type: 'application/json' })JSON.parse()on the raw bodySyntaxError): attempt to repair thepayload by double-escaping bare backslashes via regex:
/\\([^"\\/bfnrtu])/g → \\\\$1Azure Bot Service backoff, and log the error for investigation
Testing
Observed and confirmed with a real Teams bot deployment:
conversationUpdateactivity with an invalidescape sequence at position 388 → previously caused SyntaxError + Azure
backoff for subsequent messages
processed normally
backoff loops
Notes
express.raw()is already part of Express's built-in middleware (same asexpress.json()), no new dependencies neededvalid JSON escape sequences (
\",\\,\/,\b,\f,\n,\r,\t,\uXXXX) are preservedwell-formed payloads