Repository navigation
Fix(web): render search highlights as text nodes to prevent XSS #6163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mday-io
wants to merge
3
commits into
SQLMesh:main
Choose a base branch
from
mday-io:mday/cool-sagan-iqtmgi
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+123
−15
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
web/client/src/library/components/search/tests/SearchList.spec.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import { describe, it, expect, vi } from 'vitest' | ||
| import { MemoryRouter } from 'react-router' | ||
| import { render, fireEvent } from '../../../../tests/utils' | ||
| import SearchList from '../SearchList' | ||
|
|
||
| // jsdom has no ResizeObserver, which TBKResizeObserver needs to mount | ||
| vi.stubGlobal( | ||
| 'ResizeObserver', | ||
| class { | ||
| observe(): void {} | ||
| unobserve(): void {} | ||
| disconnect(): void {} | ||
| }, | ||
| ) | ||
|
|
||
| const PAYLOAD = '<img src=x onerror="alert(1)">' | ||
|
|
||
| function renderSearch( | ||
| name: string, | ||
| search: string, | ||
| showIndex: boolean, | ||
| ): HTMLElement { | ||
| const { container } = render( | ||
| <MemoryRouter> | ||
| <SearchList<{ name: string }> | ||
| list={[{ name }]} | ||
| searchBy="name" | ||
| displayBy="name" | ||
| showIndex={showIndex} | ||
| /> | ||
| </MemoryRouter>, | ||
| ) | ||
|
|
||
| fireEvent.input(container.querySelector('input')!, { | ||
| target: { value: search }, | ||
| }) | ||
|
|
||
| return container.querySelector('[role="menuitem"]')! | ||
| } | ||
|
|
||
| describe.each([true, false])('SearchList with showIndex=%s', showIndex => { | ||
| it('should render markup in project text as text', () => { | ||
| const name = `${PAYLOAD}model` | ||
| const result = renderSearch(name, 'model', showIndex) | ||
|
|
||
| expect(result.querySelector('img')).toBeNull() | ||
| expect(result.textContent).toContain(name) | ||
| expect(result.querySelector('b')?.textContent).toBe('model') | ||
| }) | ||
|
|
||
| it('should render markup in the search string as text', () => { | ||
| const result = renderSearch(`a${PAYLOAD}b`, PAYLOAD, showIndex) | ||
|
|
||
| expect(result.querySelector('img')).toBeNull() | ||
| expect(result.querySelector('b')?.textContent).toBe(PAYLOAD) | ||
| }) | ||
|
|
||
| it('should highlight every match, including adjacent and trailing ones', () => { | ||
| const result = renderSearch('ababxab', 'ab', showIndex) | ||
|
|
||
| expect( | ||
| Array.from(result.querySelectorAll('b'), b => b.textContent), | ||
| ).toEqual(['ab', 'ab', 'ab']) | ||
| expect(result.textContent).toContain('ababxab') | ||
| }) | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need the
# SPDX-License-Identifier: Apache-2.0header on new files