Skip to content

fix arbitrary code execution via mongostory route #136

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
17 changes: 12 additions & 5 deletions apps/mongostory/app/api/mongodb-playground/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,18 @@ export async function POST(req: Request) {
const client = await clientPromise
const db = client.db("mongostory")

// Execute the query in a safe sandbox environment
const result = await eval(`(async () => {
const db = client.db("mongostory")
return ${query}
})()`)
// Validate and execute the query securely
if (typeof query !== "object" || query === null) {
throw new Error("Invalid query format. Query must be a non-null object.");
}

// Example: Allow only find operations with specific constraints
if (!query.collection || !query.filter || typeof query.collection !== "string" || typeof query.filter !== "object") {
throw new Error("Invalid query structure. Must include 'collection' (string) and 'filter' (object).");
}

const collection = db.collection(query.collection);
const result = await collection.find(query.filter).toArray();

return NextResponse.json({
success: true,
Expand Down