Skip to content

fix: add sanitizeUrl to ImageElement for defense in depth - #40613

Merged
julio-rocketchat merged 3 commits into
developfrom
add-sanitizeurl-to-imageelement
May 19, 2026
Merged

fix: add sanitizeUrl to ImageElement for defense in depth#40613
julio-rocketchat merged 3 commits into
developfrom
add-sanitizeurl-to-imageelement

Conversation

@julio-rocketchat

@julio-rocketchat julio-rocketchat commented May 19, 2026

Copy link
Copy Markdown
Member

Proposed changes (including videos or screenshots)

Adding sanitizeUrl to ImageElement as part of our defense in depth strategy. This is a low severity issue/low priority improvement, not an exploitable vulnerability.

Issue(s)

https://rocketchat.atlassian.net/browse/VLN-387

Steps to test or reproduce

N/A

Further comments

N/A

Summary by CodeRabbit

  • Bug Fixes

    • Enhanced security by sanitizing image element URLs in markdown content to prevent XSS attacks. Dangerous URL schemes including javascript:, data:, and vbscript: are now blocked and safely replaced, aligning with existing link protection mechanisms.
  • Tests

    • Added comprehensive test coverage for image URL sanitization, verifying both unsafe and safe URLs are handled correctly.

Review Change Stack

@dionisio-bot

dionisio-bot Bot commented May 19, 2026

Copy link
Copy Markdown
Contributor

Looks like this PR is ready to merge! 🎉
If you have any trouble, please check the PR guidelines

@changeset-bot

changeset-bot Bot commented May 19, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: b2e2825

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 42 packages
Name Type
@rocket.chat/gazzodown Patch
@rocket.chat/meteor Patch
@rocket.chat/fuselage-ui-kit Patch
@rocket.chat/livechat Patch
@rocket.chat/uikit-playground Patch
@rocket.chat/ui-voip Patch
@rocket.chat/core-typings Patch
@rocket.chat/rest-typings Patch
@rocket.chat/api-client Patch
@rocket.chat/apps Patch
@rocket.chat/core-services Patch
@rocket.chat/cron Patch
@rocket.chat/ddp-client Patch
@rocket.chat/http-router Patch
@rocket.chat/model-typings Patch
@rocket.chat/models Patch
@rocket.chat/ui-avatar Patch
@rocket.chat/ui-client Patch
@rocket.chat/ui-contexts Patch
@rocket.chat/web-ui-registration Patch
@rocket.chat/account-service Patch
@rocket.chat/authorization-service Patch
@rocket.chat/ddp-streamer Patch
@rocket.chat/omnichannel-transcript Patch
@rocket.chat/presence-service Patch
@rocket.chat/queue-worker Patch
@rocket.chat/abac Patch
@rocket.chat/federation-matrix Patch
@rocket.chat/license Patch
@rocket.chat/media-calls Patch
@rocket.chat/omnichannel-services Patch
@rocket.chat/pdf-worker Patch
@rocket.chat/presence Patch
rocketchat-services Patch
@rocket.chat/network-broker Patch
@rocket.chat/omni-core-ee Patch
@rocket.chat/mock-providers Patch
@rocket.chat/instance-status Patch
@rocket.chat/omni-core Patch
@rocket.chat/server-fetch Patch
@rocket.chat/ui-video-conf Patch
@rocket.chat/ui-composer Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai

coderabbitai Bot commented May 19, 2026

Copy link
Copy Markdown
Contributor

Walkthrough

This PR adds XSS prevention to image rendering by sanitizing unsafe URL schemes in ImageElement. The component now blocks javascript:, data:, and vbscript: URLs before using them in href and img src attributes, aligning with existing link sanitization. Tests verify both dangerous and safe URLs are handled correctly.

Changes

Image URL Sanitization

Layer / File(s) Summary
ImageElement URL sanitization
packages/gazzodown/src/elements/ImageElement.tsx
ImageElement imports sanitizeUrl and applies it to the src prop before rendering in href, img src, and data-title attributes.
Sanitization test coverage
packages/gazzodown/src/Markup.spec.tsx
Test suite validates that unsafe image src schemes (javascript:, data:, vbscript:) are rewritten to "#" in both link and image, and safe https:// URLs remain unchanged.
Release metadata
.changeset/sanitize-image-element-url.md
Changeset documents patch version bumps for @rocket.chat/gazzodown and @rocket.chat/meteor with description of image URL sanitization for XSS prevention.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested labels

type: bug

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: adding sanitizeUrl to ImageElement as a defense-in-depth security measure, which aligns with the changeset and code modifications.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

Warning

Review ran into problems

🔥 Problems

Errors were encountered while retrieving linked issues.

Errors (1)
  • VLN-387: Request failed with status code 401

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
packages/gazzodown/src/Markup.spec.tsx (1)

372-374: ⚡ Quick win

Assert data-title sanitization/preservation too.

ImageElement now uses sanitized URLs in data-title as well; these tests should assert that sink to fully cover the change.

Proposed test additions
 			expect(screen.getByRole('link')).toHaveAttribute('href', '#');
 			expect(screen.getByRole('img')).toHaveAttribute('src', '#');
