fix(storage/sql): reject filter keys that aren't plain column names - #267
Conversation
buildQuery wrote filter keys into the WHERE clause unchanged. Only values were bound, so a key like "1=1) OR (1=1" widened the clause to every row, and Update and Delete then wrote or removed all of them. Check each key against the same pattern sort keys use, and return an error from Get, List, Count, Update and Delete when one doesn't match.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (2)
Included review availability: Your plan provides up to 1 included review per hour; 0 remain after this review. 📝 WalkthroughWalkthroughThe SQL adapter now validates filter keys before embedding them in SQL clauses. Query operations propagate validation errors. List builds the validated filter query before applying pagination. ChangesSQL filter validation
Priority: ⬆️ High Estimated code review effort: 3 (Moderate) | ~20 minutes Change: Bug fix Merge Risk: ⚪ Minimal · up to The change rejects unsafe SQL filter keys across all five operations while preserving valid query pagination, with regression coverage for rejection and mutation protection. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. A rabbit checks each query key, Comment |
Problem
buildQuerywrites filter keys into the SQL unchanged. Only the values are bound. A key can close GORM's parentheses around the filter and widen it:On
Updatethis also bypasses the primary-key condition, because the key rewrites the whole filter group. Get, List and Count use the same path.This only matters when an application builds filter keys from untrusted input. Values have always been bound.
Fix
buildQuerychecks every key againstvalidColumnName(^[a-zA-Z_][a-zA-Z0-9_]*$), the patternvalidateSortKeyalready uses, and returns an error. Get, List, Count, Update and Delete return that error before any SQL runs.Behavior change
Keys that aren't plain column names, such as
table.columnor quoted identifiers, now return an error. No test or doc in the repo uses them.Tests
TestSQLAdapterRejectsUnsafeFilterKeyscalls all five methods with the key above and checks that each one returns an error and that no row changes. Without the fix,Updatesucceeds and changes every row.Related
UpdateContextlines, so it will need a small rebase.Summary by CodeRabbit
Bug Fixes
Tests