Skip to content

MongoDB adapter should escape embedded quotes in field references#5109

Open
Samin061 wants to merge 2 commits into
apache:mainfrom
Samin061:mongo-escape-quotes
Open

MongoDB adapter should escape embedded quotes in field references#5109
Samin061 wants to merge 2 commits into
apache:mainfrom
Samin061:mongo-escape-quotes

Conversation

@Samin061

Copy link
Copy Markdown
Contributor

Changes Proposed

MongoDB pipeline injection through unescaped quotes in pushed-down field references

The Mongo adapter renders $project/$group stages as strings that are later parsed with BsonDocument.parse. MongoRules.quote wrapped a value in single quotes but left embedded single quotes and backslashes untouched (the old // TODO: handle embedded quotes), and the ITEM path that handles _MAP['key'] built '$key' the same way. Since the item key and field names come straight from the SQL statement, a value carrying a single quote closes the quoted token early and the rest is parsed as further pipeline syntax.

Reproduction: a projection such as SELECT _MAP['x'', evil: {$literal: 1}, y: ''z'] FROM mongo generates {$project: {..., evil: {$literal: 1}, ...}}, so an extra stage field lands in the pipeline sent to MongoDB. After the fix the key stays a single string.

The escaping lives in quote because that is the one place every field reference passes through; the item-key branch now routes through it, and the aggregate paths already call it. Added a unit test that fails on the old code.

@xiedeyantu

Copy link
Copy Markdown
Member

We do not oppose using AI tools to contribute code. Multiple reviewers have repeatedly advised creating a Jira ticket before submitting contributions—you acknowledged this but have yet to follow through. Moving forward, non-compliant PRs will no longer be reviewed.

@Samin061

Copy link
Copy Markdown
Contributor Author

That's on me, sorry. I'll get a CALCITE Jira filed for this one and re-title the PR to reference it, and do the same for my other open ones (#5075, #5106) before asking for review again.

import static org.hamcrest.MatcherAssert.assertThat;

/** Tests for {@link MongoRules}. */
class MongoRulesTest {

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.

I am not crazy about these tests, but a complete query covering these new rules whose output is validated on Mongo would be nice.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fair. Dropped MongoRulesTest and replaced it with an end-to-end test in MongoAdapterTest: the datatypes collection now carries two fields whose names are x', injected: {$literal: 1}, y: 'z and a\, and the test selects them through _MAP, checks the generated $project stage, and checks the values Mongo returns.

On the old code the first query parses as {$project: {C: '$x', injected: {$literal: 1}, y: 'z'}}, so the injected field lands in the pipeline and the query fails; with the escaping it returns C=quoted. :mongodb:test is green (53 passed, 6 pre-existing skips).

@Samin061

Copy link
Copy Markdown
Contributor Author

Also still on the Jira: my ASF account request hasn't come through yet, so the PR title isn't prefixed with a CALCITE- id. I'll file the ticket and retitle this and my other open PRs as soon as it does.

@mihaibudiu

Copy link
Copy Markdown
Contributor

You don't need an ASF account, just a JIRA account to file issues.
We usually approve JIRA account requests instantly.

@sonarqubecloud

Copy link
Copy Markdown

@Samin061

Copy link
Copy Markdown
Contributor Author

Got it, I had the two mixed up. Requesting the JIRA account now and I'll file the ticket for this one, then retitle this PR (and #5075, #5106) with the CALCITE- ids.

Nothing else pending on the code side: the unit test you weren't keen on is gone, replaced by an end-to-end case in MongoAdapterTest that runs the query against Mongo and checks both the generated $project stage and the returned values.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants