Skip to content

Commit ba3d5d8

Browse files
fix: remove dep on export input for export name
With this commit we're removing the dependency on export tool input for constructing the name of the export without affecting the user experience in any way.
1 parent dd7760b commit ba3d5d8

File tree

5 files changed

+6
-37
lines changed

5 files changed

+6
-37
lines changed

src/common/exportsManager.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ export class ExportsManager extends EventEmitter<ExportsManagerEvents> {
163163
}): Promise<AvailableExport> {
164164
try {
165165
this.assertIsNotShuttingDown();
166-
const exportNameWithExtension = validateExportName(ensureExtension(exportName, "json"));
166+
const exportNameWithExtension = ensureExtension(exportName, "json");
167167
if (this.storedExports[exportNameWithExtension]) {
168168
return Promise.reject(
169169
new Error("Export with same name is either already available or being generated.")
@@ -373,22 +373,6 @@ export function ensureExtension(pathOrName: string, extension: string): string {
373373
return `${pathOrName}${extWithDot}`;
374374
}
375375

376-
/**
377-
* Small utility to decoding and validating provided export name for path
378-
* traversal or no extension */
379-
export function validateExportName(nameWithExtension: string): string {
380-
const decodedName = decodeURIComponent(nameWithExtension);
381-
if (!path.extname(decodedName)) {
382-
throw new Error("Provided export name has no extension");
383-
}
384-
385-
if (decodedName.includes("..") || decodedName.includes("/") || decodedName.includes("\\")) {
386-
throw new Error("Invalid export name: path traversal hinted");
387-
}
388-
389-
return decodedName;
390-
}
391-
392376
export function isExportExpired(createdAt: number, exportTimeoutMs: number): boolean {
393377
return Date.now() - createdAt > exportTimeoutMs;
394378
}

src/resources/common/exportedData.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export class ExportedData {
7272
private autoCompleteExportName: CompleteResourceTemplateCallback = (value) => {
7373
try {
7474
return this.session.exportsManager.availableExports
75-
.filter(({ exportName }) => exportName.startsWith(value))
75+
.filter(({ exportName, exportTitle }) => exportName.startsWith(value) || exportTitle.includes(value))
7676
.map(({ exportName }) => exportName);
7777
} catch (error) {
7878
this.session.logger.error({

src/tools/mongodb/read/export.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export class ExportTool extends MongoDBToolBase {
8181
});
8282
}
8383

84-
const exportName = `${database}.${collection}.${new ObjectId().toString()}.json`;
84+
const exportName = `${new ObjectId().toString()}.json`;
8585

8686
const { exportURI, exportPath } = await this.session.exportsManager.createJSONExport({
8787
input: cursor,

tests/integration/resources/exportedData.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,10 @@ describeWithMongoDB(
155155
},
156156
argument: {
157157
name: "exportName",
158-
value: "b",
158+
value: "big",
159159
},
160160
});
161-
expect(completeResponse.completion.total).toEqual(1);
161+
expect(completeResponse.completion.total).toBeGreaterThanOrEqual(1);
162162
});
163163
});
164164
},

tests/unit/common/exportsManager.test.ts

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,7 @@ import type { FindCursor } from "mongodb";
55
import { Long } from "mongodb";
66
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
77
import type { ExportsManagerConfig } from "../../../src/common/exportsManager.js";
8-
import {
9-
ensureExtension,
10-
isExportExpired,
11-
ExportsManager,
12-
validateExportName,
13-
} from "../../../src/common/exportsManager.js";
8+
import { ensureExtension, isExportExpired, ExportsManager } from "../../../src/common/exportsManager.js";
149
import type { AvailableExport } from "../../../src/common/exportsManager.js";
1510
import { config } from "../../../src/common/config.js";
1611
import { ROOT_DIR } from "../../accuracy/sdk/constants.js";
@@ -611,16 +606,6 @@ describe("#ensureExtension", () => {
611606
});
612607
});
613608

614-
describe("#validateExportName", () => {
615-
it("should return decoded name when name is valid", () => {
616-
expect(validateExportName(encodeURIComponent("Test Name.json"))).toEqual("Test Name.json");
617-
});
618-
it("should throw when name is invalid", () => {
619-
expect(() => validateExportName("NoExtension")).toThrow("Provided export name has no extension");
620-
expect(() => validateExportName("../something.json")).toThrow("Invalid export name: path traversal hinted");
621-
});
622-
});
623-
624609
describe("#isExportExpired", () => {
625610
it("should return true if export is expired", () => {
626611
const createdAt = Date.now() - 1000;

0 commit comments

Comments
 (0)