A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
// SPDX-License-Identifier: AGPL-3.0-or-later
import type {ChannelID, GuildID, UserID} from '@app/api/BrandedTypes';
import type {ChannelRepository} from '@app/api/channel/ChannelRepository';
import type {
AuthenticatedChannel,
AuthenticatedThread,
ChannelAccess,
} from '@app/api/channel/services/AuthenticatedChannel';
import {DMPermissionValidator} from '@app/api/channel/services/DMPermissionValidator';
import {
assertPersonalNotesEnabled,
ensurePersonalNotesChannelExists,
isPersonalNotesChannelId,
} from '@app/api/channel/services/PersonalNotesChannelRepair';
import {assertThreadAllowed} from '@app/api/channel/services/thread/ThreadDenials';
import {
type ContentWarningChannelLike,
channelResponseToContentWarningView,
channelToContentWarningView,
computeEffectiveChannelNsfw,
guildResponseToContentWarningView,
} from '@app/api/channel/utils/EffectiveContentWarning';
import {SYSTEM_USER_ID} from '@app/api/constants/Core';
import {
THREAD_CHANNEL_TYPES,
THREAD_FEATURE_CHANNEL_TYPES,
THREAD_ONLY_CHANNEL_TYPES,
THREAD_PARENT_CHANNEL_TYPES,
type ThreadViewer,
viewerActive,
} from '@app/api/experiment/ChannelThreadsGate';
import {isGuildMemberTimedOut, mapGuildToGuildResponse} from '@app/api/guild/GuildModel';
import type {GuildRepository} from '@app/api/guild/repositories/GuildRepository';
import {createGuildMfaEnforcer} from '@app/api/guild/services/GuildMfaEnforcement';
import {guildVisitHistoryWindowTotal} from '@app/api/guild/visits/GuildVisitMetrics';
import {
chargeGuildVisitorBuckets,
computeVisitorChannelPermissionsFor,
GUILD_VISITOR_READ_BUCKETS,
recordGuildVisitHistoryRead,
resolveGuildVisitorAccess,
toVisitorChannelModel,
visitorHistoryCutoff,
} from '@app/api/guild/visits/GuildVisitorAccess';
import type {GuildChannelAuthContext, IGatewayService} from '@app/api/infrastructure/IGatewayService';
import type {Channel} from '@app/api/models/Channel';
import type {GuildMember} from '@app/api/models/GuildMember';
import type {User} from '@app/api/models/User';
import type {UserRepository} from '@app/api/user/repositories/UserRepository';
import {canUserAccessNsfwContent} from '@app/api/utils/AgeUtils';
import {startEarly} from '@app/api/utils/ConcurrencyUtils';
import {ChannelTypes, Permissions} from '@fluxer/constants/src/ChannelConstants';
import {GUILD_VISITOR_PERMISSIONS_MASK} from '@fluxer/constants/src/GuildVisitConstants';
import {
canViewThread,
isThreadModerator,
threadViewPermissions,
withImplicitThreadBits,
} from '@fluxer/constants/src/ThreadPermissionUtils';
import {CannotSendMessagesToUserError} from '@fluxer/errors/src/domains/channel/CannotSendMessagesToUserError';
import {UnknownChannelError} from '@fluxer/errors/src/domains/channel/UnknownChannelError';
import {AccessDeniedError} from '@fluxer/errors/src/domains/core/AccessDeniedError';
import {MissingPermissionsError} from '@fluxer/errors/src/domains/core/MissingPermissionsError';
import {UnknownGuildError} from '@fluxer/errors/src/domains/guild/UnknownGuildError';
import {NsfwContentRequiresAgeVerificationError} from '@fluxer/errors/src/domains/moderation/NsfwContentRequiresAgeVerificationError';
import {UnknownUserError} from '@fluxer/errors/src/domains/user/UnknownUserError';
import type {GuildMemberResponse} from '@fluxer/schema/src/domains/guild/GuildMemberSchemas';
import type {GuildResponse} from '@fluxer/schema/src/domains/guild/GuildResponseSchemas';
export interface ChannelAuthOptions {
errorOnMissingGuild: 'unknown_channel' | 'missing_permissions';
validateNsfw: boolean;
}
interface DMSendPermissionsByChannelParams {
channel: Channel;
userId: UserID;
}
interface DMSendPermissionsByChannelIdParams {
channelId: ChannelID;
userId: UserID;
}
type DMSendPermissionsParams = DMSendPermissionsByChannelParams | DMSendPermissionsByChannelIdParams;
export interface ThreadPermissionContext {
guild: GuildResponse;
member: GuildMemberResponse;
parentCategory: GuildChannelAuthContext['parentChannel'];
thread: AuthenticatedThread;
}
export abstract class BaseChannelAuthService {
protected abstract readonly options: ChannelAuthOptions;
protected dmPermissionValidator: DMPermissionValidator;
constructor(
protected channelRepository: ChannelRepository,
protected userRepository: UserRepository,
protected guildRepository: GuildRepository,
protected gatewayService: IGatewayService,
) {
this.dmPermissionValidator = new DMPermissionValidator({
userRepository: this.userRepository,
guildRepository: this.guildRepository,
});
}
async getChannelAuthenticated({
userId,
channelId,
viewer,
skipNsfwValidation,
access = 'member',
}: {
userId: UserID;
channelId: ChannelID;
viewer: ThreadViewer;
skipNsfwValidation?: boolean;
access?: ChannelAccess;
}): Promise<AuthenticatedChannel> {
if (isPersonalNotesChannelId({userId, channelId})) {
await assertPersonalNotesEnabled();
const channel = await ensurePersonalNotesChannelExists({
channelRepository: this.channelRepository.channelData,
userId,
});
return this.getRealPersonalNotesChannelAuth({channel, userId});
}
const channel = await this.channelRepository.channelData.findUnique(channelId);
if (!channel) throw new UnknownChannelError();
if (
THREAD_FEATURE_CHANNEL_TYPES.has(channel.type) &&
(channel.guildId === null || !viewerActive(viewer, channel.guildId))
) {
throw new UnknownChannelError();
}
if (!channel.guildId) {
const recipients = await this.userRepository.listUsers(Array.from(channel.recipientIds));
return this.getDMChannelAuth({channel, recipients, userId});
}
if (access !== 'visitor_read') {
return this.getGuildChannelAuth({channel, userId, skipNsfwValidation});
}
try {
return await this.getGuildChannelAuth({channel, userId, skipNsfwValidation});
} catch (memberError) {
const visitorAuth = await this.getVisitorChannelAuth({channel, userId});
if (visitorAuth) return visitorAuth;
throw memberError;
}
}
protected async getVisitorChannelAuth({
channel,
userId,
}: {
channel: Channel;
userId: UserID;
}): Promise<AuthenticatedChannel | null> {
const guildId = channel.guildId;
if (guildId === null) return null;
const access = await resolveGuildVisitorAccess({userId, guildId});
if (!access) return null;
const channelData = this.channelRepository.channelData;
let thread: AuthenticatedThread | undefined;
let permissions: bigint;
if (THREAD_CHANNEL_TYPES.has(channel.type)) {
const [state, parent] = await Promise.all([
this.channelRepository.threads.getState(channel.id),
channel.parentId ? channelData.findUnique(channel.parentId) : Promise.resolve(null),
]);
if (!state || !parent || parent.guildId !== guildId || !THREAD_PARENT_CHANNEL_TYPES.has(parent.type)) {
throw new UnknownChannelError();
}
const category = await this.findParentCategory(parent);
const parentPermissions = computeVisitorChannelPermissionsFor({
access,
channel: parent,
parent: category,
thread: {private: state.type === ChannelTypes.PRIVATE_THREAD},
});
permissions = threadViewPermissions(parentPermissions) & GUILD_VISITOR_PERMISSIONS_MASK;
thread = {
state,
parent: toVisitorChannelModel(parent),
member: null,
parentPermissions,
actor: {
permissions: parentPermissions,
isOwner: false,
timedOut: false,
thread: {
type: state.type,
archived: state.archived,
locked: state.locked,
invitable: state.invitable ?? true,
},
isThreadOwner: false,
isMember: false,
},
isModerator: false,
enforceMfa: () => {},
};
} else {
permissions = computeVisitorChannelPermissionsFor({
access,
channel,
parent: await this.findParentCategory(channel),
});
}
if ((permissions & Permissions.VIEW_CHANNEL) !== Permissions.VIEW_CHANNEL) throw new UnknownChannelError();
await chargeGuildVisitorBuckets({buckets: GUILD_VISITOR_READ_BUCKETS, userId, guildId, tier: access.tier});
await recordGuildVisitHistoryRead({userId, guildId, tier: access.tier});
const guild = mapGuildToGuildResponse(access.view.guild);
const canReadHistory = (permissions & Permissions.READ_MESSAGE_HISTORY) === Permissions.READ_MESSAGE_HISTORY;
const windowed = access.historyCutoff !== null;
const effectivePermissions = windowed ? permissions & ~Permissions.READ_MESSAGE_HISTORY : permissions;
const cutoff = visitorHistoryCutoff({
canReadHistory,
guildCutoff: guild.message_history_cutoff ?? null,
windowCutoff: access.historyCutoff,
});
if (windowed) guildVisitHistoryWindowTotal.inc();
const hasPermission = async (permission: bigint): Promise<boolean> =>
(effectivePermissions & permission) === permission;
return {
channel: toVisitorChannelModel(channel),
guild: {...guild, message_history_cutoff: cutoff},
member: null,
hasPermission,
checkPermission: async (permission: bigint) => {
if (!(await hasPermission(permission))) throw new MissingPermissionsError();
},
thread,
visitor: {
permissions: effectivePermissions,
tier: access.tier,
adultContentAllowed: access.adultContentAllowed,
historyCutoff: access.historyCutoff,
},
};
}
private async findParentCategory(channel: Channel): Promise<Channel | null> {
if (!channel.parentId || channel.type === ChannelTypes.GUILD_CATEGORY) return null;
return this.channelRepository.channelData.findUnique(channel.parentId);
}
protected async getRealPersonalNotesChannelAuth({
channel,
userId,
}: {
channel: Channel;
userId: UserID;
}): Promise<AuthenticatedChannel> {
if (!isPersonalNotesChannelId({userId, channelId: channel.id})) {
throw new UnknownChannelError();
}
if (channel.type !== ChannelTypes.DM_PERSONAL_NOTES) {
throw new UnknownChannelError();
}
return {
channel,
guild: null,
member: null,
hasPermission: async () => true,
checkPermission: async () => {},
};
}
protected async getDMChannelAuth({
channel,
recipients,
userId,
}: {
channel: Channel;
recipients: Array<User>;
userId: UserID;
}): Promise<AuthenticatedChannel> {
if (userId === SYSTEM_USER_ID) {
return {
channel,
guild: null,
member: null,
hasPermission: async () => true,
checkPermission: async () => {},
};
}
if (channel.type === ChannelTypes.DM && channel.ownerId != null && channel.ownerId !== userId) {
throw new UnknownChannelError();
}
const isRecipient = recipients.some((recipient) => recipient.id === userId);
if (!isRecipient) throw new UnknownChannelError();
return {
channel,
guild: null,
member: null,
hasPermission: async () => true,
checkPermission: async () => {},
};
}
async validateDMSendPermissions(params: DMSendPermissionsParams): Promise<void> {
const {userId} = params;
const channel =
'channel' in params ? params.channel : await this.channelRepository.channelData.findUnique(params.channelId);
if (!channel) throw new UnknownChannelError();
if (channel.type === ChannelTypes.GROUP_DM || channel.type === ChannelTypes.DM_PERSONAL_NOTES) {
return;
}
const recipientIds = Array.from(channel.recipientIds).filter((id) => id !== userId);
if (recipientIds.length !== 1) {
throw new CannotSendMessagesToUserError();
}
await this.dmPermissionValidator.validate({senderId: userId, recipientId: recipientIds[0]});
}
protected async getGuildChannelAuth({
channel,
userId,
skipNsfwValidation,
}: {
channel: Channel;
userId: UserID;
skipNsfwValidation?: boolean;
}): Promise<AuthenticatedChannel> {
if (THREAD_CHANNEL_TYPES.has(channel.type)) {
return this.getThreadChannelAuth({channel, userId, skipNsfwValidation});
}
const guildId = channel.guildId!;
const channelPermissionsTask = startEarly(
this.gatewayService.getUserPermissions({
guildId,
userId,
channelId: channel.id,
}),
);
const [authContextResult, guildMemberResult] = await Promise.all([
this.fetchGuildAuthContextOrThrow({guildId, userId, channelId: this.parentLookupChannelId(channel)}),
this.fetchGuildMemberOrThrow({guildId, userId}),
]);
if (!authContextResult) {
this.throwGuildAccessError();
}
const guildDataResult = authContextResult.guild;
if (!guildMemberResult.success || !guildMemberResult.memberData) {
this.throwGuildAccessError();
}
const member = await this.fillMissingMemberTimeout({
guildId,
userId,
memberData: guildMemberResult.memberData!,
});
const enforceGuildMfa = await createGuildMfaEnforcer({
userRepository: this.userRepository,
guildData: guildDataResult,
userId,
});
const channelPermissions = await channelPermissionsTask;
const hasPermission = async (permission: bigint): Promise<boolean> => {
const allowed = (channelPermissions & permission) === permission;
if (allowed) enforceGuildMfa(permission);
return allowed;
};
const checkPermission = async (permission: bigint): Promise<void> => {
const allowed = await hasPermission(permission);
if (!allowed) throw new MissingPermissionsError();
};
await checkPermission(Permissions.VIEW_CHANNEL);
const parentCategory = await this.getParentCategoryContentWarningView({
channel,
parentChannel: authContextResult.parentChannel,
});
const requiresAgeVerification = computeEffectiveChannelNsfw(
channelToContentWarningView(channel),
parentCategory,
guildResponseToContentWarningView(guildDataResult),
);
if (
this.options.validateNsfw &&
!skipNsfwValidation &&
(channel.type === ChannelTypes.GUILD_TEXT ||
channel.type === ChannelTypes.GUILD_ANNOUNCEMENT ||
channel.type === ChannelTypes.GUILD_VOICE ||
channel.type === ChannelTypes.GUILD_LINK ||
THREAD_ONLY_CHANNEL_TYPES.has(channel.type)) &&
requiresAgeVerification
) {
const user = await this.userRepository.findUnique(userId);
if (!user) throw new UnknownUserError();
if (!canUserAccessNsfwContent(user)) {
throw new NsfwContentRequiresAgeVerificationError();
}
}
return {
channel,
guild: guildDataResult,
member,
hasPermission,
checkPermission,
};
}
private async resolveThreadPermissionContext({
thread,
userId,
}: {
thread: Channel;
userId: UserID;
}): Promise<ThreadPermissionContext> {
const guildId = thread.guildId;
const parentId = thread.parentId;
if (guildId === null || parentId === null) throw new UnknownChannelError();
const [parent, state, threadMember, guildMemberResult] = await Promise.all([
this.channelRepository.channelData.findUnique(parentId),
this.channelRepository.threads.getState(thread.id),
this.channelRepository.threads.getMember(thread.id, userId),
this.fetchGuildMemberOrThrow({guildId, userId}),
]);
if (!parent || !state || parent.guildId !== guildId || !THREAD_PARENT_CHANNEL_TYPES.has(parent.type)) {
throw new UnknownChannelError();
}
if (!guildMemberResult.success || !guildMemberResult.memberData) {
this.throwGuildAccessError();
}
const [authContextResult, parentPermissions] = await Promise.all([
this.fetchGuildAuthContextOrThrow({guildId, userId, channelId: this.parentLookupChannelId(parent)}),
this.gatewayService.getUserPermissions({guildId, userId, channelId: parent.id}),
]);
if (!authContextResult) {
this.throwGuildAccessError();
}
const member = await this.fillMissingMemberTimeout({guildId, userId, memberData: guildMemberResult.memberData});
const guild = authContextResult.guild;
const enforceMfa = await createGuildMfaEnforcer({userRepository: this.userRepository, guildData: guild, userId});
const isOwner = guild.owner_id === userId.toString();
const timedOut = isGuildMemberTimedOut(member);
const actor = {
permissions: parentPermissions,
isOwner,
timedOut,
thread: {
type: state.type,
archived: state.archived,
locked: state.locked,
invitable: state.invitable ?? true,
},
isThreadOwner: thread.ownerId === userId,
isMember: threadMember !== null,
};
return {
guild,
member,
parentCategory: authContextResult.parentChannel,
thread: {
state,
parent,
member: threadMember,
parentPermissions,
actor,
isModerator: isThreadModerator(withImplicitThreadBits(parentPermissions), {isOwner, timedOut}),
enforceMfa,
},
};
}
protected async getThreadChannelAuth({
channel,
userId,
skipNsfwValidation,
}: {
channel: Channel;
userId: UserID;
skipNsfwValidation?: boolean;
}): Promise<AuthenticatedChannel> {
const context = await this.resolveThreadPermissionContext({thread: channel, userId});
const {guild, member, thread} = context;
assertThreadAllowed(canViewThread(thread.actor));
const permissions = threadViewPermissions(thread.parentPermissions);
const hasPermission = async (permission: bigint): Promise<boolean> => {
const allowed = (permissions & permission) === permission;
if (allowed) thread.enforceMfa(permission);
return allowed;
};
const checkPermission = async (permission: bigint): Promise<void> => {
if (!(await hasPermission(permission))) throw new MissingPermissionsError();
};
if (this.options.validateNsfw && !skipNsfwValidation && THREAD_PARENT_CHANNEL_TYPES.has(thread.parent.type)) {
const parentCategory = await this.getParentCategoryContentWarningView({
channel: thread.parent,
parentChannel: context.parentCategory,
});
const requiresAgeVerification = computeEffectiveChannelNsfw(
channelToContentWarningView(thread.parent),
parentCategory,
guildResponseToContentWarningView(guild),
);
if (requiresAgeVerification) {
const user = await this.userRepository.findUnique(userId);
if (!user) throw new UnknownUserError();
if (!canUserAccessNsfwContent(user)) {
throw new NsfwContentRequiresAgeVerificationError();
}
}
}
return {channel, guild, member, hasPermission, checkPermission, thread};
}
private parentLookupChannelId(channel: Channel): ChannelID | undefined {
if (!channel.parentId || channel.type === ChannelTypes.GUILD_CATEGORY) {
return undefined;
}
return channel.parentId;
}
private async getParentCategoryContentWarningView({
channel,
parentChannel,
}: {
channel: Channel;
parentChannel: GuildChannelAuthContext['parentChannel'];
}): Promise<ContentWarningChannelLike | null> {
if (!channel.parentId || channel.type === ChannelTypes.GUILD_CATEGORY) {
return null;
}
if (parentChannel) {
return channelResponseToContentWarningView(parentChannel);
}
const parentCategory = await this.channelRepository.channelData.findUnique(channel.parentId);
return parentCategory ? channelToContentWarningView(parentCategory) : null;
}
protected throwGuildAccessError(): never {
if (this.options.errorOnMissingGuild === 'missing_permissions') {
throw new MissingPermissionsError();
}
throw new UnknownChannelError();
}
private async fetchGuildAuthContextOrThrow(params: {
guildId: GuildID;
userId: UserID;
channelId?: ChannelID;
}): Promise<GuildChannelAuthContext | null> {
const {guildId, userId, channelId} = params;
try {
return await this.gatewayService.getGuildAuthContext({guildId, userId, channelId});
} catch (error) {
await this.handleGuildAccessError(error, guildId);
return null;
}
}
private async fetchGuildMemberOrThrow(params: {guildId: GuildID; userId: UserID}): Promise<{
success: boolean;
memberData?: GuildMemberResponse;
}> {
try {
return await this.gatewayService.getGuildMember(params);
} catch (error) {
await this.handleGuildAccessError(error, params.guildId);
throw error;
}
}
private async handleGuildAccessError(error: unknown, guildId: GuildID): Promise<void> {
if (error instanceof UnknownGuildError) {
if (await this.guildExists(guildId)) {
throw new AccessDeniedError();
}
throw new UnknownGuildError();
}
throw error;
}
private async guildExists(guildId: GuildID): Promise<boolean> {
const guild = await this.guildRepository.findUnique(guildId);
return guild !== null;
}
private async fillMissingMemberTimeout({
guildId,
userId,
memberData,
}: {
guildId: GuildID;
userId: UserID;
memberData: GuildMemberResponse;
}): Promise<GuildMemberResponse> {
if (memberData.communication_disabled_until !== undefined) {
return memberData;
}
const persistedMember = await this.guildRepository.getMember(guildId, userId);
if (!persistedMember) {
this.throwGuildAccessError();
}
return {
...memberData,
communication_disabled_until: this.formatCommunicationDisabledUntil(persistedMember),
};
}
private formatCommunicationDisabledUntil(member: GuildMember): string | null {
return member.communicationDisabledUntil?.toISOString() ?? null;
}
}