Fix token expiry racing with login causing wrong error to be shown (#29566)

* Fix token expiry racing with login causing wrong error to be shown

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

* Add tests

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

* yay jest

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

* Iterate

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

* Iterate

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

---------

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
Michael Telatynski
2025-03-25 12:07:07 +00:00
committed by GitHub
parent 99ea51c6f2
commit 102a1ddb9e
5 changed files with 91 additions and 14 deletions

View File

@@ -149,6 +149,7 @@ interface ILoadSessionOpts {
ignoreGuest?: boolean;
defaultDeviceDisplayName?: string;
fragmentQueryParams?: QueryDict;
abortSignal?: AbortSignal;
}
/**
@@ -196,7 +197,7 @@ export async function loadSession(opts: ILoadSessionOpts = {}): Promise<boolean>
if (enableGuest && guestHsUrl && fragmentQueryParams.guest_user_id && fragmentQueryParams.guest_access_token) {
logger.log("Using guest access credentials");
return doSetLoggedIn(
await doSetLoggedIn(
{
userId: fragmentQueryParams.guest_user_id as string,
accessToken: fragmentQueryParams.guest_access_token as string,
@@ -206,7 +207,8 @@ export async function loadSession(opts: ILoadSessionOpts = {}): Promise<boolean>
},
true,
false,
).then(() => true);
);
return true;
}
const success = await restoreSessionFromStorage({
ignoreGuest: Boolean(opts.ignoreGuest),
@@ -225,6 +227,11 @@ export async function loadSession(opts: ILoadSessionOpts = {}): Promise<boolean>
// fall back to welcome screen
return false;
} catch (e) {
// We may be aborted e.g. because our token expired, so don't show an error here
if (opts.abortSignal?.aborted) {
return false;
}
if (e instanceof AbortLoginAndRebuildStorage) {
// If we're aborting login because of a storage inconsistency, we don't
// need to show the general failure dialog. Instead, just go back to welcome.
@@ -236,7 +243,7 @@ export async function loadSession(opts: ILoadSessionOpts = {}): Promise<boolean>
return false;
}
return handleLoadSessionFailure(e);
return handleLoadSessionFailure(e, opts);
}
}
@@ -656,7 +663,7 @@ export async function restoreSessionFromStorage(opts?: { ignoreGuest?: boolean }
}
}
async function handleLoadSessionFailure(e: unknown): Promise<boolean> {
async function handleLoadSessionFailure(e: unknown, loadSessionOpts?: ILoadSessionOpts): Promise<boolean> {
logger.error("Unable to load session", e);
const modal = Modal.createDialog(SessionRestoreErrorDialog, {
@@ -671,7 +678,7 @@ async function handleLoadSessionFailure(e: unknown): Promise<boolean> {
}
// try, try again
return loadSession();
return loadSession(loadSessionOpts);
}
/**

View File

@@ -235,6 +235,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
private themeWatcher?: ThemeWatcher;
private fontWatcher?: FontWatcher;
private readonly stores: SdkContextClass;
private loadSessionAbortController = new AbortController();
public constructor(props: IProps) {
super(props);
@@ -327,7 +328,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
// When the session loads it'll be detected as soft logged out and a dispatch
// will be sent out to say that, triggering this MatrixChat to show the soft
// logout page.
Lifecycle.loadSession();
Lifecycle.loadSession({ abortSignal: this.loadSessionAbortController.signal });
return;
}
@@ -552,6 +553,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
guestHsUrl: this.getServerProperties().serverConfig.hsUrl,
guestIsUrl: this.getServerProperties().serverConfig.isUrl,
defaultDeviceDisplayName: this.props.defaultDeviceDisplayName,
abortSignal: this.loadSessionAbortController.signal,
});
})
.then((loadedSession) => {
@@ -1565,26 +1567,33 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
dis.fire(Action.FocusSendMessageComposer);
});
cli.on(HttpApiEvent.SessionLoggedOut, function (errObj) {
cli.on(HttpApiEvent.SessionLoggedOut, (errObj) => {
this.loadSessionAbortController.abort(errObj);
this.loadSessionAbortController = new AbortController();
if (Lifecycle.isLoggingOut()) return;
// A modal might have been open when we were logged out by the server
Modal.forceCloseAllModals();
if (errObj.httpStatus === 401 && errObj.data && errObj.data["soft_logout"]) {
if (errObj.httpStatus === 401 && errObj.data?.["soft_logout"]) {
logger.warn("Soft logout issued by server - avoiding data deletion");
Lifecycle.softLogout();
return;
}
dis.dispatch(
{
action: "logout",
},
true,
);
// The above dispatch closes all modals, so open the modal after calling it synchronously
Modal.createDialog(ErrorDialog, {
title: _t("auth|session_logged_out_title"),
description: _t("auth|session_logged_out_description"),
});
dis.dispatch({
action: "logout",
});
});
cli.on(HttpApiEvent.NoConsent, function (message, consentUri) {
Modal.createDialog(