File size: 2,288 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
import { getQueryArg } from '@wordpress/url';
import { translate } from 'i18n-calypso';
import React, { useEffect } from 'react';
import DocumentHead from 'calypso/components/data/document-head';
import twoStepAuthorization from 'calypso/lib/two-step-authorization';
import ReauthRequiredComponent from 'calypso/me/reauth-required';
import './style.scss';
export default function ReauthRequired() {
useEffect( () => {
const handleSuccess = () => {
const redirectTo = getQueryArg( window.location.search, 'redirect_to' );
if ( typeof redirectTo === 'string' && redirectTo ) {
// Prevent open redirect vulnerabilities by ensuring the redirect URL is relative
// or points to a trusted domain (e.g. wordpress.com).
// For simplicity here, we'll just check if it's an internal path.
// A more robust check might be needed depending on security requirements.
try {
// Use window.location.origin as the base URL for relative paths
const url = new URL( redirectTo, window.location.origin );
if ( url.origin === window.location.origin || redirectTo.startsWith( '/' ) ) {
// Use the resolved URL's href to ensure correct navigation for pathnames
window.location.href = url.href;
} else {
// eslint-disable-next-line no-console
console.warn( `Skipping potentially unsafe redirect to: ${ redirectTo }` );
// Optionally redirect to a default safe page, e.g., '/'
// window.location.href = '/';
}
} catch ( e ) {
// Handle invalid URL if necessary
// eslint-disable-next-line no-console
console.error( `Invalid redirect URL: ${ redirectTo }`, e );
}
} else {
// Optional: Redirect to a default location if redirect_to is not present or invalid
// window.location.href = '/';
}
};
const handleAuthStateChange = () => {
if ( ! twoStepAuthorization.isReauthRequired() ) {
handleSuccess();
}
};
// Listen for changes in auth state
twoStepAuthorization.on( 'change', handleAuthStateChange );
return () => {
twoStepAuthorization.off( 'change', handleAuthStateChange );
};
}, [] );
return (
<>
<DocumentHead title={ translate( 'Reauth Required' ) } />
<ReauthRequiredComponent twoStepAuthorization={ twoStepAuthorization } />
</>
);
}
|