A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
fix(api): skip every base64 upload field in the content filter (#3863)
Hampusc779783271bd
diff --git a/fluxer_api/src/api/Validator.ts b/fluxer_api/src/api/Validator.tsindex 971986fe1..899c143ff 100644--- a/fluxer_api/src/api/Validator.ts+++ b/fluxer_api/src/api/Validator.ts@@ -1,5 +1,6 @@// SPDX-License-Identifier: AGPL-3.0-or-later+import {registerRequestBodySchema} from '@app/api/middleware/RequestBodyBinaryFields';import {requireRequestJsonBody} from '@app/api/utils/RequestJsonBody';import {initializeFluxerErrorMap} from '@app/api/ZodErrorMap';import type {ValidationErrorCode} from '@fluxer/constants/src/ValidationErrorCodes';@@ -169,6 +170,9 @@ export const Validator = <): MiddlewareHandler<E, P, V> => {const options: ValidatorOptions<T, E, P, Target, V> =typeof hookOrOptions === 'function' ? {post: hookOrOptions} : (hookOrOptions ?? {});+ if (target === 'json') {+ registerRequestBodySchema(schema);+ }return async (c, next): Promise<Response | undefined> => {let value: unknown;switch (target) {diff --git a/fluxer_api/src/api/middleware/ContentFilterMiddleware.ts b/fluxer_api/src/api/middleware/ContentFilterMiddleware.tsindex e5d8b8be7..4c3b59ed7 100644--- a/fluxer_api/src/api/middleware/ContentFilterMiddleware.ts+++ b/fluxer_api/src/api/middleware/ContentFilterMiddleware.ts@@ -2,17 +2,17 @@import {Logger} from '@app/api/Logger';import {phraseBlocklistCache} from '@app/api/middleware/PhraseBlocklistCache';+import {binaryRequestBodyPaths} from '@app/api/middleware/RequestBodyBinaryFields';import {isMultipartUploadRequest} from '@app/api/middleware/RequestBodyLimitMiddleware';import {urlBlocklistCache} from '@app/api/middleware/UrlBlocklistCache';import {readRequestJsonBody} from '@app/api/utils/RequestJsonBody';import {ContentBlockedError} from '@fluxer/errors/src/domains/content/ContentBlockedError';+import {arrayItemBodyFieldPath, childBodyFieldPath} from '@fluxer/schema/src/primitives/Base64BinaryFields';import {createMiddleware} from 'hono/factory';const SKIP_FIELDS = new Set(['acls','allow',- 'avatar',- 'banner','challenge','channels','content_type',@@ -21,13 +21,10 @@ const SKIP_FIELDS = new Set(['email_token','encryption_key','endpoint',- 'embed_splash','flags','hashes',- 'icon','id','ids',- 'image','keys','locale','mfa_code',@@ -42,10 +39,7 @@ const SKIP_FIELDS = new Set(['response','roles','secret',- 'splash','state',- 'synced_preferences',- 'thumbnail','ticket','token','traits',@@ -53,7 +47,6 @@ const SKIP_FIELDS = new Set(['upload_id','users','verification_proof',- 'waveform','webauthn_challenge','webauthn_response',]);@@ -91,29 +84,31 @@ export function shouldSkipContentFilterPath(path: string): boolean {return SKIP_CONTENT_FILTER_PATH_PARTS.some((part) => path.includes(part));}-function shouldSkipField(key: string): boolean {- if (SKIP_FIELDS.has(key)) {+function shouldSkipField(key: string, path: string): boolean {+ if (binaryRequestBodyPaths.has(path) || SKIP_FIELDS.has(key)) {return true;}return SKIP_FIELD_SUFFIXES.some((suffix) => key.endsWith(suffix));}-export function extractStringValues(body: unknown, depth = 0): Array<string> {+export function extractStringValues(body: unknown, path = '', depth = 0): Array<string> {if (depth > 10) return [];const strings: Array<string> = [];if (typeof body === 'string') {strings.push(body);} else if (Array.isArray(body)) {+ const itemPath = arrayItemBodyFieldPath(path);for (const item of body) {- strings.push(...extractStringValues(item, depth + 1));+ strings.push(...extractStringValues(item, itemPath, depth + 1));}} else if (body && typeof body === 'object') {for (const [key, value] of Object.entries(body as Record<string, unknown>)) {- if (shouldSkipField(key)) continue;+ const fieldPath = childBodyFieldPath(path, key);+ if (shouldSkipField(key, fieldPath)) continue;if (typeof value === 'string' && value.length > 0) {strings.push(value);} else if (typeof value === 'object' && value !== null) {- strings.push(...extractStringValues(value, depth + 1));+ strings.push(...extractStringValues(value, fieldPath, depth + 1));}}}diff --git a/fluxer_api/src/api/middleware/RequestBodyBinaryFields.ts b/fluxer_api/src/api/middleware/RequestBodyBinaryFields.tsnew file mode 100644index 000000000..5369b62a6--- /dev/null+++ b/fluxer_api/src/api/middleware/RequestBodyBinaryFields.ts@@ -0,0 +1,18 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++import {collectBodyFieldPaths} from '@fluxer/schema/src/primitives/Base64BinaryFields';+import type {ZodType} from 'zod';++const bodySchemas = new Set<ZodType>();+const binaryPaths = new Set<string>();++export const requestBodySchemas: ReadonlySet<ZodType> = bodySchemas;+export const binaryRequestBodyPaths: ReadonlySet<string> = binaryPaths;++export function registerRequestBodySchema(schema: ZodType): void {+ if (bodySchemas.has(schema)) return;+ bodySchemas.add(schema);+ for (const path of collectBodyFieldPaths(schema).binary) {+ binaryPaths.add(path);+ }+}diff --git a/fluxer_api/src/api/middleware/ResponseTypeMiddleware.ts b/fluxer_api/src/api/middleware/ResponseTypeMiddleware.tsindex 4f601e9a7..78aaedec6 100644--- a/fluxer_api/src/api/middleware/ResponseTypeMiddleware.ts+++ b/fluxer_api/src/api/middleware/ResponseTypeMiddleware.ts@@ -1,6 +1,7 @@// SPDX-License-Identifier: AGPL-3.0-or-laterimport {Config} from '@app/api/Config';+import {registerRequestBodySchema} from '@app/api/middleware/RequestBodyBinaryFields';import type {HonoEnv} from '@app/api/types/HonoEnv';import {APIErrorCodes} from '@fluxer/constants/src/ApiErrorCodes';import {InternalServerError} from '@fluxer/errors/src/domains/core/InternalServerError';@@ -105,6 +106,9 @@ function validateOperationId(operationId: string): void {export function OpenAPI(metadata: OpenAPIRouteMetadata): MiddlewareHandler<HonoEnv> {validateOperationId(metadata.operationId);+ if (metadata.requestSchema) {+ registerRequestBodySchema(metadata.requestSchema);+ }const {bodylessStatusCodes, responseContentType} = metadata;const schema = metadata.responseSchema;const mediaType = responseContentType?.split(';', 1)[0]?.trim().toLowerCase();diff --git a/fluxer_api/src/api/middleware/tests/ContentFilterMiddleware.test.ts b/fluxer_api/src/api/middleware/tests/ContentFilterMiddleware.test.tsindex 4ba851f92..3348a7607 100644--- a/fluxer_api/src/api/middleware/tests/ContentFilterMiddleware.test.ts+++ b/fluxer_api/src/api/middleware/tests/ContentFilterMiddleware.test.ts@@ -1,14 +1,22 @@// SPDX-License-Identifier: AGPL-3.0-or-later+import fs from 'node:fs';+import path from 'node:path';+import {registerControllers} from '@app/api/app/ControllerRegistry';+import {Config} from '@app/api/Config';import ContentFilterMiddleware, {extractStringValues,shouldSkipContentFilterPath,} from '@app/api/middleware/ContentFilterMiddleware';import {phraseBlocklistCache} from '@app/api/middleware/PhraseBlocklistCache';+import {requestBodySchemas} from '@app/api/middleware/RequestBodyBinaryFields';import {MultipartUploadRoute} from '@app/api/middleware/RequestBodyLimitMiddleware';+import {OpenAPI} from '@app/api/middleware/ResponseTypeMiddleware';import type {HonoEnv} from '@app/api/types/HonoEnv';import {requireRequestJsonBody} from '@app/api/utils/RequestJsonBody';import {AppErrorHandler} from '@fluxer/errors/src/domains/core/ErrorHandlers';+import {MessageRequestSchema} from '@fluxer/schema/src/domains/message/MessageRequestSchemas';+import {collectBodyFieldPaths} from '@fluxer/schema/src/primitives/Base64BinaryFields';import {Hono} from 'hono';import {afterEach, beforeEach, describe, expect, test} from 'vitest';@@ -18,6 +26,18 @@ function buildApp() {const app = new Hono<HonoEnv>({strict: true});app.use(ContentFilterMiddleware);app.patch('/profile', async (ctx) => ctx.json(await requireRequestJsonBody(ctx.req)));+ app.post(+ '/messages',+ OpenAPI({+ operationId: 'send_test_message',+ summary: 'Send a test message',+ description: 'Echoes the message body',+ requestSchema: MessageRequestSchema,+ responseSchema: null,+ tags: 'Test',+ }),+ async (ctx) => ctx.json(await requireRequestJsonBody(ctx.req)),+ );app.post('/upload', MultipartUploadRoute(), async (ctx) => ctx.json({ok: true}));app.post('/form', async (ctx) => ctx.json(Object.fromEntries(await ctx.req.formData())));app.onError(AppErrorHandler);@@ -40,6 +60,46 @@ describe('extractStringValues', () => {});});+describe('binary request body fields', () => {+ async function schemaNames(): Promise<Map<unknown, string>> {+ const root = path.resolve(import.meta.dirname, '../../../../../packages/schema/src/domains');+ const names = new Map<unknown, string>();+ for (const file of fs.readdirSync(root, {recursive: true, encoding: 'utf8'})) {+ if (!file.endsWith('.ts') || file.endsWith('.test.ts')) continue;+ const exports: Record<string, unknown> = await import(path.join(root, file));+ for (const [exportName, value] of Object.entries(exports)) {+ names.set(value, `${path.basename(file, '.ts')}.${exportName}`);+ }+ }+ return names;+ }++ test('no request body field is base64 in one schema and text in another', async () => {+ registerControllers(new Hono<HonoEnv>(), {+ ...Config,+ dev: {...Config.dev, testModeEnabled: true},+ instance: {...Config.instance, selfHosted: false},+ });+ const names = await schemaNames();+ const binarySchemas = new Map<string, Array<string>>();+ const otherSchemas = new Map<string, Array<string>>();+ for (const schema of requestBodySchemas) {+ const name = names.get(schema) ?? 'inline schema';+ const paths = collectBodyFieldPaths(schema);+ for (const path of paths.binary) binarySchemas.set(path, [...(binarySchemas.get(path) ?? []), name]);+ for (const path of paths.other) otherSchemas.set(path, [...(otherSchemas.get(path) ?? []), name]);+ }+ const collisions = [...binarySchemas.keys()]+ .filter((path) => otherSchemas.has(path))+ .map(+ (path) =>+ `${path}: base64 in ${binarySchemas.get(path)!.join(', ')}, text in ${otherSchemas.get(path)!.join(', ')}`,+ );+ expect(binarySchemas.size).toBeGreaterThan(0);+ expect(collisions).toEqual([]);+ });+});+describe('shouldSkipContentFilterPath', () => {test('skips credential-only request bodies', () => {const paths = [@@ -82,6 +142,26 @@ describe('content filter content type handling', () => {expect(response.status).toBe(403);});+ test('blocks a banned phrase in an embed image url', async () => {+ const response = await buildApp().request('/profile', {+ method: 'PATCH',+ headers: {'Content-Type': 'application/json'},+ body: JSON.stringify({embeds: [{image: {url: `https://example.com/${BANNED}.png`}}]}),+ });+ expect(response.status).toBe(403);+ });++ test('skips a voice message waveform but still screens the message text', async () => {+ const send = (content: string) =>+ buildApp().request('/messages', {+ method: 'POST',+ headers: {'Content-Type': 'application/json'},+ body: JSON.stringify({content, attachments: [{id: 0, filename: 'voice.ogg', waveform: `AAAA${BANNED}A`}]}),+ });+ expect((await send('hello')).status).toBe(200);+ expect((await send(`hello ${BANNED}`)).status).toBe(403);+ });+test('leaves multipart bodies on upload routes unread', async () => {const form = new FormData();form.set('payload_json', JSON.stringify({content: `hello ${BANNED}`}));diff --git a/fluxer_api/src/api/test/TestMediaService.ts b/fluxer_api/src/api/test/TestMediaService.tsindex 063df287b..2a88c863e 100644--- a/fluxer_api/src/api/test/TestMediaService.ts+++ b/fluxer_api/src/api/test/TestMediaService.ts@@ -102,8 +102,13 @@ export class TestMediaService extends IMediaService {return image;}- async getAudioDurationSeconds(_base64: string): Promise<number | null> {- return null;+ async getAudioDurationSeconds(base64: string): Promise<number | null> {+ const buffer = Buffer.from(base64, 'base64');+ if (!this.isWav(buffer) || buffer.length < 44) {+ return null;+ }+ const byteRate = buffer.readUInt32LE(28);+ return byteRate > 0 ? buffer.readUInt32LE(40) / byteRate : null;}private analyzeBase64Image(base64: string): MediaProxyMetadataResponse | null {@@ -112,7 +117,7 @@ export class TestMediaService extends IMediaService {if (buffer.length === 0) {return null;}- const format = this.detectImageFormat(buffer);+ const format = this.detectImageFormat(buffer) ?? (this.isWav(buffer) ? 'wav' : null);if (!format) {return null;}@@ -202,6 +207,10 @@ export class TestMediaService extends IMediaService {);}+ private isWav(bytes: Buffer): boolean {+ return bytes.toString('latin1', 0, 4) === 'RIFF' && bytes.toString('latin1', 8, 12) === 'WAVE';+ }+private isJpeg(bytes: Buffer): boolean {return bytes[0] === 0xff && bytes[1] === 0xd8 && bytes[2] === 0xff;}@@ -224,6 +233,7 @@ export class TestMediaService extends IMediaService {private contentTypeForFormat(format: string): string {if (format === 'jpeg') return 'image/jpeg';if (format === 'svg') return 'image/svg+xml';+ if (format === 'wav') return 'audio/wav';return `image/${format}`;}}diff --git a/fluxer_api/src/api/user/tests/EntranceSoundContentFilter.test.ts b/fluxer_api/src/api/user/tests/EntranceSoundContentFilter.test.tsnew file mode 100644index 000000000..a077dde0a--- /dev/null+++ b/fluxer_api/src/api/user/tests/EntranceSoundContentFilter.test.ts@@ -0,0 +1,72 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++import {createTestAccount, type TestAccount} from '@app/api/auth/tests/AuthTestUtils';+import {ensureSessionStarted} from '@app/api/message/tests/MessageTestUtils';+import {phraseBlocklistCache} from '@app/api/middleware/PhraseBlocklistCache';+import {type ApiTestHarness, createApiTestHarness} from '@app/api/test/ApiTestHarness';+import {HTTP_STATUS} from '@app/api/test/TestConstants';+import {createBuilder} from '@app/api/test/TestRequestBuilder';+import {APIErrorCodes} from '@fluxer/constants/src/ApiErrorCodes';+import {afterAll, afterEach, beforeAll, beforeEach, describe, expect, it} from 'vitest';++const BANNED = 'zebracrossingbanned';+const SAMPLE_RATE = 8000;++function wavWithBase64Text(text: string): string {+ const header = Buffer.alloc(44);+ const samples = Buffer.alloc(SAMPLE_RATE / 2, 0x80);+ const textBytes = Buffer.from(`${text}A`, 'base64');+ textBytes.copy(samples, 1);+ header.write('RIFF', 0, 'latin1');+ header.writeUInt32LE(36 + samples.length, 4);+ header.write('WAVEfmt ', 8, 'latin1');+ header.writeUInt32LE(16, 16);+ header.writeUInt16LE(1, 20);+ header.writeUInt16LE(1, 22);+ header.writeUInt32LE(SAMPLE_RATE, 24);+ header.writeUInt32LE(SAMPLE_RATE, 28);+ header.writeUInt16LE(1, 32);+ header.writeUInt16LE(8, 34);+ header.write('data', 36, 'latin1');+ header.writeUInt32LE(samples.length, 40);+ return Buffer.concat([header, samples]).toString('base64');+}++describe('Entrance sound uploads and the content filter', () => {+ let harness: ApiTestHarness;+ let account: TestAccount;+ beforeAll(async () => {+ harness = await createApiTestHarness();+ });+ beforeEach(async () => {+ await harness.reset();+ account = await createTestAccount(harness);+ await ensureSessionStarted(harness, account.token);+ phraseBlocklistCache.add(BANNED);+ });+ afterEach(() => {+ phraseBlocklistCache.remove(BANNED);+ });+ afterAll(async () => {+ await harness?.shutdown();+ });++ it('accepts audio whose base64 payload contains a banned phrase', async () => {+ const audio = wavWithBase64Text(BANNED);+ expect(audio).toContain(BANNED);+ const sound = await createBuilder<{name: string; content_type: string}>(harness, account.token)+ .post('/users/@me/entrance-sounds')+ .body({name: 'doorbell', audio})+ .expect(HTTP_STATUS.CREATED)+ .execute();+ expect(sound).toMatchObject({name: 'doorbell', content_type: 'audio/wav'});+ });++ it('still blocks the banned phrase in the sound name', async () => {+ await createBuilder(harness, account.token)+ .post('/users/@me/entrance-sounds')+ .body({name: `my ${BANNED}`, audio: wavWithBase64Text('plainaudio')})+ .expect(HTTP_STATUS.FORBIDDEN, APIErrorCodes.CONTENT_BLOCKED)+ .execute();+ });+});diff --git a/packages/schema/src/SchemaMetadata.ts b/packages/schema/src/SchemaMetadata.tsindex 51bd4fad7..b0cf32844 100644--- a/packages/schema/src/SchemaMetadata.ts+++ b/packages/schema/src/SchemaMetadata.ts@@ -21,6 +21,7 @@ interface SchemaMetadata {preserveEmptyValues?: boolean;preserveNullFields?: boolean;experiment?: string;+ base64Binary?: boolean;}export const schemaMetadata = z.registry<SchemaMetadata>();diff --git a/packages/schema/src/domains/admin/AdminBadgeSchemas.ts b/packages/schema/src/domains/admin/AdminBadgeSchemas.tsindex 129f32f5b..0163a2df3 100644--- a/packages/schema/src/domains/admin/AdminBadgeSchemas.ts+++ b/packages/schema/src/domains/admin/AdminBadgeSchemas.ts@@ -14,6 +14,7 @@ import {HIDEABLE_BUILT_IN_BADGE_KINDS,} from '@fluxer/constants/src/BadgeConstants';import {ValidationErrorCodes} from '@fluxer/constants/src/ValidationErrorCodes';+import {withBase64Binary} from '@fluxer/schema/src/primitives/Base64BinaryFields';import {createQueryIntegerType} from '@fluxer/schema/src/primitives/QueryValidators';import {createNamedStringLiteralUnion,@@ -41,10 +42,9 @@ const CustomBadgeHrefType = URLType.refine(ValidationErrorCodes.INVALID_URL_FORMAT,).describe('Absolute https URL opened when the badge is clicked');-const CustomBadgeImageType = z- .string()- .max(16_000_000)- .describe('The badge image as a base64 data URL. PNG, JPEG, GIF, WebP and SVG images are accepted');+const CustomBadgeImageType = withBase64Binary(z.string().max(16_000_000)).describe(+ 'The badge image as a base64 data URL. PNG, JPEG, GIF, WebP and SVG images are accepted',+);export const CustomBadgePositionType = z.string()diff --git a/packages/schema/src/domains/admin/AdminSchemas.ts b/packages/schema/src/domains/admin/AdminSchemas.tsindex 777f0aa0c..1587f2ccb 100644--- a/packages/schema/src/domains/admin/AdminSchemas.ts+++ b/packages/schema/src/domains/admin/AdminSchemas.ts@@ -67,6 +67,7 @@ import {} from '@fluxer/schema/src/domains/instance/InstanceSchemas';import {MessageEmbedResponse} from '@fluxer/schema/src/domains/message/EmbedSchemas';import {MessageResponseSchema} from '@fluxer/schema/src/domains/message/MessageResponseSchemas';+import {withBase64Binary} from '@fluxer/schema/src/primitives/Base64BinaryFields';import {ChannelTypeSchema} from '@fluxer/schema/src/primitives/ChannelValidators';import {ContentWarningLevelSchema,@@ -1064,7 +1065,7 @@ export const BrandingAssetUploadRequest = z.object({'premium_showcase_stream','premium_showcase_upload',]),- image: z.string().max(16_000_000).nullish(),+ image: withBase64Binary(z.string().max(16_000_000)).nullish(),});export type BrandingAssetUploadRequest = z.infer<typeof BrandingAssetUploadRequest>;diff --git a/packages/schema/src/domains/channel/ChannelRequestSchemas.ts b/packages/schema/src/domains/channel/ChannelRequestSchemas.tsindex c53dc33d5..96018b3d1 100644--- a/packages/schema/src/domains/channel/ChannelRequestSchemas.ts+++ b/packages/schema/src/domains/channel/ChannelRequestSchemas.ts@@ -31,6 +31,7 @@ import {} from '@fluxer/schema/src/domains/channel/ForumRequestSchemas';import {ThreadAutoArchiveDurationSchema} from '@fluxer/schema/src/domains/channel/ThreadRequestSchemas';import {ReadStateResponse} from '@fluxer/schema/src/domains/gateway/GatewaySchemas';+import {withBase64Binary} from '@fluxer/schema/src/primitives/Base64BinaryFields';import {ChannelOverwriteTypeSchema, GeneralChannelNameType} from '@fluxer/schema/src/primitives/ChannelValidators';import {createBase64ImageType} from '@fluxer/schema/src/primitives/FileValidators';import {ContentWarningLevelSchema} from '@fluxer/schema/src/primitives/GuildValidators';@@ -510,7 +511,7 @@ export type StreamUpdateBodySchema = z.infer<typeof StreamUpdateBodySchema>;export const StreamPreviewUploadBodySchema = z.object({channel_id: SnowflakeType.describe('The ID of the channel where the stream is active'),- thumbnail: createStringType(1, 2000000).describe('Base64-encoded thumbnail image data'),+ thumbnail: withBase64Binary(createStringType(1, 2000000)).describe('Base64-encoded thumbnail image data'),content_type: createStringType(1, 64).optional().describe('MIME type of the thumbnail image'),});diff --git a/packages/schema/src/domains/message/AttachmentSchemas.ts b/packages/schema/src/domains/message/AttachmentSchemas.tsindex 31ad6f58d..aad44ddd5 100644--- a/packages/schema/src/domains/message/AttachmentSchemas.ts+++ b/packages/schema/src/domains/message/AttachmentSchemas.ts@@ -1,6 +1,7 @@// SPDX-License-Identifier: AGPL-3.0-or-laterimport {MessageAttachmentFlags, MessageAttachmentFlagsDescriptions} from '@fluxer/constants/src/ChannelConstants';+import {withBase64Binary} from '@fluxer/schema/src/primitives/Base64BinaryFields';import {FilenameType} from '@fluxer/schema/src/primitives/FileValidators';import {coerceNumberFromString,@@ -26,7 +27,7 @@ const ClientAttachmentBase = z.object({),).default(0),duration: Int32Type.nullish().describe('The duration of the audio file in seconds'),- waveform: createStringType(1, 4096).nullish().describe('Base64 encoded audio waveform data'),+ waveform: withBase64Binary(createStringType(1, 4096)).nullish().describe('Base64 encoded audio waveform data'),});export const ClientAttachmentRequest = ClientAttachmentBase.extend({id: coerceNumberFromString(Int32Type).describe('The client-side identifier for this attachment'),diff --git a/packages/schema/src/domains/user/UserRequestSchemas.ts b/packages/schema/src/domains/user/UserRequestSchemas.tsindex 34a274edd..f8b142ad3 100644--- a/packages/schema/src/domains/user/UserRequestSchemas.ts+++ b/packages/schema/src/domains/user/UserRequestSchemas.ts@@ -23,6 +23,7 @@ import {isValidSyncedPreferencesEncoding,SYNCED_PREFERENCES_MAX_ENCODED_LENGTH,} from '@fluxer/schema/src/domains/user/SyncedPreferencesCodec';+import {withBase64Binary} from '@fluxer/schema/src/primitives/Base64BinaryFields';import {isValidSingleUnicodeEmoji} from '@fluxer/schema/src/primitives/EmojiValidators';import {createBase64ImageType} from '@fluxer/schema/src/primitives/FileValidators';import {LocaleSchema} from '@fluxer/schema/src/primitives/LocaleSchema';@@ -430,12 +431,14 @@ export const UserSettingsUpdateRequest = z.describe('Version of the privacy setup the user just reviewed. The server also records privacy_setup_completed_at as the current time.',),- synced_preferences: z- .string()- .max(SYNCED_PREFERENCES_MAX_ENCODED_LENGTH)- .refine((value) => isValidSyncedPreferencesEncoding(value), {- message: ValidationErrorCodes.INVALID_FORMAT,- })+ synced_preferences: withBase64Binary(+ z+ .string()+ .max(SYNCED_PREFERENCES_MAX_ENCODED_LENGTH)+ .refine((value) => isValidSyncedPreferencesEncoding(value), {+ message: ValidationErrorCodes.INVALID_FORMAT,+ }),+ ).nullish().describe('Account-wide client preferences as a base64-encoded protobuf snapshot. Replaces the entire stored snapshot; pass null to clear it.',diff --git a/packages/schema/src/domains/webhook/WebhookRequestSchemas.ts b/packages/schema/src/domains/webhook/WebhookRequestSchemas.tsindex a3c790480..df2b15c20 100644--- a/packages/schema/src/domains/webhook/WebhookRequestSchemas.ts+++ b/packages/schema/src/domains/webhook/WebhookRequestSchemas.ts@@ -19,6 +19,7 @@ import {RichEmbedRequest,} from '@fluxer/schema/src/domains/message/MessageRequestSchemas';import {AllowedMentionsRequest, MessageReferenceRequest} from '@fluxer/schema/src/domains/message/SharedMessageSchemas';+import {withBase64Binary} from '@fluxer/schema/src/primitives/Base64BinaryFields';import {createBase64ImageType} from '@fluxer/schema/src/primitives/FileValidators';import {QueryBooleanType} from '@fluxer/schema/src/primitives/QueryValidators';import {@@ -76,7 +77,7 @@ const WebhookMultipartAttachmentRequest = z.object({width: z.number().int().optional().describe('Width of the image/video in pixels'),ephemeral: z.boolean().optional().describe('Whether this attachment is ephemeral'),duration: z.number().optional().describe('Duration of audio file in seconds'),- waveform: createStringType().optional().describe('Base64-encoded bytearray of audio waveform'),+ waveform: withBase64Binary(createStringType()).optional().describe('Base64-encoded bytearray of audio waveform'),flags: createBitflagInt32Type(MessageAttachmentFlags,MessageAttachmentFlagsDescriptions,diff --git a/packages/schema/src/primitives/Base64BinaryFields.ts b/packages/schema/src/primitives/Base64BinaryFields.tsnew file mode 100644index 000000000..7f3b7b4f6--- /dev/null+++ b/packages/schema/src/primitives/Base64BinaryFields.ts@@ -0,0 +1,79 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++import {schemaMetadata, withSchemaMetadata} from '@fluxer/schema/src/SchemaMetadata';+import type {z} from 'zod';++interface BodyFieldPaths {+ binary: Set<string>;+ other: Set<string>;+}++export function withBase64Binary<T extends z.ZodType>(schema: T): T {+ return withSchemaMetadata(schema, {...schemaMetadata.get(schema), base64Binary: true});+}++export function childBodyFieldPath(parent: string, key: string): string {+ return parent === '' ? key : `${parent}.${key}`;+}++export function arrayItemBodyFieldPath(parent: string): string {+ return `${parent}[]`;+}++export function collectBodyFieldPaths(schema: z.core.$ZodType): BodyFieldPaths {+ const paths: BodyFieldPaths = {binary: new Set(), other: new Set()};+ visitBodyField(schema, '', paths, new Set());+ return paths;+}++function visitBodyField(+ schema: z.core.$ZodType,+ path: string,+ paths: BodyFieldPaths,+ visiting: Set<z.core.$ZodType>,+): void {+ if (schemaMetadata.get(schema)?.base64Binary) {+ paths.binary.add(path);+ return;+ }+ if (visiting.has(schema)) return;+ visiting.add(schema);+ const def = (schema as z.core.$ZodTypes)._zod.def;+ switch (def.type) {+ case 'optional':+ case 'nullable':+ case 'default':+ case 'prefault':+ case 'nonoptional':+ case 'readonly':+ case 'catch':+ visitBodyField(def.innerType, path, paths, visiting);+ break;+ case 'pipe':+ visitBodyField(def.in._zod.def.type === 'transform' ? def.out : def.in, path, paths, visiting);+ break;+ case 'lazy':+ visitBodyField(def.getter(), path, paths, visiting);+ break;+ case 'union':+ for (const option of def.options) visitBodyField(option, path, paths, visiting);+ break;+ case 'intersection':+ visitBodyField(def.left, path, paths, visiting);+ visitBodyField(def.right, path, paths, visiting);+ break;+ case 'object':+ if (path !== '') paths.other.add(path);+ for (const [key, field] of Object.entries(def.shape)) {+ visitBodyField(field, childBodyFieldPath(path, key), paths, visiting);+ }+ break;+ case 'array':+ if (path !== '') paths.other.add(path);+ visitBodyField(def.element, arrayItemBodyFieldPath(path), paths, visiting);+ break;+ default:+ if (path !== '') paths.other.add(path);+ }+ visiting.delete(schema);+}diff --git a/packages/schema/src/primitives/FileValidators.ts b/packages/schema/src/primitives/FileValidators.tsindex a4df6b592..1887ddfed 100644--- a/packages/schema/src/primitives/FileValidators.ts+++ b/packages/schema/src/primitives/FileValidators.ts@@ -2,6 +2,7 @@import {BASE64_IMAGE_MAX_SIZE} from '@fluxer/constants/src/LimitConstants';import {ValidationErrorCodes} from '@fluxer/constants/src/ValidationErrorCodes';+import {withBase64Binary} from '@fluxer/schema/src/primitives/Base64BinaryFields';import {normalizeString,withOpenApiType,@@ -79,7 +80,7 @@ export function base64LengthForBytes(maxBytes: number): number {}export function createBase64StringType(minLength = 1, maxLength = 256) {- return withOpenApiType(+ const schema = withOpenApiType(z.string().overwrite((value) => {@@ -106,6 +107,7 @@ export function createBase64StringType(minLength = 1, maxLength = 256) {}),'Base64ImageType',);+ return withBase64Binary(schema);}export function createBase64ImageType() {