Files
bd5adc61c8 feat(bitrix24): imbot.v2 migration, 2-way media, openline sender-tag echo, and hardening (#1236)
* refactor(bitrix24): rename "Path B" framing to maintainer-specified naming [B24:2794]

Per maintainer hard rule #10 (no generic "Path A/B" framing) from PR #1061
review. The Bitrix24 MCP auto-onboard flow is Bitrix-specific glue
("Bitrix24 OAuth -> existing mcp_user_credentials bridge"), NOT a generic
MCP architecture pattern.

Naming convention applied consistently:
- First mention per file: full "Bitrix24 OAuth -> existing
  mcp_user_credentials bridge" (matches maintainer comment verbatim).
- Subsequent mentions in same file: shortened "mcp_user_credentials bridge".
- Test/log context referencing literal endpoint /api/auto-onboard: keep
  "auto-onboard" reference (it's the actual API endpoint name).

Changes are documentation-only:
- Rename in code comments + test descriptions + plan docs.
- Clarify framing in mcp_client.go + provisioner.go doc comments to
  emphasize Bitrix-specific glue (not generic MCP infra).
- Reuse existing mcp_user_credentials table + MCPServerStore methods
  (no schema / store / abstraction change).

Files:
- cmd/gateway.go (factory registration doc)
- internal/channels/bitrix24/{channel,factory,mcp_client,provisioner}.go
- internal/channels/bitrix24/{mcp_client,provisioner}_test.go
- plan/goclaw-mcp-integration.md (21 occurrences)

Verified: go build + MCP-related tests pass (TestProvision*,
TestInitMCPProvisioner*, TestMCPClient*).

Phase 1 of Path C execution per
plans/reports/decision-log-260519-1555-bitrix24-pr-fork-decision.md.

* fix: confine outbound media paths to agent workspace [B24:2794]

Tool MEDIA:<path> output reached channel file-upload sinks (Bitrix
imbot.v2.File.upload, Telegram sendDocument, etc.) verbatim via
parseMediaResult, with no workspace-boundary check. A malicious or buggy
tool emitting MEDIA:/etc/passwd could exfiltrate arbitrary files to chat.

Extract the EvalSymlinks+Rel containment from extractMediaFromContent into
a shared confineToWorkspace helper and apply it at the parseMediaResult
sink in processToolResult. Fixing at the source/egress boundary protects
every channel at once rather than per-channel. Paths that escape the
workspace are dropped and logged (security.media_path_rejected).

Add TestConfineToWorkspace (boundary unit) and
TestParseMediaResultConfinedToWorkspace (sink regression for H2).

* feat(bitrix24): support inbound + outbound media via imbot.v2 File API [B24:2794]

Bitrix24 channel was text-only; attachments were parsed but dropped.
- Inbound: download chat files via imbot.v2.File.download (one-time URL),
  forward to the agent with MIME preserved (internal/channels/bitrix24/download.go).
- Outbound: upload agent media to the chat via imbot.v2.File.upload
  (internal/channels/bitrix24/send_media.go).
- Add BaseChannel.HandleMessageMedia to preserve MIME/filename through the bus.
- Per-channel media_max_mb cap (default 20) applies to both directions.

Tests: 92 pass (internal/channels/bitrix24 + internal/channels), go vet clean (PG + sqliteonly).

* refactor(bitrix24): migrate messaging/bot-list/unregister to imbot v2 API [B24:2794]

Move outbound REST calls to the imbot v2 family (keeps register on v1):
- imbot.message.add -> imbot.v2.Chat.Message.send (fields.message shape, live-verified)
- imbot.bot.list (+ legacy imbot.list fallback) -> imbot.v2.Bot.list; add botListRows
  to normalize the v2 {bots:[...]} envelope, legacy array, and id-keyed map forms
- imbot.unregister -> imbot.v2.Bot.unregister

Bot registration stays on v1 imbot.register: v2 imbot.v2.Bot.register changes the
event-delivery model (per-event handler URLs -> eventMode), which would require
rewriting the inbound event parser. No user-facing behavior change.

Tests: bitrix24 package green; go vet ./... clean.

* feat(bitrix24): route whisper via v1 SKIP_CONNECTOR + add v2 replyId [B24:2794]

Bot was leaking HiddenMessage (whisper) replies to the external Zalo
connector because every outbound call went through imbot.v2.Chat.Message.send,
which has no equivalent of the v1 SKIP_CONNECTOR flag. Branch the outbound
path on inbound visibility:

  whisper → imbot.message.add + SKIP_CONNECTOR=Y  (v1, send_v1.go)
  public  → imbot.v2.Chat.Message.send + fields.replyId  (v2, send_v2.go)

Pipeline:
  events.go        parse data[PARAMS][PARAMS][COMPONENT_ID]=HiddenMessage
                   into EventParams.IsHiddenMessage (form + JSON variants)
  handle.go        set bitrix_visibility on InboundMessage.Metadata
  consumer         forward visibility + message_id into OutboundMessage
  send.go          resolveSendOptions + sendChunk dispatcher +
                   shared callWithRateLimitRetry helper
  metadata_keys.go single source of truth for the keys + values

Defaults preserve pre-refactor behaviour: callers that don't populate
bitrix_visibility still go through v2 public, and replyId is omitted
unless a numeric bitrix_message_id arrives in metadata.

Tests:
  TestParseEvent_FormURLEncoded_IsHiddenMessage  (3 cases)
  TestParseEvent_JSON_IsHiddenMessage             (3 cases)
  TestResolveSendOptions                          (8 cases)
  TestSend_BranchesOnVisibility                   (4 cases)

* feat(bitrix24): openline sender-tag echo on replies [B24:2794]

Openline sender-tag echo (this change):
- Capture the connector sender tag ("[name #id]:" or "[name] #id:") from
  inbound openline group messages, strip it from the body the agent sees,
  and re-prepend the canonical "[name] #id:" form to the reply so the Open
  Channel connector routes the answer back to the right external user.
- New sender_prefix.go helper (+ test) accepts both inbound layouts and
  emits one canonical form; scoped to messages carrying the tag, so plain
  chats are unaffected.
- metadata_keys.go: MetaKeySenderPrefix; handle.go capture/strip/stash;
  gateway_consumer_normal.go forwards the key; send.go prepends it on the
  first chunk before chunking.

Bundled bitrix24 channel-core work already on this branch:
- handle.go: @mention is the sole trigger for both staff and connector
  customers; unmentioned traffic is dropped (was: drop all connector msgs).
- isGroupMessageType: treat SONET_GROUP "B" as a group.
- handle_test.go, mcp_client_test.go: cover the above.

* feat(bitrix24): accept colon-less openline sender tag, echo [name] #id [B24:2794]

The Open Channel connector dropped the trailing colon from its sender tag:
inbound now arrives as "[Name] #id <msg>" (was "[Name] #id: <msg>"). The
id-bearing patterns required the colon, so the tag fell through to the
name-only branch and the reply echoed "[Name]" — dropping the #id the
connector needs to route the answer back.

- sender_prefix.go: make the trailing ":" optional on both id layouts
  ([name #id] / [name] #id, with or without colon) and echo the canonical
  "[name] #id" (no colon) to match the connector's current format. Bare
  "[name]" (no id) still echoes "[name]" for Open Channel only.
- handle.go: gate the bare name-only layout to Open Channel (isOpenChannel)
  so ordinary group chats starting with "[x] ..." are left untouched.
- sender_prefix_test.go: cover colon/no-colon x id-inside/id-outside, the
  name-only openline case, and the non-openline no-op.

* fix: security and robustness fixes from the bitrix24 channel review [B24:2794]

- download.go: block redirect-based SSRF on inbound media. CheckRedirect
  re-validates each hop (http(s) only, reject private/loopback/link-local
  hosts, cap hops); the initial portal-domain pin is no longer bypassable
  via a 3xx to an internal service. Public-host redirects still allowed.
- handle.go: extract/echo the openline sender tag only for Open Channel
  sessions (was: any group chat), removing bogus prefixes in CRM group
  chats and narrowing the forged-tag misroute surface.
- loop_tools.go + loop_media.go: confine result.Media to the agent / team /
  tenant-allowed roots (new confineToAnyRoot) before a channel uploads it,
  so a prompt-injected out-of-workspace path (e.g. /etc/passwd) cannot
  exfiltrate, while legitimate cross-workspace media (team files, delegatee
  output) still flows.
- send_media.go: bounded outbound read via io.LimitReader replaces the
  os.Stat + os.ReadFile pair, closing the TOCTOU size-cap bypass; cap a
  single message's outbound attachments at 10 (mirrors inbound).
- register.go: paginate imbot.v2.Bot.list (limit/offset + hasNextPage,
  capped at 40 pages) so verify/lookup see bots past the first 50.
- mcp_client.go: redact access_token / refresh_token / client_secret from an
  echoed MCP error body before it is logged or returned (+ test).

* fix(security): validate resolved dial IP on Bitrix media redirects [B24:2794]

The inbound media download redirect guard only string-checked the redirect
hostname (isPrivateOrLoopback on req.URL.Hostname()), so a redirect to a public
hostname that resolves to 127.0.0.1 / 169.254.169.254 / an RFC1918 address — or a
DNS-rebinding swap between check and dial — still passed the guard and the client
would connect. Reported in PR review.

Add security.NewRedirectFollowingSafeClient: it follows redirects but validates
the RESOLVED destination IP of every hop at dial time via net.Dialer.Control,
reusing the existing blocked-CIDR list. The IP it checks is the IP actually
dialed, so both redirect-to-internal and DNS rebinding are refused, while
legitimate public CDN redirects still succeed. download.go now uses it instead of
the hostname-string guard.

Tests: deterministic dial-control table (loopback / link-local / private /
multicast / unspecified / public, v4 + v6), malformed/non-IP addr, test bypass,
loopback-dial-blocked client wiring, and redirect cap + scheme checks.

* feat(bitrix24): per-participant Zalo openline identity from 3-token sender tag [B24:2794]

Parse the connector's "[Name] #uid #msgId" sender tag so each external
customer in a shared Open Channel group gets its own contact + USER.md
instead of collapsing onto the connector proxy id. Identity minting is
gated on IS_CONNECTOR=Y to reject operator forged tags. Echo back the
msgId only ("#msgId") on replies; keep the legacy single-number and
name-only layouts unchanged. Zero DB migration.

- sender_prefix.go: parseOpenlineSenderTag() classifies 3-token / legacy / name-only
- handle.go: synthetic senderID "openlines:{instance}:{chat}:{uid}" + participant_user_id metadata, gated on FromIsConnector
- gateway_consumer_normal.go: deriveGroupUserID() routes participant -> per-person scope, group fallback otherwise
- send.go: buildAddressMention numeric-id guard so synthetic ids don't emit invalid [USER=...] BBCode
- MetaKeyMessageID kept as Bitrix MESSAGE_ID (drives v2 fields.replyId); connector msgId surfaced only via echo prefix

---------

Co-authored-by: DangTinh311 <dangtinh31193@gmail.com>
Co-authored-by: Chinh Dang <chinhdang@192.168.68.104>
2026-06-22 14:23:34 +07:00

535 lines
17 KiB
Go

package bitrix24
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
)
// buildBitrixForm returns a url.Values populated with a realistic
// ONIMBOTMESSAGEADD shape captured from a Bitrix portal. Individual tests
// override specific keys.
func buildBitrixForm() url.Values {
v := url.Values{}
v.Set("event", "ONIMBOTMESSAGEADD")
v.Set("ts", "1713564321")
// auth[*]
v.Set("auth[domain]", "portal.bitrix24.com")
v.Set("auth[application_token]", "APPSECRET")
v.Set("auth[access_token]", "AT")
v.Set("auth[refresh_token]", "RT")
v.Set("auth[member_id]", "mem1")
v.Set("auth[expires_in]", "3600")
// data[PARAMS][*]
v.Set("data[PARAMS][MESSAGE_ID]", "42")
v.Set("data[PARAMS][DIALOG_ID]", "chat1234")
v.Set("data[PARAMS][CHAT_ID]", "1234")
v.Set("data[PARAMS][FROM_USER_ID]", "7")
v.Set("data[PARAMS][TO_USER_ID]", "914")
v.Set("data[PARAMS][MESSAGE]", "hello")
v.Set("data[PARAMS][MESSAGE_TYPE]", "chat")
// data[BOT][914][BOT_ID]
v.Set("data[BOT][914][BOT_ID]", "914")
return v
}
func TestParseEvent_FormURLEncoded_Minimal(t *testing.T) {
v := buildBitrixForm()
req := httptest.NewRequest(http.MethodPost, "/bitrix24/events", strings.NewReader(v.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
evt, err := ParseEvent(req)
if err != nil {
t.Fatalf("ParseEvent: %v", err)
}
if evt.Type != "ONIMBOTMESSAGEADD" {
t.Errorf("Type = %q", evt.Type)
}
if evt.Auth.Domain != "portal.bitrix24.com" {
t.Errorf("Auth.Domain = %q", evt.Auth.Domain)
}
if evt.Auth.AppToken != "APPSECRET" {
t.Errorf("Auth.AppToken = %q", evt.Auth.AppToken)
}
if evt.Auth.MemberID != "mem1" {
t.Errorf("Auth.MemberID = %q", evt.Auth.MemberID)
}
if evt.Auth.ExpiresIn != 3600 {
t.Errorf("Auth.ExpiresIn = %d", evt.Auth.ExpiresIn)
}
if evt.Params.MessageID != "42" {
t.Errorf("Params.MessageID = %q", evt.Params.MessageID)
}
if evt.Params.BotID != 914 {
t.Errorf("Params.BotID = %d", evt.Params.BotID)
}
if evt.Params.DialogID != "chat1234" {
t.Errorf("Params.DialogID = %q", evt.Params.DialogID)
}
if evt.Params.FromUserID != "7" {
t.Errorf("Params.FromUserID = %q", evt.Params.FromUserID)
}
if evt.Params.Message != "hello" {
t.Errorf("Params.Message = %q", evt.Params.Message)
}
if evt.Ts.Unix() != 1713564321 {
t.Errorf("Ts = %v", evt.Ts)
}
if evt.Raw == nil {
t.Errorf("Raw should be set for form inputs")
}
}
func TestParseEvent_FormURLEncoded_WithFiles(t *testing.T) {
v := buildBitrixForm()
// First file (image)
v.Set("data[PARAMS][FILES][0][id]", "f1")
v.Set("data[PARAMS][FILES][0][name]", "cat.png")
v.Set("data[PARAMS][FILES][0][type]", "image")
v.Set("data[PARAMS][FILES][0][urlMachine]", "https://portal.bitrix24.com/disk/downloadFile/1/")
v.Set("data[PARAMS][FILES][0][urlPreview]", "https://portal.bitrix24.com/disk/preview/1/")
v.Set("data[PARAMS][FILES][0][size]", "12345")
v.Set("data[PARAMS][FILES][0][mime]", "image/png")
// Second file (voice)
v.Set("data[PARAMS][FILES][1][name]", "voice.ogg")
v.Set("data[PARAMS][FILES][1][type]", "audio")
v.Set("data[PARAMS][FILES][1][urlMachine]", "https://portal.bitrix24.com/disk/downloadFile/2/")
v.Set("data[PARAMS][FILES][1][size]", "6789")
req := httptest.NewRequest(http.MethodPost, "/bitrix24/events", strings.NewReader(v.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
evt, err := ParseEvent(req)
if err != nil {
t.Fatalf("ParseEvent: %v", err)
}
if len(evt.Params.Files) != 2 {
t.Fatalf("expected 2 files, got %d", len(evt.Params.Files))
}
if evt.Params.Files[0].Name != "cat.png" || evt.Params.Files[0].Type != "image" || evt.Params.Files[0].Size != 12345 {
t.Errorf("file[0] mismatch: %+v", evt.Params.Files[0])
}
if evt.Params.Files[0].URL == "" {
t.Errorf("file[0].URL missing (urlMachine should populate it)")
}
if evt.Params.Files[1].Name != "voice.ogg" || evt.Params.Files[1].Type != "audio" {
t.Errorf("file[1] mismatch: %+v", evt.Params.Files[1])
}
}
// TestParseEvent_FormURLEncoded_WithFiles_IDKeyed covers the live Bitrix24
// webhook shape where FILES is indexed by the file id (e.g. FILES[29968]),
// not by a 0-based array index. Older fixtures use array-style keys; both
// forms must populate evt.Params.Files.
func TestParseEvent_FormURLEncoded_WithFiles_IDKeyed(t *testing.T) {
v := buildBitrixForm()
v.Set("data[PARAMS][FILES][29968][id]", "29968")
v.Set("data[PARAMS][FILES][29968][name]", "photo.jpg")
v.Set("data[PARAMS][FILES][29968][type]", "image")
v.Set("data[PARAMS][FILES][29968][urlDownload]", "https://portal.bitrix24.com/bitrix/services/main/ajax.php?action=disk.api.file.download&fileId=29968")
v.Set("data[PARAMS][FILES][29968][size]", "211220")
req := httptest.NewRequest(http.MethodPost, "/bitrix24/events", strings.NewReader(v.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
evt, err := ParseEvent(req)
if err != nil {
t.Fatalf("ParseEvent: %v", err)
}
if len(evt.Params.Files) != 1 {
t.Fatalf("expected 1 file, got %d", len(evt.Params.Files))
}
f := evt.Params.Files[0]
if f.ID != "29968" || f.Name != "photo.jpg" || f.Type != "image" || f.Size != 211220 {
t.Errorf("file mismatch: %+v", f)
}
if f.URL == "" {
t.Errorf("file.URL missing — urlDownload should populate it when urlMachine/url absent")
}
}
// TestParseEvent_FormURLEncoded_IsConnector covers parsing of data[USER][IS_CONNECTOR]
// for both Y and N values (form-encoded path). Used by the Open Channel gate
// in handle.go to distinguish customers (Y) from internal staff (N).
func TestParseEvent_FormURLEncoded_IsConnector(t *testing.T) {
cases := []struct {
name string
raw string
expected bool
}{
{"Y", "Y", true},
{"y", "y", true},
{"N", "N", false},
{"empty", "", false},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
v := buildBitrixForm()
if tc.raw != "" {
v.Set("data[USER][IS_CONNECTOR]", tc.raw)
}
req := httptest.NewRequest(http.MethodPost, "/bitrix24/events", strings.NewReader(v.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
evt, err := ParseEvent(req)
if err != nil {
t.Fatalf("ParseEvent: %v", err)
}
if evt.Params.FromIsConnector != tc.expected {
t.Errorf("FromIsConnector = %v; want %v", evt.Params.FromIsConnector, tc.expected)
}
})
}
}
// TestParseEvent_JSON_IsConnector covers the JSON path: data.USER.IS_CONNECTOR
// → EventParams.FromIsConnector.
func TestParseEvent_JSON_IsConnector(t *testing.T) {
cases := []struct {
name string
raw string
expected bool
}{
{"Y", `"Y"`, true},
{"N", `"N"`, false},
{"missing", `null`, false},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
body := `{
"event": "ONIMBOTMESSAGEADD",
"auth": {"domain":"x","application_token":"t","access_token":"a","refresh_token":"r","member_id":"m"},
"data": {
"USER": {"IS_CONNECTOR": ` + tc.raw + `},
"PARAMS": {"MESSAGE_ID":"1","DIALOG_ID":"chat1","FROM_USER_ID":"42","MESSAGE":"hi","MESSAGE_TYPE":"L"}
}
}`
req := httptest.NewRequest(http.MethodPost, "/bitrix24/events", strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
evt, err := ParseEvent(req)
if err != nil {
t.Fatalf("ParseEvent: %v", err)
}
if evt.Params.FromIsConnector != tc.expected {
t.Errorf("FromIsConnector = %v; want %v", evt.Params.FromIsConnector, tc.expected)
}
})
}
}
// TestParseEvent_FormURLEncoded_IsHiddenMessage covers parsing of
// data[PARAMS][PARAMS][COMPONENT_ID] → EventParams.IsHiddenMessage. The
// nested PARAMS path is how Bitrix24 marks whisper / internal-only messages
// in Open Channel sessions (do not forward to external connectors).
func TestParseEvent_FormURLEncoded_IsHiddenMessage(t *testing.T) {
cases := []struct {
name string
value string // value for data[PARAMS][PARAMS][COMPONENT_ID]; empty = field absent
expected bool
}{
{"hidden_message", "HiddenMessage", true},
{"other_component", "ChatJoin", false},
{"absent", "", false},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
v := buildBitrixForm()
if tc.value != "" {
v.Set("data[PARAMS][PARAMS][COMPONENT_ID]", tc.value)
}
req := httptest.NewRequest(http.MethodPost, "/bitrix24/events", strings.NewReader(v.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
evt, err := ParseEvent(req)
if err != nil {
t.Fatalf("ParseEvent: %v", err)
}
if evt.Params.IsHiddenMessage != tc.expected {
t.Errorf("IsHiddenMessage = %v; want %v", evt.Params.IsHiddenMessage, tc.expected)
}
})
}
}
// TestParseEvent_JSON_IsHiddenMessage covers the JSON path: data.PARAMS.PARAMS.
// COMPONENT_ID → EventParams.IsHiddenMessage.
func TestParseEvent_JSON_IsHiddenMessage(t *testing.T) {
cases := []struct {
name string
nested string // JSON snippet for inner PARAMS; empty = field absent
expected bool
}{
{"hidden_message", `,"PARAMS":{"COMPONENT_ID":"HiddenMessage"}`, true},
{"other_component", `,"PARAMS":{"COMPONENT_ID":"ChatJoin"}`, false},
{"absent", ``, false},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
body := `{
"event": "ONIMBOTMESSAGEADD",
"auth": {"domain":"x","application_token":"t","access_token":"a","refresh_token":"r","member_id":"m"},
"data": {
"PARAMS": {"MESSAGE_ID":"1","DIALOG_ID":"chat1","FROM_USER_ID":"42","MESSAGE":"hi","MESSAGE_TYPE":"L"` + tc.nested + `}
}
}`
req := httptest.NewRequest(http.MethodPost, "/bitrix24/events", strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
evt, err := ParseEvent(req)
if err != nil {
t.Fatalf("ParseEvent: %v", err)
}
if evt.Params.IsHiddenMessage != tc.expected {
t.Errorf("IsHiddenMessage = %v; want %v", evt.Params.IsHiddenMessage, tc.expected)
}
})
}
}
func TestParseEvent_SystemFlag(t *testing.T) {
v := buildBitrixForm()
v.Set("data[PARAMS][SYSTEM]", "Y")
req := httptest.NewRequest(http.MethodPost, "/bitrix24/events", strings.NewReader(v.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
evt, err := ParseEvent(req)
if err != nil {
t.Fatalf("ParseEvent: %v", err)
}
if !evt.Params.SystemMessage {
t.Errorf("SystemMessage expected true")
}
}
func TestParseEvent_MissingEventType(t *testing.T) {
v := url.Values{}
v.Set("auth[domain]", "x.bitrix24.com")
req := httptest.NewRequest(http.MethodPost, "/bitrix24/events", strings.NewReader(v.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
_, err := ParseEvent(req)
if err == nil {
t.Fatal("expected error on missing event type")
}
if !strings.Contains(err.Error(), "missing event type") {
t.Errorf("unexpected error: %v", err)
}
}
func TestParseEvent_EmptyBody(t *testing.T) {
req := httptest.NewRequest(http.MethodPost, "/bitrix24/events", strings.NewReader(""))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
_, err := ParseEvent(req)
if err == nil {
t.Fatal("expected error on empty body")
}
}
func TestParseEvent_JSON(t *testing.T) {
payload := map[string]any{
"event": "ONIMBOTMESSAGEADD",
"ts": 1713564321,
"auth": map[string]any{
"domain": "portal.bitrix24.com",
"application_token": "APPSECRET",
"member_id": "mem1",
"expires_in": 3600,
},
"data": map[string]any{
"BOT": map[string]any{
"914": map[string]any{"BOT_ID": 914},
},
"PARAMS": map[string]any{
"MESSAGE_ID": 42,
"DIALOG_ID": "chat1234",
"FROM_USER_ID": 7,
"MESSAGE": "json hello",
"MESSAGE_TYPE": "chat",
"SYSTEM": "N",
"FILES": []map[string]any{
{
"id": "f1",
"name": "doc.pdf",
"type": "file",
"urlMachine": "https://portal.bitrix24.com/disk/downloadFile/5/",
"size": 999,
},
},
},
},
}
body, _ := json.Marshal(payload)
req := httptest.NewRequest(http.MethodPost, "/bitrix24/events", bytes.NewReader(body))
req.Header.Set("Content-Type", "application/json")
evt, err := ParseEvent(req)
if err != nil {
t.Fatalf("ParseEvent: %v", err)
}
if evt.Type != "ONIMBOTMESSAGEADD" {
t.Errorf("Type = %q", evt.Type)
}
if evt.Auth.Domain != "portal.bitrix24.com" || evt.Auth.AppToken != "APPSECRET" {
t.Errorf("Auth = %+v", evt.Auth)
}
if evt.Auth.ExpiresIn != 3600 {
t.Errorf("ExpiresIn = %d", evt.Auth.ExpiresIn)
}
if evt.Params.MessageID != "42" || evt.Params.BotID != 914 {
t.Errorf("Params = %+v", evt.Params)
}
if evt.Params.FromUserID != "7" {
t.Errorf("FromUserID = %q", evt.Params.FromUserID)
}
if evt.Params.Message != "json hello" {
t.Errorf("Message = %q", evt.Params.Message)
}
if evt.Params.SystemMessage {
t.Errorf("SystemMessage should be false when SYSTEM=N")
}
if len(evt.Params.Files) != 1 || evt.Params.Files[0].Size != 999 {
t.Errorf("Files = %+v", evt.Params.Files)
}
if evt.Ts.Unix() != 1713564321 {
t.Errorf("Ts = %v", evt.Ts)
}
}
func TestParseEvent_JSON_MissingEvent(t *testing.T) {
body := []byte(`{"auth":{"domain":"x"}}`)
req := httptest.NewRequest(http.MethodPost, "/bitrix24/events", bytes.NewReader(body))
req.Header.Set("Content-Type", "application/json")
_, err := ParseEvent(req)
if err == nil {
t.Fatal("expected error on missing event type (json)")
}
}
func TestParseEvent_NilRequest(t *testing.T) {
if _, err := ParseEvent(nil); err == nil {
t.Fatal("expected error on nil request")
}
}
// TestParseEvent_Form_ChatEntity verifies CHAT_ENTITY_TYPE + CHAT_ENTITY_ID
// surface on EventParams for both CRM-bound and Tasks-bound chats. These
// fields drive MCP "this deal/task" resolution downstream — without parsing
// them the agent has no deterministic way to know which entity the chat
// belongs to. Fixtures match real Bitrix24 webhooks captured against
// tamgiac.bitrix24.com (see plans/.../reports/event-payloads/05 + 07).
func TestParseEvent_Form_ChatEntity(t *testing.T) {
cases := []struct {
name string
entityType string
entityID string
messageType string
}{
{name: "crm_deal", entityType: "CRM", entityID: "DEAL|2064", messageType: "C"},
{name: "crm_lead", entityType: "CRM", entityID: "LEAD|7", messageType: "C"},
{name: "tasks_task", entityType: "TASKS_TASK", entityID: "2704", messageType: "X"},
{name: "plain_group_no_entity", entityType: "", entityID: "", messageType: "C"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
v := buildBitrixForm()
v.Set("data[PARAMS][MESSAGE_TYPE]", tc.messageType)
if tc.entityType != "" {
v.Set("data[PARAMS][CHAT_ENTITY_TYPE]", tc.entityType)
}
if tc.entityID != "" {
v.Set("data[PARAMS][CHAT_ENTITY_ID]", tc.entityID)
}
req := httptest.NewRequest(http.MethodPost, "/bitrix24/events", strings.NewReader(v.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
evt, err := ParseEvent(req)
if err != nil {
t.Fatalf("ParseEvent: %v", err)
}
if evt.Params.ChatEntityType != tc.entityType {
t.Errorf("ChatEntityType = %q; want %q", evt.Params.ChatEntityType, tc.entityType)
}
if evt.Params.ChatEntityID != tc.entityID {
t.Errorf("ChatEntityID = %q; want %q", evt.Params.ChatEntityID, tc.entityID)
}
})
}
}
// TestParseEvent_JSON_ChatEntity is the JSON-payload counterpart. Bitrix24
// rarely sends JSON in production but the parser accepts it, so we keep the
// two paths in lockstep.
func TestParseEvent_JSON_ChatEntity(t *testing.T) {
payload := map[string]any{
"event": "ONIMBOTMESSAGEADD",
"auth": map[string]any{"domain": "portal.bitrix24.com", "application_token": "X"},
"data": map[string]any{
"BOT": map[string]any{"914": map[string]any{"BOT_ID": 914}},
"PARAMS": map[string]any{
"MESSAGE_TYPE": "X",
"CHAT_ENTITY_TYPE": "TASKS_TASK",
"CHAT_ENTITY_ID": "2704",
},
},
}
body, _ := json.Marshal(payload)
req := httptest.NewRequest(http.MethodPost, "/bitrix24/events", bytes.NewReader(body))
req.Header.Set("Content-Type", "application/json")
evt, err := ParseEvent(req)
if err != nil {
t.Fatalf("ParseEvent: %v", err)
}
if evt.Params.ChatEntityType != "TASKS_TASK" {
t.Errorf("ChatEntityType = %q", evt.Params.ChatEntityType)
}
if evt.Params.ChatEntityID != "2704" {
t.Errorf("ChatEntityID = %q", evt.Params.ChatEntityID)
}
}
func TestFormGet(t *testing.T) {
v := url.Values{}
v.Set("a[b][c]", "deep")
v.Set("simple", "flat")
if got := formGet(v, "simple"); got != "flat" {
t.Errorf("flat: %q", got)
}
if got := formGet(v, "a", "b", "c"); got != "deep" {
t.Errorf("nested: %q", got)
}
if got := formGet(v, "absent"); got != "" {
t.Errorf("absent: %q (want empty)", got)
}
}
func TestAsStringAndAsInt(t *testing.T) {
cases := []struct {
in any
asStr string
asInt int
}{
{nil, "", 0},
{"abc", "abc", 0},
{"42", "42", 42},
{42, "42", 42},
{int64(99), "99", 99},
{float64(12.0), "12", 12},
{float64(3.14), "3.14", 3},
{true, "Y", 0},
{false, "N", 0},
{json.Number("7"), "7", 7},
}
for _, c := range cases {
if got := asString(c.in); got != c.asStr {
t.Errorf("asString(%v) = %q, want %q", c.in, got, c.asStr)
}
if got := asInt(c.in); got != c.asInt {
t.Errorf("asInt(%v) = %d, want %d", c.in, got, c.asInt)
}
}
}