Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/LfClassicData/LfClassicMiniLcmApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ private async IAsyncEnumerable<Entry> Query(QueryOptions? options = null, string
.Ascending(entry => entry.MorphologyType)
.Ascending(entry => entry.Guid))
.Skip(options.Offset)
.Limit(options.Count)
.Limit(options.Count == QueryOptions.QueryAll ? 0 : options.Count)
Comment on lines 213 to +214
Copy link
Copy Markdown

@coderabbitai coderabbitai bot Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "=== QueryOptions definitions / count validation ==="
fd -i 'QueryOptions' backend -x sed -n '1,220p' {}

echo
echo "=== LFClassic count binding and limit call sites ==="
rg -n -C3 --glob 'backend/**/*.cs' 'QueryAll|\.Limit\s*\(|\bCount\b|\bcount\b|ArgumentOutOfRangeException'

Repository: sillsdev/languageforge-lexbox

Length of output: 50386


🏁 Script executed:

# Check if QueryOptions.Count has validation attributes
rg -A 5 'record QueryOptions|int Count' backend/FwLite/MiniLcm/IMiniLcmReadApi.cs

# Check if LfClassicRoutes.Count has validation
rg -B 2 -A 5 'public int\? Count' backend/LfClassicData/LfClassicRoutes.cs

# Check for any validation on negative counts in the Query method or upstream
rg -B 3 -A 8 'public.*Query.*QueryOptions' backend/LfClassicData/LfClassicMiniLcmApi.cs | head -40

Repository: sillsdev/languageforge-lexbox

Length of output: 533


🏁 Script executed:

# Check for any ArgumentException or validation in Query method implementation
rg -B 5 -A 25 'private.*IAsyncEnumerable.*Query.*QueryOptions' backend/LfClassicData/LfClassicMiniLcmApi.cs

# Check if there's any validation in the route handler or controller
rg -B 3 -A 10 'Count.*QueryAll|ArgumentOutOfRangeException.*Count|ArgumentException.*Count' backend/LfClassicData/

Repository: sillsdev/languageforge-lexbox

Length of output: 3551


Add validation to reject non-sentinel negative counts.

This fixes the QueryOptions.QueryAll case (count=-1), but options.Count < -1 still reaches .Limit(...). There is no upstream validation on the Count parameter in LfClassicRoutes or within the Query method itself. If a client sends count=-2, it will pass the conditional check and cause MongoDB to reject the aggregation pipeline with a 500 error, reproducing the original issue.

Add a guard to reject every negative value except QueryAll:

Suggested fix
+        var limit = options.Count switch
+        {
+            QueryOptions.QueryAll => 0,
+            < 0 => throw new ArgumentOutOfRangeException(nameof(options.Count), "Count must be >= 0 or QueryAll."),
+            _ => options.Count
+        };
+
         pipeline = AppendHeadWord(sortWs, pipeline)
             .Sort(Builders<HeadwordEntry>.Sort
                 .Ascending(entry => entry.headword)
                 .Ascending(entry => entry.MorphologyType)
                 .Ascending(entry => entry.Guid))
             .Skip(options.Offset)
-            .Limit(options.Count == QueryOptions.QueryAll ? 0 : options.Count)
+            .Limit(limit)
             .Project(entry => entry as Entities.Entry);
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@backend/LfClassicData/LfClassicMiniLcmApi.cs` around lines 213 - 214, Add
input validation in the Query method to reject any options.Count that is
negative and not equal to QueryOptions.QueryAll; specifically check
options.Count and if (options.Count < 0 && options.Count !=
QueryOptions.QueryAll) return a client error (e.g., BadRequest / throw
ArgumentException) rather than letting the value reach the .Limit(...) call,
ensuring only the sentinel -1 is allowed and all other negative counts are
rejected.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@myieye Would you prefer if I also add this check for invalid negative counts?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

.Project(entry => entry as Entities.Entry);

await foreach (var entry in Entries.Aggregate(pipeline).ToAsyncEnumerable())
Expand Down
Loading