Skip to content

Commit 7ac69d6

Browse files
Alleny244jeremylenz
authored andcommitted
Fixes #39302 - avoid hosts search URI decode crash
1 parent 7efc221 commit 7ac69d6

2 files changed

Lines changed: 30 additions & 3 deletions

File tree

webpack/assets/javascripts/react_app/components/PF4/TableIndexPage/Table/TableHooks.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ export const useBulkSelect = ({
316316
};
317317

318318
export const friendlySearchParam = searchParam =>
319-
decodeURIComponent(searchParam.replace(/\+/g, ' '));
319+
searchParam.replace(/\+/g, ' ');
320320

321321
// takes a url query like ?type=security&search=name+~+foo
322322
// and returns an object
@@ -330,7 +330,6 @@ export const useUrlParams = () => {
330330
new URLSearchParams(location.search).entries()
331331
);
332332
const searchParam = urlSearchParam ? friendlySearchParam(urlSearchParam) : '';
333-
334333
return {
335334
searchParam,
336335
...urlParams,

webpack/assets/javascripts/react_app/components/PF4/TableIndexPage/Table/TableHooks.test.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import { act, renderHook } from '@testing-library/react-hooks';
2-
import { useBulkSelect } from './TableHooks';
2+
import { useLocation } from 'react-router-dom';
3+
import { friendlySearchParam, useBulkSelect, useUrlParams } from './TableHooks';
4+
5+
jest.mock('react-router-dom', () => ({
6+
useLocation: jest.fn(),
7+
}));
8+
9+
const mockUseLocation = useLocation;
310

411
const isSelectable = () => true;
512
const idColumn = 'errata_id';
@@ -97,3 +104,24 @@ it('adds filter dropdown query to scoped search string', () => {
97104

98105
expect(result.current.fetchBulkParams({})).toBe('severity=Low and errata_id !^ (RHSA-2022:2031)');
99106
});
107+
108+
it('normalizes + separators in friendlySearchParam', () => {
109+
expect(friendlySearchParam('name+~+foo/bar')).toBe('name ~ foo/bar');
110+
});
111+
112+
it('keeps encoded percent fragments untouched in friendlySearchParam', () => {
113+
expect(friendlySearchParam('name+~+foo%2Fbar')).toBe('name ~ foo%2Fbar');
114+
expect(friendlySearchParam('name+~+foo%2')).toBe('name ~ foo%2');
115+
});
116+
117+
it('parses hosts search query params without crashing on percent search', () => {
118+
mockUseLocation.mockReturnValue({
119+
search: '?search=%25&page=1',
120+
});
121+
122+
const { result } = renderHook(() => useUrlParams());
123+
expect(result.current).toEqual({
124+
searchParam: '%',
125+
page: '1',
126+
});
127+
});

0 commit comments

Comments
 (0)