Skip to content

fix(markups): render markdown images in messages - #1447

Open
anikethinge wants to merge 2 commits into
RocketChat:developfrom
anikethinge:fix/1432-markdown-image
Open

anikethinge wants to merge 2 commits into
RocketChat:developfrom
anikethinge:fix/1432-markdown-image

Conversation

@anikethinge

Copy link
Copy Markdown

What?

Adds an ImageElement component in @embeddedchat/markups and handles IMAGE nodes in InlineElements.

Why?

Fixes #1432. Previously, @rocket.chat/message-parser IMAGE nodes (![alt](url) and ![](url)) fell through to default: return null in InlineElements.js, causing markdown images to disappear.

How?

  • Added packages/markups/src/elements/ImageElement.js:
    • Sanitizes src URLs to allow only http:, https:, or single-slash relative paths (blocking javascript:, data:, and protocol-relative // URLs).
    • Extracts alt text safely for string, object, array, or empty labels.
    • Renders the <img> inside an <a> tag with rel="noopener noreferrer" and target="_blank".
  • Wired case 'IMAGE': in packages/markups/src/elements/InlineElements.js.

Verification

  • npm run lint — passed
  • npm run format:check — passed
  • npm run build — passed

@swarnendusahu10-dotcom

Copy link
Copy Markdown
Contributor

Hey @anikethinge! 👋 Great initiative adding markdown IMAGE rendering support to @embeddedchat/markups.

I pulled your branch and tested the implementation locally, and noticed a couple of edge cases worth checking out:

  1. Security / Relative URL Bypass (/\): In sanitizeUrl, the check trimmed.startsWith('/') && !trimmed.startsWith('//') allows paths starting with /\. In modern browser WHATWG URL resolution, /\evil.com resolves directly to https://evil.com/, which can lead to an open redirect when the image link is clicked. Adding a check against /\ would seal this bypass.
  2. UI Container Height Overflow: In ImageElement.js, using height: 'auto' without a maxHeight limit means very tall vertical images (like infographics or long screenshots) will expand freely and blow out the chat message view. Adding a reasonable maxHeight (e.g., 300px or 350px) with objectFit: 'contain' will keep the chat layout clean across all screen sizes.
  3. Fallback UX on Invalid URLs: Currently, if sanitizeUrl fails, ImageElement returns null, causing that entire section of the message to disappear into thin air. It might be friendlier to render the alt text / label as fallback plain text so the user's text isn't lost.

Hope this helps! Let me know what you think. 🚀

@anikethinge

Copy link
Copy Markdown
Author

Thanks for the thorough review @swarnendusahu10-dotcom — good catches! Updated ImageElement.js in commit 8d54809a to address all three:

  1. Relative URL bypass: Blocked /\ and added origin verification against a placeholder base URL so WHATWG backslash and control-character redirects (e.g., /\evil.com, /\t/evil.com) are rejected.
  2. Height overflow: Added maxHeight: 300px and objectFit: contain to keep tall images bounded in the message view.
  3. Invalid URL fallback: Render the extracted alt text as plain text when available instead of returning null.

@swarnendusahu10-dotcom

Copy link
Copy Markdown
Contributor

@anikethinge welcome ;)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: markdown images ![alt](url) are not rendered in messages

2 participants