Skip to content

feat(bson-parsing): restrict web worker scope COMPASS-11128 - #892

Merged
mabaasit merged 11 commits into
web-worker-parsingfrom
restrict-worker-scope
Oct 15, 2026
Merged

mabaasit merged 11 commits into
web-worker-parsingfrom
restrict-worker-scope

Conversation

@mabaasit

@mabaasit mabaasit commented Sep 21, 2026 •

Copy link
Copy Markdown
Collaborator

Description

COMPASS-11128

Open Questions

Checklist


Stack created with GitHub Stacks CLI • Give Feedback 💬

@mabaasit
mabaasit requested review from a team as code owners September 21, 2026 08:51
@mabaasit
mabaasit requested review from LuciaHarcekova and removed request for a team September 21, 2026 08:51
@mabaasit
mabaasit added this pull request to stack #894 September 21, 2026 08:51
@mabaasit mabaasit changed the title feat(bson-parsin): restrict web worker scope feat(bson-parsing): restrict web worker scope Sep 21, 2026
// Used by this file.
'self',
'onmessage',
'postMessage',

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Adding const { self, onmessage, postMessage } = globalThis; above this statement should remove this requirement, and we do want to avoid a situation where the "sandbox" could call postMessage()

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yes, that's much better and will clean this up

'parseInt',
'encodeURIComponent',
'decodeURIComponent',
'Buffer',

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Buffer is a Node.js concept, do we need that?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

It was added to support the usage around LegacyUUID in scope.ts. I switched to bson.ByteUtils instead in 4623dfa.

Also, now that we added support for node as well, I've to keep it as web-worker package relies on it

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Hi @mabaasit, the ticket says to remove Node globals, but the PR’s allowlist still includes both process and Buffer. The comment says process is needed by the web-worker package. You mentioned the code switched to bson.ByteUtils instead of Buffer (I can't open 4623dfa link - if it's addressed there). Confirming whether Buffer is still needed and whether keeping process is an acceptable exception to the task’s security goal.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

'__defineSetter__',
'__lookupGetter__',
'__lookupSetter__',
'constructor',

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Object.prototype.constructor is part of standard JS, why are we excluding that?

@mabaasit mabaasit changed the title feat(bson-parsing): restrict web worker scope feat(bson-parsing): restrict web worker scope COMPASS-11128 Sep 22, 2026
@mabaasit
mabaasit marked this pull request as draft September 22, 2026 08:58
Copilot AI lite review requested due to automatic review settings September 23, 2026 11:19
@mabaasit
mabaasit force-pushed the restrict-worker-scope branch from b1f6d4c to 2460ab7 Compare September 23, 2026 11:19

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.

Copilot review overview

🟡 Changes recommended

The worker response callback can break, and test environment cleanup is incorrect.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 High severity

Open (1)
What changed in this PR

This pull request hardens the BSON parser web worker by restricting its scope and expanding worker tests.

Changes:

  • Restricts worker globals and prototypes.
  • Supports direct worker URLs in tests.
  • Adds worker lifecycle and security coverage.
File Summary
packages/​shell-bson-parser/​src/​worker.ts Restricts worker capabilities. Critical (3 votes): postMessage must remain available.
packages/​shell-bson-parser/​src/​worker-client.ts Adds test worker URL handling.
packages/​shell-bson-parser/​src/​index.spec.ts Expands worker behavior tests. Moderate (1 vote): delete the environment variable instead of assigning undefined.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +61 to +62
'Buffer',
]);

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

addressed in a1d77b8

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

c6d9db7 thanks to rebasing :) (commit title: clean up postMessage)

@mabaasit
mabaasit force-pushed the restrict-worker-scope branch from 2460ab7 to b4a5690 Compare September 23, 2026 14:48
@mabaasit
mabaasit force-pushed the restrict-worker-scope branch from b4a5690 to a1d77b8 Compare September 28, 2026 12:20
@mabaasit
mabaasit marked this pull request as ready for review September 28, 2026 12:31
@mabaasit
mabaasit force-pushed the restrict-worker-scope branch from a1d77b8 to 9442c14 Compare September 28, 2026 12:33
@codeowners-service-app

Copy link
Copy Markdown

Assigned lerouxb for team mongosh-developers because LuciaHarcekova is out of office.

@mabaasit
mabaasit force-pushed the restrict-worker-scope branch from c8d93e9 to 9ca9089 Compare September 28, 2026 15:28
@mabaasit
mabaasit force-pushed the restrict-worker-scope branch 4 times, most recently from 6c62c27 to 43001aa Compare October 5, 2026 13:00
@mabaasit
mabaasit force-pushed the restrict-worker-scope branch 2 times, most recently from fe7be76 to e9934b1 Compare October 5, 2026 13:19
@mabaasit
mabaasit force-pushed the restrict-worker-scope branch 2 times, most recently from 660996f to 3b999a9 Compare October 5, 2026 20:30
@coveralls

coveralls commented Oct 6, 2026 •

Copy link
Copy Markdown

Coverage Status

coverage: 79.958% (-0.02%) from 79.981% — restrict-worker-scope into web-worker-parsing

@mabaasit
mabaasit force-pushed the restrict-worker-scope branch from 3b999a9 to 4ac37a8 Compare October 8, 2026 21:42
@mabaasit
mabaasit force-pushed the restrict-worker-scope branch from 4ac37a8 to 8120d01 Compare October 9, 2026 23:03
'Map',
'Set',
'WeakMap',
'WeakSet',

@LuciaHarcekova LuciaHarcekova Oct 12, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Based on what data is the full allow list constructed?

I could see "console" isn't in allow list. Could be the case web-worker's Node shim calls console.error when a handler throws, which would turn errors into confusing ReferenceErrors.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This list is based on the current tests, integration with compass, globals needed for parsing etc.
Nice catch with console. I'll add that to this list

@LuciaHarcekova LuciaHarcekova left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Approved.

The core approach looks solid. The allowlist-then-delete pattern is appropriate, and Object.getOwnPropertyNames correctly includes non-enumerable globals. restrictObjectPrototype is also a valuable layer of protection against prototype-pollution gadgets.

I verified that require, global, fetch, eval, XMLHttpRequest, WebAssembly, and setTimeout are removed. Thanks for addressing the review feedback, including switching from Buffer to bson.ByteUtils and capturing postMessage before restricting the global scope.

nit: please update the PR description with links to the relevant data and a brief note mapping the requirements from those sources to the implementation, so reviewers can verify that the full scope is covered.

@mabaasit
mabaasit force-pushed the restrict-worker-scope branch from 9c092b3 to 3d91aa4 Compare October 12, 2026 21:18
@mabaasit
mabaasit force-pushed the restrict-worker-scope branch from 3d91aa4 to f67a2de Compare October 14, 2026 09:52
@mabaasit
mabaasit force-pushed the restrict-worker-scope branch from f67a2de to d45ff0c Compare October 15, 2026 10:54
@mabaasit
mabaasit merged commit d1423bc into main Oct 15, 2026
58 of 61 checks passed
@mabaasit
mabaasit deleted the restrict-worker-scope branch October 15, 2026 15:02
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.

7 participants