Skip to content

fix(editor): prevent field name in field value autocompletion COMPASS-7297#7924

Open
Anemy wants to merge 1 commit intomainfrom
COMPASS-7297-autocomplete-field-values
Open

fix(editor): prevent field name in field value autocompletion COMPASS-7297#7924
Anemy wants to merge 1 commit intomainfrom
COMPASS-7297-autocomplete-field-values

Conversation

@Anemy
Copy link
Copy Markdown
Member

@Anemy Anemy commented Mar 30, 2026

COMPASS-7297

Updates our query autocomplete to do the filtering ourselves instead of passing it all to codemirror. This makes it so we can now offer different results based on if we're in a field or a value in a query.

We still have some indirection with support for ace autocomplete in two of the other autocompleters which I think we can remove altogether. These changes should make that a bit easier as it removed the usage in query. I opted not to refactor unrelated parts as part of these changes.

This did get me thinking, ideally with value autocomplete we'd be suggesting things like false or true for fields we know are booleans, or other types of values that exist in the documents. I reckon it would help folks out. Not doing that here, but something we should keep in mind and discuss when we want to invest more in improving our autocomplete.

before after
Screenshot 2026-03-29 at 19 07 31 Screenshot 2026-03-29 at 19 07 40
Screenshot 2026-03-29 at 19 14 37 Screenshot 2026-03-29 at 19 14 30

@Anemy Anemy requested a review from a team as a code owner March 30, 2026 02:20
@Anemy Anemy requested review from Copilot and mabaasit March 30, 2026 02:20
@github-actions github-actions bot added the fix label Mar 30, 2026
'expr:*',
'conv',
'bson',
'bson-legacy-uuid',
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

drive-by. I totally missed this in COMPASS-9690 🙈

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates the Compass editor’s MongoDB query autocomplete to be context-aware so it can offer different suggestions when completing a field name vs completing a value (addressing COMPASS-7297).

Changes:

  • Implement context-sensitive query completions by detecting whether the cursor is in a property value and filtering suggestions accordingly.
  • Centralize MongoDB→CodeMirror completion mapping utilities and add a helper for detecting “value position”.
  • Extend stage autocompletion to include bson-legacy-uuid and update/expand tests for the new behavior.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
packages/compass-editor/src/codemirror/utils.ts Adds isPropertyValue and moves completion-mapping helper into shared utilities.
packages/compass-editor/src/codemirror/query-autocompleter.ts Switches query autocompletion to do its own filtering and choose field vs value completion sources.
packages/compass-editor/src/codemirror/query-autocompleter.test.ts Updates tests to validate field/value-aware query completion filtering.
packages/compass-editor/src/codemirror/query-autocompleter-with-history.test.ts Adjusts expectations to reflect the new base query completion filtering behavior.
packages/compass-editor/src/codemirror/stage-autocompleter.ts Adds bson-legacy-uuid to available stage completion meta types.
packages/compass-editor/src/codemirror/stage-autocompleter.test.ts Expands coverage for $match field vs value completion and asserts new meta type.
packages/compass-editor/src/codemirror/ace-compat-autocompleter.ts Removes duplicated completion-mapping logic in favor of shared utility.

Comment on lines +174 to +188
const infoNode = document.createElement('div');
infoNode.classList.add('completion-info');
infoNode.addEventListener('mousedown', (evt) => {
// If we are clicking a link inside the info block, we have to prevent
// default browser behavior that will remove the focus from the editor
// and cause the autocompleter to dissapear before browser handles the
// actual click. This is very similar to how codemirror handles clicks
// on the list items
// @see {@link https://github.com/codemirror/autocomplete/blob/82480a7d51d60ad933808e42f6189d841a5a6bc8/src/tooltip.ts#L96-L97}
if ((evt.target as HTMLElement).nodeName === 'A') {
evt.preventDefault();
}
});
infoNode.innerHTML = completion.description;
return infoNode;
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

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

completion.description is inserted via innerHTML, which can lead to XSS if any completion descriptions originate from user-controlled data (e.g., field/schema descriptions). Consider sanitizing allowed markup before insertion (or using textContent for plain-text descriptions and only rendering trusted HTML), and ensure links opened with target="_blank" include rel="noopener noreferrer" to avoid reverse-tabnabbing.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I was thinking about this one when I moved this function over, we should make the link usage in

more explicit here and restrict the content to text with an optional link that we then create an intentional element for. For the reviewer, think it's worth a ticket? It is a legit XSS possibility if we expanded our autocomplete, and I could see us missing this.

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