[SERVER-28739] make the js scripting engine correctly verify that accessed collections are unsharded Created: 11/Apr/17  Updated: 12/Oct/17  Resolved: 14/Apr/17

Status: Closed
Project: Core Server
Component/s: Sharding
Affects Version/s: 3.5.5
Fix Version/s: 3.5.6

Type: Bug Priority: Major - P3
Reporter: Esha Maharishi (Inactive) Assignee: Esha Maharishi (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible
Operating System: ALL
Sprint: Sharding 2017-04-17, Sharding 2017-05-08
Participants:

 Description   

The eval command on mongod calls into the js scripting engine.

Since eval() is called on a database, not a namespace, it does not check the shardVersion sent in the request (or the shardVersion for the connection) directly.

Instead, any time a namespace is accessed by the script, the scripting engine checks that the namespace is not sharded (in DBInfo::getProperty() and DBCollectionInfo::construct).

It does this check by calling ShardingState::haveLocalShardingInfo():

bool haveLocalShardingInfo(OperationContext* opCtx, const string& ns) {
    if (!ShardingState::get(opCtx)->enabled()) {
        return false;
    }
 
    const auto& oss = OperationShardingState::get(opCtx);
    if (oss.hasShardVersion()) {
        return true;
    }
 
    const auto& sci = ShardedConnectionInfo::get(opCtx->getClient(), false);
    if (sci && !sci->getVersion(ns).isStrictlyEqualTo(ChunkVersion::UNSHARDED())) {
        return true;
    }
 
    return false;
}

This function assumes the collection is sharded if:

1) the OperationShardingState has a shardVersion (aka, a shardVersion was attached to the command in the request)
2) the connection used for sending the eval() command previously set a shardVersion for the namespace.

The mongos EvalCmd, which forwards the command to the primary shard for the database eval() was run on, does not attach a shardVersion to the cmdObj.

On the other hand, the passthrough() function called by mongos's EvalCmd uses ShardConnection on an empty namespace.

Therefore, only if the connection mongos gets from the pool had previously set a shardVersion for a namespace accessed by the script (e.g., due to a different, prior command), will the shard host have a ShardedConnectionInfo with a non-UNSHARDED version for the namespace, and only in this case will the haveLocalShardingInfo() check on mongod work if the collection is sharded.



 Comments   
Comment by Githook User [ 17/Apr/17 ]

Author:

{u'username': u'EshaMaharishi', u'name': u'Esha Maharishi', u'email': u'esha.maharishi@mongodb.com'}

Message: SERVER-28739 make the js scripting engine correctly verify that accessed collections are unsharded (uassert rather than invariant)
Branch: master
https://github.com/mongodb/mongo/commit/92eaa6337842a52623e5489058e8417e857615cc

Comment by Githook User [ 17/Apr/17 ]

Author:

{u'username': u'EshaMaharishi', u'name': u'Esha Maharishi', u'email': u'esha.maharishi@mongodb.com'}

Message: Revert "SERVER-28739 make the js scripting engine correctly verify that accessed collections are unsharded (uassert rather than invariant)"

This reverts commit d44e25dde3c61d729b8e97a2118e15ec80683c83.
Branch: master
https://github.com/mongodb/mongo/commit/90eedaffecf8d90d86464329a9fd7fe30717de34

Comment by Githook User [ 17/Apr/17 ]

Author:

{u'username': u'EshaMaharishi', u'name': u'Esha Maharishi', u'email': u'esha.maharishi@mongodb.com'}

Message: SERVER-28739 make the js scripting engine correctly verify that accessed collections are unsharded (uassert rather than invariant)
Branch: master
https://github.com/mongodb/mongo/commit/d44e25dde3c61d729b8e97a2118e15ec80683c83

Comment by Githook User [ 14/Apr/17 ]

Author:

{u'username': u'EshaMaharishi', u'name': u'Esha Maharishi', u'email': u'esha.maharishi@mongodb.com'}

Message: SERVER-28739 make the js scripting engine correctly verify that accessed collections are unsharded
Branch: master
https://github.com/mongodb/mongo/commit/e96a5090084d5d80f1349b3223d38f5f52c013f0

Generated at Thu Feb 08 04:18:55 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.