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

Use process.nextTick before wrapping Collection (mongodb v6.10.0) #471

Merged
merged 4 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions library/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
"mongodb": "~6.9",
"mongodb-v4": "npm:mongodb@^4.0.0",
"mongodb-v5": "npm:mongodb@^5.0.0",
"mongodb-v6": "npm:mongodb@~6.9",
"mongodb-v6": "npm:mongodb@^6.0.0",
"mysql": "^2.18.1",
"mysql2": "^3.10.0",
"needle": "^3.3.1",
Expand Down
60 changes: 38 additions & 22 deletions library/sinks/MongoDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import type { Collection } from "mongodb-v6";
import { Hooks } from "../agent/hooks/Hooks";
import { InterceptorResult } from "../agent/hooks/InterceptorResult";
import type { WrapPackageInfo } from "../agent/hooks/WrapPackageInfo";
import { detectNoSQLInjection } from "../vulnerabilities/nosql-injection/detectNoSQLInjection";
import { isPlainObject } from "../helpers/isPlainObject";
import { Context, getContext } from "../agent/Context";
Expand Down Expand Up @@ -186,33 +187,48 @@ export class MongoDB implements Wrapper {
return undefined;
}

private wrapCollection(
exports: typeof import("mongodb-v6"),
pkgInfo: WrapPackageInfo
) {
const collectionProto = exports.Collection.prototype;

OPERATIONS_WITH_FILTER.forEach((operation) => {
wrapExport(collectionProto, operation, pkgInfo, {
inspectArgs: (args, agent, collection) =>
this.inspectOperation(operation, args, collection as Collection),
});
});

wrapExport(collectionProto, "bulkWrite", pkgInfo, {
inspectArgs: (args, agent, collection) =>
this.inspectBulkWrite(args, collection as Collection),
});

wrapExport(collectionProto, "aggregate", pkgInfo, {
inspectArgs: (args, agent, collection) =>
this.inspectAggregate(args, collection as Collection),
});

wrapExport(collectionProto, "distinct", pkgInfo, {
inspectArgs: (args, agent, collection) =>
this.inspectDistinct(args, collection as Collection),
});
}

wrap(hooks: Hooks) {
hooks
.addPackage("mongodb")
.withVersion("^4.0.0 || ^5.0.0 || ^6.0.0")
.onRequire((exports, pkgInfo) => {
const collectionProto = exports.Collection.prototype;

OPERATIONS_WITH_FILTER.forEach((operation) => {
wrapExport(collectionProto, operation, pkgInfo, {
inspectArgs: (args, agent, collection) =>
this.inspectOperation(operation, args, collection as Collection),
});
});

wrapExport(collectionProto, "bulkWrite", pkgInfo, {
inspectArgs: (args, agent, collection) =>
this.inspectBulkWrite(args, collection as Collection),
});

wrapExport(collectionProto, "aggregate", pkgInfo, {
inspectArgs: (args, agent, collection) =>
this.inspectAggregate(args, collection as Collection),
});

wrapExport(collectionProto, "distinct", pkgInfo, {
inspectArgs: (args, agent, collection) =>
this.inspectDistinct(args, collection as Collection),
// From mongodb v6.10.0, the Collection is undefined
// It's defined like:
// exports.Collection = void 0;
// const collection_1 = require("./collection");
// Object.defineProperty(exports, "Collection", { enumerable: true, get: function () { return collection_1.Collection; } });
// So we need to wait for the next tick to wrap the Collection
process.nextTick(() => {
hansott marked this conversation as resolved.
Show resolved Hide resolved
this.wrapCollection(exports, pkgInfo);
});
});
}
Expand Down
20 changes: 9 additions & 11 deletions sample-apps/hono-mongodb/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sample-apps/hono-mongodb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"@aikidosec/firewall": "file:../../build",
"@hono/node-server": "^1.11.2",
"hono": "^4.4.2",
"mongodb": "^6.3.0"
"mongodb": "^6.11.0"
}
}
Loading