Files
mongo/jstests/replsets/too_nested_applyops_cmd_must_always_fail.js
Zac 591928c619 SERVER-108478 JS formatted by prettier and remove clang-format (#39656)
GitOrigin-RevId: 6c8f6aded47f260aa4f7c231b17dae3302cb1e04
2025-08-21 17:27:09 +00:00

83 lines
2.7 KiB
JavaScript

/**
* Ensures that applyOps command with too many nested applyOps instances fails validation
* in case of presense of an empty oplog entry among other oplog entries. Previously validation
* routine was returning before validation in a few cases of super user requirement and thus
* allowing a su to run a potentially malformed command. Running this test against code versions
* without the fix will fail for all above cases(empty object, create and renameCollection)
*
* @tags: [
* requires_fcv_83
* ]
*/
import {ReplSetTest} from "jstests/libs/replsettest.js";
const rst = new ReplSetTest({nodes: 2});
rst.startSet();
rst.initiate();
const primaryDB = rst.getPrimary().getDB("admin");
let nestedCmd = {op: "n", ns: ""};
for (let i = 0; i < 10; i++) {
nestedCmd = {op: "c", ns: "admin.$cmd", o: {applyOps: [nestedCmd]}};
}
// 10 recursion steps are still fine
assert.commandWorked(primaryDB.adminCommand({"applyOps": [nestedCmd]}));
// adding 10 more to break the max recursion depth rule
for (let i = 0; i < 10; i++) {
nestedCmd = {op: "c", ns: "admin.$cmd", o: {applyOps: [nestedCmd]}};
}
assert.commandFailedWithCode(primaryDB.adminCommand({"applyOps": [nestedCmd]}), ErrorCodes.FailedToParse);
// Adding an empty entry to existing command. Empty entry requires a superuser, but we expect
// parsing failure now too.
assert.commandFailedWithCode(
primaryDB.adminCommand({"applyOps": [nestedCmd, {op: "c", ns: "admin.$cmd", o: {"applyOps": []}}]}),
ErrorCodes.FailedToParse,
);
// Prepending a "create" command to our nested command, without the fix we would have succeeded here
// because of early return
assert.commandFailedWithCode(
primaryDB.adminCommand({
"applyOps": [
{
op: "c",
ns: "admin.$cmd",
o: {
"applyOps": [
{
"op": "c",
"ns": "admin.$cmd",
"o": {
"create": "x",
},
},
nestedCmd,
],
},
},
],
}),
ErrorCodes.FailedToParse,
);
// Prepending a "renameCollection" command to our nested command, without the fix we would have
// succeeded here because of early return
assert.commandFailedWithCode(
primaryDB.adminCommand({
"applyOps": [
{
op: "c",
ns: "admin.$cmd",
o: {
"applyOps": [{"op": "c", "ns": "admin.$cmd", "o": {renameCollection: "", to: "test.b"}}, nestedCmd],
},
},
],
}),
ErrorCodes.FailedToParse,
);
rst.stopSet();