Fix SQL injection via orderBy direction in Drizzle adapter#22
Merged
Conversation
Contributor
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Co-authored-by: gblikas <13577108+gblikas@users.noreply.github.com>
- Validate direction parameter strictly before using sql.raw() - Normalize any input to either 'ASC' or 'DESC' only - Add comprehensive security tests for SQL injection prevention - Tests verify malicious payloads (DROP TABLE, SELECT, DELETE, etc.) are blocked Co-authored-by: gblikas <13577108+gblikas@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] [SEC-456] Fix SQL injection via orderBy direction in Drizzle adapter
Fix SQL injection via orderBy direction in Drizzle adapter
Mar 16, 2026
Changed injection payloads to reference the actual 'todos' table defined in the test schema instead of a non-existent 'users' table. This makes the tests more meaningful by demonstrating threats against the real data structure. Co-authored-by: gblikas <13577108+gblikas@users.noreply.github.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The
orderBydirection parameter was passed tosql.raw()without validation when the field wasn't found in the schema, enabling SQL injection in ORDER BY context.Changes
src/adapters/drizzle/index.ts): Normalize direction toASC/DESConly before passing tosql.raw()src/adapters/drizzle/drizzle-adapter.test.ts): Added 7 tests covering DROP TABLE, SELECT, DELETE injection vectorsBefore/After
Malicious input like
'asc; DROP TABLE todos;--'now normalizes to'ASC'.Original prompt
This section details on the original issue you should resolve
<issue_title>Security: SQL injection via orderBy direction in Drizzle adapter</issue_title>
<issue_description>## Summary
The Drizzle adapter passes the
orderBydirection parameter tosql.raw()without validation when the field is not found in the schema. Since QueryKit is often used in contexts where query parameters originate from client input (e.g., Next.js server actions, REST APIs), this creates a SQL injection vector.Affected Code
src/adapters/drizzle/index.tslines 166-180:Attack Vector
When
orderByis called with:elsebranch)The direction is passed directly to
sql.raw(), enabling SQL injection in the ORDER BY context.Impact
Current Safe Path
When the field is in the schema, the code is safe:
This boolean comparison means malicious direction values simply result in
desc()being used—no injection possible.Suggested Fix
Apply the same safe pattern to the fallback path:
This ensures that regardless of input, only
ASCorDESCreaches the SQL.Alternative Approaches
'asc'or'desc'allowedOrderFieldsin the security configuration to restrict which fields can be used for orderingSuggested Unit Tests
Environment
Workaround
Applications can mitigate this by validating
orderByparameters before passing to QueryKit:However, this defense should ideally be built into QueryKit itself.
</issue_description>
<agent_instructions>Validate that this issue exists. If it does, fix it by creating unit tests first, testing (examining failure). A...
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.