+			expect(screen.getByRole('img')).toHaveAttribute('data-title', '#');
 		},
 	);

@@
 		expect(screen.getByRole('link')).toHaveAttribute('href', 'https://rocket.chat/logo.svg');
 		expect(screen.getByRole('img')).toHaveAttribute('src', 'https://rocket.chat/logo.svg');
+		expect(screen.getByRole('img')).toHaveAttribute('data-title', 'https://rocket.chat/logo.svg');
 	});
 });

Also applies to: 397-398

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/gazzodown/src/Markup.spec.tsx` around lines 372 - 374, Update the
test assertions to verify that the sanitized URL is preserved in the data-title
attribute for both the link and image elements: after the existing expects
(expect(screen.getByRole('link')).toHaveAttribute('href', '#') and
expect(screen.getByRole('img')).toHaveAttribute('src', '#')), add assertions
that the link element (found via screen.getByRole('link')) has the expected
sanitized value in its data-title attribute and the img element (found via
screen.getByRole('img')) has the same sanitized value in its data-title; apply
the same additional assertions to the second occurrence around lines where the
other pair of expects (the ones at 397-398) are present so both test cases cover
ImageElement data-title sanitization/preservation.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@packages/gazzodown/src/Markup.spec.tsx`:
- Around line 372-374: Update the test assertions to verify that the sanitized
URL is preserved in the data-title attribute for both the link and image
elements: after the existing expects
(expect(screen.getByRole('link')).toHaveAttribute('href', '#') and
expect(screen.getByRole('img')).toHaveAttribute('src', '#')), add assertions
that the link element (found via screen.getByRole('link')) has the expected
sanitized value in its data-title attribute and the img element (found via
screen.getByRole('img')) has the same sanitized value in its data-title; apply
the same additional assertions to the second occurrence around lines where the
other pair of expects (the ones at 397-398) are present so both test cases cover
ImageElement data-title sanitization/preservation.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 29312274-946d-4f66-9109-053f6fe0837c

📥 Commits

Reviewing files that changed from the base of the PR and between 78814a8 and 2cf7fd6.

📒 Files selected for processing (3)
  • .changeset/sanitize-image-element-url.md
  • packages/gazzodown/src/Markup.spec.tsx
  • packages/gazzodown/src/elements/ImageElement.tsx
📜 Review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: cubic · AI code reviewer
  • GitHub Check: CodeQL-Build
🧰 Additional context used
📓 Path-based instructions (1)
**/*.{ts,tsx,js}

📄 CodeRabbit inference engine (.cursor/rules/playwright.mdc)

**/*.{ts,tsx,js}: Write concise, technical TypeScript/JavaScript with accurate typing in Playwright tests
Avoid code comments in the implementation

Files:

  • packages/gazzodown/src/elements/ImageElement.tsx
  • packages/gazzodown/src/Markup.spec.tsx
🧠 Learnings (5)
📚 Learning: 2026-03-16T21:50:37.589Z
Learnt from: amitb0ra
Repo: RocketChat/Rocket.Chat PR: 39676
File: .changeset/migrate-users-register-openapi.md:3-3
Timestamp: 2026-03-16T21:50:37.589Z
Learning: For changes related to OpenAPI migrations in Rocket.Chat/OpenAPI, when removing endpoint types and validators from rocket.chat/rest-typings (e.g., UserRegisterParamsPOST, /v1/users.register) document this as a minor changeset (not breaking) per RocketChat/Rocket.Chat-Open-API#150 Rule 7. Note that the endpoint type is re-exposed via a module augmentation .d.ts in the consuming package (e.g., packages/web-ui-registration/src/users-register.d.ts). In reviews, ensure the changeset clearly states: this is a non-breaking change, the major version should not be bumped, and the changeset reflects a minor version bump. Do not treat this as a breaking change during OpenAPI migrations.

Applied to files:

  • .changeset/sanitize-image-element-url.md
📚 Learning: 2026-03-27T14:52:56.865Z
Learnt from: dougfabris
Repo: RocketChat/Rocket.Chat PR: 39892
File: apps/meteor/client/views/room/contextualBar/Threads/Thread.tsx:150-155
Timestamp: 2026-03-27T14:52:56.865Z
Learning: In Rocket.Chat, there are two different `ModalBackdrop` components with different prop APIs. During review, confirm the import source: (1) `rocket.chat/fuselage` `ModalBackdrop` uses `ModalBackdropProps` based on `BoxProps` (so it supports `onClick` and other Box/DOM props) and does not have an `onDismiss` prop; (2) `rocket.chat/ui-client` `ModalBackdrop` uses a narrower props interface like `{ children?: ReactNode; onDismiss?: () => void }` and handles Escape keypress and outside mouse-up, and it does not forward arbitrary DOM props such as `onClick`. Flag mismatched props (e.g., `onDismiss` passed to the fuselage component or `onClick` passed to the ui-client component) and ensure the usage matches the correct component being imported.

Applied to files:

  • packages/gazzodown/src/elements/ImageElement.tsx
  • packages/gazzodown/src/Markup.spec.tsx
