Skip to content

CSHARP-6181: Reject @<current> and @<elem> filter field names that collide with path markers - #2195

Merged
damieng merged 1 commit into
mongodb:v3.xfrom
damieng:CSHARP-6181-v3x
Nov 12, 2026
Merged

damieng merged 1 commit into
mongodb:v3.xfrom
damieng:CSHARP-6181-v3x

Conversation

@damieng

@damieng damieng commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to the CSHARP-6181 / CSHARP-6183 fix on v3.x, mirroring the review fix on the main port (#2167).

@<current> and @<elem> are the markers the filter AST uses for the implied document and element. As the first path component they are absorbed by AstFilterField.SubField and AstFieldOperationFilter.Render, so a root-level key with either name silently retargeted the root field that followed it. For a root BsonDocument, x => x[key]["Ssn"] == "v" rendered { Ssn : "v" } instead of { "<key>.Ssn" : "v" }.

  • FilterFieldNameValidator now rejects both names when the parent is the implied document.
  • AstSimplifier declines the $anyElementTrue/$map rewrite when the field path starts with a marker, which closes the same retargeting from the aggregation side.
  • Regression tests and the Linq/AGENTS.md note are updated.

The MongoDB.Driver.Tests.Linq namespace passes on net10.0.

@damieng
damieng requested a review from a team as a code owner November 6, 2026 16:02
@damieng
damieng requested review from ajcvickers and removed request for a team November 6, 2026 16:02
@damieng damieng added the bug Fixes issues or unintended behavior. label Nov 6, 2026
@damieng
damieng requested review from adelinowona and removed request for ajcvickers November 6, 2026 16:10
@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 field-name validation rejects valid array-element predicates for both marker keys; it should restrict rejection to an @<current> parent and add regression tests inside Any.

1 open issue
What changed in this PR

This follow-up to CSHARP-6181 protects LINQ filter and update paths from field names that collide with internal markers, aligning the v3.x fix with the main-branch work in #2167.

Changes:

  • Adds shared marker checks and rejects marker-named fields under implied-document parents.
  • Prevents an aggregation rewrite from silently retargeting marker-prefixed field paths.
  • Adds regression tests and documents the validation rules.
File Description
tests/​MongoDB.Driver.Tests/​Linq/​Linq3Implementation/​FilterFieldNameInjectionTests.cs Tests marker rejection and aggregation rewrite behavior.
src/​MongoDB.Driver/​Linq/​Linq3Implementation/​Translators/​ExpressionToFilterTranslators/​ToFilterFieldTranslators/​FilterFieldNameValidator.cs Adds validation for internal marker collisions.
src/​MongoDB.Driver/​Linq/​Linq3Implementation/​Ast/​Optimizers/​AstSimplifier.cs Guards the constant-array Any rewrite against marker-prefixed paths.
src/​MongoDB.Driver/​Linq/​Linq3Implementation/​Ast/​Filters/​AstFilterField.cs Centralizes marker recognition helpers.
src/​MongoDB.Driver/​Linq/​AGENTS.md Documents marker validation and the simplifier guard.

Review effort: Balanced

throw new UnsafeFilterFieldNameException(expression, $"the name \"{segment}\" contains \".\", which the server would read as a path to a nested field; access each level separately instead, for example x[\"a\"][\"b\"]");
}

if (parent.Ast.IsImpliedDocument && AstFilterField.IsImpliedDocumentMarker(segment))

@adelinowona adelinowona 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.

@damieng Can we fix the reported copilot issue, same issue also exists in #2167 and I mentioned there as well.

@damieng
damieng merged commit 0197b1f into mongodb:v3.x Nov 12, 2026
3 of 4 checks passed
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