Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(NODE-5233)!: prevent session from one client from being used on another #3790

Merged
merged 6 commits into from
Aug 4, 2023

Conversation

nbbeeken
Copy link
Contributor

@nbbeeken nbbeeken commented Aug 1, 2023

Description

What is changing?

  • Add a check for the same client reference
  • Throw if they do not match
Is there new documentation needed for these changes?

API docs on startSession updated

What is the motivation for this change?

See highlights

Release Highlight

Driver methods throw if a session is provided from a different MongoClient

Providing a session from one MongoClient to a method on a different MongoClient has never been a supported use case and leads to undefined behavior. To prevent this mistake, the driver now throws a if session is provided to a driver helper from a different MongoClient.

// pre v6
const session = client1.startSession();
client2.db('foo').collection('bar').insertOne({ name: 'john doe' }, { session }); // no error thrown, undefined behavior

// v6+
const session = client1.startSession();
client2.db('foo').collection('bar').insertOne({ name: 'john doe' }, { session }); 
// MongoInvalidArgumentError thrown

Double check the following

  • Ran npm run check:lint script
  • Self-review completed using the steps outlined here
  • PR title follows the correct format: type(NODE-xxxx)[!]: description
    • Example: feat(NODE-1234)!: rewriting everything in coffeescript
  • Changes are covered by tests
  • New TODOs have a related JIRA ticket

@nbbeeken nbbeeken force-pushed the NODE-5233-session-from-other-client branch from 305a959 to 6ac44d4 Compare August 1, 2023 21:04
@nbbeeken nbbeeken marked this pull request as ready for review August 2, 2023 17:13
@nbbeeken nbbeeken force-pushed the NODE-5233-session-from-other-client branch from 6ac44d4 to 3e70911 Compare August 2, 2023 17:13
Copy link
Contributor

@baileympearson baileympearson left a comment

Choose a reason for hiding this comment

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

questions from kickoff:

Did we decide referential equality was okay?

  • best way to identify client? - instanceof is brittle, we can generate a unique id and/or use a symbol property; needs to account for legacy client usage
  • what other identifier is there?
  • what problems are there with checking referential equality?

Do we want to throw if a session from a LegacyMongoClient is passed into a MongoClient or vice versa?


Session prose test 5 says the following:

Repeat the above for all methods that take a session parameter.

Did we decide not to implement this and if so, should we add more APIs to the test? I don't think we need to exhaustively test the driver, but we should probably test more than a single insertOne.


I see the following as tests in the kickoff but not in the PR:

  • Unit test can check when sessions are and are not supported
  • Check when a session is and is not from the client passed to executeOperation

I thought the plan was to unit test this in some way (specifically to unit test execute operation). Did the plan change?

@@ -118,6 +118,8 @@ async function executeOperationAsync<
throw new MongoExpiredSessionError('Use of expired sessions is not permitted');
} else if (session.snapshotEnabled && !topology.capabilities.supportsSnapshotReads) {
throw new MongoCompatibilityError('Snapshot reads require MongoDB 5.0 or later');
} else if (session.client !== client) {
throw new MongoRuntimeError('ClientSession must be from the same MongoClient');
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be a MongoInvalidArgument error? We this error should be thrown when the user provides an invalid session as an argument.

@nbbeeken
Copy link
Contributor Author

nbbeeken commented Aug 3, 2023

From huddle:

  • reference check good
  • Add test for legacy client usage
  • Add cursor and gridfs operations to the prose testing
  • No unit tests

@nbbeeken nbbeeken requested a review from baileympearson August 3, 2023 18:20
@baileympearson baileympearson merged commit 9268b35 into main Aug 4, 2023
@baileympearson baileympearson deleted the NODE-5233-session-from-other-client branch August 4, 2023 15:51
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.

2 participants