element-web icon indicating copy to clipboard operation
element-web copied to clipboard

Bug Report: `/addwidget` command fails with 400 Bad Request due to unencoded `!` in room ID

Open Marcel-Bich opened this issue 11 months ago • 1 comments

Steps to reproduce

Bug Report: /addwidget command fails with 400 Bad Request due to unencoded ! in room ID

Summary

The /addwidget slash command in Element Web fails with 400 Bad Request when the homeserver is behind Traefik reverse proxy. The root cause is that Element Web does not URL-encode the ! character in room IDs when constructing the PUT request URL.

Environment

  • Element Web: app.element.io (also reproducible with self-hosted Element Web)
  • Homeserver: Synapse v1.144.0 (self-hosted)
  • Reverse Proxy: Traefik v2.11
  • Browser: Chrome 143 (Windows 10)
  • Matrix Room ID format: !roomid:domain

Steps to Reproduce

  1. Log into Element Web (app.element.io or self-hosted)
  2. Connect to a self-hosted Synapse homeserver behind Traefik
  3. Open any room
  4. Type /addwidget https://www.wikipedia.org
  5. Press Enter

Expected Behavior

Widget should be created successfully via PUT request to:

/_matrix/client/v3/rooms/%21roomid%3Adomain/state/im.vector.modular.widgets/widget_id

Note: ! should be encoded as %21 and : as %3A

Actual Behavior

Request fails with 400 Bad Request. Browser console shows:

Server-Fehler
fetch failed: Failed to fetch

Network tab shows OPTIONS preflight request returning 400 Bad Request with empty body.

Root Cause Analysis

The Problem

Element Web constructs the request URL with an unencoded ! character:

PUT /_matrix/client/v3/rooms/!JpcHTNepkvDnJjgyCw:matrix.example.com/state/im.vector.modular.widgets/...

Instead of the properly encoded version:

PUT /_matrix/client/v3/rooms/%21JpcHTNepkvDnJjgyCw%3Amatrix.example.com/state/im.vector.modular.widgets/...

Why It Fails

Traefik's underlying Go HTTP server strictly validates URL paths. While RFC 3986 technically allows ! as a sub-delimiter without encoding, Go's net/http package rejects it, returning 400 Bad Request before the request even reaches Synapse.

Proof of Concept

Failing request (unencoded !):

curl -X OPTIONS \
  -H "Origin: https://app.element.io" \
  -H "Access-Control-Request-Method: PUT" \
  'https://matrix.example.com/_matrix/client/v3/rooms/!test:matrix.example.com/state/im.vector.modular.widgets/test'

# Returns: HTTP/2 400

Working request (encoded %21):

curl -X OPTIONS \
  -H "Origin: https://app.element.io" \
  -H "Access-Control-Request-Method: PUT" \
  'https://matrix.example.com/_matrix/client/v3/rooms/%21test%3Amatrix.example.com/state/im.vector.modular.widgets/test'

# Returns: HTTP/2 200 with CORS headers

Successful widget creation with proper encoding:

curl -X PUT \
  -H "Authorization: Bearer <token>" \
  -H "Content-Type: application/json" \
  -d '{"type":"m.custom","url":"https://www.wikipedia.org","name":"Test","data":{}}' \
  'https://matrix.example.com/_matrix/client/v3/rooms/%21roomid%3Amatrix.example.com/state/im.vector.modular.widgets/test_widget'

# Returns: HTTP/2 200 {"event_id":"$..."}

Direct Synapse Test (bypassing Traefik)

Synapse itself handles unencoded ! correctly:

docker exec synapse curl -s -o /dev/null -w "%{http_code}" \
  'http://localhost:8008/_matrix/client/v3/rooms/!test:matrix.example.com/state'

# Returns: 401 (needs auth, but URL parsing succeeded)

This confirms the issue is in the Traefik/Go HTTP layer, but Element Web should encode the URL properly for maximum compatibility.

Browser Network Export

OPTIONS Preflight Request (from Chrome DevTools):

Request URL: https://matrix.example.com/_matrix/client/v3/rooms/!roomid%3Amatrix.example.com/state/im.vector.modular.widgets/...
Request Method: OPTIONS
Status Code: 400 Bad Request

Request Headers:
  access-control-request-headers: authorization,content-type
  access-control-request-method: PUT
  origin: https://app.element.io
  sec-fetch-mode: cors
  sec-fetch-site: cross-site

Response Headers:
  content-length: 0
  date: Wed, 17 Dec 2025 15:42:09 GMT

Note: The : IS encoded as %3A, but ! is NOT encoded.

Affected Code Location (Probable)

The issue likely originates in the room ID handling when constructing Matrix API URLs. The ! character in room IDs should be percent-encoded when building request URLs.

Possibly in:

  • matrix-js-sdk URL construction
  • Element Web's /addwidget command handler
  • Any code path that builds URLs containing room IDs

Impact

  • Self-hosted Matrix deployments using Traefik (very common in Docker setups) cannot use /addwidget
  • Potentially affects other commands/features that construct URLs with room IDs
  • Users must use workarounds (direct API calls with proper encoding)

Suggested Fix

Ensure all room IDs are properly URL-encoded when constructing request URLs:

// Instead of:
const url = `/_matrix/client/v3/rooms/${roomId}/state/...`;

// Use:
const url = `/_matrix/client/v3/rooms/${encodeURIComponent(roomId)}/state/...`;

Or ensure the HTTP client library properly encodes path segments.

Workaround

Users can add widgets via direct API calls with proper URL encoding:

curl -X PUT \
  -H "Authorization: Bearer <access_token>" \
  -H "Content-Type: application/json" \
  -d '{"type":"m.custom","url":"https://example.com","name":"Widget Name","data":{}}' \
  'https://homeserver/_matrix/client/v3/rooms/%21ROOM_ID%3Ahomeserver/state/im.vector.modular.widgets/unique_widget_id'

Related Issues

  • This may affect other Matrix clients or SDKs that don't properly encode room IDs
  • Similar issues have been reported with Traefik and special characters: https://github.com/traefik/traefik/issues/10806

Additional Context

  • Normal Element Web operations (sync, messaging, room joins) work correctly, suggesting those code paths DO encode URLs properly
  • The issue is specific to the /addwidget command (and possibly other slash commands)
  • Tested with both app.element.io and would affect self-hosted Element Web identically

Outcome

Image Image

Operating system

No response

Browser information

No response

URL for webapp

No response

Application version

No response

Homeserver

No response

Will you send logs?

No

Marcel-Bich avatar Dec 17 '25 16:12 Marcel-Bich

This is a bug due to Traefik, they recently tweaked their defaults and make it incompatible with the Matrix API. They also block encoded forward slash / which is required in some crypto APIs. Nothing in the Matrix spec says the ! must be url encoded. In fact the spec says the opposite.

Image

Element Web is not a Traefik client, so if you want such a change as you propose it'd need to be changed in the Matrix spec first.

t3chguy avatar Dec 18 '25 08:12 t3chguy