fix: CORS preflight (#10604)

This commit is contained in:
Tom Moor authored and GitHub committed 2025-11-09 09:12:08 -05:00
1 parent 959dccf119
commit 7a0e88cd3c
1 file changed
+12 -1
+12 -1
View File
@@ -88,7 +88,18 @@ export const uploadFile = async (
xhr.addEventListener("loadend", () => {
resolve(xhr.readyState === 4 && xhr.status >= 200 && xhr.status < 400);
});
xhr.withCredentials = true;
// Do not send credentials if uploading to a different origin, as the combination
// of CORS and cookies will cause preflight request failure. However S3-like storage
// on the same host can work with credentials.
if (data.uploadUrl.startsWith("/")) {
xhr.withCredentials = true;
} else {
const parsed = new URL(data.uploadUrl);
const requiresPreflightRequest = parsed.origin !== window.location.origin;
xhr.withCredentials = !requiresPreflightRequest;
}
xhr.open("POST", data.uploadUrl, true);
xhr.send(formData);
});