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 crypto from 'node:crypto';
import fs from 'node:fs';
import {type ChannelID, createAttachmentID, type UserID} from '@app/api/BrandedTypes';
import {Config} from '@app/api/Config';
import type {AttachmentToProcess} from '@app/api/channel/AttachmentDTOs';
import type {AttachmentUploadTraceRepository} from '@app/api/channel/repositories/message/AttachmentUploadTraceRepository';
import {makeAttachmentCdnKey, validateAttachmentIds} from '@app/api/channel/services/message/MessageHelpers';
import type {MessageAttachment} from '@app/api/database/types/MessageTypes';
import {contentModerationService, type ModerationContext} from '@app/api/infrastructure/ContentModerationService';
import type {
IMediaService,
MediaProxyMetadataResponse,
MediaProxyNsfwMode,
} from '@app/api/infrastructure/IMediaService';
import type {ISnowflakeService} from '@app/api/infrastructure/ISnowflakeService';
import type {IStorageService} from '@app/api/infrastructure/IStorageService';
import {hashFileSha256} from '@app/api/infrastructure/StorageObjectHelpers';
import {Logger} from '@app/api/Logger';
import type {Channel} from '@app/api/models/Channel';
import type {Message} from '@app/api/models/Message';
import {mapWithConcurrency} from '@app/api/utils/ConcurrencyUtils';
import {MessageAttachmentFlags} from '@fluxer/constants/src/ChannelConstants';
import {MAX_MEDIA_DURATION_SECONDS} from '@fluxer/constants/src/LimitConstants';
import {ValidationErrorCodes} from '@fluxer/constants/src/ValidationErrorCodes';
import {ContentBlockedError} from '@fluxer/errors/src/domains/content/ContentBlockedError';
import {InputValidationError} from '@fluxer/errors/src/domains/core/InputValidationError';
import {ExplicitContentCannotBeSentError} from '@fluxer/errors/src/domains/moderation/ExplicitContentCannotBeSentError';
import type {GuildMemberResponse} from '@fluxer/schema/src/domains/guild/GuildMemberSchemas';
import type {GuildResponse} from '@fluxer/schema/src/domains/guild/GuildResponseSchemas';
import {getContentTypeFromFilename, isSupportedMediaContentType} from '@pkgs/mime_utils/src/ContentTypeUtils';
import type {IVirusScanService} from '@pkgs/virus_scan/src/IVirusScanService';
import {temporaryFile} from 'tempy';
const ATTACHMENT_PROCESSING_CONCURRENCY = 2;
const METADATA_PROBE_DEGRADED_CONTEXT = 'message_attachment';
interface ComputeAttachmentsParams {
message: Message;
attachments: Array<AttachmentToProcess>;
uploadUserId: UserID;
uploadChannelId?: ChannelID;
channel?: Channel;
guild?: GuildResponse | null;
member?: GuildMemberResponse | null;
nsfwMode: MediaProxyNsfwMode;
}
interface ComputedAttachments {
attachments: Array<MessageAttachment>;
hasVirusDetected: boolean;
}
interface ProcessAttachmentParams {
message: Message;
attachment: AttachmentToProcess;
index: number;
uploadUserId: UserID;
uploadChannelId?: ChannelID;
channel?: Channel;
guild?: GuildResponse | null;
member?: GuildMemberResponse | null;
nsfwMode: MediaProxyNsfwMode;
snapshotKeys: Array<string>;
}
interface AttachmentCopyOperation {
sourceBucket: string;
sourceKey: string;
destinationBucket: string;
destinationKey: string;
newContentType: string;
}
interface ProcessedAttachment {
attachment: MessageAttachment;
uploadKey: string;
copyOperation: AttachmentCopyOperation;
hasVirusDetected: boolean;
applyFinalObjectMetadata: boolean;
sniffedContentType: string | null;
}
interface StoredAttachmentObject {
contentType: string;
contentLength: number;
contentHash: string;
width?: number;
height?: number;
}
export class AttachmentProcessingService {
constructor(
private storageService: IStorageService,
private attachmentUploadTraceRepository: AttachmentUploadTraceRepository,
private mediaService: IMediaService,
private virusScanService: IVirusScanService,
private snowflakeService: ISnowflakeService,
) {}
async computeAttachments(params: ComputeAttachmentsParams): Promise<ComputedAttachments> {
validateAttachmentIds(params.attachments.map((a) => ({id: BigInt(a.id)})));
const snapshotKeys: Array<string> = [];
try {
return await this.processAndStoreAttachments({...params, snapshotKeys});
} finally {
for (const key of snapshotKeys) {
this.deleteUploadObject(Config.s3.buckets.uploads, key);
}
}
}
private async processAndStoreAttachments(
params: ComputeAttachmentsParams & {snapshotKeys: Array<string>},
): Promise<ComputedAttachments> {
const results = await mapWithConcurrency(
params.attachments,
ATTACHMENT_PROCESSING_CONCURRENCY,
(attachment, index) =>
this.processAttachment({
message: params.message,
attachment,
index,
uploadUserId: params.uploadUserId,
uploadChannelId: params.uploadChannelId,
channel: params.channel,
guild: params.guild,
member: params.member,
nsfwMode: params.nsfwMode,
snapshotKeys: params.snapshotKeys,
}),
);
const hasVirusDetected = results.some((result) => result.hasVirusDetected);
if (hasVirusDetected) {
for (const result of results) {
this.deleteUploadObject(Config.s3.buckets.uploads, result.uploadKey);
}
return {attachments: [], hasVirusDetected: true};
}
let copyResults: Array<StoredAttachmentObject | null>;
try {
copyResults = await mapWithConcurrency(results, ATTACHMENT_PROCESSING_CONCURRENCY, (result) =>
this.storeAttachmentObject(result.copyOperation, params.message),
);
} catch (error) {
if (error instanceof ContentBlockedError) {
for (const result of results) {
this.deleteUploadObject(Config.s3.buckets.uploads, result.uploadKey);
this.deleteUploadObject(result.copyOperation.destinationBucket, result.copyOperation.destinationKey);
}
}
throw error;
}
const bindingResults = await Promise.all(
results.map(async (result, index) => ({
index,
result,
bound: await this.attachmentUploadTraceRepository.bindAttachment(
result.uploadKey,
result.attachment.attachment_id,
),
})),
);
for (const result of results) {
this.deleteUploadObject(Config.s3.buckets.uploads, result.uploadKey);
}
const unboundResult = bindingResults.find(({bound}) => bound === null);
if (unboundResult) {
for (const result of results) {
this.deleteUploadObject(result.copyOperation.destinationBucket, result.copyOperation.destinationKey);
}
throw InputValidationError.fromCode(
`attachments.${unboundResult.index}.upload_filename`,
ValidationErrorCodes.UPLOADED_ATTACHMENT_NOT_FOUND,
{filename: unboundResult.result.attachment.filename},
);
}
const processedAttachments: Array<MessageAttachment> = results.map((result, index) => {
const finalObject = copyResults[index];
if (result.applyFinalObjectMetadata && finalObject) {
return {
...result.attachment,
content_type: finalObject.contentType,
content_hash: finalObject.contentHash,
size: BigInt(finalObject.contentLength),
width: finalObject.width ?? result.attachment.width,
height: finalObject.height ?? result.attachment.height,
};
}
return result.attachment;
});
return {attachments: processedAttachments, hasVirusDetected: false};
}
private async processAttachment(params: ProcessAttachmentParams): Promise<ProcessedAttachment> {
const {message, attachment, index, nsfwMode} = params;
const pendingUpload = await this.attachmentUploadTraceRepository.getPendingUpload({
uploadKey: attachment.upload_filename,
userId: params.uploadUserId,
channelId: params.uploadChannelId ?? message.channelId,
});
if (!pendingUpload) {
throw InputValidationError.fromCode(
`attachments.${index}.upload_filename`,
ValidationErrorCodes.UPLOADED_ATTACHMENT_NOT_FOUND,
{filename: attachment.filename},
);
}
const uploadKey = attachment.upload_filename;
const sourceKey =
pendingUpload.upload_mode === 'presigned_singlepart'
? await this.snapshotUploadObject(uploadKey, params.snapshotKeys)
: uploadKey;
const uploadedFile = sourceKey
? await this.storageService.getObjectMetadata(Config.s3.buckets.uploads, sourceKey)
: null;
if (!sourceKey || !uploadedFile) {
throw InputValidationError.fromCode(`attachments.${index}.upload_filename`, ValidationErrorCodes.FILE_NOT_FOUND);
}
const attachmentId = createAttachmentID(await this.snowflakeService.generate());
const cdnKey = makeAttachmentCdnKey(message.channelId, attachmentId, attachment.filename);
let contentType = pendingUpload.content_type ?? getContentTypeFromFilename(attachment.filename);
let size = BigInt(uploadedFile.contentLength);
const clientFlags =
(attachment.flags ?? 0) & (MessageAttachmentFlags.IS_SPOILER | MessageAttachmentFlags.CONTAINS_EXPLICIT_MEDIA);
let flags = clientFlags;
let width: number | null = null;
let height: number | null = null;
let placeholder: string | null = null;
let duration: number | null = null;
let hasVirusDetected = false;
let nsfw: boolean | null = null;
let contentHash: string | null = null;
let applyFinalObjectMetadata = false;
const clientDuration: number | null = attachment.duration ?? null;
const waveform: string | null = attachment.waveform ?? null;
const sniffedContentType = isSupportedMediaContentType(contentType)
? null
: await this.sniffAttachmentMediaType({
index,
uploadFilename: sourceKey,
filename: attachment.filename,
});
if (sniffedContentType !== null) {
Logger.warn(
{
surface: 'message_attachment',
userId: params.uploadUserId.toString(),
guildId: params.guild?.id ?? null,
channelId: message.channelId.toString(),
messageId: message.id.toString(),
attachmentId: attachmentId.toString(),
uploadKey: attachment.upload_filename,
filename: attachment.filename,
filenameContentType: contentType,
sniffedContentType,
},
'content_moderation.attachment_type_mismatch',
);
}
const isMedia = isSupportedMediaContentType(contentType) || sniffedContentType !== null;
let metadata: MediaProxyMetadataResponse | null = null;
if (isMedia) {
metadata = await this.getAttachmentMediaMetadata({
index,
uploadFilename: sourceKey,
filename: attachment.filename,
nsfwMode,
sizeBytes: uploadedFile.contentLength,
});
if (metadata) {
applyFinalObjectMetadata = true;
contentType = metadata.content_type;
contentHash = metadata.content_hash;
size = BigInt(metadata.size);
placeholder = metadata.placeholder ?? null;
duration =
metadata.duration && metadata.duration > 0 ? Math.min(metadata.duration, MAX_MEDIA_DURATION_SECONDS) : null;
width = metadata.width ?? null;
height = metadata.height ?? null;
if (metadata.animated) {
flags |= MessageAttachmentFlags.IS_ANIMATED;
}
if (metadata.nsfw) {
flags |= MessageAttachmentFlags.CONTAINS_EXPLICIT_MEDIA;
}
nsfw = metadata.nsfw;
}
}
const needsLocalDownload = this.virusScanService.enabled || metadata == null;
let sourceLocalPath: string | null = null;
try {
if (needsLocalDownload) {
sourceLocalPath = temporaryFile();
await this.storageService.writeObjectToDisk(Config.s3.buckets.uploads, sourceKey, sourceLocalPath);
}
if (metadata) {
await this.scanContentModerationHash({
contentHash: metadata.content_hash,
bucket: Config.s3.buckets.uploads,
key: uploadKey,
message,
});
} else {
await this.scanContentModerationWithLocalFile({
localPath: sourceLocalPath,
bucket: Config.s3.buckets.uploads,
key: uploadKey,
message,
});
}
const scanResult = await this.scanMalware(sourceLocalPath);
const processedAttachment = {
attachment_id: attachmentId,
filename: attachment.filename,
size,
title: attachment.title ?? null,
description: attachment.description ?? null,
height,
width,
content_type: contentType,
content_hash: contentHash,
placeholder,
flags,
duration: duration ?? clientDuration,
nsfw,
waveform,
};
const copyOperation = {
sourceBucket: Config.s3.buckets.uploads,
sourceKey,
destinationBucket: Config.s3.buckets.cdn,
destinationKey: cdnKey,
newContentType: contentType,
};
if (scanResult.isVirusDetected) {
hasVirusDetected = true;
await this.storageService.deleteObject(Config.s3.buckets.uploads, uploadKey);
return {
attachment: processedAttachment,
uploadKey,
copyOperation,
hasVirusDetected,
applyFinalObjectMetadata,
sniffedContentType,
};
}
const isAudio = contentType.startsWith('audio/');
if (waveform && !isAudio) {
throw InputValidationError.fromCode(
`attachments.${index}.upload_filename`,
ValidationErrorCodes.VOICE_MESSAGES_ATTACHMENT_MUST_BE_AUDIO,
);
}
return {
attachment: processedAttachment,
uploadKey,
copyOperation,
hasVirusDetected,
applyFinalObjectMetadata,
sniffedContentType,
};
} finally {
if (sourceLocalPath) {
await fs.promises.unlink(sourceLocalPath).catch(() => undefined);
}
}
}
private async snapshotUploadObject(uploadKey: string, snapshotKeys: Array<string>): Promise<string | null> {
const bucket = Config.s3.buckets.uploads;
if (!(await this.storageService.getObjectMetadata(bucket, uploadKey))) {
return null;
}
const snapshotKey = crypto.randomUUID();
snapshotKeys.push(snapshotKey);
await this.storageService.copyObject({
sourceBucket: bucket,
sourceKey: uploadKey,
destinationBucket: bucket,
destinationKey: snapshotKey,
});
return snapshotKey;
}
private async storeAttachmentObject(
operation: AttachmentCopyOperation,
message: Message,
): Promise<StoredAttachmentObject | null> {
const {sourceBucket, sourceKey, destinationBucket, destinationKey, newContentType} = operation;
const copyOriginal = () =>
this.storageService.copyObject({sourceBucket, sourceKey, destinationBucket, destinationKey, newContentType});
if (!isSupportedMediaContentType(newContentType)) {
await copyOriginal();
return null;
}
const strippedPath = temporaryFile();
try {
const stripped = await this.mediaService.stripObjectMetadata(
{bucket: sourceBucket, key: sourceKey},
newContentType,
strippedPath,
);
if (!stripped) {
Logger.error(
{contentType: newContentType, sourceBucket, sourceKey},
'Failed to strip attachment media metadata',
);
await copyOriginal();
return null;
}
const [contentHash, {size: contentLength}] = await Promise.all([
hashFileSha256(strippedPath),
fs.promises.stat(strippedPath),
]);
contentModerationService.scanSha256(contentHash, this.moderationContext(message));
await this.storageService.uploadObjectFromFile({
bucket: destinationBucket,
key: destinationKey,
filePath: strippedPath,
contentType: stripped.contentType,
contentLength,
});
return {...stripped, contentLength, contentHash};
} finally {
await fs.promises.unlink(strippedPath).catch(() => undefined);
}
}
private async sniffAttachmentMediaType(params: {
index: number;
uploadFilename: string;
filename: string;
}): Promise<string | null> {
const sniff = await this.mediaService.sniffUpload(params.uploadFilename);
if (sniff) {
return sniff.content_type;
}
Logger.warn(
{
context: METADATA_PROBE_DEGRADED_CONTEXT,
attachmentIndex: params.index,
uploadFilename: params.uploadFilename,
filename: params.filename,
},
'Attachment content sniff unavailable, storing attachment with its filename type',
);
return null;
}
async screenStoredAttachment(params: {
attachment: MessageAttachment;
message: Message;
nsfwMode: MediaProxyNsfwMode;
}): Promise<MessageAttachment> {
const {attachment, message, nsfwMode} = params;
const bucket = Config.s3.buckets.cdn;
const key = makeAttachmentCdnKey(message.channelId, attachment.attachment_id, attachment.filename);
if (attachment.content_hash) {
await this.scanContentModerationHash({contentHash: attachment.content_hash, bucket, key, message});
} else {
await this.scanContentModerationWithLocalFile({localPath: null, bucket, key, message});
}
return this.classifyStoredMedia({attachment, bucket, key, nsfwMode});
}
async classifyStoredMedia(params: {
attachment: MessageAttachment;
bucket: string;
key: string;
nsfwMode: MediaProxyNsfwMode;
}): Promise<MessageAttachment> {
const {attachment, bucket, key, nsfwMode} = params;
if (!(await this.isExplicitStoredMedia({contentType: attachment.content_type, bucket, key, nsfwMode}))) {
return attachment;
}
return {
...attachment,
nsfw: true,
flags: attachment.flags | MessageAttachmentFlags.CONTAINS_EXPLICIT_MEDIA,
};
}
async isExplicitStoredMedia(params: {
contentType: string | null;
bucket: string;
key: string;
nsfwMode: MediaProxyNsfwMode;
}): Promise<boolean> {
const {contentType, bucket, key, nsfwMode} = params;
if (!contentType || !isSupportedMediaContentType(contentType)) {
return false;
}
let metadata: MediaProxyMetadataResponse | null;
try {
metadata = await this.mediaService.getMetadata({type: 's3', bucket, key, nsfw: nsfwMode});
} catch (error) {
if (error instanceof ExplicitContentCannotBeSentError) {
await this.storageService.deleteObject(bucket, key);
throw error;
}
Logger.warn({error, context: METADATA_PROBE_DEGRADED_CONTEXT, key}, 'Stored media probe failed');
return false;
}
return metadata?.nsfw === true;
}
private async getAttachmentMediaMetadata(params: {
index: number;
uploadFilename: string;
filename: string;
nsfwMode: MediaProxyNsfwMode;
sizeBytes: number;
}): Promise<MediaProxyMetadataResponse | null> {
try {
const metadata = await this.mediaService.getMetadata(
{
type: 'upload',
upload_filename: params.uploadFilename,
filename: params.filename,
nsfw: params.nsfwMode,
},
{sizeBytes: params.sizeBytes},
);
if (metadata) {
return metadata;
}
Logger.warn(
{
context: METADATA_PROBE_DEGRADED_CONTEXT,
attachmentIndex: params.index,
uploadFilename: params.uploadFilename,
filename: params.filename,
},
'Attachment media metadata unavailable; storing attachment as a plain file',
);
return null;
} catch (error) {
if (error instanceof ExplicitContentCannotBeSentError) {
throw error;
}
Logger.warn(
{
error,
context: METADATA_PROBE_DEGRADED_CONTEXT,
attachmentIndex: params.index,
uploadFilename: params.uploadFilename,
filename: params.filename,
},
'Attachment media metadata probe failed; storing attachment as a plain file',
);
return null;
}
}
private async scanContentModerationWithLocalFile(params: {
localPath: string | null;
bucket: string;
key: string;
message: Message;
}): Promise<void> {
const sha = params.localPath
? await hashFileSha256(params.localPath)
: await this.storageService.computeObjectSha256(params.bucket, params.key);
await this.scanContentModerationHash({
contentHash: sha,
bucket: params.bucket,
key: params.key,
message: params.message,
});
}
private async scanContentModerationHash(params: {
contentHash: string;
bucket: string;
key: string;
message: Message;
}): Promise<void> {
const {bucket, key, message} = params;
try {
contentModerationService.scanSha256(params.contentHash, this.moderationContext(message));
} catch (error) {
if (error instanceof ContentBlockedError) {
await this.storageService.deleteObject(bucket, key);
}
throw error;
}
}
private moderationContext(message: Message): ModerationContext {
return {
userId: message.authorId ?? null,
guildId: null,
channelId: message.channelId,
messageId: message.id,
surface: 'message_attachment',
};
}
private async scanMalware(localPath: string | null): Promise<{
isVirusDetected: boolean;
}> {
if (!this.virusScanService.enabled || !localPath) {
return {isVirusDetected: false};
}
const scanResult = await this.virusScanService.scanFile(localPath);
return {isVirusDetected: !scanResult.isClean};
}
private deleteUploadObject(bucket: string, key: string): void {
void this.storageService.deleteObject(bucket, key).catch((error) => {
Logger.warn({bucket, key, error}, 'Failed to delete temporary upload object');
});
}
}