Skip to content

fix(bson-transpilers): escape apostrophes in Python object keys COMPASS-11398 - #8716

Merged
LuciaHarcekova merged 3 commits into
mainfrom
COMPASS-11398
Nov 20, 2026
Merged

LuciaHarcekova merged 3 commits into
mainfrom
COMPASS-11398

Conversation

@LuciaHarcekova

Copy link
Copy Markdown
Collaborator

Description

ObjectTypeArgsTemplate wraps Python object keys in ' but only escaped ". An apostrophe in a field name ends the literal, and the rest of the key becomes Python code.

# field named:  it's a field
'it's a field': 1                                               # SyntaxError

# field named:  a': 1}}]; __import__('os').system('id'); y=[{ 'm': { 'X
'a': 1}}]; __import__('os').system('id'); y=[{ 'm': { 'X': 1    # runs on paste

The first breaks for any user with an apostrophe in a field name. The second is injection: field names come from server documents, so anyone with write access to the collection, or control of the server, supplies them. Compass never executes the output, the user copies and runs it.

#8334 fixed the value, URI and collection-name paths. Keys bypass StringTypeTemplate entirely, so they were missed. Fixes SECBUG-6921.

Solution

One character in symbols/python/templates.yaml, matching the JavaScript, Ruby and PHP key emitters:

- return `'${newStr.replace(/\\([\s\S])|(")/g, '\\$1$2')}'`;
+ return `'${newStr.replace(/\\([\s\S])|(')/g, '\\$1$2')}'`;
'it\'s a field': 1
'a\': 1}}]; __import__(\'os\').system(\'id\'); y=[{ \'m\': { \'X': 1

Plus the regenerated lib/symbol-table/shelltopython.js (npm run symbol-table)

Checklist

  • New tests and/or benchmarks are included
  • Documentation is changed or added
  • If this change updates the UI, screenshots/videos are added and a design review is requested
  • If this change could impact the load on the MongoDB cluster, please describe the expected and worst case impact
  • I have signed the MongoDB Contributor License Agreement (https://www.mongodb.com/legal/contributor-agreement)

Motivation and Context

  • Bugfix
  • New feature
  • Dependency update
  • Misc

@LuciaHarcekova
LuciaHarcekova merged commit 5c512c1 into main Nov 20, 2026
57 of 60 checks passed
@LuciaHarcekova
LuciaHarcekova deleted the COMPASS-11398 branch November 20, 2026 14:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants