Skip to content

Commit

Permalink
Add sanitize function
Browse files Browse the repository at this point in the history
  • Loading branch information
kengreeff committed Sep 5, 2023
1 parent 2f8f562 commit 6722ce0
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 6 deletions.
7 changes: 6 additions & 1 deletion src/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ const initialState = {
const SESSION_PREFIX = 'pkce-verifier';

const KINDE_SITE_URL = process.env.KINDE_SITE_URL;
const KINDE_AUTH_API_PATH = process.env.KINDE_AUTH_API_PATH || '/api/auth';

// We need to use NEXT_PUBLIC for frontend vars
const KINDE_AUTH_API_PATH = process.env.NEXT_PUBLIC_KINDE_AUTH_API_PATH
|| process.env.KINDE_AUTH_API_PATH
|| '/api/auth';

const KINDE_POST_LOGIN_REDIRECT_URL =
process.env.KINDE_POST_LOGIN_REDIRECT_URL ||
process.env.KINDE_POST_LOGIN_URL_REDIRECT_URL;
Expand Down
1 change: 1 addition & 0 deletions src/frontend/AuthProvider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ export const KindeProvider = ({children}) => {
error,
isLoading
} = state;

return (
<AuthContext.Provider
value={{
Expand Down
9 changes: 7 additions & 2 deletions src/handlers/pageRouter/callback.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import jwt_decode from 'jwt-decode';

import {config} from '../../config/index';
import {isTokenValid} from '../../utils/pageRouter/isTokenValid';
import {version} from '../../utils/version';
import jwt_decode from 'jwt-decode';
import {sanitizeRedirect} from '../../utils/sanitizeRedirect';

var cookie = require('cookie');

Expand All @@ -21,7 +23,10 @@ export const callback = async (req, res) => {
} = JSON.parse(jsonCookieValue);

if (options?.callback_url) {
redirectUrl = options.callback_url;
redirectUrl = sanitizeRedirect({
baseUrl: new URL(config.redirectURL).origin,
url: options.callback_url
});
}

const response = await fetch(
Expand Down
1 change: 0 additions & 1 deletion src/utils/appRouter/prepareForRedirect.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {config} from '../../config/index';
import {setupChallenge} from '../setupChallenge';
import {setVerifierCookie} from './setVerifierCookie';
import {generateAuthUrl} from '../generateAuthUrl';
Expand Down
1 change: 0 additions & 1 deletion src/utils/generateAuthUrl.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {config} from '../config/index';
import {setupChallenge} from './setupChallenge';

export function generateAuthUrl(options, type = 'login') {
const {org_code, is_create_org, org_name = ''} = options;
Expand Down
1 change: 0 additions & 1 deletion src/utils/pageRouter/prepareForRedirect.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {config} from '../../config/index';
import {setupChallenge} from '../setupChallenge';
import {setVerifierCookie} from './setVerifierCookie';
import {generateAuthUrl} from '../generateAuthUrl';
Expand Down
9 changes: 9 additions & 0 deletions src/utils/sanitizeRedirect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const sanitizeRedirect = ({baseUrl, url}) => {
if (url.startsWith("/")){
return `${baseUrl}${url}`
} else if (new URL(url).origin === baseUrl) {
return url
}

return baseUrl
}

0 comments on commit 6722ce0

Please sign in to comment.