From 8ee97a74c32f68e9e4e35fa435cbdab04c6a465b Mon Sep 17 00:00:00 2001 From: Hampus Date: Wed, 2 Dec 2026 23:03:18 +0100 Subject: [PATCH] fix(api): read and write only hashed login networks (#4376) --- fluxer_api/src/api/Tables.ts | 14 --- .../src/api/database/capture/CdcRecords.ts | 3 - .../src/api/database/types/AuthTypes.ts | 16 --- .../auth/IpAuthorizationRepository.ts | 109 +++--------------- tools/dev/cassandra_target_schema.json | 30 ----- 5 files changed, 16 insertions(+), 156 deletions(-) diff --git a/fluxer_api/src/api/Tables.ts b/fluxer_api/src/api/Tables.ts index aa20df499..d4dc2378d 100644 --- a/fluxer_api/src/api/Tables.ts +++ b/fluxer_api/src/api/Tables.ts @@ -57,10 +57,6 @@ import { type EmailVerificationTokenRow, IP_AUTHORIZATION_TOKEN_COLUMNS, type IpAuthorizationTokenRow, - LEGACY_AUTHORIZED_IP_COLUMNS, - LEGACY_AUTHORIZED_IP_TRUST_KEY_COLUMNS, - type LegacyAuthorizedIpRow, - type LegacyAuthorizedIpTrustKeyRow, MFA_BACKUP_CODE_COLUMNS, type MfaBackupCodeRow, PASSWORD_CHANGE_TICKET_COLUMNS, @@ -1169,16 +1165,6 @@ export const IpAuthorizationTokens = defineTable({ - name: 'authorized_ips_v2', - columns: LEGACY_AUTHORIZED_IP_COLUMNS, - primaryKey: ['user_id', 'ip'], -}); -export const LegacyAuthorizedIpTrustKeys = defineTable({ - name: 'authorized_ip_trust_keys_v1', - columns: LEGACY_AUTHORIZED_IP_TRUST_KEY_COLUMNS, - primaryKey: ['user_id', 'trust_key'], -}); export const AuthorizedNetworks = defineTable({ name: 'authorized_networks_v1', columns: AUTHORIZED_NETWORK_COLUMNS, diff --git a/fluxer_api/src/api/database/capture/CdcRecords.ts b/fluxer_api/src/api/database/capture/CdcRecords.ts index 5f31c2511..78fe42b01 100644 --- a/fluxer_api/src/api/database/capture/CdcRecords.ts +++ b/fluxer_api/src/api/database/capture/CdcRecords.ts @@ -79,8 +79,6 @@ export const CDC_TABLES: ReadonlySet = new Set([ 'applications', 'auth_session_tombstones', 'auth_sessions', - 'authorized_ip_trust_keys_v1', - 'authorized_ips_v2', 'banned_avatar_hashes', 'banned_emails', 'banned_file_shas', @@ -354,7 +352,6 @@ export const CDC_KEPT_SENSITIVE: Readonly; -export const LEGACY_AUTHORIZED_IP_COLUMNS = ['user_id', 'ip'] as const satisfies ReadonlyArray< - keyof LegacyAuthorizedIpRow ->; -export const LEGACY_AUTHORIZED_IP_TRUST_KEY_COLUMNS = ['user_id', 'trust_key'] as const satisfies ReadonlyArray< - keyof LegacyAuthorizedIpTrustKeyRow ->; export const AUTHORIZED_NETWORK_COLUMNS = ['user_id', 'network_hash', 'last_used_at'] as const satisfies ReadonlyArray< keyof AuthorizedNetworkRow >; diff --git a/fluxer_api/src/api/user/repositories/auth/IpAuthorizationRepository.ts b/fluxer_api/src/api/user/repositories/auth/IpAuthorizationRepository.ts index 4c43910d5..07602e756 100644 --- a/fluxer_api/src/api/user/repositories/auth/IpAuthorizationRepository.ts +++ b/fluxer_api/src/api/user/repositories/auth/IpAuthorizationRepository.ts @@ -5,21 +5,11 @@ import {userHasMfa} from '@app/api/auth/services/SudoMethods'; import {createIpAuthorizationToken, type UserID} from '@app/api/BrandedTypes'; import {Config} from '@app/api/Config'; import {deleteOneOrMany, fetchMany, fetchOne, upsertOne} from '@app/api/database/CassandraQueryExecution'; -import type { - AuthorizedNetworkRow, - IpAuthorizationTokenRow, - LegacyAuthorizedIpRow, - LegacyAuthorizedIpTrustKeyRow, -} from '@app/api/database/types/AuthTypes'; -import { - AuthorizedNetworks, - IpAuthorizationTokens, - LegacyAuthorizedIps, - LegacyAuthorizedIpTrustKeys, -} from '@app/api/Tables'; +import type {AuthorizedNetworkRow, IpAuthorizationTokenRow} from '@app/api/database/types/AuthTypes'; +import {AuthorizedNetworks, IpAuthorizationTokens} from '@app/api/Tables'; import type {UserAccountRepository} from '@app/api/user/repositories/account/UserAccountRepository'; import {UserFlags} from '@fluxer/constants/src/UserConstants'; -import {getSameIpDecisionKey, normalizeIpString, parseIpAddress} from '@fluxer/ip_utils/src/IpAddress'; +import {getSameIpDecisionKey} from '@fluxer/ip_utils/src/IpAddress'; import {ms} from 'itty-time'; const AUTHORIZED_NETWORK_IDLE_MS = ms('90 days'); @@ -38,21 +28,6 @@ const GET_AUTHORIZED_NETWORK_CQL = AuthorizedNetworks.selectCql({ const LIST_AUTHORIZED_NETWORKS_CQL = AuthorizedNetworks.selectCql({ where: AuthorizedNetworks.where.eq('user_id'), }); -const CHECK_LEGACY_AUTHORIZED_IP_CQL = LegacyAuthorizedIps.selectCql({ - where: [LegacyAuthorizedIps.where.eq('user_id'), LegacyAuthorizedIps.where.eq('ip')], - limit: 1, -}); -const CHECK_LEGACY_TRUST_KEY_CQL = LegacyAuthorizedIpTrustKeys.selectCql({ - where: [LegacyAuthorizedIpTrustKeys.where.eq('user_id'), LegacyAuthorizedIpTrustKeys.where.eq('trust_key')], - limit: 1, -}); -const LIST_LEGACY_AUTHORIZED_IPS_CQL = LegacyAuthorizedIps.selectCql({ - where: LegacyAuthorizedIps.where.eq('user_id'), -}); -const LIST_LEGACY_AUTHORIZED_IP_TRUST_KEYS_CQL = LegacyAuthorizedIpTrustKeys.selectCql({ - where: LegacyAuthorizedIpTrustKeys.where.eq('user_id'), -}); - interface MfaSubject { id: UserID; authenticatorTypes?: Set | null; @@ -66,10 +41,6 @@ function hashAuthorizedNetwork(userId: UserID, networkKey: string): string { return createHmac('sha256', networkHashKey()).update(`${userId}:${networkKey}`).digest('base64url').slice(0, 22); } -function normalizeLegacyIp(ip: string): string { - return parseIpAddress(ip)?.normalized ?? normalizeIpString(ip); -} - function isFresh(lastUsedAt: Date, now: number): boolean { return now - lastUsedAt.getTime() < AUTHORIZED_NETWORK_IDLE_MS; } @@ -79,27 +50,21 @@ export class IpAuthorizationRepository { async checkIpAuthorized(userId: UserID, ip: string, now = Date.now()): Promise { const networkKey = getSameIpDecisionKey(ip); - if (networkKey) { - const networkHash = hashAuthorizedNetwork(userId, networkKey); - const row = await fetchOne(GET_AUTHORIZED_NETWORK_CQL, { - user_id: userId, - network_hash: networkHash, - }); - if (row && isFresh(row.last_used_at, now)) { - if (now - row.last_used_at.getTime() >= AUTHORIZED_NETWORK_REFRESH_MS) { - await upsertOne( - AuthorizedNetworks.insert({user_id: userId, network_hash: networkHash, last_used_at: new Date(now)}), - ); - } - return true; - } - } - if (!(await this.checkLegacyIpAuthorized(userId, ip))) { + if (!networkKey) { return false; } - const user = await this.userAccountRepository.findUnique(userId); - if (networkKey && user && !userHasMfa(user)) { - await this.rememberNetwork(userId, networkKey, now); + const networkHash = hashAuthorizedNetwork(userId, networkKey); + const row = await fetchOne(GET_AUTHORIZED_NETWORK_CQL, { + user_id: userId, + network_hash: networkHash, + }); + if (!row || !isFresh(row.last_used_at, now)) { + return false; + } + if (now - row.last_used_at.getTime() >= AUTHORIZED_NETWORK_REFRESH_MS) { + await upsertOne( + AuthorizedNetworks.insert({user_id: userId, network_hash: networkHash, last_used_at: new Date(now)}), + ); } return true; } @@ -113,12 +78,6 @@ export class IpAuthorizationRepository { return; } await this.rememberNetwork(user.id, networkKey, now); - const legacyIp = normalizeLegacyIp(ip); - await upsertOne(LegacyAuthorizedIps.insert({user_id: user.id, ip: legacyIp})); - const legacyTrustKey = getSameIpDecisionKey(legacyIp); - if (legacyTrustKey) { - await upsertOne(LegacyAuthorizedIpTrustKeys.insert({user_id: user.id, trust_key: legacyTrustKey})); - } } async createIpAuthorizationToken(userId: UserID, token: string, email: string): Promise { @@ -162,42 +121,6 @@ export class IpAuthorizationRepository { async deleteAllAuthorizedIps(userId: UserID): Promise { await deleteOneOrMany(AuthorizedNetworks.deletePartition({user_id: userId})); - await this.deleteLegacyAuthorizedIps(userId); - } - - private async deleteLegacyAuthorizedIps(userId: UserID): Promise { - const [ips, trustKeys] = await Promise.all([ - fetchMany(LIST_LEGACY_AUTHORIZED_IPS_CQL, {user_id: userId}), - fetchMany(LIST_LEGACY_AUTHORIZED_IP_TRUST_KEYS_CQL, {user_id: userId}), - ]); - for (const row of ips) { - await deleteOneOrMany(LegacyAuthorizedIps.deleteByPk({user_id: userId, ip: row.ip})); - } - for (const row of trustKeys) { - await deleteOneOrMany(LegacyAuthorizedIpTrustKeys.deleteByPk({user_id: userId, trust_key: row.trust_key})); - } - } - - private async checkLegacyIpAuthorized(userId: UserID, ip: string): Promise { - const legacyIp = normalizeLegacyIp(ip); - const trustKey = getSameIpDecisionKey(legacyIp); - if ( - trustKey && - (await fetchOne(CHECK_LEGACY_TRUST_KEY_CQL, { - user_id: userId, - trust_key: trustKey, - })) - ) { - return true; - } - if (await fetchOne(CHECK_LEGACY_AUTHORIZED_IP_CQL, {user_id: userId, ip: legacyIp})) { - return true; - } - if (!trustKey || trustKey === legacyIp) { - return false; - } - const rows = await fetchMany(LIST_LEGACY_AUTHORIZED_IPS_CQL, {user_id: userId}); - return rows.some((row) => getSameIpDecisionKey(row.ip) === trustKey); } private async rememberNetwork(userId: UserID, networkKey: string, now: number): Promise { diff --git a/tools/dev/cassandra_target_schema.json b/tools/dev/cassandra_target_schema.json index b107d34b4..d8d584103 100644 --- a/tools/dev/cassandra_target_schema.json +++ b/tools/dev/cassandra_target_schema.json @@ -1575,21 +1575,6 @@ "primary_key": "((user_id), session_id_hash)", "options": "" }, - { - "name": "authorized_ip_trust_keys_v1", - "columns": [ - { - "name": "user_id", - "type": "bigint" - }, - { - "name": "trust_key", - "type": "text" - } - ], - "primary_key": "((user_id), trust_key)", - "options": "" - }, { "name": "authorized_networks_v1", "columns": [ @@ -1609,21 +1594,6 @@ "primary_key": "((user_id), network_hash)", "options": "default_time_to_live = 7776000" }, - { - "name": "authorized_ips_v2", - "columns": [ - { - "name": "user_id", - "type": "bigint" - }, - { - "name": "ip", - "type": "text" - } - ], - "primary_key": "((user_id), ip)", - "options": "" - }, { "name": "banned_avatar_hashes", "columns": [