Skip to content
Open
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
15 changes: 15 additions & 0 deletions packages/server/src/services/mongodb/relay.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,19 @@ describe("mongodb relay", () => {
])("rejects %s", async (_label, invoke, message) => {
await expect(invoke()).rejects.toThrow(message);
});

it("treats a connection string with malformed percent-encoding as having no URI database", async () => {
await expect(
listMongoCollections({
credentials: {
connectionString: "mongodb://user:pass@localhost:27017/db%zz",
database: "admin",
type: "mongodb" as const,
},
request: { database: "other" },
})
).rejects.toThrow(
'Database "other" is not allowed by this data source configuration'
);
});
});
11 changes: 10 additions & 1 deletion packages/server/src/services/mongodb/relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,16 @@ function extractDatabaseFromMongoUri(connectionString: string): string | null {
}
const url = new URL(normalizedConnectionString);
const pathname = url.pathname ? url.pathname.replace(/^\//, "") : "";
const decoded = decodeURIComponent(pathname).trim();
// Comment: URL parsing does not validate percent-encoding, so a connection
// string whose database path contains malformed escapes (for example a
// literal "%") must be treated like an unparseable URI instead of crashing
// with an uncaught URIError.
let decoded: string;
try {
decoded = decodeURIComponent(pathname).trim();
} catch {
return null;
}
if (decoded.length === 0) {
return null;
}
Expand Down