Skip to content

Commit

Permalink
Merge pull request #367 from navikt/fix-decoding-and-percentage-sign
Browse files Browse the repository at this point in the history
Catch error when trying to decode the percentage sign itself
  • Loading branch information
otenav authored Apr 22, 2022
2 parents 3e9273a + 7271782 commit 6f2a4ba
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
26 changes: 18 additions & 8 deletions src/components/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ export function parseQueryString(queryString = "?") {
parameters.forEach((parameter) => {
const pair = parameter.split("=");
if (pair[0] !== undefined && pair[0] !== "") {
let key = decodeURIComponent(pair[0]);
const val = pair[1] !== undefined ? decodeURIComponent(pair[1]) : "";
let key = pair[0];
const val = pair[1] !== undefined ? pair[1] : "";

if (key === "international") {
object[key] = val === "true" ? true : "false";
Expand Down Expand Up @@ -148,15 +148,25 @@ export function stringifyQueryObject(object = {}) {
}

/**
* Dekoder en url til den ikke lenger er kodet.
* En kodet url er i dette tilfellet en url som inneholder '%'.
* After login, the redirect url back to this page may have been encoded several times.
* This function decodes url until it is no longer encoded.
* We assume that an encoded url contains '%', for example %20 (space)
*/
export function decodeUrl(url) {
let decodedUrl = url;
while (decodedUrl.includes("%")) {
decodedUrl = decodeURIComponent(decodedUrl);
let successfullyDecodedUrl = url;
try {
while (successfullyDecodedUrl.includes("%")) {
const decodeAttempt = decodeURIComponent(successfullyDecodedUrl);
successfullyDecodedUrl = decodeAttempt;
}
} catch (e) {
// When url is fully decoded, it may try to decode again if the url
// contains the percentage sign itself, and this will throw an error.
// This can happen for example when searching for part-time job '?=50%'.
return successfullyDecodedUrl
}
return decodedUrl;

return successfullyDecodedUrl;
}

export function extractParam(param, nullValue) {
Expand Down
10 changes: 5 additions & 5 deletions src/context/AuthenticationProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useEffect, useState } from "react";
import PropTypes from "prop-types";
import { captureException } from "@sentry/browser";
import { AD_USER_API, CONTEXT_PATH, LOGIN_URL, LOGOUT_URL, STILLINGSOK_URL } from "../environment";
import { extractParam, parseQueryString, stringifyQueryObject } from "../components/utils";
import { extractParam, stringifyQueryObject } from "../components/utils";

export const AuthenticationContext = React.createContext({});

Expand All @@ -29,15 +29,15 @@ const allowedRedirectUrls = [
*/
export function fixUrlAfterLogin() {
if (window.location.pathname === `${CONTEXT_PATH}/stilling`) {
const { uuid, ...otherQueryParams } = parseQueryString(document.location.search);
const uuid = extractParam('uuid');
window.history.replaceState(
{},
"",
`${CONTEXT_PATH}/stilling/${uuid}${stringifyQueryObject(otherQueryParams)}`
`${CONTEXT_PATH}/stilling/${uuid}`
);
} else if (window.location.pathname === `${CONTEXT_PATH}/intern`) {
const { uuid, ...otherQueryParams } = parseQueryString(document.location.search);
window.history.replaceState({}, "", `${CONTEXT_PATH}/intern/${uuid}${stringifyQueryObject(otherQueryParams)}`);
const uuid = extractParam('uuid');
window.history.replaceState({}, "", `${CONTEXT_PATH}/intern/${uuid}`);
}
}

Expand Down

0 comments on commit 6f2a4ba

Please sign in to comment.