RocketChat / RocketChat/Rocket.Chat

/api/v1/chat.postMessage: bad request

Open
#31,286 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
46.1k
Forks
13.9k
Avg merge
3d 3h
Merged PRs (30d)
130

Description

Description:

We send messages to rocket.chat using api via a bash script that acts in a way like:

curl \
        -s -q \
        --connect-timeout 10 --max-time 20 \
        -H "X-Auth-Token: XXX" \
        -H "X-User-Id: XXX" \
        -H "Content-type: application/json" \
        "https://ROCKET_CHAT_SERVER/api/v1/chat.postMessage" \
        -d '{"channel":"'"${CHANNEL}"'","alias":"'"${NICKNAME}"'","avatar":"'"${ICON_URL}"'","text":"'"${TEXT}"'"}'

So, it is enough to do:

export NICKNAME="myname"
export TEXT="simple text"
export ICON_URL="https://i.imgur.com/Hg2VK4Z.png"
./script

In the recent past we found in the rocket.chat http logs something like:

$IP - - [19/Dec/2023:23:58:57 +0100] "POST /api/v1/chat.postMessage HTTP/1.1" 400 839 "-" "curl/7.29.0" "-"

So after a bit of investigation I discovered that when TEXT comes from a shell command, and there are spaces, it does not work; example:

export TEXT="a b" -> works

mkdir -p /tmp/dir ; cd /tmp/dir ; touch a ; export TEXT="$( ls )" -> works
mkdir -p /tmp/dir ; cd /tmp/dir ; touch a b ; export TEXT="$( ls )" -> DOES NOT WORK

NOTE: we keep logs for one month, so I'm not able to say in which version the regression happened, but this is not just the 6.5.0 update since it was released on Dec 1, while I have records of this issue also in November.
In the past, that way of sending messages always worked for us, no matter of the TEXT content.

Server Setup Information:
  • Version of Rocket.Chat Server: 6.5.0
  • Operating System: Gentoo
  • Deployment Method: tar
  • Number of Running Instances: 1
  • DB Replicaset Oplog:
  • NodeJS Version: 14
  • MongoDB Version: 5.0

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the curl invocation for /api/v1/chat.postMessage and reproduce the failure using TEXT populated by $(ls) when filenames contain spaces. Compare the successful and failing JSON requests and inspect the chat.postMessage API entry point. Done means the reproduced request is handled correctly and a regression test covers this TEXT content.

Written by the indexing model from the issue text.

Assessment

Tech stack
bash, typescript
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.