Skip to content

CSHARP-6276: Fix LINQ filter and update field path handling (CSHARP-6181/CSHARP-6183) on main - #2167

Open
damieng wants to merge 3 commits into
mongodb:mainfrom
damieng:CSHARP-6181-main
Open

damieng wants to merge 3 commits into
mongodb:mainfrom
damieng:CSHARP-6181-main

Conversation

@damieng

@damieng damieng commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Fixes CSHARP-6181 / CSHARP-6183 on main.

A filter or update field path is joined with "." and emitted verbatim as an element name, so a segment taken from application data could be reinterpreted by the server or by the translator: a dotted name became a nested path, a $-prefixed name became an operator (including $where via Mql.Field), a null or malformed array filter identifier turned $[i] into $[] (every element), and a name equal to one of the translator's own path markers (@<current>, @<elem>) silently targeted the root field instead of the one the expression names.

Each segment is now validated at the translator that takes it from a folded constant (dictionary and BsonValue indexers, ContainsKey, Mql.Field, AllMatchingElements) through a shared FilterFieldNameValidator. Unsafe names throw rather than falling back to $getField.

The path markers are also reachable from the aggregation side: AstSimplifier turns $anyElementTrue/$map over a constant array into a filter on the field path, where a leading @<elem>. would be stripped when the filter is rendered. It now declines to do that when the path starts with a marker, so the query stays an $expr that treats the name as a literal field name.

Notes:

  • AstFieldName.cs already had the null check from CSHARP-6209 on main; the new IsSafeArrayFilterIdentifier is added on top.
  • src/MongoDB.Driver/Linq/AGENTS.md documents the segment validation, the marker rule and the simplifier guard.
  • FilterFieldNameInjectionTests and the full MongoDB.Driver.Tests.Linq namespace pass on net10.0.

@damieng damieng added the bug Fixes issues or unintended behavior. label Oct 26, 2026
@damieng
damieng marked this pull request as ready for review October 27, 2026 15:32
@damieng
damieng requested a review from a team as a code owner October 27, 2026 15:32
@damieng
damieng requested review from BorisDog and a balanced review from Copilot October 27, 2026 15:32
@damieng damieng changed the title CSHARP-6181: Port LINQ filter and update field path fix from v3.x (CSHARP-6183) CSHARP-6276: Port LINQ filter and update field path fix CSHARP-6181/61783 from v3.x branch Oct 27, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The validator accepts root keys that collide with internal path markers, causing filter and update expressions to target root fields instead of the intended subdocument fields.

1 open issue
What changed in this PR

Ports the LINQ field-path validation fix from v3.x to main, preventing application-supplied names from being reinterpreted as nested paths or operators in filters and updates.

Changes:

  • Adds shared field-segment validation and array-filter identifier checks.
  • Rejects constant null field names in aggregation translators.
  • Adds regression tests and documents validation and fallback behavior.
File Description
tests/​MongoDB.Driver.Tests/​Linq/​Linq3Implementation/​FilterFieldNameInjectionTests.cs Tests unsafe-name rejection and preservation of safe paths.
src/​MongoDB.Driver/​Linq/​Linq3Implementation/​Translators/​ExpressionToFilterTranslators/​ToFilterFieldTranslators/​GetItemMethodToFilterFieldTranslator.cs Validates dictionary and BSON indexer keys.
src/​MongoDB.Driver/​Linq/​Linq3Implementation/​Translators/​ExpressionToFilterTranslators/​ToFilterFieldTranslators/​FilterFieldNameValidator.cs Adds shared validation and a non-fallback exception.
src/​MongoDB.Driver/​Linq/​Linq3Implementation/​Translators/​ExpressionToFilterTranslators/​ToFilterFieldTranslators/​FieldMethodToFilterFieldTranslator.cs Validates Mql.Field names.
src/​MongoDB.Driver/​Linq/​Linq3Implementation/​Translators/​ExpressionToFilterTranslators/​ToFilterFieldTranslators/​AllMatchingElementsMethodToFilterFieldTranslator.cs Rejects unsafe array-filter identifiers.
src/​MongoDB.Driver/​Linq/​Linq3Implementation/​Translators/​ExpressionToFilterTranslators/​MethodTranslators/​ContainsKeyMethodToFilterTranslator.cs Validates document-represented dictionary keys.
src/​MongoDB.Driver/​Linq/​Linq3Implementation/​Translators/​ExpressionToAggregationExpressionTranslators/​MethodTranslators/​GetItemMethodToAggregationExpressionTranslator.cs Rejects constant null BSON indexer keys.
src/​MongoDB.Driver/​Linq/​Linq3Implementation/​Translators/​ExpressionToAggregationExpressionTranslators/​MethodTranslators/​FieldMethodToAggregationExpressionTranslator.cs Rejects null aggregation field names.
src/​MongoDB.Driver/​Linq/​Linq3Implementation/​Ast/​Filters/​AstFilterField.cs Identifies implied document paths for validation.
src/​MongoDB.Driver/​Linq/​Linq3Implementation/​Ast/​AstFieldName.cs Adds array-filter identifier validation.
src/​MongoDB.Driver/​Linq/​AGENTS.md Documents segment validation and rejection without fallback.

Review effort: Balanced

@damieng damieng changed the title CSHARP-6276: Port LINQ filter and update field path fix CSHARP-6181/61783 from v3.x branch CSHARP-6276: Fix LINQ filter and update field path handling (CSHARP-6181/CSHARP-6183) on main Oct 29, 2026
@BorisDog
BorisDog requested review from adelinowona and removed request for BorisDog October 29, 2026 20:34
@adelinowona
adelinowona requested a balanced review from Copilot November 7, 2026 07:09

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The test fixture fails before assertions on supported MongoDB 4.4 servers without a version guard, and the validator rejects valid literal marker keys inside $elemMatch.

2 open issues
1 resolved since last review

Review effort: Balanced

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Fixes issues or unintended behavior.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants