fix(upload): handle encoded path conflicts safely (#6040)

This commit is contained in:
Archit Goyal authored and GitHub committed 2026-07-25 07:29:49 +02:00
1 parent c05c668148
commit 7361d91ea2
4 files changed
+339 -3

No files matched your search

@@ -286,4 +286,102 @@ describe("checkConflict", () => {
expect(conflicts).toHaveLength(1);
});
it.each([
["forward slashes", "/target/My SubDir/file.txt"],
["Windows backslashes", "\\target\\My SubDir\\file.txt"],
])(
"detects conflicts below an encoded destination path with %s",
async (_label, serverPath) => {
vi.mocked(api.fetchAll).mockResolvedValue([
{
path: serverPath,
name: "file.txt",
size: 10,
modified: "2026-06-04T00:00:00Z",
isDir: false,
},
]);
const conflicts = await checkConflict(
[moveItem("file.txt", "/files/target/My%20SubDir/")],
"/files/target/My%20SubDir/"
);
expect(conflicts).toHaveLength(1);
expect(conflicts[0].name).toBe("/target/My SubDir/file.txt");
}
);
it("detects nested folder-upload conflicts below an encoded destination path", async () => {
vi.mocked(api.fetchAll).mockResolvedValue([
{
path: "\\target\\My SubDir\\folder\\nested file.txt",
name: "nested file.txt",
size: 10,
modified: "2026-06-04T00:00:00Z",
isDir: false,
},
]);
const conflicts = await checkConflict(
[
{
name: "nested file.txt",
size: 12,
isDir: false,
fullPath: "folder/nested file.txt",
},
],
"/files/target/My%20SubDir/"
);
expect(conflicts).toHaveLength(1);
expect(conflicts[0].name).toBe("/target/My SubDir/folder/nested file.txt");
});
it("leaves malformed destination path segments unchanged", async () => {
vi.mocked(api.fetchAll).mockResolvedValue([
{
path: "/target/bad%ZZ/file.txt",
name: "file.txt",
size: 10,
modified: "2026-06-04T00:00:00Z",
isDir: false,
},
]);
const conflicts = await checkConflict(
[moveItem("file.txt", "/files/target/bad%ZZ/")],
"/files/target/bad%ZZ/"
);
expect(conflicts).toHaveLength(1);
});
it("does not match files outside the decoded destination path", async () => {
vi.mocked(api.fetchAll).mockResolvedValue([
{
path: "/target/My SubDir/other.txt",
name: "other.txt",
size: 10,
modified: "2026-06-04T00:00:00Z",
isDir: false,
},
{
path: "/target/My Other SubDir/file.txt",
name: "file.txt",
size: 10,
modified: "2026-06-04T00:00:00Z",
isDir: false,
},
]);
const conflicts = await checkConflict(
[moveItem("file.txt", "/files/target/My%20SubDir/")],
"/files/target/My%20SubDir/"
);
expect(conflicts).toHaveLength(0);
});
});
+14 -1
View File
@@ -44,13 +44,26 @@ function conflictPath(entry: ServerConflictEntry): string {
return entry.path.replace(/\\/g, "/");
}
function decodePath(path: string): string {
return path
.split("/")
.map((segment) => {
try {
return decodeURIComponent(segment);
} catch {
return segment;
}
})
.join("/");
}
function buildConflictMap(
serverEntries: ServerConflictEntry[],
basePath: string,
includeDirectories: boolean
): Map<string, ServerConflictEntry> {
const serverMap = new Map<string, ServerConflictEntry>();
const normBase = removePrefix(basePath).replace(/\/+$/, "");
const normBase = decodePath(removePrefix(basePath)).replace(/\/+$/, "");
for (const entry of serverEntries) {
// A Windows server may return OS-native backslash separators; normalize to
// forward slashes so the prefix strip and key lookup line up.