Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.
TypeScript cloud-alternativecollaborationcollaborative-editingdropboxfile-serverfile-sharingfile-storagefile-syncfull-text-searchhomelabnextcloudon-premiseopen-sourceprivacy-firstprivate-cloudrealtimesecureself-hostedstorage-serverwebdav
fix(backend:database): prevent SQL injection in dynamic queries
johavenf1921e61d9ef
diff --git a/backend/src/applications/comments/services/comments-queries.service.ts b/backend/src/applications/comments/services/comments-queries.service.tsindex b89ec4d9..fe47b562 100644--- a/backend/src/applications/comments/services/comments-queries.service.ts+++ b/backend/src/applications/comments/services/comments-queries.service.ts@@ -109,7 +109,7 @@ export class CommentsQueries {file: {name: sql<string>`IF (${files.id} = ${shareFile.id}, ${shares.name}, ${files.name})`.as('name'),path: sql<string>`- CONCAT_WS('/', '${sql.raw(SPACE_REPOSITORY.SHARES)}',+ CONCAT_WS('/', ${SPACE_REPOSITORY.SHARES},IF (${shareFile.id} IS NOT NULL,IF (${files.id} = ${shareFile.id}, NULL, REGEXP_REPLACE(${files.path}, ${filePathSQL(shareFile)}, ${shares.alias})),CONCAT_WS('/', ${shares.alias}, IF (${files.path} = '.', NULL, ${files.path}))@@ -164,8 +164,8 @@ export class CommentsQueries {name: files.name,path: sql<string>`CONCAT_WS('/',- IF (${files.inTrash} = 0, '${sql.raw(SPACE_REPOSITORY.FILES)}', '${sql.raw(SPACE_REPOSITORY.TRASH)}'),- '${sql.raw(SPACE_ALIAS.PERSONAL)}',+ IF (${files.inTrash} = 0, ${SPACE_REPOSITORY.FILES}, ${SPACE_REPOSITORY.TRASH}),+ ${SPACE_ALIAS.PERSONAL},IF (${files.path} = '.', NULL, ${files.path}))`.as('path'),mime: files.mime,@@ -195,8 +195,8 @@ export class CommentsQueries {name: sql<string>`IF (${files.id} = ${spacesRoots.fileId}, ${spacesRoots.name}, ${files.name})`.as('name'),path: sql<string>`CONCAT_WS('/',- IF (${files.inTrash} = 0, '${sql.raw(SPACE_REPOSITORY.FILES)}', '${sql.raw(SPACE_REPOSITORY.TRASH)}'),- IF (${files.ownerId} = ${userId}, '${sql.raw(SPACE_ALIAS.PERSONAL)}', ${spaces.alias}),+ IF (${files.inTrash} = 0, ${SPACE_REPOSITORY.FILES}, ${SPACE_REPOSITORY.TRASH}),+ IF (${files.ownerId} = ${userId}, ${SPACE_ALIAS.PERSONAL}, ${spaces.alias}),IF (${spaceRootFile.id} IS NOT NULL,IF (${files.id} = ${spaceRootFile.id}, NULL, IF (${files.path} = '.', NULL, REGEXP_REPLACE(${files.path}, ${filePathSQL(spaceRootFile)}, ${spacesRoots.alias}))),NULLIF(CONCAT_WS('/', IF (${files.spaceExternalRootId} = ${spacesRoots.id}, ${spacesRoots.alias}, NULL), IF (${files.path} = '.', NULL, ${files.path})), '')diff --git a/backend/src/applications/files/adapters/files-content-store-mysql.service.spec.ts b/backend/src/applications/files/adapters/files-content-store-mysql.service.spec.tsindex 0d180ed2..a84bf836 100644--- a/backend/src/applications/files/adapters/files-content-store-mysql.service.spec.ts+++ b/backend/src/applications/files/adapters/files-content-store-mysql.service.spec.ts@@ -1,4 +1,5 @@import { Test, TestingModule } from '@nestjs/testing'+import { MySqlDialect } from 'drizzle-orm/mysql-core'import { DB_TOKEN_PROVIDER } from '../../../infrastructure/database/constants'import { FilesContentStoreMySQL } from './files-content-store-mysql.service'import { Mock } from 'vitest'@@ -7,6 +8,7 @@ describe(FilesContentStoreMySQL.name, () => {let module: TestingModulelet filesIndexerMySQL: FilesContentStoreMySQLlet db: { execute: Mock }+ const dialect = new MySqlDialect()const sqlText = (query: any): string => {if (typeof query === 'string') return query@@ -40,30 +42,41 @@ describe(FilesContentStoreMySQL.name, () => {})describe('indexesList', () => {- it('should list tables starting with prefix', async () => {- db.execute.mockResolvedValueOnce([[{ t: 'files_content_u_1' }, { t: 'files_content_s_2' }]])+ it('should list only managed content index tables', async () => {+ db.execute.mockResolvedValueOnce([[{ t: 'files_content_user_1' }, { t: 'filesXcontentYuser_2' }]])const res = await filesIndexerMySQL.indexesList()- expect(res).toEqual(['files_content_u_1', 'files_content_s_2'])+ expect(res).toEqual(['files_content_user_1'])expect(db.execute).toHaveBeenCalledTimes(1)})})describe('getIndexName', () => {it('should build table name with prefix', () => {- expect(filesIndexerMySQL.getIndexName('u_123')).toBe('files_content_u_123')+ expect(filesIndexerMySQL.getIndexName('user_123')).toBe('files_content_user_123')})})describe('existingIndexes', () => {it('should filter suffixes to existing tables', async () => {- db.execute.mockResolvedValueOnce([[{ t: 'files_content_u_1' }, { t: 'files_content_s_2' }]])- const res = await filesIndexerMySQL.existingIndexes(['u_1', 's_3', 's_2'])- expect(res.sort()).toEqual(['files_content_s_2', 'files_content_u_1'].sort())+ db.execute.mockResolvedValueOnce([[{ t: 'files_content_user_1' }, { t: 'files_content_space_2' }]])+ const res = await filesIndexerMySQL.existingIndexes(['user_1', 'space_3', 'space_2'])+ expect(res.sort()).toEqual(['files_content_space_2', 'files_content_user_1'].sort())})})describe('createIndex', () => {+ it('should escape the table name as an identifier', async () => {+ db.execute.mockResolvedValueOnce([{}])+ db.execute.mockResolvedValueOnce([[{ Field: 'seen_run_id' }]])+ const tableName = 'files_content_user_1`; DROP TABLE users; --'++ await expect(filesIndexerMySQL.createIndex(tableName)).resolves.toBe(true)++ const query = dialect.sqlToQuery(db.execute.mock.calls[0][0])+ expect(query.sql).toContain('CREATE TABLE IF NOT EXISTS `files_content_user_1``; DROP TABLE users; --`')+ })+it('should return true when creation succeeds', async () => {db.execute.mockResolvedValueOnce([{}])db.execute.mockResolvedValueOnce([[{ Field: 'seen_run_id' }]])@@ -87,15 +100,20 @@ describe(FilesContentStoreMySQL.name, () => {})describe('dropIndex', () => {+ it('should reject unmanaged table names', async () => {+ await expect(filesIndexerMySQL.dropIndex('filesXcontentYuser_2')).resolves.toBe(false)+ expect(db.execute).not.toHaveBeenCalled()+ })+it('should return true when drop succeeds', async () => {db.execute.mockResolvedValueOnce([{}])- await expect(filesIndexerMySQL.dropIndex('files_content_u_1')).resolves.toBe(true)+ await expect(filesIndexerMySQL.dropIndex('files_content_user_1')).resolves.toBe(true)expect(db.execute).toHaveBeenCalledTimes(1)})it('should return false when drop fails', async () => {db.execute.mockRejectedValueOnce(new Error('boom'))- await expect(filesIndexerMySQL.dropIndex('files_content_u_1')).resolves.toBe(false)+ await expect(filesIndexerMySQL.dropIndex('files_content_user_1')).resolves.toBe(false)})})@@ -455,11 +473,11 @@ describe(FilesContentStoreMySQL.name, () => {describe('cleanIndexes', () => {it('should drop tables that are not in provided suffixes', async () => {// existing tables- db.execute.mockResolvedValueOnce([[{ t: 'files_content_u_1' }, { t: 'files_content_u_2' }, { t: 'files_content_s_1' }]])+ db.execute.mockResolvedValueOnce([[{ t: 'files_content_user_1' }, { t: 'files_content_user_2' }, { t: 'files_content_space_1' }]])// each drop returns somethingdb.execute.mockResolvedValue([{}])- await filesIndexerMySQL.cleanIndexes(['u_1']) // keep only files_content_u_1; drop the others+ await filesIndexerMySQL.cleanIndexes(['user_1']) // keep only files_content_user_1; drop the other managed indexes// 1 call for indexesList + 2 drops expectedexpect(db.execute).toHaveBeenCalledTimes(3)diff --git a/backend/src/applications/files/adapters/files-content-store-mysql.service.ts b/backend/src/applications/files/adapters/files-content-store-mysql.service.tsindex 8b40a114..ba710a23 100644--- a/backend/src/applications/files/adapters/files-content-store-mysql.service.ts+++ b/backend/src/applications/files/adapters/files-content-store-mysql.service.ts@@ -3,6 +3,7 @@ import { SQL, sql } from 'drizzle-orm'import { MySqlQueryResult } from 'drizzle-orm/mysql2'import { DB_TOKEN_PROVIDER } from '../../../infrastructure/database/constants'import { DBSchema } from '../../../infrastructure/database/interfaces/database.interface'+import { FILE_REPOSITORY } from '../constants/operations'import { FilesContentStore } from '../models/files-content-store'import { FileContent, FileContentRecordMetadata, FileContentRecordMetadataMap } from '../schemas/file-content.interface'import { createTableFilesContent, FILES_CONTENT_TABLE_PREFIX } from '../schemas/files-content.schema'@@ -12,6 +13,7 @@ type SearchCandidate = Pick<FileContent, 'id' | 'score'> & { sourceIndex: stringtype SearchRecord = FileContent & { sourceIndex: string }const HIGHLIGHT_CONTEXT_WORD = '(?<![\\p{L}\\p{N}])[\\p{L}\\p{N}]+(?![\\p{L}\\p{N}])'const HIGHLIGHT_CONTEXT_SEPARATOR = '[^\\p{L}\\p{N}]'+const FILES_CONTENT_TABLE_PATTERN = new RegExp(`^${FILES_CONTENT_TABLE_PREFIX}(?:${Object.values(FILE_REPOSITORY).join('|')})_[0-9]+$`)@Injectable()export class FilesContentStoreMySQL implements FilesContentStore {@@ -20,11 +22,13 @@ export class FilesContentStoreMySQL implements FilesContentStore {constructor(@Inject(DB_TOKEN_PROVIDER) private readonly db: DBSchema) {}async indexesList(): Promise<string[]> {- return (await this.getIndexes()).flatMap((r: Record<string, string>) => Object.values(r))+ return (await this.getIndexes())+ .flatMap((r: Record<string, string>) => Object.values(r))+ .filter((tableName) => FILES_CONTENT_TABLE_PATTERN.test(tableName))}async indexesCount(): Promise<number> {- return (await this.getIndexes()).length+ return (await this.indexesList()).length}getIndexName(tableSuffix: string): string {@@ -48,8 +52,12 @@ export class FilesContentStoreMySQL implements FilesContentStore {}async dropIndex(tableName: string): Promise<boolean> {+ if (!FILES_CONTENT_TABLE_PATTERN.test(tableName)) {+ this.logger.error({ tag: this.dropIndex.name, msg: `refusing to drop unmanaged table: ${tableName}` })+ return false+ }try {- await this.db.execute(sql`DROP TABLE IF EXISTS ${sql.raw(tableName)} `)+ await this.db.execute(sql`DROP TABLE IF EXISTS ${sql.identifier(tableName)} `)return true} catch (e) {this.logger.error({ tag: this.dropIndex.name, msg: `${tableName} : ${e}` })@@ -60,7 +68,7 @@ export class FilesContentStoreMySQL implements FilesContentStore {async insertRecord(tableName: string, fc: FileContent, runId: string): Promise<boolean> {try {await this.db.execute(sql`- INSERT INTO ${sql.raw(tableName)} (id, path, name, mime, size, mtime, content, seen_run_id)+ INSERT INTO ${sql.identifier(tableName)} (id, path, name, mime, size, mtime, content, seen_run_id)VALUES ${sql`(${fc.id}, ${fc.path}, ${fc.name}, ${fc.mime}, ${fc.size}, ${fc.mtime}, ${fc.content}, ${runId})`}ON DUPLICATE KEY UPDATE path = VALUES(path),name = VALUES(name),@@ -82,7 +90,7 @@ export class FilesContentStoreMySQL implements FilesContentStore {return new Map()}const [r]: { id: number; path: string; name: string; size: number }[][] = (await this.db.execute(- sql`SELECT id, path, name, size FROM ${sql.raw(tableName)} WHERE id IN (${idsSqlList(ids)})`+ sql`SELECT id, path, name, size FROM ${sql.identifier(tableName)} WHERE id IN (${idsSqlList(ids)})`)) as MySqlQueryResultreturn new Map(r.map((row) => [row.id, { path: row.path, name: row.name, size: row.size }] satisfies [FileContent['id'], FileContentRecordMetadata])@@ -92,7 +100,7 @@ export class FilesContentStoreMySQL implements FilesContentStore {async markRecordsSeen(tableName: string, ids: number[], runId: string): Promise<boolean> {if (!ids.length) return truetry {- await this.db.execute(sql`UPDATE ${sql.raw(tableName)} SET seen_run_id = ${runId} WHERE id IN (${idsSqlList(ids)})`)+ await this.db.execute(sql`UPDATE ${sql.identifier(tableName)} SET seen_run_id = ${runId} WHERE id IN (${idsSqlList(ids)})`)return true} catch (e) {this.logger.error({ tag: this.markRecordsSeen.name, msg: `${tableName} : ${e}` })@@ -102,7 +110,7 @@ export class FilesContentStoreMySQL implements FilesContentStore {async deleteRecords(tableName: string, ids: number[]): Promise<void> {try {- const [r] = await this.db.execute(sql`DELETE FROM ${sql.raw(tableName)} WHERE id IN (${idsSqlList(ids)})`)+ const [r] = await this.db.execute(sql`DELETE FROM ${sql.identifier(tableName)} WHERE id IN (${idsSqlList(ids)})`)if (r.affectedRows !== ids.length) {this.logger.warn({ tag: this.deleteRecords.name, msg: `${tableName} - deleted : ${r.affectedRows}/${ids.length}` })}@@ -113,7 +121,7 @@ export class FilesContentStoreMySQL implements FilesContentStore {async deleteUnseenRecords(tableName: string, runId: string): Promise<number> {try {- const [r] = await this.db.execute(sql`DELETE FROM ${sql.raw(tableName)} WHERE seen_run_id IS NULL OR seen_run_id <> ${runId}`)+ const [r] = await this.db.execute(sql`DELETE FROM ${sql.identifier(tableName)} WHERE seen_run_id IS NULL OR seen_run_id <> ${runId}`)return r.affectedRows ?? 0} catch (e) {this.logger.error({ tag: this.deleteUnseenRecords.name, msg: `${tableName} : ${e}` })@@ -145,10 +153,10 @@ export class FilesContentStoreMySQL implements FilesContentStore {const excludedMatch = createContentMatch(excludedTerms, ' AND ', true)const score = positiveTerms.reduce<SQL>((value, term) => sql`${value} + IF(content LIKE ${toLikePattern(term.rawValue)} ESCAPE '=', 1, 0)`,- sql.raw('0')+ sql`0`)return sql`(SELECT ${tableName} as sourceIndex, id, ${score} as score- FROM ${sql.raw(tableName)}+ FROM ${sql.identifier(tableName)}WHERE (${positiveMatch})${excludedMatch ? sql`AND ${excludedMatch}` : sql``}ORDER BY score DESC@@ -157,12 +165,12 @@ export class FilesContentStoreMySQL implements FilesContentStore {const fullTextMatch = sql`MATCH (content) AGAINST ( ${search} IN BOOLEAN MODE )`return sql`(SELECT ${tableName} as sourceIndex, id, ${fullTextMatch} as score- FROM ${sql.raw(tableName)}+ FROM ${sql.identifier(tableName)}WHERE ${fullTextMatch}ORDER BY score DESCLIMIT ${limit})`}),- sql.raw(' UNION ALL ')+ sql` UNION ALL `).append(sql` ORDER BY score DESC LIMIT ${limit}`)@@ -185,11 +193,11 @@ export class FilesContentStoreMySQL implements FilesContentStore {if (!ids?.length) return []return [sql`SELECT ${tableName} as sourceIndex, id, path, name, mime, mtime, content- FROM ${sql.raw(tableName)}+ FROM ${sql.identifier(tableName)}WHERE id IN (${idsSqlList(ids)})`]}),- sql.raw(' UNION ALL ')+ sql` UNION ALL `)const [loadedRecords]: SearchRecord[][] = (await this.db.execute(recordsQuery)) as MySqlQueryResultconst recordsByKey = new Map(loadedRecords.map((record) => [`${record.sourceIndex}:${record.id}`, record]))@@ -231,22 +239,22 @@ export class FilesContentStoreMySQL implements FilesContentStore {}async dropAllIndexes(): Promise<void> {- for (const i of (await this.getIndexes()).flatMap((r: Record<string, string>) => Object.values(r))) {+ for (const i of await this.indexesList()) {await this.dropIndex(i)}}private async getIndexes(): Promise<Record<string, string>[]> {- return (await this.db.execute(sql`SHOW TABLES LIKE '${sql.raw(FILES_CONTENT_TABLE_PREFIX)}%'`))[0] as any+ return (await this.db.execute(sql`SHOW TABLES LIKE ${`${FILES_CONTENT_TABLE_PREFIX}%`}`))[0] as any}private async ensureRunIdColumn(tableName: string): Promise<void> {// migration for old versions of the application- const [columns] = (await this.db.execute(sql`SHOW COLUMNS FROM ${sql.raw(tableName)} LIKE 'seen_run_id'`)) as MySqlQueryResult+ const [columns] = (await this.db.execute(sql`SHOW COLUMNS FROM ${sql.identifier(tableName)} LIKE 'seen_run_id'`)) as MySqlQueryResultif ((columns as unknown[]).length) {return}- await this.db.execute(sql`ALTER TABLE ${sql.raw(tableName)} ADD COLUMN seen_run_id varchar(64), ADD INDEX seen_run_id (seen_run_id)`)+ await this.db.execute(sql`ALTER TABLE ${sql.identifier(tableName)} ADD COLUMN seen_run_id varchar(64), ADD INDEX seen_run_id (seen_run_id)`)}}@@ -260,13 +268,13 @@ function createContentMatch(terms: SearchTerm[], separator: ' AND ' | ' OR ', neterms.map(({ rawValue }) =>negate ? sql`content NOT LIKE ${toLikePattern(rawValue)} ESCAPE '='` : sql`content LIKE ${toLikePattern(rawValue)} ESCAPE '='`),- sql.raw(separator)+ separator === ' AND ' ? sql` AND ` : sql` OR `)}function idsSqlList(ids: number[]): SQL {return sql.join(ids.map((id) => sql`${id}`),- sql.raw(', ')+ sql`, `)}diff --git a/backend/src/applications/files/schemas/files-content.schema.ts b/backend/src/applications/files/schemas/files-content.schema.tsindex 7e2253bd..276c8546 100644--- a/backend/src/applications/files/schemas/files-content.schema.ts+++ b/backend/src/applications/files/schemas/files-content.schema.ts@@ -1,9 +1,11 @@+import { SQL, sql } from 'drizzle-orm'+export const FILES_CONTENT_TABLE_PREFIX = 'files_content_' as const// The utf8mb4_uca1400_ai_ci COLLATE is better for precision but slower-export function createTableFilesContent(tableName: string): string {- return `- CREATE TABLE IF NOT EXISTS ${tableName}+export function createTableFilesContent(tableName: string): SQL {+ return sql`+ CREATE TABLE IF NOT EXISTS ${sql.identifier(tableName)}(id bigint unsigned NOT NULL,path varchar(4096) NOT NULL,diff --git a/backend/src/applications/files/schemas/files-trash.schema.ts b/backend/src/applications/files/schemas/files-trash.schema.tsindex f3d20dbd..d7b6c209 100644--- a/backend/src/applications/files/schemas/files-trash.schema.ts+++ b/backend/src/applications/files/schemas/files-trash.schema.ts@@ -1,9 +1,11 @@+import { SQL, sql } from 'drizzle-orm'+export const FILES_TRASH_TABLE_PREFIX = 'files_trash_' as const// The utf8mb4_uca1400_ai_ci COLLATE is better for precision but slower-export function createTableFilesTrash(tableName: string): string {- return `- CREATE TABLE IF NOT EXISTS ${tableName}+export function createTableFilesTrash(tableName: string): SQL {+ return sql`+ CREATE TABLE IF NOT EXISTS ${sql.identifier(tableName)}(id bigint unsigned NOT NULL,path varchar(4096) NOT NULL,diff --git a/backend/src/applications/files/services/files-trash-retention.service.spec.ts b/backend/src/applications/files/services/files-trash-retention.service.spec.tsindex 8dadc125..1145351c 100644--- a/backend/src/applications/files/services/files-trash-retention.service.spec.ts+++ b/backend/src/applications/files/services/files-trash-retention.service.spec.ts@@ -1,4 +1,5 @@import { Test, TestingModule } from '@nestjs/testing'+import { MySqlDialect } from 'drizzle-orm/mysql-core'import fs from 'node:fs/promises'import os from 'node:os'import path from 'node:path'@@ -14,6 +15,7 @@ describe(FilesTrashRetention.name, () => {let module: TestingModulelet service: FilesTrashRetentionlet db: { execute: Mock }+ const dialect = new MySqlDialect()const asyncGen = <T>(items: T[]) =>(async function* () {@@ -74,7 +76,7 @@ describe(FilesTrashRetention.name, () => {})it('should create trash tables with a seen run id column', () => {- const schema = createTableFilesTrash('files_trash_user_1')+ const schema = dialect.sqlToQuery(createTableFilesTrash('files_trash_user_1')).sqlexpect(schema).toContain('deletedAt date')expect(schema).toContain('INDEX is_dir_deleted_at (isDir, deletedAt)')@@ -82,6 +84,14 @@ describe(FilesTrashRetention.name, () => {expect(schema).toContain('INDEX seen_run_id (seen_run_id)')})+ it('should escape trash table names as identifiers', () => {+ const tableName = 'files_trash_user_1`; DROP TABLE users; --'++ const query = dialect.sqlToQuery(createTableFilesTrash(tableName))++ expect(query.sql).toContain('CREATE TABLE IF NOT EXISTS `files_trash_user_1``; DROP TABLE users; --`')+ })+it('should process trash files by batches of 1000 and delete unseen records', async () => {const files = Array.from({ length: 1001 }, (_, i) => fileTrash(i + 1))mockTrashPath(files)@@ -205,12 +215,12 @@ describe(FilesTrashRetention.name, () => {await expect((service as any).getExpiredRecords('files_trash_user_1', true, 30)).resolves.toEqual([])- const query = sqlText(db.execute.mock.calls[0][0])- expect(query).toContain('NOT EXISTS')- expect(query).toContain('FROM files_trash_user_1 AS child')- expect(query).toContain('child.path LIKE CONCAT')- expect(query).toContain("ESCAPE '='")- expect(query).toContain('ORDER BY trash_record.deletedAt, trash_record.id')+ const query = dialect.sqlToQuery(db.execute.mock.calls[0][0])+ expect(query.sql).toContain('NOT EXISTS')+ expect(query.sql).toContain('FROM `files_trash_user_1` AS child')+ expect(query.sql).toContain('child.path LIKE CONCAT')+ expect(query.sql).toContain("ESCAPE '='")+ expect(query.sql).toContain('ORDER BY trash_record.deletedAt, trash_record.id')})it('should resolve trash record paths inside the trash root only', () => {diff --git a/backend/src/applications/files/services/files-trash-retention.service.ts b/backend/src/applications/files/services/files-trash-retention.service.tsindex 41937729..17e94edd 100644--- a/backend/src/applications/files/services/files-trash-retention.service.ts+++ b/backend/src/applications/files/services/files-trash-retention.service.ts@@ -3,7 +3,7 @@ import { DB_TOKEN_PROVIDER } from '../../../infrastructure/database/constants'import { DBSchema } from '../../../infrastructure/database/interfaces/database.interface'import { FILE_REPOSITORY } from '../constants/operations'import { users } from '../../users/schemas/users.schema'-import { and, eq, inArray, lte, sql } from 'drizzle-orm'+import { and, eq, inArray, lte, SQL, sql } from 'drizzle-orm'import { USER_ROLE } from '../../users/constants/user'import { UserModel } from '../../users/models/user.model'import { isPathExists, isPathInside, removeFiles } from '../utils/files'@@ -226,7 +226,7 @@ export class FilesTrashRetention {private async dropTable(tableName: string): Promise<boolean> {try {- await this.db.execute(sql`DROP TABLE IF EXISTS ${sql.raw(tableName)} `)+ await this.db.execute(sql`DROP TABLE IF EXISTS ${sql.identifier(tableName)} `)return true} catch (e) {this.logger.error({ tag: this.dropTable.name, msg: `${tableName} : ${e}` })@@ -239,7 +239,7 @@ export class FilesTrashRetention {return new Map()}const [r]: { id: number; path: string; isDir: boolean }[][] = (await this.db.execute(- sql`SELECT id, path, isDir FROM ${sql.raw(tableName)} WHERE id IN (${sql.raw(ids.join(','))})`+ sql`SELECT id, path, isDir FROM ${sql.identifier(tableName)} WHERE id IN (${idsSqlList(ids)})`)) as MySqlQueryResultreturn new Map(r.map((row) => [row.id, { path: row.path, isDir: Boolean(row.isDir) }] satisfies [FileTrash['id'], FileTrashRecordMetadata]))}@@ -247,7 +247,7 @@ export class FilesTrashRetention {private async markRecordsSeen(tableName: string, ids: number[], runId: string): Promise<boolean> {if (!ids.length) return truetry {- await this.db.execute(sql`UPDATE ${sql.raw(tableName)} SET seen_run_id = ${runId} WHERE id IN (${sql.raw(ids.join(','))})`)+ await this.db.execute(sql`UPDATE ${sql.identifier(tableName)} SET seen_run_id = ${runId} WHERE id IN (${idsSqlList(ids)})`)return true} catch (e) {this.logger.error({ tag: this.markRecordsSeen.name, msg: `${tableName} : ${e}` })@@ -258,7 +258,7 @@ export class FilesTrashRetention {private async insertRecord(tableName: string, ft: FileTrash, runId: string): Promise<boolean> {try {await this.db.execute(sql`- INSERT INTO ${sql.raw(tableName)} (id, path, isDir, seen_run_id)+ INSERT INTO ${sql.identifier(tableName)} (id, path, isDir, seen_run_id)VALUES ${sql`(${ft.id}, ${ft.path}, ${ft.isDir}, ${runId})`}-- If an inode is reused or moved inside trash, restart retention for the new metadata.ON DUPLICATE KEY UPDATE path = VALUES(path),@@ -275,7 +275,7 @@ export class FilesTrashRetention {private async deleteUnseenRecords(tableName: string, runId: string): Promise<number | null> {try {- const [r] = await this.db.execute(sql`DELETE FROM ${sql.raw(tableName)} WHERE seen_run_id IS NULL OR seen_run_id <> ${runId}`)+ const [r] = await this.db.execute(sql`DELETE FROM ${sql.identifier(tableName)} WHERE seen_run_id IS NULL OR seen_run_id <> ${runId}`)return r.affectedRows ?? 0} catch (e) {this.logger.error({ tag: this.deleteUnseenRecords.name, msg: `${tableName} : ${e}` })@@ -341,14 +341,14 @@ export class FilesTrashRetention {}private async getExpiredRecords(tableName: string, isDir: boolean, retentionDays: number, ignoredIds: number[] = []): Promise<FileTrash[]> {- const ignoredRecordsFilter = ignoredIds.length ? sql`AND trash_record.id NOT IN (${sql.raw(ignoredIds.join(','))})` : sql``+ const ignoredRecordsFilter = ignoredIds.length ? sql`AND trash_record.id NOT IN (${idsSqlList(ignoredIds)})` : sql``const escapedRecordPath = sql`REPLACE(REPLACE(REPLACE(trash_record.path, '=', '=='), '%', '=%'), '_', '=_')`// Directories are eligible only after their indexed children are gone.// Once selected, removeFiles can delete any remaining filesystem content recursively.const emptyDirectoryFilter = isDir? sql`AND NOT EXISTS (SELECT 1- FROM ${sql.raw(tableName)} AS child+ FROM ${sql.identifier(tableName)} AS childWHERE child.id <> trash_record.idAND child.path LIKE CONCAT(${escapedRecordPath}, '/%') ESCAPE '=')`@@ -356,7 +356,7 @@ export class FilesTrashRetention {// Keep the predicate and ordering aligned with the (isDir, deletedAt) index.const req = sql`SELECT trash_record.id, trash_record.path, trash_record.isDir, trash_record.deletedAt- FROM ${sql.raw(tableName)} AS trash_record+ FROM ${sql.identifier(tableName)} AS trash_recordWHERE trash_record.deletedAt < DATE_SUB(CURRENT_DATE, INTERVAL ${retentionDays} DAY)AND trash_record.isDir = ${isDir}${ignoredRecordsFilter}@@ -373,7 +373,7 @@ export class FilesTrashRetention {return 0}try {- const [r] = await this.db.execute(sql`DELETE FROM ${sql.raw(tableName)} WHERE id IN (${sql.raw(ids.join(','))})`)+ const [r] = await this.db.execute(sql`DELETE FROM ${sql.identifier(tableName)} WHERE id IN (${idsSqlList(ids)})`)if (r.affectedRows !== ids.length) {this.logger.warn({ tag: this.deleteRecordsByIds.name, msg: `${tableName} - deleted : ${r.affectedRows}/${ids.length}` })}@@ -408,3 +408,10 @@ export class FilesTrashRetention {throw new Error(`${realPath} is outside trash path ${basePath}`)}}++function idsSqlList(ids: number[]): SQL {+ return sql.join(+ ids.map((id) => sql`${id}`),+ sql`, `+ )+}diff --git a/backend/src/applications/shares/services/shares-queries.service.ts b/backend/src/applications/shares/services/shares-queries.service.tsindex 9fe601ad..0cc58bb0 100644--- a/backend/src/applications/shares/services/shares-queries.service.ts+++ b/backend/src/applications/shares/services/shares-queries.service.ts@@ -270,7 +270,7 @@ export class SharesQueries {linkId: links.id,login: linkUsers.login,name: links.name,- type: sql.raw(`'${MEMBER_TYPE.USER}'`),+ type: sql`${MEMBER_TYPE.USER}`,description: links.email,permissions: shareMembers.permissions,createdAt: dateTimeUTC(shareMembers.createdAt)@@ -691,7 +691,7 @@ export class SharesQueries {const filters: SQL[] = [or(isNull(shares.ownerId), ne(shares.ownerId, sql.placeholder('userId')))]const fromUser = this.fromUserQuery(selectUnion, filters).$dynamic()const fromGroups = this.fromGroupsQuery(selectUnion, filters).$dynamic()- const fromAdminShares = this.fromAdminSharesQuery({ ...selectUnion, rootPermissions: sql.raw(`'${SHARE_ALL_OPERATIONS}'`) }, filters).$dynamic()+ const fromAdminShares = this.fromAdminSharesQuery({ ...selectUnion, rootPermissions: sql`${SHARE_ALL_OPERATIONS}` }, filters).$dynamic()for (const q of [fromUser, fromGroups, fromAdminShares]) {q.leftJoin(shareSpaceRoot, and(isNull(shares.externalPath), isNull(shares.fileId), eq(shareSpaceRoot.id, shares.spaceRootId))).leftJoin(@@ -707,7 +707,7 @@ export class SharesQueries {.leftJoin(childShare,and(- eq(sql.placeholder('withShares'), sql.raw('1')),+ eq(sql.placeholder('withShares'), sql`1`),eq(childShare.ownerId, sql.placeholder('userId')),eq(childShare.parentId, shares.id),or(@@ -722,11 +722,11 @@ export class SharesQueries {)))- .leftJoin(syncClients, and(eq(sql.placeholder('withSyncs'), sql.raw('1')), eq(syncClients.ownerId, sql.placeholder('userId'))))+ .leftJoin(syncClients, and(eq(sql.placeholder('withSyncs'), sql`1`), eq(syncClients.ownerId, sql.placeholder('userId')))).leftJoin(syncPaths,and(- eq(sql.placeholder('withSyncs'), sql.raw('1')),+ eq(sql.placeholder('withSyncs'), sql`1`),eq(syncPaths.clientId, syncClients.id),isNull(syncPaths.fileId),eq(syncPaths.shareId, shares.id)@@ -823,7 +823,7 @@ export class SharesQueries {const filters: SQL[] = [eq(shares.alias, sql.placeholder('shareAlias'))]const fromUser = this.fromUserQuery(selectUnion, filters).$dynamic()const fromGroups = this.fromGroupsQuery(selectUnion, filters).$dynamic()- const fromAdminShares = this.fromAdminSharesQuery({ ...selectUnion, permissions: sql.raw(`'${SHARE_ALL_OPERATIONS}'`) }, filters).$dynamic()+ const fromAdminShares = this.fromAdminSharesQuery({ ...selectUnion, permissions: sql`${SHARE_ALL_OPERATIONS}` }, filters).$dynamic()for (const q of [fromUser, fromGroups, fromAdminShares]) {q.leftJoin(shareSpaceRoot, and(isNull(shares.externalPath), isNull(shares.fileId), eq(shareSpaceRoot.id, shares.spaceRootId))).leftJoin(@@ -950,7 +950,7 @@ export class SharesQueries {}private fromAdminSharesQuery(select: SelectedFields<any, any>, filters: SQL[] = []) {- const where: SQL[] = [eq(sql.placeholder('isAdmin'), sql.raw('1')), isNull(shares.ownerId), ...filters]+ const where: SQL[] = [eq(sql.placeholder('isAdmin'), sql`1`), isNull(shares.ownerId), ...filters]return this.db.select(select).from(shares)diff --git a/backend/src/applications/spaces/schemas/spaces.schema.ts b/backend/src/applications/spaces/schemas/spaces.schema.tsindex 0c3e86ca..b2fa64d3 100644--- a/backend/src/applications/spaces/schemas/spaces.schema.ts+++ b/backend/src/applications/spaces/schemas/spaces.schema.ts@@ -31,5 +31,5 @@ export const spaces = mysqlTable((table) => [uniqueIndex('alias_idx').on(table.alias)])-export const spaceGroupConcatPermissions = (column: any, separator = SPACE_PERMS_SEP): SQL<string> =>- sql`IFNULL(GROUP_CONCAT(DISTINCT(IF(${column} = '', null, ${column})) SEPARATOR ${sql.raw(`'${separator}'`)}), '')`+export const spaceGroupConcatPermissions = (column: any): SQL<string> =>+ sql`IFNULL(GROUP_CONCAT(DISTINCT(IF(${column} = '', null, ${column})) SEPARATOR ${SPACE_PERMS_SEP}), '')`diff --git a/backend/src/applications/spaces/services/spaces-queries.service.ts b/backend/src/applications/spaces/services/spaces-queries.service.tsindex 7c9d7d1e..dcd38816 100644--- a/backend/src/applications/spaces/services/spaces-queries.service.ts+++ b/backend/src/applications/spaces/services/spaces-queries.service.ts@@ -179,7 +179,7 @@ export class SpacesQueries {id: linkUsers.id,linkId: links.id,name: links.name,- type: sql.raw(`'${MEMBER_TYPE.USER}'`),+ type: sql`${MEMBER_TYPE.USER}`,spaceRole: sql`${SPACE_ROLE.IS_MEMBER}`,description: links.email,permissions: otherMembers.permissions,@@ -294,18 +294,18 @@ export class SpacesQueries {.leftJoin(shares,and(- eq(sql.placeholder('withShares'), sql.raw('1')),+ eq(sql.placeholder('withShares'), sql`1`),eq(shares.ownerId, sql.placeholder('userId')),isNull(shares.fileId),isNull(shares.parentId),eq(shares.spaceRootId, spacesRoots.id)))- .leftJoin(syncClients, and(eq(sql.placeholder('withSyncs'), sql.raw('1')), eq(syncClients.ownerId, sql.placeholder('userId'))))+ .leftJoin(syncClients, and(eq(sql.placeholder('withSyncs'), sql`1`), eq(syncClients.ownerId, sql.placeholder('userId')))).leftJoin(syncPaths,and(- eq(sql.placeholder('withSyncs'), sql.raw('1')),+ eq(sql.placeholder('withSyncs'), sql`1`),eq(syncPaths.clientId, syncClients.id),eq(syncPaths.spaceId, sql.placeholder('spaceId')),eq(syncPaths.spaceRootId, spacesRoots.id),@@ -584,7 +584,7 @@ export class SpacesQueries {login: managers.login,name: userFullNameSQL(managers),description: managers.email,- type: sql.raw(`'${MEMBER_TYPE.USER}'`),+ type: sql`${MEMBER_TYPE.USER}`,spaceRole: spacesMembers.role,permissions: sql<string>`''`,createdAt: dateTimeUTC(spacesMembers.createdAt)@@ -632,7 +632,7 @@ export class SpacesQueries {login: managers.login,name: userFullNameSQL(managers),description: managers.email,- type: sql.raw(`'${MEMBER_TYPE.USER}'`),+ type: sql`${MEMBER_TYPE.USER}`,spaceRole: spacesMembers.role,permissions: sql<string>`''`,createdAt: dateTimeUTC(spacesMembers.createdAt)diff --git a/backend/src/applications/users/services/admin-users-queries.service.ts b/backend/src/applications/users/services/admin-users-queries.service.tsindex 0344a674..1869e897 100644--- a/backend/src/applications/users/services/admin-users-queries.service.ts+++ b/backend/src/applications/users/services/admin-users-queries.service.ts@@ -46,13 +46,12 @@ export class AdminUsersQueries {lastName: users.lastName,notification: users.notification,permissions: users.permissions,- twoFaEnabled:- sql<boolean>`JSON_UNQUOTE(JSON_EXTRACT(${users.secrets}, ${sql.raw(`'$.${USER_SECRET.TWO_FA_SECRET}'`)})) IS NOT NULL`.mapWith(Boolean),+ twoFaEnabled: sql<boolean>`JSON_UNQUOTE(JSON_EXTRACT(${users.secrets}, ${`$.${USER_SECRET.TWO_FA_SECRET}`})) IS NOT NULL`.mapWith(Boolean),groups: concatDistinctObjectsInArray(groups.id, {id: groups.id,name: groups.name,description: groups.description,- type: sql.raw(`'${MEMBER_TYPE.GROUP}'`),+ type: sql`${MEMBER_TYPE.GROUP}`,permissions: groups.permissions,createdAt: dateTimeUTC(usersGroups.createdAt)} satisfies Record<keyof Pick<Member, 'id' | 'name' | 'description' | 'type' | 'permissions' | 'createdAt'>, any>)diff --git a/backend/src/applications/users/services/users-queries.service.ts b/backend/src/applications/users/services/users-queries.service.tsindex 32876f84..9b6ca191 100644--- a/backend/src/applications/users/services/users-queries.service.ts+++ b/backend/src/applications/users/services/users-queries.service.ts@@ -278,7 +278,7 @@ export class UsersQueries {name: userFullNameSQL(users).as('name'),description: users.email,createdAt: usersGroups.createdAt,- type: sql<MEMBER_TYPE>`IF(${users.role} = ${USER_ROLE.GUEST}, ${sql.raw(`'${MEMBER_TYPE.GUEST}'`)}, ${sql.raw(`'${MEMBER_TYPE.USER}'`)})`,+ type: sql<MEMBER_TYPE>`IF(${users.role} = ${USER_ROLE.GUEST}, ${MEMBER_TYPE.GUEST}, ${MEMBER_TYPE.USER})`,groupRole: sql<USER_GROUP_ROLE>`${usersGroups.role}`} satisfies Member | SelectedFields<any, any>).from(groups)@@ -335,13 +335,13 @@ export class UsersQueries {description: groups.description,createdAt: groups.createdAt,modifiedAt: groups.modifiedAt,- type: sql<MEMBER_TYPE>`IF(${groups.type} = ${GROUP_TYPE.USER}, ${sql.raw(`'${MEMBER_TYPE.GROUP}'`)}, ${sql.raw(`'${MEMBER_TYPE.PGROUP}'`)})`,+ type: sql<MEMBER_TYPE>`IF(${groups.type} = ${GROUP_TYPE.USER}, ${MEMBER_TYPE.GROUP}, ${MEMBER_TYPE.PGROUP})`,members: concatDistinctObjectsInArray(users.id, {id: users.id,login: users.login,name: userFullNameSQL(users),description: users.email,- type: sql<MEMBER_TYPE>`IF(${users.role} = ${USER_ROLE.GUEST}, ${sql.raw(`'${MEMBER_TYPE.GUEST}'`)}, ${sql.raw(`'${MEMBER_TYPE.USER}'`)})`,+ type: sql<MEMBER_TYPE>`IF(${users.role} = ${USER_ROLE.GUEST}, ${MEMBER_TYPE.GUEST}, ${MEMBER_TYPE.USER})`,groupRole: usersGroupsAlias.role,createdAt: dateTimeUTC(usersGroupsAlias.createdAt)} satisfies Record<keyof Pick<Member, 'id' | 'name' | 'login' | 'description' | 'type' | 'groupRole' | 'createdAt'>, any>)@@ -467,7 +467,7 @@ export class UsersQueries {id: managersAlias.id,login: managersAlias.login,name: userFullNameSQL(managersAlias),- type: sql.raw(`'${MEMBER_TYPE.USER}'`),+ type: sql`${MEMBER_TYPE.USER}`,description: managersAlias.email,createdAt: dateTimeUTC(managersGuestAlias.createdAt)} satisfies Record<keyof Pick<Member, 'id' | 'name' | 'login' | 'description' | 'type' | 'createdAt'>, any>),@@ -476,7 +476,7 @@ export class UsersQueries {id: groups.id,name: groups.name,description: groups.description,- type: sql.raw(`'${MEMBER_TYPE.PGROUP}'`),+ type: sql`${MEMBER_TYPE.PGROUP}`,permissions: groups.permissions,createdAt: dateTimeUTC(usersGroups.createdAt)} satisfies Record<keyof Pick<Member, 'id' | 'name' | 'description' | 'type' | 'permissions' | 'createdAt'>, any>)@@ -549,7 +549,7 @@ export class UsersQueries {ON ${usersGroups.userId} = ${users.id}INNER JOIN visible_groupsON visible_groups.id = ${usersGroups.groupId}- WHERE ${users.role} <= ${sql.raw(`${lowerOrEqualUserRole}`)}+ WHERE ${users.role} <= ${lowerOrEqualUserRole}UNION@@ -559,7 +559,7 @@ export class UsersQueries {WHERE${showUngroupedUsers} = 1AND- ${users.role} <= ${sql.raw(`${lowerOrEqualUserRole}`)}+ ${users.role} <= ${lowerOrEqualUserRole}-- Guests without (personal) groups are not globally visible.-- They are allowed only through part 1 (shared visible group) or part 3 (manager relation).AND ${users.role} != ${USER_ROLE.GUEST}diff --git a/backend/src/infrastructure/database/utils.spec.ts b/backend/src/infrastructure/database/utils.spec.tsnew file mode 100644index 00000000..c0138811--- /dev/null+++ b/backend/src/infrastructure/database/utils.spec.ts@@ -0,0 +1,16 @@+import { MySqlDialect, mysqlTable, varchar } from 'drizzle-orm/mysql-core'+import { concatDistinctObjectsInArray } from './utils'++describe(concatDistinctObjectsInArray.name, () => {+ const dialect = new MySqlDialect()+ const records = mysqlTable('records', { value: varchar('value', { length: 255 }) })++ it('should bind JSON object keys instead of interpolating them', () => {+ const key = "name', (SELECT password FROM users), 'value"++ const query = dialect.sqlToQuery(concatDistinctObjectsInArray(records.value, { [key]: records.value }))++ expect(query.sql).not.toContain(key)+ expect(query.params).toContain(key)+ })+})diff --git a/backend/src/infrastructure/database/utils.ts b/backend/src/infrastructure/database/utils.tsindex 42dd5407..ed9ccb58 100644--- a/backend/src/infrastructure/database/utils.ts+++ b/backend/src/infrastructure/database/utils.ts@@ -73,13 +73,13 @@ export function concatDistinctObjectsInArray(mustBeNotNull: Column, object: Recoconst keys = Object.keys(object)keys.forEach((k, index) => {if (object[k].constructor.name === 'Object') {- expr.append(sql`${sql.raw(`'${k}'`)},JSON_OBJECT(`)+ expr.append(sql`${k},JSON_OBJECT(`)const subKeys = Object.keys(object[k])subKeys.forEach((subK, subIndex) => {- expr.append(sql`${sql.raw(`'${subK}'`)},${sql`${object[k][subK]}`}${subIndex === subKeys.length - 1 ? sql`)` : sql`,`}`)+ expr.append(sql`${subK},${sql`${object[k][subK]}`}${subIndex === subKeys.length - 1 ? sql`)` : sql`,`}`)})} else {- expr.append(sql`${sql.raw(`'${k}'`)},${sql`${object[k]}`}`)+ expr.append(sql`${k},${sql`${object[k]}`}`)}if (index !== keys.length - 1) {expr.append(sql`,`)