mirror of
https://github.com/fluxerapp/fluxer.git
synced 2026-12-04 16:13:04 -08:00
fix(api): read and write only hashed login networks (#4376)
This commit is contained in:
1 parent
366450988b
commit
8ee97a74c3
5 files changed
+16
-156
No files matched your search
@@ -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<IpAuthorizationTokenRow, 'token
|
||||
primaryKey: ['token_', 'user_id'],
|
||||
defaultTtlSeconds: seconds('30 minutes'),
|
||||
});
|
||||
export const LegacyAuthorizedIps = defineTable<LegacyAuthorizedIpRow, 'user_id' | 'ip'>({
|
||||
name: 'authorized_ips_v2',
|
||||
columns: LEGACY_AUTHORIZED_IP_COLUMNS,
|
||||
primaryKey: ['user_id', 'ip'],
|
||||
});
|
||||
export const LegacyAuthorizedIpTrustKeys = defineTable<LegacyAuthorizedIpTrustKeyRow, 'user_id' | 'trust_key'>({
|
||||
name: 'authorized_ip_trust_keys_v1',
|
||||
columns: LEGACY_AUTHORIZED_IP_TRUST_KEY_COLUMNS,
|
||||
primaryKey: ['user_id', 'trust_key'],
|
||||
});
|
||||
export const AuthorizedNetworks = defineTable<AuthorizedNetworkRow, 'user_id' | 'network_hash', 'user_id'>({
|
||||
name: 'authorized_networks_v1',
|
||||
columns: AUTHORIZED_NETWORK_COLUMNS,
|
||||
|
||||
@@ -79,8 +79,6 @@ export const CDC_TABLES: ReadonlySet<string> = 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<Record<string, Readonly<Record<string,
|
||||
client_ip: MODERATION_IP,
|
||||
session_id_hash: 'the partition key, sha256 of a random session token, never accepted as a credential',
|
||||
},
|
||||
authorized_ips_v2: {ip: MODERATION_IP},
|
||||
banned_avatar_hashes: {hash_short: IMAGE_HASH},
|
||||
banned_emails: {email_lower: MODERATION_EMAIL},
|
||||
banned_ips: {ip: MODERATION_IP},
|
||||
|
||||
@@ -90,16 +90,6 @@ export interface UserSsoIdentityRow {
|
||||
claimed_at: Date;
|
||||
}
|
||||
|
||||
export interface LegacyAuthorizedIpRow {
|
||||
user_id: UserID;
|
||||
ip: string;
|
||||
}
|
||||
|
||||
export interface LegacyAuthorizedIpTrustKeyRow {
|
||||
user_id: UserID;
|
||||
trust_key: string;
|
||||
}
|
||||
|
||||
export interface AuthorizedNetworkRow {
|
||||
user_id: UserID;
|
||||
network_hash: string;
|
||||
@@ -206,12 +196,6 @@ export const USER_SSO_IDENTITY_COLUMNS = [
|
||||
'user_id',
|
||||
'claimed_at',
|
||||
] as const satisfies ReadonlyArray<keyof UserSsoIdentityRow>;
|
||||
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
|
||||
>;
|
||||
|
||||
@@ -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<number> | 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<boolean> {
|
||||
const networkKey = getSameIpDecisionKey(ip);
|
||||
if (networkKey) {
|
||||
const networkHash = hashAuthorizedNetwork(userId, networkKey);
|
||||
const row = await fetchOne<AuthorizedNetworkRow>(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<AuthorizedNetworkRow>(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<void> {
|
||||
@@ -162,42 +121,6 @@ export class IpAuthorizationRepository {
|
||||
|
||||
async deleteAllAuthorizedIps(userId: UserID): Promise<void> {
|
||||
await deleteOneOrMany(AuthorizedNetworks.deletePartition({user_id: userId}));
|
||||
await this.deleteLegacyAuthorizedIps(userId);
|
||||
}
|
||||
|
||||
private async deleteLegacyAuthorizedIps(userId: UserID): Promise<void> {
|
||||
const [ips, trustKeys] = await Promise.all([
|
||||
fetchMany<LegacyAuthorizedIpRow>(LIST_LEGACY_AUTHORIZED_IPS_CQL, {user_id: userId}),
|
||||
fetchMany<LegacyAuthorizedIpTrustKeyRow>(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<boolean> {
|
||||
const legacyIp = normalizeLegacyIp(ip);
|
||||
const trustKey = getSameIpDecisionKey(legacyIp);
|
||||
if (
|
||||
trustKey &&
|
||||
(await fetchOne<LegacyAuthorizedIpTrustKeyRow>(CHECK_LEGACY_TRUST_KEY_CQL, {
|
||||
user_id: userId,
|
||||
trust_key: trustKey,
|
||||
}))
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
if (await fetchOne<LegacyAuthorizedIpRow>(CHECK_LEGACY_AUTHORIZED_IP_CQL, {user_id: userId, ip: legacyIp})) {
|
||||
return true;
|
||||
}
|
||||
if (!trustKey || trustKey === legacyIp) {
|
||||
return false;
|
||||
}
|
||||
const rows = await fetchMany<LegacyAuthorizedIpRow>(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<void> {
|
||||
|
||||
@@ -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": [
|
||||
|
||||
Reference in new issue
Block a user