Repository navigation
feat(admin): add raw SELECT query execution API with pagination #841
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
12 commits
Select commit
Hold shift + click to select a range
15ad6de
feat(admin): add raw SELECT query execution with pagination
nevil-mathew 091cf7b
updated: response message
nevil-mathew 68ce70b
feat(admin): add rawQueryUtils for secure query validation
nevil-mathew d32a714
updated: hasForbiddenPatterns
nevil-mathew a1e666f
Merge branch 'develop' of https://github.com/ELEVATE-Project/user int…
nevil-mathew 88d9333
fix(auth): ensure decodedToken set and next() called properly
nevil-mathew 65b1739
updated: getPaginationParams to handle default values
nevil-mathew f01b6df
updated: error response message
nevil-mathew debdaa4
add: comment for early return for admin users.
nevil-mathew 3a77d3d
add: explicit role validation
nevil-mathew 3a29ad6
add: validation for executeRawQuery
nevil-mathew 0469661
fix: message
nevil-mathew 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
Some comments aren't visible on the classic Files Changed page.
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| /** | ||
| * Utilities for validating and processing raw SQL SELECT queries | ||
| * @module rawQueryUtils | ||
| */ | ||
|
|
||
| /** | ||
| * Comprehensive security validation for SQL queries | ||
| * @param {string} query - The raw SQL query to validate | ||
| * @throws {Error} - If validation fails | ||
| */ | ||
| function validateQuerySecurity(query) { | ||
| const lowerQuery = query.toLowerCase() | ||
|
|
||
| // Check starts with SELECT or WITH | ||
| if (!/^\s*(with|select)\b/.test(lowerQuery)) { | ||
| throw new Error('ONLY_SELECT_QUERIES_ALLOWED') | ||
| } | ||
|
|
||
| // Check for unbalanced quotes | ||
| if (!hasValidQuotes(query)) { | ||
| throw new Error('QUERY_INVALID_OR_UNBALANCED_QUOTES') | ||
| } | ||
|
|
||
| // Check for critical injection patterns | ||
| if (hasCriticalInjectionPatterns(lowerQuery)) { | ||
| throw new Error('QUERY_FORBIDDEN_INJECTION_PATTERNS') | ||
| } | ||
|
|
||
| // Normalize and check for forbidden patterns | ||
| const normalizedQuery = normalizeQuery(query) | ||
| if (hasForbiddenPatterns(normalizedQuery)) { | ||
| throw new Error('QUERY_FORBIDDEN_PATTERNS') | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Calculates pagination parameters with validation | ||
| * @param {number} pageNo - Page number | ||
| * @param {number} pageSize - Page size | ||
| * @returns {Object} - { limit, offset } | ||
| */ | ||
| function getPaginationParams(pageNo, pageSize) { | ||
| const validPageNo = Math.max(1, parseInt(pageNo) || 1) | ||
| const validPageSize = Math.min(Math.max(1, parseInt(pageSize) || 100), 100) // Cap at 100 | ||
| const offset = (validPageNo - 1) * validPageSize | ||
|
|
||
| return { limit: validPageSize, offset } | ||
| } | ||
|
nevil-mathew marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Checks for critical injection patterns | ||
| * @param {string} lowerQuery - Lowercased query | ||
| * @returns {boolean} - True if critical patterns are found | ||
| */ | ||
| function hasCriticalInjectionPatterns(lowerQuery) { | ||
| const criticalPatterns = [';', '--', '/*', '*/', '#', '\\'] | ||
| return criticalPatterns.some((pattern) => lowerQuery.includes(pattern)) | ||
| } | ||
|
|
||
| /** | ||
| * Removes comments and string literals for pattern analysis | ||
| * @param {string} query - The raw query | ||
| * @returns {string} - Normalized query | ||
| */ | ||
| function normalizeQuery(query) { | ||
| return query | ||
| .toLowerCase() | ||
| .replace(/\/\*[\s\S]*?\*\//g, ' ') // Remove /* */ comments | ||
| .replace(/--.*$/gm, ' ') // Remove -- comments | ||
| .replace(/#[^\n]*/g, ' ') // Remove # comments | ||
| .replace(/'(?:''|[^'])*'/g, ' ') // Remove single-quoted strings | ||
| .replace(/"(?:""|[^"])*"/g, ' ') // Remove double-quoted strings | ||
| .replace(/\s+/g, ' ') // Normalize whitespace | ||
| .trim() | ||
| } | ||
|
|
||
| /** | ||
| * Checks if normalized query contains forbidden operations | ||
| * @param {string} normalizedQuery - Normalized query | ||
| * @returns {boolean} - True if forbidden patterns are found | ||
| */ | ||
| function hasForbiddenPatterns(normalizedQuery) { | ||
| const forbiddenPatterns = /\b(drop|alter|truncate|insert|update|delete|union|concat|\|\|)\b/i | ||
| return forbiddenPatterns.test(normalizedQuery) | ||
| } | ||
|
|
||
| /** | ||
| * Validates balanced quotes with escaping | ||
| * @param {string} query - The raw query | ||
| * @returns {boolean} - True if quotes are balanced | ||
| */ | ||
| function hasValidQuotes(query) { | ||
| let inSingleQuote = false | ||
| let inDoubleQuote = false | ||
|
|
||
| for (let i = 0; i < query.length; i++) { | ||
| const char = query[i] | ||
| const nextChar = query[i + 1] | ||
|
|
||
| if (!inDoubleQuote && char === "'") { | ||
| if (inSingleQuote && nextChar === "'") { | ||
| i++ // Skip escaped '' | ||
| continue | ||
| } | ||
| inSingleQuote = !inSingleQuote | ||
| } else if (!inSingleQuote && char === '"') { | ||
| if (inDoubleQuote && nextChar === '"') { | ||
| i++ // Skip escaped "" | ||
| continue | ||
| } | ||
| inDoubleQuote = !inDoubleQuote | ||
| } | ||
| } | ||
|
|
||
| return !inSingleQuote && !inDoubleQuote | ||
| } | ||
|
|
||
| module.exports = { | ||
| validateQuerySecurity, | ||
| getPaginationParams, | ||
| hasCriticalInjectionPatterns, | ||
| normalizeQuery, | ||
| hasForbiddenPatterns, | ||
| hasValidQuotes, | ||
| } | ||
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
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.