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:files): parameterize child path SQL operations
johavenbf9c5ef1ea6b
diff --git a/backend/src/applications/files/schemas/files.schema.spec.ts b/backend/src/applications/files/schemas/files.schema.spec.tsnew file mode 100644index 00000000..bf4a4aaf--- /dev/null+++ b/backend/src/applications/files/schemas/files.schema.spec.ts@@ -0,0 +1,37 @@+import { sql } from 'drizzle-orm'+import { MySqlDialect } from 'drizzle-orm/mysql-core'+import { childFilesMatch, childFilesReplacePath, files } from './files.schema'++describe('files path SQL helpers', () => {+ const dialect = new MySqlDialect()++ it('binds paths instead of interpolating them into the SQL statement', () => {+ const srcPath = "source\\'directory"+ const dstPath = "destination\\'directory"+ const query = dialect.sqlToQuery(+ sql`UPDATE ${files} SET ${files.path} = ${childFilesReplacePath(srcPath, dstPath)} WHERE ${childFilesMatch(srcPath)}`+ )++ expect(query.sql).not.toContain(srcPath)+ expect(query.sql).not.toContain(dstPath)+ expect(query.sql).not.toContain('REGEXP')+ expect(query.params).toEqual([dstPath, srcPath, srcPath, srcPath])+ })++ it('matches an exact path or a slash-separated descendant without ignoring trailing spaces', () => {+ const path = 'documents/project '+ const query = dialect.sqlToQuery(childFilesMatch(path))++ expect(query.sql).toBe("LEFT(CONCAT(`files`.`path`, '/'), CHAR_LENGTH(?) + 1) = CONCAT(?, '/')")+ expect(query.params).toEqual([path, path])+ })++ it('replaces only the matched source prefix and preserves its suffix', () => {+ const srcPath = 'documents/project'+ const dstPath = 'archives/project'+ const query = dialect.sqlToQuery(childFilesReplacePath(srcPath, dstPath))++ expect(query.sql).toBe('CONCAT(?, SUBSTRING(`files`.`path`, CHAR_LENGTH(?) + 1))')+ expect(query.params).toEqual([dstPath, srcPath])+ })+})diff --git a/backend/src/applications/files/schemas/files.schema.ts b/backend/src/applications/files/schemas/files.schema.tsindex 19632db7..f49e3e15 100644--- a/backend/src/applications/files/schemas/files.schema.ts+++ b/backend/src/applications/files/schemas/files.schema.ts@@ -1,6 +1,5 @@import { SQL, sql, SQLWrapper } from 'drizzle-orm'import { AnyMySqlColumn, bigint, boolean, index, mysqlTable, varchar } from 'drizzle-orm/mysql-core'-import { escapeSQLRegexp } from '../../../common/functions'import { shares } from '../../shares/schemas/shares.schema'import { spacesRoots } from '../../spaces/schemas/spaces-roots.schema'import { spaces } from '../../spaces/schemas/spaces.schema'@@ -46,10 +45,13 @@ export const files = mysqlTable(// supports the case where path = '.' or './sync-in' and removes '.' or './' if existsexport const filePathSQL = (file: any): SQL<string> => sql`REGEXP_REPLACE(CONCAT(${file.path}, '/', ${file.name}), '^(\\\\.\\\\/){0,1}(.*)', '\\\\2')`-export const childPathFindRegexp = (pathSQL: SQLWrapper, path: string): SQL<string> =>- sql`${pathSQL}${sql.raw(`REGEXP '^${escapeSQLRegexp(path)}(/|$)'`)}`+// Appending '/' handles both an exact path and its descendants while keeping trailing spaces significant with PAD SPACE collations.+export const childPathMatch = (pathSQL: SQLWrapper, path: string): SQL<string> =>+ sql`LEFT(CONCAT(${pathSQL}, '/'), CHAR_LENGTH(${path}) + 1) = CONCAT(${path}, '/')`-export const childFilesFindRegexp = (path: string): SQL<string> => childPathFindRegexp(files.path, path)+// Applies the literal path-boundary match to the path stored on file records.+export const childFilesMatch = (path: string): SQL<string> => childPathMatch(files.path, path)-export const childFilesReplaceRegexp = (srcPath: string, dstPath: string): SQL<string> =>- sql`REGEXP_REPLACE(${files.path}, '^${sql.raw(escapeSQLRegexp(srcPath))}', '${sql.raw(escapeSQLRegexp(dstPath))}')`+// Replaces a previously matched source prefix while preserving the remaining child-path suffix.+export const childFilesReplacePath = (srcPath: string, dstPath: string): SQL<string> =>+ sql`CONCAT(${dstPath}, SUBSTRING(${files.path}, CHAR_LENGTH(${srcPath}) + 1))`diff --git a/backend/src/applications/files/services/files-queries.service.ts b/backend/src/applications/files/services/files-queries.service.tsindex f84296c2..bae54131 100644--- a/backend/src/applications/files/services/files-queries.service.ts+++ b/backend/src/applications/files/services/files-queries.service.ts@@ -14,7 +14,7 @@ import { FileProps } from '../interfaces/file-props.interface'import type { FileRecent, FileRecentLocation, FileRecentUpdate } from '../schemas/file-recent.interface'import { File } from '../schemas/file.interface'import { filesRecents } from '../schemas/files-recents.schema'-import { childFilesFindRegexp, childFilesReplaceRegexp, childPathFindRegexp, filePathSQL, files } from '../schemas/files.schema'+import { childFilesMatch, childFilesReplacePath, childPathMatch, filePathSQL, files } from '../schemas/files.schema'import { dirName, fileName, isPathInside } from '../utils/files'@Injectable()@@ -186,7 +186,7 @@ export class FilesQueries {}// prepare (or not) the child files update/delete- const childFiles = isDir ? childFilesFindRegexp(props.path) : null+ const childFiles = isDir ? childFilesMatch(props.path) : nullif (fileProps.inTrash || force) {// delete fileawait this.db.delete(files).where(and(...convertToWhere(files, fileProps)))@@ -247,10 +247,10 @@ export class FilesQueries {.where(and(...convertToWhere(files, srcFileDB)))if (isDir) {// prepare child file props update- const childFiles: SQL<string> = childFilesFindRegexp(srcProps.path)+ const childFiles: SQL<string> = childFilesMatch(srcProps.path)const childProps: Omit<FileDBProps, 'path'> & { path: SQL<string> } = {...dstCommonProps,- path: childFilesReplaceRegexp(srcProps.path, dstProps.path)+ path: childFilesReplacePath(srcProps.path, dstProps.path)}// update child file propsawait this.db@@ -341,7 +341,7 @@ export class FilesQueries {try {await this.db.delete(filesRecents)- .where(and(...where, or(...batch.map((sourcePath) => childPathFindRegexp(filePathSQL(filesRecents), sourcePath)))))+ .where(and(...where, or(...batch.map((sourcePath) => childPathMatch(filePathSQL(filesRecents), sourcePath)))))} catch (e) {this.logger.error({ tag: this.deleteRecents.name, msg: `${e}` })}diff --git a/backend/src/common/functions.ts b/backend/src/common/functions.tsindex 851ad6cd..2d09ba48 100755--- a/backend/src/common/functions.ts+++ b/backend/src/common/functions.ts@@ -12,8 +12,6 @@ import { decodeUrl } from './shared'const DUMMY_PASSWORD_HASH = '$2a$10$tjgA0v/cGe.vAfAJgNHpZeNrIdMxu82i0kGEjbtYkaVUCDkVzHRjG'export const regexpEscape = /[.*+?^${}()|[\]\\]/g-export const regexSpecialChars = /[-[\]{}()*+!<=:?./\\^$|#,]/g-export const regexSpecialCharsWithSpace = /[-[\]{}()*+!<=:?./\\^$|#\s,]/gexport async function loadOptionalModule(moduleName: string): Promise<any> {return await import(moduleName)@@ -23,14 +21,6 @@ export async function sleep(ms: number): Promise<void> {await setTimeout(ms)}-export function escapeSQLRegexp(input: string): string {- return input.replace(regexSpecialCharsWithSpace, '\\\\$&').replaceAll("'", "''")-}--export function escapeString(input: string): string {- return input.replace(regexSpecialChars, '\\$&')-}-export function escapePath(path: string): string {return path.replace(regexpEscape, '\\$&')}