feat(fts): update imports from breaking up large fts_search file

This commit is contained in:
perfectra1n 2025-11-28 20:59:50 -08:00
parent 9940ee3bee
commit 574a3441ee
6 changed files with 13 additions and 67 deletions

View File

@ -10,7 +10,7 @@ import cls from "../../services/cls.js";
import attributeFormatter from "../../services/attribute_formatter.js"; import attributeFormatter from "../../services/attribute_formatter.js";
import ValidationError from "../../errors/validation_error.js"; import ValidationError from "../../errors/validation_error.js";
import type SearchResult from "../../services/search/search_result.js"; import type SearchResult from "../../services/search/search_result.js";
import ftsSearchService from "../../services/search/fts_search.js"; import { ftsSearchService } from "../../services/search/fts/index.js";
import log from "../../services/log.js"; import log from "../../services/log.js";
import hoistedNoteService from "../../services/hoisted_note.js"; import hoistedNoteService from "../../services/hoisted_note.js";
import beccaService from "../../becca/becca_service.js"; import beccaService from "../../becca/becca_service.js";

View File

@ -20,7 +20,7 @@ import {
getRegex, getRegex,
FUZZY_SEARCH_CONFIG FUZZY_SEARCH_CONFIG
} from "../utils/text_utils.js"; } from "../utils/text_utils.js";
import ftsSearchService, { FTSError, FTSQueryError } from "../fts_search.js"; import { ftsSearchService, FTSError, FTSQueryError } from "../fts/index.js";
const ALLOWED_OPERATORS = new Set(["=", "!=", "*=*", "*=", "=*", "%=", "~=", "~*"]); const ALLOWED_OPERATORS = new Set(["=", "!=", "*=*", "*=", "=*", "%=", "~=", "~*"]);

View File

@ -77,12 +77,6 @@ export function sanitizeFTS5Token(token: string): string {
return "__empty_token__"; return "__empty_token__";
} }
// Additional validation: ensure token doesn't contain SQL injection attempts
if (sanitized.includes(';') || sanitized.includes('--')) {
log.error(`Potential SQL injection attempt detected in token: "${token}"`);
return "__invalid_token__";
}
return sanitized; return sanitized;
} }

View File

@ -14,7 +14,7 @@
*/ */
import { describe, it, expect, beforeEach, vi } from "vitest"; import { describe, it, expect, beforeEach, vi } from "vitest";
import { ftsSearchService } from "./fts_search.js"; import { ftsSearchService } from "./fts/index.js";
import searchService from "./services/search.js"; import searchService from "./services/search.js";
import BNote from "../../becca/entities/bnote.js"; import BNote from "../../becca/entities/bnote.js";
import BBranch from "../../becca/entities/bbranch.js"; import BBranch from "../../becca/entities/bbranch.js";

View File

@ -55,7 +55,7 @@ describe('FTS5 Search Service Improvements', () => {
vi.doMock('../protected_session.js', () => ({ default: mockProtectedSession })); vi.doMock('../protected_session.js', () => ({ default: mockProtectedSession }));
// Import the service after mocking // Import the service after mocking
const module = await import('./fts_search.js'); const module = await import('./fts/index.js');
ftsSearchService = module.ftsSearchService; ftsSearchService = module.ftsSearchService;
}); });
@ -151,15 +151,15 @@ describe('FTS5 Search Service Improvements', () => {
); );
}); });
it('should detect potential SQL injection attempts', () => { it('should allow tokens with semicolons and dashes (valid search content)', () => {
mockSql.getValue.mockReturnValue(1); mockSql.getValue.mockReturnValue(1);
// Users may search for SQL code snippets or other content containing these characters
const query = ftsSearchService.convertToFTS5Query(['test; DROP TABLE'], '='); const query = ftsSearchService.convertToFTS5Query(['test; DROP TABLE'], '=');
expect(query).toContain('__invalid_token__'); // Should preserve the content, not reject it
expect(mockLog.error).toHaveBeenCalledWith( expect(query).toBe('"test; DROP TABLE"');
expect.stringContaining('Potential SQL injection attempt detected') expect(query).not.toContain('__invalid_token__');
);
}); });
it('should properly sanitize valid tokens', () => { it('should properly sanitize valid tokens', () => {
@ -268,7 +268,7 @@ describe('searchWithLike - Substring Search with LIKE Queries', () => {
vi.doMock('../protected_session.js', () => ({ default: mockProtectedSession })); vi.doMock('../protected_session.js', () => ({ default: mockProtectedSession }));
// Import the service after mocking // Import the service after mocking
const module = await import('./fts_search.js'); const module = await import('./fts/index.js');
ftsSearchService = module.ftsSearchService; ftsSearchService = module.ftsSearchService;
}); });
@ -1320,7 +1320,7 @@ describe('Exact Match with Word Boundaries (= operator)', () => {
vi.doMock('../protected_session.js', () => ({ default: mockProtectedSession })); vi.doMock('../protected_session.js', () => ({ default: mockProtectedSession }));
// Import the service after mocking // Import the service after mocking
const module = await import('./fts_search.js'); const module = await import('./fts/index.js');
ftsSearchService = module.ftsSearchService; ftsSearchService = module.ftsSearchService;
}); });

View File

@ -1,48 +0,0 @@
/**
* FTS5 Search Service
*
* This module re-exports from the fts/ folder for backward compatibility.
* New code should import directly from './fts/index.js' or './fts/<module>.js'.
*/
export {
// Error classes
FTSError,
FTSQueryError,
// Types and configuration
FTS_CONFIG,
type FTSSearchResult,
type FTSSearchOptions,
type FTSErrorInfo,
type FTSIndexStats,
// Query building utilities
convertToFTS5Query,
sanitizeFTS5Token,
escapeLikeWildcards,
containsExactPhrase,
generateSnippet,
// Index management
assertFTS5Available,
checkFTS5Availability,
updateNoteIndex,
removeNoteFromIndex,
syncMissingNotes,
rebuildIndex,
getIndexStats,
filterNonProtectedNoteIds,
// Search operations
searchWithLike,
searchSync,
searchAttributesSync,
searchProtectedNotesSync,
// Legacy class-based API
ftsSearchService
} from "./fts/index.js";
// Default export for backward compatibility
export { default } from "./fts/index.js";