Skip to content

armgit / ente

Public Imported from GitHub
0

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

harden file proxy workers

Manav Rathi8ec48c97145d
diff --git a/infra/workers/cast-albums/src/index.ts b/infra/workers/cast-albums/src/index.ts
index fc93d93443..9759f22bc1 100644
--- a/infra/workers/cast-albums/src/index.ts
+++ b/infra/workers/cast-albums/src/index.ts
@@ -76,5 +76,48 @@ const handleGET = async (request: Request) => {
response = new Response(response.body, response);
response.headers.set("Access-Control-Allow-Origin", "*");
+ hardenResponseHeaders(response.headers);
return response;
};
+
+const hardenResponseHeaders = (headers: Headers) => {
+ headers.set("X-Content-Type-Options", "nosniff");
+ headers.set("Content-Security-Policy", "sandbox allow-downloads");
+ headers.set("X-Frame-Options", "DENY");
+ headers.set("Referrer-Policy", "no-referrer");
+
+ const contentType = headers.get("Content-Type");
+ if (contentType && isHTMLLikeContentType(contentType)) {
+ enforceAttachmentDisposition(headers);
+ }
+};
+
+const isHTMLLikeContentType = (contentType: string) => {
+ const normalized = contentType.split(";")[0]?.trim().toLowerCase();
+ if (!normalized) return false;
+ return (
+ normalized === "text/html" ||
+ normalized === "application/xhtml+xml" ||
+ normalized === "image/svg+xml" ||
+ normalized === "text/xml" ||
+ normalized === "application/xml"
+ );
+};
+
+const enforceAttachmentDisposition = (headers: Headers) => {
+ const contentDisposition = headers.get("Content-Disposition");
+ if (contentDisposition && /attachment/i.test(contentDisposition)) {
+ return;
+ }
+
+ const filenameParams = contentDisposition
+ ?.split(";")
+ .map((part) => part.trim())
+ .filter((part, index) => {
+ if (index === 0) return false;
+ return /^filename/i.test(part);
+ });
+
+ const disposition = ["attachment", ...(filenameParams ?? [])].join("; ");
+ headers.set("Content-Disposition", disposition);
+};
diff --git a/infra/workers/files/src/index.ts b/infra/workers/files/src/index.ts
index 904a669acc..a6645ddb29 100644
--- a/infra/workers/files/src/index.ts
+++ b/infra/workers/files/src/index.ts
@@ -88,5 +88,48 @@ const handleGET = async (request: Request) => {
response = new Response(response.body, response);
response.headers.set("Access-Control-Allow-Origin", "*");
+ hardenResponseHeaders(response.headers);
return response;
};
+
+const hardenResponseHeaders = (headers: Headers) => {
+ headers.set("X-Content-Type-Options", "nosniff");
+ headers.set("Content-Security-Policy", "sandbox allow-downloads");
+ headers.set("X-Frame-Options", "DENY");
+ headers.set("Referrer-Policy", "no-referrer");
+
+ const contentType = headers.get("Content-Type");
+ if (contentType && isHTMLLikeContentType(contentType)) {
+ enforceAttachmentDisposition(headers);
+ }
+};
+
+const isHTMLLikeContentType = (contentType: string) => {
+ const normalized = contentType.split(";")[0]?.trim().toLowerCase();
+ if (!normalized) return false;
+ return (
+ normalized === "text/html" ||
+ normalized === "application/xhtml+xml" ||
+ normalized === "image/svg+xml" ||
+ normalized === "text/xml" ||
+ normalized === "application/xml"
+ );
+};
+
+const enforceAttachmentDisposition = (headers: Headers) => {
+ const contentDisposition = headers.get("Content-Disposition");
+ if (contentDisposition && /attachment/i.test(contentDisposition)) {
+ return;
+ }
+
+ const filenameParams = contentDisposition
+ ?.split(";")
+ .map((part) => part.trim())
+ .filter((part, index) => {
+ if (index === 0) return false;
+ return /^filename/i.test(part);
+ });
+
+ const disposition = ["attachment", ...(filenameParams ?? [])].join("; ");
+ headers.set("Content-Disposition", disposition);
+};
diff --git a/infra/workers/public-albums/src/index.ts b/infra/workers/public-albums/src/index.ts
index 75751a867a..05a0fe2bb6 100644
--- a/infra/workers/public-albums/src/index.ts
+++ b/infra/workers/public-albums/src/index.ts
@@ -85,5 +85,48 @@ const handleGET = async (request: Request) => {
response = new Response(response.body, response);
response.headers.set("Access-Control-Allow-Origin", "*");
+ hardenResponseHeaders(response.headers);
return response;
};
+
+const hardenResponseHeaders = (headers: Headers) => {
+ headers.set("X-Content-Type-Options", "nosniff");
+ headers.set("Content-Security-Policy", "sandbox allow-downloads");
+ headers.set("X-Frame-Options", "DENY");
+ headers.set("Referrer-Policy", "no-referrer");
+
+ const contentType = headers.get("Content-Type");
+ if (contentType && isHTMLLikeContentType(contentType)) {
+ enforceAttachmentDisposition(headers);
+ }
+};
+
+const isHTMLLikeContentType = (contentType: string) => {
+ const normalized = contentType.split(";")[0]?.trim().toLowerCase();
+ if (!normalized) return false;
+ return (
+ normalized === "text/html" ||
+ normalized === "application/xhtml+xml" ||
+ normalized === "image/svg+xml" ||
+ normalized === "text/xml" ||
+ normalized === "application/xml"
+ );
+};
+
+const enforceAttachmentDisposition = (headers: Headers) => {
+ const contentDisposition = headers.get("Content-Disposition");
+ if (contentDisposition && /attachment/i.test(contentDisposition)) {
+ return;
+ }
+
+ const filenameParams = contentDisposition
+ ?.split(";")
+ .map((part) => part.trim())
+ .filter((part, index) => {
+ if (index === 0) return false;
+ return /^filename/i.test(part);
+ });
+
+ const disposition = ["attachment", ...(filenameParams ?? [])].join("; ");
+ headers.set("Content-Disposition", disposition);
+};
diff --git a/infra/workers/thumbnails/src/index.ts b/infra/workers/thumbnails/src/index.ts
index 2e2bd89733..bfdd981204 100644
--- a/infra/workers/thumbnails/src/index.ts
+++ b/infra/workers/thumbnails/src/index.ts
@@ -80,5 +80,48 @@ const handleGET = async (request: Request) => {
response = new Response(response.body, response);
response.headers.set("Access-Control-Allow-Origin", "*");
+ hardenResponseHeaders(response.headers);
return response;
};
+
+const hardenResponseHeaders = (headers: Headers) => {
+ headers.set("X-Content-Type-Options", "nosniff");
+ headers.set("Content-Security-Policy", "sandbox allow-downloads");
+ headers.set("X-Frame-Options", "DENY");
+ headers.set("Referrer-Policy", "no-referrer");
+
+ const contentType = headers.get("Content-Type");
+ if (contentType && isHTMLLikeContentType(contentType)) {
+ enforceAttachmentDisposition(headers);
+ }
+};
+
+const isHTMLLikeContentType = (contentType: string) => {
+ const normalized = contentType.split(";")[0]?.trim().toLowerCase();
+ if (!normalized) return false;
+ return (
+ normalized === "text/html" ||
+ normalized === "application/xhtml+xml" ||
+ normalized === "image/svg+xml" ||
+ normalized === "text/xml" ||
+ normalized === "application/xml"
+ );
+};
+
+const enforceAttachmentDisposition = (headers: Headers) => {
+ const contentDisposition = headers.get("Content-Disposition");
+ if (contentDisposition && /attachment/i.test(contentDisposition)) {
+ return;
+ }
+
+ const filenameParams = contentDisposition
+ ?.split(";")
+ .map((part) => part.trim())
+ .filter((part, index) => {
+ if (index === 0) return false;
+ return /^filename/i.test(part);
+ });
+
+ const disposition = ["attachment", ...(filenameParams ?? [])].join("; ");
+ headers.set("Content-Disposition", disposition);
+};