-
-
Notifications
You must be signed in to change notification settings - Fork 13
Enhance performance and security across router and query parameter handling #43
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
7 commits
Select commit
Hold shift + click to select a range
4aa2e15
Enhance performance and security across router and query parameter ha…
jkyberneees 426f3b5
linting
jkyberneees 5ffc14d
Update lib/utils/queryparams.js
jkyberneees b74dcaa
Update tooling/advanced-pollution-test.js
jkyberneees a8c47d4
linting fix
jkyberneees fafa866
Refactor route handler definition for prototype pollution test
jkyberneees ef9e6e8
Remove pre-created EMPTY_PARAMS object and use Object.create(null)
jkyberneees 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 was deleted.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -1,20 +1,58 @@ | ||
| // Pre-create Set for dangerous properties - faster O(1) lookup vs string comparisons | ||
| const DANGEROUS_PROPERTIES = new Set(['__proto__', 'constructor', 'prototype']) | ||
|
|
||
| // Pre-created empty query object to avoid allocations | ||
| const EMPTY_QUERY = Object.freeze(Object.create(null)) | ||
|
|
||
| module.exports = (req, url) => { | ||
| const query = {} | ||
| const indexOfQuestionMark = url.indexOf('?') | ||
| const path = indexOfQuestionMark !== -1 ? url.slice(0, indexOfQuestionMark) : url | ||
| const search = indexOfQuestionMark !== -1 ? url.slice(indexOfQuestionMark + 1) : '' | ||
|
|
||
| if (search.length > 0) { | ||
| const searchParams = new URLSearchParams(search.replace(/\[\]=/g, '=')) | ||
| for (const [name, value] of searchParams.entries()) { | ||
| if (query[name]) { | ||
| Array.isArray(query[name]) ? query[name].push(value) : (query[name] = [query[name], value]) | ||
| // Single indexOf call - more efficient than multiple operations | ||
| const questionMarkIndex = url.indexOf('?') | ||
|
|
||
| if (questionMarkIndex === -1) { | ||
| // Fast path: no query string | ||
| req.path = url | ||
| req.query = EMPTY_QUERY | ||
| return | ||
| } | ||
|
|
||
| // Use Object.create(null) for prototype pollution protection | ||
| const query = Object.create(null) | ||
|
|
||
| // Extract path and search in one operation each | ||
| req.path = url.slice(0, questionMarkIndex) | ||
| const search = url.slice(questionMarkIndex + 1) | ||
|
|
||
| if (search.length === 0) { | ||
| // Fast path: empty query string | ||
| req.query = query | ||
| return | ||
| } | ||
|
|
||
| // Process query parameters with optimized URLSearchParams handling | ||
| const searchParams = new URLSearchParams(search.replace(/\[\]=/g, '=')) | ||
|
|
||
| for (const [name, value] of searchParams.entries()) { | ||
| // Split parameter name into segments by dot or bracket notation | ||
| /* eslint-disable-next-line */ | ||
|
||
| const segments = name.split(/[\.\[\]]+/).filter(Boolean) | ||
|
|
||
| // Check each segment against the dangerous properties set | ||
| if (segments.some(segment => DANGEROUS_PROPERTIES.has(segment))) { | ||
| continue // Skip dangerous property names | ||
| } | ||
|
|
||
| const existing = query[name] | ||
| if (existing !== undefined) { | ||
| // Optimized array handling - check type once, then branch | ||
| if (Array.isArray(existing)) { | ||
| existing.push(value) | ||
| } else { | ||
| query[name] = value | ||
| query[name] = [existing, value] | ||
| } | ||
| } else { | ||
| query[name] = value | ||
| } | ||
| } | ||
|
|
||
| req.path = path | ||
| req.query = query | ||
| } | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider normalizing req.method (e.g., using toUpperCase()) when constructing the cache key to ensure consistency regardless of method casing.