mirror of
https://github.com/SableClient/Sable.git
synced 2026-12-04 07:42:24 -08:00
fix: strip credentials from token-refresher tempClient to prevent logout cascade
This commit is contained in:
5 files changed
+16
-24
No files matched your search
@@ -42,11 +42,11 @@ describe('expiresInMsFromToken', () => {
|
||||
});
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
oauth2Context: vi.fn(),
|
||||
oauth2Context: vi.fn<(context: unknown) => void>(),
|
||||
completeAuthorizationCodeGrant: vi.fn<(code: string) => Promise<BearerTokenResponse>>(),
|
||||
getAuthMetadata: vi.fn<() => Promise<ValidatedAuthMetadata>>(),
|
||||
whoami: vi.fn(),
|
||||
setAccessToken: vi.fn(),
|
||||
whoami: vi.fn<() => Promise<{ user_id: string; device_id?: string }>>(),
|
||||
setAccessToken: vi.fn<(token: string) => void>(),
|
||||
}));
|
||||
|
||||
vi.mock('$types/matrix-sdk', async (importOriginal) => {
|
||||
@@ -60,11 +60,13 @@ vi.mock('$types/matrix-sdk', async (importOriginal) => {
|
||||
public completeAuthorizationCodeGrant = mocks.completeAuthorizationCodeGrant;
|
||||
public context = { deviceId: 'dev', codeVerifier: 'verifier' };
|
||||
},
|
||||
createClient: vi.fn().mockImplementation(() => ({
|
||||
getAuthMetadata: mocks.getAuthMetadata,
|
||||
setAccessToken: mocks.setAccessToken,
|
||||
whoami: mocks.whoami,
|
||||
})),
|
||||
createClient: vi
|
||||
.fn<(opts: { baseUrl: string; fetchFn: typeof fetch }) => Record<string, unknown>>()
|
||||
.mockImplementation(() => ({
|
||||
getAuthMetadata: mocks.getAuthMetadata,
|
||||
setAccessToken: mocks.setAccessToken,
|
||||
whoami: mocks.whoami,
|
||||
})),
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
@@ -86,10 +86,6 @@ const startBackgroundClient = async (session: Session): Promise<MatrixClient> =>
|
||||
const tempClient = createClient({
|
||||
baseUrl: session.baseUrl,
|
||||
fetchFn: fetch,
|
||||
accessToken: session.accessToken,
|
||||
refreshToken: session.refreshToken,
|
||||
userId: session.userId,
|
||||
deviceId: session.deviceId,
|
||||
});
|
||||
const tokenRefresher = createSessionTokenRefresher(session, tempClient);
|
||||
|
||||
|
||||
@@ -32,11 +32,11 @@ describe('getAppPathFromHref', () => {
|
||||
).toBe('/login?code=c&state=s');
|
||||
});
|
||||
|
||||
it('returns empty when the href origin does not match the base', () => {
|
||||
expect(getAppPathFromHref('https://app.sable.moe/', 'https://tauri.localhost/')).toBe('');
|
||||
it('extracts the path from the href when the origin does not match the base (Tauri)', () => {
|
||||
expect(getAppPathFromHref('https://app.sable.moe/', 'https://tauri.localhost/')).toBe('/');
|
||||
expect(
|
||||
getAppPathFromHref('https://app.sable.moe/', 'https://tauri.localhost/login?code=c&state=s')
|
||||
).toBe('');
|
||||
).toBe('/login?code=c&state=s');
|
||||
});
|
||||
|
||||
it('returns empty when a hash-router base is paired with a hashless href', () => {
|
||||
|
||||
@@ -71,18 +71,16 @@ export const getAppPathFromHref = (baseUrl: string, href: string): string => {
|
||||
const trimmedBaseUrl = trimLeadingSlash(baseUrl.slice(baseHashIndex + 1));
|
||||
const trimmedHref = trimLeadingSlash(href.slice(hrefHashIndex + 1));
|
||||
|
||||
// On Tauri the base origin is synthetic and never matches the href origin; guard
|
||||
// against slicing a basename the href does not start with.
|
||||
const appPath = trimmedHref.startsWith(trimmedBaseUrl)
|
||||
? trimmedHref.slice(trimmedBaseUrl.length)
|
||||
: '';
|
||||
return `/${trimLeadingSlash(appPath)}`;
|
||||
}
|
||||
|
||||
// On Tauri the base origin is synthetic and never matches the href origin; return
|
||||
// empty instead of slicing a garbage path that would be stored as a redirect.
|
||||
const trimmedBaseUrl = trimTrailingSlash(baseUrl);
|
||||
return href.startsWith(trimmedBaseUrl) ? href.slice(trimmedBaseUrl.length) : '';
|
||||
if (href.startsWith(trimmedBaseUrl)) return href.slice(trimmedBaseUrl.length);
|
||||
const { pathname, search } = new URL(href);
|
||||
return pathname + search;
|
||||
};
|
||||
|
||||
export const getRootPath = (): string => ROOT_PATH;
|
||||
|
||||
@@ -260,10 +260,6 @@ const buildClient = async (session: Session): Promise<BuiltClient> => {
|
||||
const tempClient = createClient({
|
||||
baseUrl: session.baseUrl,
|
||||
fetchFn: fetch,
|
||||
accessToken: session.accessToken,
|
||||
refreshToken: session.refreshToken,
|
||||
userId: session.userId,
|
||||
deviceId: session.deviceId,
|
||||
});
|
||||
const tokenRefresher = createSessionTokenRefresher(session, tempClient);
|
||||
|
||||
|
||||
Reference in new issue
Block a user