discord / discord-api-docs

Official Discord API Documentation
https://discord.com/developers/docs/intro
Other
5.95k stars 1.26k forks source link

Create Message with malformed data returns 500 Internal Server Error #4776

Closed splatterxl closed 2 years ago

splatterxl commented 2 years ago

Description

When calling Create Message (POST /channels/:channel_id/messages) with malformed data from incorrect encoding of attachments, the server returns 500 Internal Server Error instead of a 400 Bad Request response with the corresponding error.

Steps to Reproduce

----------------------------040047503040194731759086
Content-Disposition: form-data; name="files[0]"; filename="ping.json"
Content-Type: image/png

{ "content": "pong" }
----------------------------040047503040194731759086
Content-Disposition: form-data; name="payload_json"
Content-Type: application/json

{"content":"pong","flags":0,"tts":false,"nonce":1649794889919,"embeds":null,"attachments":[{"options":{"data":"{ \"content\": \"pong\" }","name":"ping.json","description":"pong"},"name":"ping.json","data":{"type":"Buffer","data":[123,32,34,99,111,110,116,101,110,116,34,58,32,34,112,111,110,103,34,32,125]},"contentType":"image/png","description":"pong"},{"id":"0","filename":"ping.json","description":"pong"},{"id":"0","filename":"ping.json","description":"pong"}]}
----------------------------040047503040194731759086--

Expected Behavior

The server would respond with a 400 Bad Request error informing me that my data is horribly wrong.

Current Behavior

The server responds with 500 Internal Server Error.

Screenshots/Videos

No response

Client and System Information

night commented 2 years ago

can you provide a curl sample we can test with? I am not able to reproduce any issues locally. If the issue is happening upstream (like in the HTTP layer) it is not likely we can return any error but a 500.

splatterxl commented 2 years ago

Sure! cURL reproduction script:

curl -H "Authorization: Bot $DISCORD_TOKEN" -i \
-XPOST -F "files[0]=@data.json" -F "payload_json=$(cat data.txt)" \
https://discord.com/api/v10/channels/$CHANNEL_ID/messages

data.txt:

{"content":"pong","flags":0,"tts":false,"nonce":1649794889919,"embeds":null,"attachments":[{"options":{"data":"{ \"content\": \"pong\" }","name":"ping.json","description":"pong"},"name":"ping.json","data":{"type":"Buffer","data":[123,32,34,99,111,110,116,101,110,116,34,58,32,34,112,111,110,103,34,32,125]},"contentType":"image/png","description":"pong"},{"id":"0","filename":"ping.json","description":"pong"},{"id":"0","filename":"ping.json","description":"pong"}]}

data.json is just:

{
  "content": "pong"
}

and this all returns

{"message": "500: Internal Server Error", "code": 0}
night commented 2 years ago

Thanks for the additional information. This will be fixed in the next API deploy.