Skip to content

Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.

fix(backend:files): bound search result limit

johaven4df1b8dac26f
diff --git a/backend/src/applications/files/constants/search.ts b/backend/src/applications/files/constants/search.ts
new file mode 100644
index 00000000..28775a30
--- /dev/null
+++ b/backend/src/applications/files/constants/search.ts
@@ -0,0 +1,3 @@
+export const SEARCH_FILES_MIN_LIMIT = 1
+export const SEARCH_FILES_DEFAULT_LIMIT = 100
+export const SEARCH_FILES_MAX_LIMIT = 100
diff --git a/backend/src/applications/files/dto/file-operations.dto.ts b/backend/src/applications/files/dto/file-operations.dto.ts
index 23c43e42..1fe721fd 100644
--- a/backend/src/applications/files/dto/file-operations.dto.ts
+++ b/backend/src/applications/files/dto/file-operations.dto.ts
@@ -1,8 +1,9 @@
import { Transform } from 'class-transformer'
-import { ArrayMinSize, IsArray, IsBoolean, IsDefined, IsIn, IsInt, IsNotEmpty, IsOptional, IsString, IsUrl } from 'class-validator'
+import { ArrayMinSize, IsArray, IsBoolean, IsDefined, IsIn, IsInt, IsNotEmpty, IsOptional, IsString, IsUrl, Max, Min } from 'class-validator'
import { RejectIfMatch } from '../../../common/decorators'
import { regExpInvalidFileName } from '../../../common/shared'
import { TAR_EXTENSION, ZIP_EXTENSION } from '../constants/compress'
+import { SEARCH_FILES_DEFAULT_LIMIT, SEARCH_FILES_MAX_LIMIT, SEARCH_FILES_MIN_LIMIT } from '../constants/search'
export class CopyMoveFileDto {
@IsNotEmpty()
@@ -78,5 +79,7 @@ export class SearchFilesDto {
@IsOptional()
@IsInt()
- limit?: number = 100
+ @Min(SEARCH_FILES_MIN_LIMIT)
+ @Max(SEARCH_FILES_MAX_LIMIT)
+ limit?: number = SEARCH_FILES_DEFAULT_LIMIT
}
diff --git a/backend/src/applications/files/services/files-search-manager.service.spec.ts b/backend/src/applications/files/services/files-search-manager.service.spec.ts
index 88f806d8..309ec916 100644
--- a/backend/src/applications/files/services/files-search-manager.service.spec.ts
+++ b/backend/src/applications/files/services/files-search-manager.service.spec.ts
@@ -111,6 +111,16 @@ describe(FilesSearchManager.name, () => {
expect(result).toEqual([fileContent('report.pdf')])
})
+ it('should normalize search limit before routing', async () => {
+ const nameSearchSpy = vi.spyOn(service as any, 'searchFileNames').mockResolvedValue([])
+
+ await service.search({ id: 3, isAdmin: false } as any, { content: 'report', fullText: false, limit: 1000 } as any)
+ await service.search({ id: 3, isAdmin: false } as any, { content: 'report', fullText: false, limit: 0 } as any)
+ await service.search({ id: 3, isAdmin: false } as any, { content: 'report', fullText: false } as any)
+
+ expect(nameSearchSpy.mock.calls.map(([, , , , limit]) => limit)).toEqual([100, 1, 100])
+ })
+
it('should return empty results when no full-text index exists', async () => {
filesIndexer.existingIndexes.mockResolvedValueOnce([])
@@ -125,14 +135,11 @@ describe(FilesSearchManager.name, () => {
filesIndexer.existingIndexes.mockResolvedValueOnce(['user_5'])
filesIndexer.searchRecords.mockRejectedValueOnce(new Error('Invalid regular expression: /[/'))
- try {
- await (service as any).searchFullText(5, [], [], '[', 5)
- fail('searchFullText should throw')
- } catch (e) {
- expect(e).toBeInstanceOf(HttpException)
- expect((e as HttpException).getStatus()).toBe(HttpStatus.BAD_REQUEST)
- expect((e as HttpException).message).toBe('SyntaxError (check special characters)')
- }
+ const error = await (service as any).searchFullText(5, [], [], '[', 5).catch((e: HttpException) => e)
+
+ expect(error).toBeInstanceOf(HttpException)
+ expect(error.getStatus()).toBe(HttpStatus.BAD_REQUEST)
+ expect(error.message).toBe('SyntaxError (check special characters)')
})
it('should stop filename search when the limit is reached', async () => {
diff --git a/backend/src/applications/files/services/files-search-manager.service.ts b/backend/src/applications/files/services/files-search-manager.service.ts
index bee83647..c605c4da 100644
--- a/backend/src/applications/files/services/files-search-manager.service.ts
+++ b/backend/src/applications/files/services/files-search-manager.service.ts
@@ -10,7 +10,7 @@ import { SearchFilesDto } from '../dto/file-operations.dto'
import { FilesContentStore } from '../models/files-content-store'
import { FileContent } from '../schemas/file-content.interface'
import { dirName, fileName, getMimeType } from '../utils/files'
-import { genRegexPositiveAndNegativeTerms } from '../utils/files-search'
+import { genRegexPositiveAndNegativeTerms, normalizeSearchLimit } from '../utils/files-search'
import { FilesContentParser } from './files-content-parser.service'
import { FILE_REPOSITORY } from '../constants/operations'
import { genIndexingKey } from '../utils/indexing'
@@ -31,11 +31,12 @@ export class FilesSearchManager {
if (search.fullText && !configuration.applications.files.contentIndexing.enabled) {
throw new HttpException('Full-text search is disabled', HttpStatus.BAD_REQUEST)
}
+ const limit = normalizeSearchLimit(search.limit)
const [spaceIds, shareIds] = await Promise.all([this.spacesQueries.spaceIds(user.id), this.sharesQueries.shareIds(user.id, +user.isAdmin)])
if (search.fullText) {
- return await this.searchFullText(user.id, spaceIds, shareIds, search.content, search.limit)
+ return await this.searchFullText(user.id, spaceIds, shareIds, search.content, limit)
} else {
- return await this.searchFileNames(user.id, spaceIds, shareIds, search.content, search.limit)
+ return await this.searchFileNames(user.id, spaceIds, shareIds, search.content, limit)
}
}
diff --git a/backend/src/applications/files/utils/files-search.spec.ts b/backend/src/applications/files/utils/files-search.spec.ts
index 98de927e..bdc0d4c4 100644
--- a/backend/src/applications/files/utils/files-search.spec.ts
+++ b/backend/src/applications/files/utils/files-search.spec.ts
@@ -4,6 +4,7 @@ import {
genTermsPattern,
likeSearchTermStartPattern,
MaxSortedList,
+ normalizeSearchLimit,
parseSearchTerms,
requiresLikeSearch
} from './files-search'
@@ -69,6 +70,15 @@ describe('files search utilities', () => {
})
})
+ describe(normalizeSearchLimit.name, () => {
+ it('should keep search limits within the accepted range', () => {
+ expect(normalizeSearchLimit(25)).toBe(25)
+ expect(normalizeSearchLimit(1000)).toBe(100)
+ expect(normalizeSearchLimit(0)).toBe(1)
+ expect(normalizeSearchLimit()).toBe(100)
+ })
+ })
+
describe(genTermsPattern.name, () => {
it('should generate an accent-insensitive alternative pattern', () => {
const regexp = new RegExp(`^(${genTermsPattern(['resume', 'canyon'])})$`, 'iu')
diff --git a/backend/src/applications/files/utils/files-search.ts b/backend/src/applications/files/utils/files-search.ts
index 8145aeab..efcae78b 100644
--- a/backend/src/applications/files/utils/files-search.ts
+++ b/backend/src/applications/files/utils/files-search.ts
@@ -1,5 +1,6 @@
import { regexpEscape } from '../../../common/functions'
import { MIN_CHARS_TO_SEARCH } from '../constants/indexing'
+import { SEARCH_FILES_DEFAULT_LIMIT, SEARCH_FILES_MAX_LIMIT, SEARCH_FILES_MIN_LIMIT } from '../constants/search'
const regexMatchSearchBoolean = new RegExp(`([+-]?)(?:"([^"]+)"|(\\S+))`)
const regexMatchesSearchBoolean = new RegExp(regexMatchSearchBoolean.source, 'g')
@@ -135,6 +136,11 @@ export function likeSearchTermStartPattern(): string {
return `(?=${LIKE_SEARCH_CHAR})`
}
+export function normalizeSearchLimit(limit?: number): number {
+ if (!Number.isInteger(limit)) return SEARCH_FILES_DEFAULT_LIMIT
+ return Math.min(Math.max(limit, SEARCH_FILES_MIN_LIMIT), SEARCH_FILES_MAX_LIMIT)
+}
+
function termBoundaryPattern(term: string, endBoundary = false): string {
if (requiresLikeSearch(term)) {
return term