-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restore factory URL parameters after redirect (#906)
- Loading branch information
Showing
22 changed files
with
619 additions
and
420 deletions.
There are no files selected for viewing
This file contains 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,80 @@ | ||
/* | ||
* Copyright (c) 2018-2023 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
|
||
import { | ||
sanitizeLocation, | ||
sanitizeSearchParams, | ||
sanitizePathname, | ||
} from '../sanitize'; | ||
|
||
describe('sanitize', () => { | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('sanitizeLocation', () => { | ||
it('sanitizeLocation', () => { | ||
const location = { | ||
search: | ||
'url=https%3A%2F%2Fgithub.com%2Ftest-samples&storageType=persistent', | ||
pathname: '/f', | ||
}; | ||
|
||
const sanitizedLocation = sanitizeLocation(location); | ||
|
||
expect(sanitizedLocation).toEqual( | ||
expect.objectContaining({ | ||
search: | ||
'url=https%3A%2F%2Fgithub.com%2Ftest-samples&storageType=persistent', | ||
pathname: '/f', | ||
}), | ||
); | ||
}); | ||
}); | ||
|
||
describe('sanitizeSearchParams', () => { | ||
it('should return sanitized value of location.search if it is without encoding)', () => { | ||
const search = | ||
'url=https://github.com/test-samples&state=9284564475&session=98765&session_state=45645654567&code=9844646765&storageType=persistent&new'; | ||
|
||
const searchParams = new URLSearchParams(search); | ||
const sanitizedSearchParams = sanitizeSearchParams(searchParams); | ||
|
||
expect(sanitizedSearchParams.toString()).toEqual( | ||
'url=https%3A%2F%2Fgithub.com%2Ftest-samples&storageType=persistent&new=', | ||
); | ||
}); | ||
|
||
it('should return sanitized value of location.search if it is encoded', () => { | ||
const search = | ||
'url=https%3A%2F%2Fgithub.com%2Ftest-samples%26state%3D9284564475%26session%3D98765%26session_state%3D45645654567%26code%3D9844646765%26storageType%3Dpersistent'; | ||
|
||
const searchParams = new URLSearchParams(search); | ||
const sanitizedSearchParams = sanitizeSearchParams(searchParams); | ||
|
||
expect(sanitizedSearchParams.toString()).toEqual( | ||
'url=https%3A%2F%2Fgithub.com%2Ftest-samples%26storageType%3Dpersistent', | ||
); | ||
}); | ||
}); | ||
|
||
describe('sanitizePathname', () => { | ||
it('should remove oauth redirect leftovers', () => { | ||
const pathname = | ||
'/f&code=12345&session=67890&session_state=13579&state=24680'; | ||
|
||
const newPathname = sanitizePathname(pathname); | ||
|
||
expect(newPathname).toEqual('/f'); | ||
}); | ||
}); | ||
}); |
This file contains 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 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,78 @@ | ||
/* | ||
* Copyright (c) 2018-2023 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
|
||
const oauthParams = ['state', 'session', 'session_state', 'code']; | ||
|
||
/** | ||
* Remove oauth params. | ||
*/ | ||
export function sanitizeLocation< | ||
T extends { search: string; pathname: string } = Location, | ||
>(location: T, removeParams: string[] = []): T { | ||
const sanitizedSearchParams = sanitizeSearchParams( | ||
new URLSearchParams(location.search), | ||
removeParams, | ||
); | ||
const sanitizedPathname = sanitizePathname(location.pathname, removeParams); | ||
|
||
return { | ||
...location, | ||
search: sanitizedSearchParams.toString(), | ||
searchParams: sanitizedSearchParams, | ||
pathname: sanitizedPathname, | ||
}; | ||
} | ||
|
||
export function sanitizeSearchParams( | ||
searchParams: URLSearchParams, | ||
removeParams: string[] = [], | ||
): URLSearchParams { | ||
const toRemove = [...oauthParams, ...removeParams]; | ||
|
||
// remove oauth params | ||
toRemove.forEach(val => searchParams.delete(val)); | ||
|
||
// sanitize each query param | ||
const sanitizedSearchParams = new URLSearchParams(); | ||
searchParams.forEach((value, param) => { | ||
if (!value) { | ||
sanitizedSearchParams.set(param, value); | ||
return; | ||
} | ||
|
||
const sanitizedValue = sanitizeStr(value, toRemove); | ||
sanitizedSearchParams.set(param, sanitizedValue); | ||
}); | ||
|
||
return sanitizedSearchParams; | ||
} | ||
|
||
export function sanitizePathname( | ||
pathname: string, | ||
removeParams: string[] = [], | ||
): string { | ||
const toRemove = [...oauthParams, ...removeParams]; | ||
|
||
// sanitize pathname | ||
const sanitizedPathname = sanitizeStr(pathname, toRemove); | ||
|
||
return sanitizedPathname; | ||
} | ||
|
||
function sanitizeStr(str: string, removeParams: string[] = []): string { | ||
removeParams.forEach(param => { | ||
const re = new RegExp('&' + param + '=.+?(?=(?:[?&/#]|$))', 'i'); | ||
str = str.replace(re, ''); | ||
}); | ||
|
||
return str; | ||
} |
This file contains 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 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 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 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 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 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
Oops, something went wrong.