Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[web] Pass the auth token as a query param when fetching files

Manav Rathi89b703498c48
diff --git a/web/apps/photos/src/services/download/clients/photos.ts b/web/apps/photos/src/services/download/clients/photos.ts
index 6a4d9cddba..896c593830 100644
--- a/web/apps/photos/src/services/download/clients/photos.ts
+++ b/web/apps/photos/src/services/download/clients/photos.ts
@@ -59,15 +59,49 @@ export class PhotosDownloadClient implements DownloadClient {
}
async downloadFileStream(file: EnteFile): Promise<Response> {
- if (!this.token) {
- throw Error(CustomError.TOKEN_MISSING);
- }
- return retryAsyncFunction(() =>
- fetch(getFileURL(file.id), {
- headers: {
- "X-Auth-Token": this.token,
- },
- }),
- );
+ const token = this.token;
+ if (!token) throw Error(CustomError.TOKEN_MISSING);
+
+ // [Note: Passing credentials for self-hosted file fetches]
+ //
+ // Fetching files (or thumbnails) in the default self-hosted Ente
+ // configuration involves a redirection:
+ //
+ // 1. The browser makes a HTTP GET to a museum with credentials. Museum
+ // inspects the credentials, in this case the auth token, and if
+ // they're valid, returns a HTTP 307 redirect to the pre-signed S3
+ // URL that to the file in the configured S3 bucket.
+ //
+ // 2. The browser follows the redirect to get the actual file. The URL
+ // is pre-signed, i.e. already has all credentials needed to prove to
+ // the S3 object storage that it should serve this response.
+ //
+ // For the first step normally we'd pass the auth the token via the
+ // "X-Auth-Token" HTTP header. In this case though, that would be
+ // problematic because the browser preserves the request headers when it
+ // follows the HTTP 307 redirect, and the "X-Auth-Token" header also
+ // gets sent to the redirected S3 request made in second step.
+ //
+ // To avoid this, we pass the token as a query parameter. Generally this
+ // is not a good idea, but in this case (a) the URL is not a user
+ // visible one and (b) even if it gets logged, it'll be in the
+ // self-hosters own service.
+ //
+ // Note that Ente's own servers don't have these concerns because we use
+ // a slightly different flow involving a proxy instead of directly
+ // connecting to the S3 storage.
+ //
+ // 1. The web browser makes a HTTP GET request to a proxy passing it the
+ // credentials in the "X-Auth-Token".
+ //
+ // 2. The proxy then does both the original steps: (a). Use the
+ // credentials to get the pre signed URL, and (b) fetch that pre
+ // signed URL and stream back the response.
+
+ const params = new URLSearchParams({ token });
+ const getFile = () =>
+ fetch(`${getFileURL(file.id)}?${params.toString()}`);
+
+ return retryAsyncFunction(getFile);
}
}