📚 Learning: 2026-05-06T12:21:44.083Z
Learnt from: juliajforesti
Repo: RocketChat/Rocket.Chat PR: 40256
File: apps/meteor/client/components/CreateDiscussion/CreateDiscussion.tsx:121-149
Timestamp: 2026-05-06T12:21:44.083Z
Learning: Field wrappers in rocket.chat/fuselage-forms (Field, FieldLabel, FieldRow, FieldError, FieldHint) auto-create htmlFor/id associations, aria-describedby, and role="alert" for errors. Do not manually set htmlFor, id, aria-describedby, or role attributes when using these wrappers. This automatic wiring does not apply to plain rocket.chat/fuselage components, which require explicit ID wiring per the accessibility docs. In code reviews, prefer using fuselage-forms wrappers for form fields and verify there is no unnecessary manual ID/aria wiring in files that use these wrappers. If a component uses plain fuselage components, ensure proper id wiring as per docs.

Applied to files:

  • packages/gazzodown/src/elements/ImageElement.tsx
  • packages/gazzodown/src/Markup.spec.tsx
📚 Learning: 2026-03-06T18:02:11.732Z
Learnt from: tassoevan
Repo: RocketChat/Rocket.Chat PR: 39397
File: packages/gazzodown/src/elements/Timestamp/RelativeTime.spec.tsx:63-70
Timestamp: 2026-03-06T18:02:11.732Z
Learning: In Rocket.Chat, tests within the gazzodown package (and the broader test suite) should run in UTC to make locale/timezone-sensitive outputs from toLocaleString() deterministic. Ensure snapshot tests that capture values like title attributes based on toLocaleString() remain stable by running tests in UTC. Actions you can take: - configure the test environment to TZ=UTC (e.g., in CI or local npm/yarn test scripts), - optionally set process.env.TZ = 'UTC' in Jest/setupFiles or the test bootstrap, - avoid asserting locale-sensitive strings directly; prefer targeted assertions or mocks where necessary. Apply this guideline to all spec files under packages/gazzodown/src that involve timestamp rendering (pattern above).

Applied to files:

  • packages/gazzodown/src/Markup.spec.tsx
📚 Learning: 2026-03-06T18:10:15.268Z
Learnt from: tassoevan
Repo: RocketChat/Rocket.Chat PR: 39397
File: packages/gazzodown/src/code/CodeBlock.spec.tsx:47-68
Timestamp: 2026-03-06T18:10:15.268Z
Learning: In tests (especially those using testing-library/dom/jsdom) for Rocket.Chat components, the HTML <code> element has an implicit ARIA role of 'code'. Therefore, screen.getByRole('code') or screen.findByRole('code') will locate <code> elements even without a role attribute. Do not flag findByRole('code') as invalid in reviews; prefer using the implicit role instead of adding role="code" unless necessary for accessibility.

Applied to files:

  • packages/gazzodown/src/Markup.spec.tsx
🪛 LanguageTool
.changeset/sanitize-image-element-url.md

[uncategorized] ~6-~6: Did you mean the formatting language “Markdown” (= proper noun)?
Context: ...ching the protection already applied to markdown links. Defense-in-depth against XSS via...

(MARKDOWN_NNP)

🔇 Additional comments (3)
.changeset/sanitize-image-element-url.md (1)

1-7: LGTM!

packages/gazzodown/src/elements/ImageElement.tsx (1)

5-5: LGTM!

Also applies to: 52-56

packages/gazzodown/src/Markup.spec.tsx (1)

349-370: LGTM!

Also applies to: 377-395

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 3 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread packages/gazzodown/src/elements/ImageElement.tsx
@codecov

codecov Bot commented May 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 69.66%. Comparing base (dda4c12) to head (b2e2825).
⚠️ Report is 5 commits behind head on develop.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop   #40613      +/-   ##
===========================================
- Coverage    69.68%   69.66%   -0.03%     
===========================================
  Files         3323     3323              
  Lines       122803   122805       +2     
  Branches     21911    21903       -8     
===========================================
- Hits         85581    85556      -25     
- Misses       33873    33904      +31     
+ Partials      3349     3345       -4     
Flag Coverage Δ
e2e 59.31% <ø> (-0.05%) ⬇️
e2e-api 46.23% <ø> (-0.85%) ⬇️
unit 70.33% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@julio-rocketchat julio-rocketchat added this to the 8.5.0 milestone May 19, 2026
@julio-rocketchat julio-rocketchat added the stat: QA assured Means it has been tested and approved by a company insider label May 19, 2026
@dionisio-bot dionisio-bot Bot added the stat: ready to merge PR tested and approved waiting for merge label May 19, 2026
@julio-rocketchat
julio-rocketchat added this pull request to the merge queue May 19, 2026
Merged via the queue into develop with commit 0b7a763 May 19, 2026
82 of 84 checks passed
@julio-rocketchat
julio-rocketchat deleted the add-sanitizeurl-to-imageelement branch May 19, 2026 22:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stat: QA assured Means it has been tested and approved by a company insider stat: ready to merge PR tested and approved waiting for merge type: bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants