fix: add sanitizeUrl to ImageElement for defense in depth - #40613
Conversation
|
Looks like this PR is ready to merge! 🎉 |
🦋 Changeset detectedLatest commit: b2e2825 The changes in this PR will be included in the next version bump. This PR includes changesets to release 42 packages
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 |
WalkthroughThis PR adds XSS prevention to image rendering by sanitizing unsafe URL schemes in ImageElement. The component now blocks ChangesImage URL Sanitization
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Suggested labels
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Warning Review ran into problems🔥 ProblemsErrors were encountered while retrieving linked issues. Errors (1)
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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/gazzodown/src/Markup.spec.tsx (1)
372-374: ⚡ Quick winAssert
data-titlesanitization/preservation too.
ImageElementnow uses sanitized URLs indata-titleas 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
📒 Files selected for processing (3)
.changeset/sanitize-image-element-url.mdpackages/gazzodown/src/Markup.spec.tsxpackages/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.tsxpackages/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.tsxpackages/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.tsxpackages/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
There was a problem hiding this comment.
1 issue found across 3 files
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ 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
Flags with carried forward coverage won't be shown. Click here to find out more. 🚀 New features to boost your workflow:
|
Proposed changes (including videos or screenshots)
Adding
sanitizeUrltoImageElementas 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
javascript:,data:, andvbscript:are now blocked and safely replaced, aligning with existing link protection mechanisms.Tests