fix: Major bug cleanup pass 1
Queue Release Build / prepare (push) Successful in 19s
Deploy Web Apps / deploy (push) Successful in 8m12s
Queue Release Build / build-windows (push) Successful in 27m44s
Queue Release Build / build-linux (push) Successful in 48m1s
Queue Release Build / finalize (push) Successful in 2m42s
Queue Release Build / build-android (push) Successful in 22m7s

This commit is contained in:
2026-06-09 17:59:54 +02:00
parent 80d7728e66
commit eb51f043ac
127 changed files with 2731 additions and 322 deletions
@@ -9,6 +9,7 @@ import { UsersActions } from '../../../../store/users/users.actions';
import type { User } from '../../../../shared-kernel';
export const DEFAULT_POST_AUTH_URL = '/dashboard';
export const AUTH_MODE_AUTHORIZE = 'authorize';
const AUTH_ROUTE_PATHS = new Set(['/login', '/register']);
const MAX_RETURN_URL_DEPTH = 10;
@@ -79,15 +80,27 @@ export function resolveSafeReturnUrl(
export function buildLoginReturnQueryParams(
currentUrl: string,
fallback = DEFAULT_POST_AUTH_URL
): { returnUrl?: string } {
fallback = DEFAULT_POST_AUTH_URL,
extra: Record<string, string | undefined> = {}
): Record<string, string> {
const safeReturnUrl = resolveSafeReturnUrl(currentUrl, fallback);
const queryParams: Record<string, string> = {};
if (safeReturnUrl === fallback) {
return {};
if (safeReturnUrl !== fallback) {
queryParams['returnUrl'] = safeReturnUrl;
}
return { returnUrl: safeReturnUrl };
for (const [key, value] of Object.entries(extra)) {
if (value?.trim()) {
queryParams[key] = value.trim();
}
}
return queryParams;
}
export function isAuthorizeAuthMode(mode: string | null | undefined): boolean {
return mode?.trim() === AUTH_MODE_AUTHORIZE;
}
export function waitForAuthenticationOutcome(
@@ -16,13 +16,9 @@ describe('auth-session.rules', () => {
} as Pick<User, 'homeSignalServerUrl'>;
it('collects home and active server urls without duplicates', () => {
expect(collectSessionTokenLookupUrls(user, 'https://signal.example.com')).toEqual([
'https://signal.example.com'
]);
expect(collectSessionTokenLookupUrls(user, 'http://localhost:3001')).toEqual([
'http://localhost:3001',
'https://signal.example.com'
]);
expect(collectSessionTokenLookupUrls(user, 'https://signal.example.com')).toEqual(['https://signal.example.com']);
expect(collectSessionTokenLookupUrls(user, 'http://localhost:3001')).toEqual(['http://localhost:3001', 'https://signal.example.com']);
});
it('requires a valid token for a known server url', () => {
@@ -0,0 +1,53 @@
import {
describe,
expect,
it
} from 'vitest';
import { isSelfPresenceUserId, resolveSelfPresenceUserIds } from './self-presence-identity.rules';
describe('resolveSelfPresenceUserIds', () => {
it('includes home user id and oderId', () => {
const ids = resolveSelfPresenceUserIds({
homeUserId: 'home-id',
homeOderId: 'peer-a'
});
expect([...ids]).toEqual(['home-id', 'peer-a']);
});
it('includes the per-server actor user id when provisioned on a foreign server', () => {
const ids = resolveSelfPresenceUserIds({
homeUserId: 'home-id',
homeOderId: 'peer-a',
actorUserId: 'foreign-id'
});
expect([...ids]).toEqual([
'home-id',
'peer-a',
'foreign-id'
]);
});
it('deduplicates when actor id matches home id', () => {
const ids = resolveSelfPresenceUserIds({
homeUserId: 'same-id',
homeOderId: 'same-id',
actorUserId: 'same-id'
});
expect([...ids]).toEqual(['same-id']);
});
});
describe('isSelfPresenceUserId', () => {
it('returns true when the user id is part of the self set', () => {
const selfIds = resolveSelfPresenceUserIds({
homeUserId: 'home-id',
actorUserId: 'foreign-id'
});
expect(isSelfPresenceUserId('foreign-id', selfIds)).toBe(true);
expect(isSelfPresenceUserId('other-id', selfIds)).toBe(false);
});
});
@@ -0,0 +1,31 @@
export interface SelfPresenceIdentityInput {
homeUserId?: string;
homeOderId?: string;
actorUserId?: string;
}
/** Collect every user id that represents the local user on a room's signal server. */
export function resolveSelfPresenceUserIds(input: SelfPresenceIdentityInput): ReadonlySet<string> {
const ids = new Set<string>();
if (input.homeUserId?.trim()) {
ids.add(input.homeUserId.trim());
}
if (input.homeOderId?.trim()) {
ids.add(input.homeOderId.trim());
}
if (input.actorUserId?.trim()) {
ids.add(input.actorUserId.trim());
}
return ids;
}
export function isSelfPresenceUserId(
userId: string | undefined,
selfIds: ReadonlySet<string>
): boolean {
return !!userId?.trim() && selfIds.has(userId.trim());
}
@@ -0,0 +1,55 @@
import {
describe,
it,
expect
} from 'vitest';
import { resolveSignalIdentity } from './signal-server-credential-resolution.rules';
describe('resolveSignalIdentity', () => {
const homeUser = {
id: 'home-user-1',
displayName: 'Alice',
homeSignalServerUrl: 'https://signal.example.com'
};
it('prefers the per-signal credential when present', () => {
const resolved = resolveSignalIdentity(
{ userId: 'provisioned-1', token: 'cred-token', displayName: 'Alice On Foreign' },
{ token: 'legacy-token' },
homeUser
);
expect(resolved).toEqual({
userId: 'provisioned-1',
token: 'cred-token',
displayName: 'Alice On Foreign',
homeSignalServerUrl: 'https://signal.example.com'
});
});
it('falls back to the legacy session token using the home identity when no credential exists', () => {
const resolved = resolveSignalIdentity(
null,
{ token: 'legacy-token' },
homeUser
);
expect(resolved).toEqual({
userId: 'home-user-1',
token: 'legacy-token',
displayName: 'Alice',
homeSignalServerUrl: 'https://signal.example.com'
});
});
it('returns null when neither a credential nor a legacy token is available', () => {
expect(resolveSignalIdentity(null, null, homeUser)).toBeNull();
});
it('does not fall back to the legacy token without a known home user id', () => {
expect(resolveSignalIdentity(null, { token: 'legacy-token' }, null)).toBeNull();
expect(
resolveSignalIdentity(null, { token: 'legacy-token' }, { displayName: 'Alice' })
).toBeNull();
});
});
@@ -0,0 +1,53 @@
export interface ResolvableHomeUser {
id?: string;
displayName?: string;
homeSignalServerUrl?: string;
}
export interface ResolvedSignalIdentity {
userId: string;
token: string;
displayName: string;
homeSignalServerUrl?: string;
}
/**
* Resolve the identity (oder id + session token) used to `identify` on a signal
* server.
*
* Order of precedence:
* 1. The per-signal-server credential (the authoritative source for both home
* and provisioned foreign servers).
* 2. The legacy single-session token store, reconstructed with the home user's
* identity. This keeps `identify` working for sessions restored from disk
* that pre-date the per-signal credential store (otherwise the client never
* authenticates and the user appears alone in every room).
*
* Foreign servers are never reconstructed from the legacy token: their account
* id is the provisioned id, which only the per-signal credential carries.
*/
export function resolveSignalIdentity(
credential: { userId: string; token: string; displayName: string } | null,
legacyToken: { token: string } | null,
homeUser: ResolvableHomeUser | null | undefined
): ResolvedSignalIdentity | null {
if (credential) {
return {
userId: credential.userId,
token: credential.token,
displayName: credential.displayName,
homeSignalServerUrl: homeUser?.homeSignalServerUrl
};
}
if (legacyToken && homeUser?.id) {
return {
userId: homeUser.id,
token: legacyToken.token,
displayName: homeUser.displayName ?? '',
homeSignalServerUrl: homeUser.homeSignalServerUrl
};
}
return null;
}
@@ -0,0 +1,36 @@
import {
describe,
it,
expect
} from 'vitest';
import {
ProvisionUsernameCollisionError,
buildProvisionUsernameCandidates,
shortHomeUserId
} from './signal-server-provision.rules';
describe('signal-server-provision.rules', () => {
it('derives a stable short id from a home user uuid', () => {
expect(shortHomeUserId('a3f2b1c4-5678-90ab-cdef-1234567890ab')).toBe('a3f2b1');
});
it('orders username candidates with preferred first then suffixed fallback', () => {
expect(
buildProvisionUsernameCandidates('alice', 'a3f2b1c4-5678-90ab-cdef-1234567890ab')
).toEqual(['alice', 'alice-a3f2b1']);
});
it('deduplicates candidates when suffix would repeat preferred username', () => {
expect(
buildProvisionUsernameCandidates('alice-a3f2b1', 'a3f2b1c4-5678-90ab-cdef-1234567890ab')
).toEqual(['alice-a3f2b1']);
});
it('exposes attempted usernames on collision errors', () => {
const error = new ProvisionUsernameCollisionError('https://signal.example.com', ['alice', 'alice-a3f2b1']);
expect(error.name).toBe('ProvisionUsernameCollisionError');
expect(error.serverUrl).toBe('https://signal.example.com');
expect(error.attemptedUsernames).toEqual(['alice', 'alice-a3f2b1']);
});
});
@@ -0,0 +1,34 @@
export class ProvisionUsernameCollisionError extends Error {
constructor(
readonly serverUrl: string,
readonly attemptedUsernames: readonly string[]
) {
super(`Could not provision account on ${serverUrl}`);
this.name = 'ProvisionUsernameCollisionError';
}
}
export function shortHomeUserId(homeUserId: string): string {
return homeUserId.replace(/-/g, '').slice(0, 6)
.toLowerCase();
}
export function buildProvisionUsernameCandidates(
preferredUsername: string,
homeUserId: string
): string[] {
const trimmed = preferredUsername.trim();
if (!trimmed) {
return [];
}
const candidates = [trimmed];
const suffix = shortHomeUserId(homeUserId);
if (suffix && !trimmed.endsWith(`-${suffix}`)) {
candidates.push(`${trimmed}-${suffix}`);
}
return [...new Set(candidates)];
}
@@ -0,0 +1,9 @@
export interface SignalServerCredential {
serverUrl: string;
userId: string;
username: string;
displayName: string;
token: string;
expiresAt: number;
provisioned: boolean;
}