mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-20 00:51:17 -08:00
fix: guard getAppPathFromHref against Tauri synthetic origin mismatch
This commit is contained in:
2 files changed
+42
-3
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { getSettingsPath } from './pathUtils';
|
||||
import { getAppPathFromHref, getSettingsPath } from './pathUtils';
|
||||
|
||||
describe('getSettingsPath', () => {
|
||||
it('returns the settings root path', () => {
|
||||
@@ -13,3 +13,33 @@ describe('getSettingsPath', () => {
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getAppPathFromHref', () => {
|
||||
it('extracts the app path for a matching browser-router origin', () => {
|
||||
expect(getAppPathFromHref('https://app.sable.moe/', 'https://app.sable.moe/')).toBe('/');
|
||||
expect(getAppPathFromHref('https://app.sable.moe/', 'https://app.sable.moe/login')).toBe(
|
||||
'/login'
|
||||
);
|
||||
expect(
|
||||
getAppPathFromHref('https://app.sable.moe/', 'https://app.sable.moe/home/room/%21abc')
|
||||
).toBe('/home/room/%21abc');
|
||||
});
|
||||
|
||||
it('extracts the app path for a matching hash-router origin', () => {
|
||||
expect(getAppPathFromHref('https://app.sable.moe/#/', 'https://app.sable.moe/#/')).toBe('/');
|
||||
expect(
|
||||
getAppPathFromHref('https://app.sable.moe/#/', 'https://app.sable.moe/#/login?code=c&state=s')
|
||||
).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('');
|
||||
expect(
|
||||
getAppPathFromHref('https://app.sable.moe/', 'https://tauri.localhost/login?code=c&state=s')
|
||||
).toBe('');
|
||||
});
|
||||
|
||||
it('returns empty when a hash-router base is paired with a hashless href', () => {
|
||||
expect(getAppPathFromHref('https://app.sable.moe/#/', 'https://tauri.localhost/')).toBe('');
|
||||
});
|
||||
});
|
||||
@@ -64,16 +64,25 @@ export const getAppPathFromHref = (baseUrl: string, href: string): string => {
|
||||
const baseHashIndex = baseUrl.indexOf('#');
|
||||
if (baseHashIndex > -1) {
|
||||
const hrefHashIndex = href.indexOf('#');
|
||||
if (hrefHashIndex === -1) return '';
|
||||
|
||||
// href may/not have "/" around "#"
|
||||
// we need to take care of this when extracting app path
|
||||
const trimmedBaseUrl = trimLeadingSlash(baseUrl.slice(baseHashIndex + 1));
|
||||
const trimmedHref = trimLeadingSlash(href.slice(hrefHashIndex + 1));
|
||||
|
||||
const appPath = trimmedHref.slice(trimmedBaseUrl.length);
|
||||
// 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)}`;
|
||||
}
|
||||
|
||||
return href.slice(trimTrailingSlash(baseUrl).length);
|
||||
// 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) : '';
|
||||
};
|
||||
|
||||
export const getRootPath = (): string => ROOT_PATH;
|
||||
|
||||
Reference in new issue
Block a user