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
45 changes: 45 additions & 0 deletions packages/core/src/sync/webdav-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,51 @@ describe("WebDavClient PROPFIND parsing", () => {
]);
});

it("treats MKCOL auth failure as success when the parent listing shows the directory", async () => {
const calls: { method: string; url: string }[] = [];
installFetchStub((url, options) => {
const method = String(options?.method ?? "GET");
calls.push({ method, url });

if (method === "PROPFIND" && url.endsWith("/readany/")) {
return new Response("", { status: 404 });
}
if (method === "MKCOL") {
return new Response("", { status: 401 });
}
return new Response(
`<?xml version="1.0" encoding="utf-8"?>
<d:multistatus xmlns:d="DAV:">
<d:response>
<d:href>/dav/</d:href>
<d:propstat><d:prop><d:resourcetype><d:collection /></d:resourcetype></d:prop></d:propstat>
</d:response>
<d:response>
<d:href>/dav/readany/</d:href>
<d:propstat><d:prop><d:resourcetype><d:collection /></d:resourcetype></d:prop></d:propstat>
</d:response>
</d:multistatus>`,
{ status: 207 },
);
});

const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
try {
const client = new WebDavClient("https://dav.example.com/dav", "alice", "secret");
await client.ensureDirectory("/readany");
} finally {
warnSpy.mockRestore();
}

expect(calls.map((call) => call.method)).toEqual(["PROPFIND", "MKCOL", "PROPFIND", "PROPFIND"]);
expect(calls.map((call) => call.url)).toEqual([
"https://dav.example.com/dav/readany/",
"https://dav.example.com/dav/readany/",
"https://dav.example.com/dav/readany/",
"https://dav.example.com/dav/",
]);
});

it("uses a collection path when safely reading a directory", async () => {
const calls: { method: string; url: string }[] = [];
installFetchStub((url, options) => {
Expand Down
38 changes: 37 additions & 1 deletion packages/core/src/sync/webdav-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@ function toCollectionPath(path: string): string {
return path.endsWith("/") ? path : `${path}/`;
}

function getParentCollectionPath(path: string): { parent: string; name: string } | null {
const collectionPath = toCollectionPath(path);
if (collectionPath === "/") return null;

const trimmed = collectionPath.replace(/\/+$/g, "");
const lastSlash = trimmed.lastIndexOf("/");
const name = trimmed.slice(lastSlash + 1);
const parent = lastSlash <= 0 ? "/" : trimmed.slice(0, lastSlash);
return { parent: toCollectionPath(parent), name };
}

function createHttpWebDavError(
status: number,
statusText: string | undefined,
Expand Down Expand Up @@ -407,7 +418,7 @@ export class WebDavClient {
try {
resp = await this.request("MKCOL", collectionPath);
} catch (e) {
if (await this.propfindExists(collectionPath, { timeoutMs: DIRECTORY_PROBE_TIMEOUT_MS })) {
if (await this.collectionExists(collectionPath)) {
console.warn(`[WebDAV] MKCOL ${path} failed but directory exists; continuing`);
return;
}
Expand All @@ -420,6 +431,10 @@ export class WebDavClient {
if (status === 405 || status === 409) {
return;
}
if ((status === 401 || status === 403) && (await this.collectionExists(collectionPath))) {
console.warn(`[WebDAV] MKCOL ${path} returned ${status} but directory exists; continuing`);
return;
}
throw new Error(`WebDAV MKCOL failed for ${path}: ${status} ${resp.statusText || ""}`);
}

Expand Down Expand Up @@ -661,6 +676,27 @@ export class WebDavClient {
}
}

private async parentListingContainsCollection(path: string): Promise<boolean> {
const parentInfo = getParentCollectionPath(path);
if (!parentInfo) return true;

try {
const resources = await this.propfind(parentInfo.parent);
return resources.some(
(resource) => resource.isCollection && resource.name === parentInfo.name,
);
} catch {
return false;
}
}

private async collectionExists(path: string): Promise<boolean> {
if (await this.propfindExists(path, { timeoutMs: DIRECTORY_PROBE_TIMEOUT_MS })) {
return true;
}
return this.parentListingContainsCollection(path);
}

/** List directory contents (PROPFIND Depth 1) */
async propfind(path: string): Promise<DavResource[]> {
const resp = await this.request("PROPFIND", path, {
Expand Down