fix(api): strip query params on redirect (#58608)

This commit is contained in:
Oliver Eyton-Williams
2025-02-07 10:55:41 +01:00
committed by GitHub
parent 991f6a2f36
commit 436d27da52
2 changed files with 23 additions and 1 deletions
+17
View File
@@ -165,6 +165,23 @@ describe('redirection', () => {
expect(result).toEqual(expectedReturn);
});
it('should strip off any query parameters from the referer', () => {
const req = {
headers: {
referer: `https://www.freecodecamp.org/espanol/learn/rosetta-code/?query=param`
}
};
const expectedReturn = {
origin: 'https://www.freecodecamp.org',
pathPrefix: 'espanol',
returnTo: 'https://www.freecodecamp.org/espanol/learn/rosetta-code/'
};
const result = getRedirectParams(req);
expect(result).toEqual(expectedReturn);
});
it('should use HOME_LOCATION with missing referer', () => {
const req = {
headers: {}
+6 -1
View File
@@ -113,7 +113,12 @@ function getParamsFromUrl(
// if this is not one of the client languages, validation will convert
// this to '' before it is used.
const pathPrefix = returnUrl.pathname.split('/')[1] ?? '';
return normalize({ returnTo: returnUrl.href, origin, pathPrefix });
return normalize({
// strip off any query parameters
returnTo: returnUrl.origin + returnUrl.pathname,
origin,
pathPrefix
});
}
/**