-
Notifications
You must be signed in to change notification settings - Fork 1
[996] Case Study Search Panel #252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
website/modules/case-studies-page/services/SearchService.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /** | ||
| * SearchService - Shared search term normalization and safe regex building | ||
| * | ||
| * Used by case-studies-page index query and NavigationService so search | ||
| * behavior and escaping stay consistent and safe (ReDoS prevention). | ||
| */ | ||
|
|
||
| const REGEX_ESCAPE = /[$()*+.?[\\\]^{|}]/gu; | ||
|
|
||
| const MAX_SEARCH_TERM_LENGTH = 200; | ||
|
|
||
| /** | ||
| * Normalizes search param from query (handles missing, array, non-string) | ||
| * @param {Object} queryParams - Request query object (e.g. req.query) | ||
| * @returns {string} Trimmed search string, or empty string | ||
| */ | ||
| const getSearchTerm = function (queryParams) { | ||
| if (!queryParams || !queryParams.search) { | ||
| return ''; | ||
| } | ||
| const raw = queryParams.search; | ||
| let value = raw; | ||
| if (Array.isArray(raw)) { | ||
| [value] = raw; | ||
| } | ||
| if (typeof value !== 'string') { | ||
| return ''; | ||
| } | ||
| return value.trim(); | ||
| }; | ||
|
|
||
| /** | ||
| * Builds a safe MongoDB regex pattern from search term (escape + word match) | ||
| * Search term is capped at MAX_SEARCH_TERM_LENGTH to avoid pathologically long patterns | ||
| * @param {string} searchTerm - User search string | ||
| * @returns {string|null} Pattern for $regex, or null if no search | ||
| */ | ||
| const buildSearchRegexPattern = function (searchTerm) { | ||
| if (!searchTerm || !searchTerm.trim()) { | ||
| return null; | ||
| } | ||
| const trimmed = searchTerm.trim(); | ||
| if (!trimmed) { | ||
| return null; | ||
| } | ||
| let capped = trimmed; | ||
| if (trimmed.length > MAX_SEARCH_TERM_LENGTH) { | ||
| capped = trimmed.slice(0, MAX_SEARCH_TERM_LENGTH); | ||
| } | ||
| const escaped = capped.replace(REGEX_ESCAPE, '\\$&'); | ||
| return escaped.split(/\s+/u).join('.*'); | ||
| }; | ||
|
|
||
| /** | ||
| * Builds MongoDB $or condition for case study search (single source of truth for searchable fields) | ||
| * @param {string} searchTerm - User search string | ||
| * @returns {Object|null} Condition to pass to query.and(), or null if no search | ||
| */ | ||
| const buildSearchCondition = function (searchTerm) { | ||
| const regexPattern = buildSearchRegexPattern(searchTerm); | ||
| if (!regexPattern) { | ||
| return null; | ||
| } | ||
| const regexOpts = { $regex: regexPattern, $options: 'i' }; | ||
| return { | ||
| $or: [{ title: regexOpts }, { portfolioTitle: regexOpts }], | ||
| }; | ||
| }; | ||
|
|
||
| module.exports = { | ||
| buildSearchCondition, | ||
| buildSearchRegexPattern, | ||
| getSearchTerm, | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.