Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix(assets): prevent active content execution (#1373)

Tin Sever39b787a6c2ec
diff --git a/apps/api/src/index.ts b/apps/api/src/index.ts
index 8b6099375..665cf8b93 100644
--- a/apps/api/src/index.ts
+++ b/apps/api/src/index.ts
@@ -111,7 +111,19 @@ type ApiVariables = {
};
};
-function buildContentDisposition(filename: string) {
+const SAFE_INLINE_ASSET_TYPES = new Set([
+ "image/apng",
+ "image/avif",
+ "image/gif",
+ "image/heic",
+ "image/heif",
+ "image/jpeg",
+ "image/jpg",
+ "image/png",
+ "image/webp",
+]);
+
+function buildContentDisposition(filename: string, inline: boolean) {
const normalized = filename
.normalize("NFC")
.replace(/[\r\n"]/g, "")
@@ -130,7 +142,8 @@ function buildContentDisposition(filename: string) {
(char) => `%${char.charCodeAt(0).toString(16).toUpperCase()}`,
);
- return `inline; filename="${asciiFallback}"; filename*=UTF-8''${encodedFilename}`;
+ const disposition = inline ? "inline" : "attachment";
+ return `${disposition}; filename="${asciiFallback}"; filename*=UTF-8''${encodedFilename}`;
}
export function createApp() {
@@ -292,15 +305,27 @@ export function createApp() {
try {
const object = await getPrivateObject(asset.objectKey);
+ const storedContentType =
+ (object.contentType || asset.mimeType)
+ .toLowerCase()
+ .split(";")[0]
+ ?.trim() ?? "";
+ const inline = SAFE_INLINE_ASSET_TYPES.has(storedContentType);
return new Response(object.body as BodyInit, {
headers: {
"Cache-Control": asset.isPublic
? "public, max-age=300"
: "private, max-age=120",
- "Content-Disposition": buildContentDisposition(asset.filename),
+ "Content-Disposition": buildContentDisposition(
+ asset.filename,
+ inline,
+ ),
"Content-Length": object.contentLength?.toString() || "",
- "Content-Type": object.contentType || asset.mimeType,
+ "Content-Type": inline
+ ? storedContentType
+ : "application/octet-stream",
+ "X-Content-Type-Options": "nosniff",
ETag: object.etag || "",
"Last-Modified": object.lastModified?.toUTCString() || "",
},
diff --git a/apps/api/src/storage/s3.ts b/apps/api/src/storage/s3.ts
index cbe69d5a4..f53e3fa0e 100644
--- a/apps/api/src/storage/s3.ts
+++ b/apps/api/src/storage/s3.ts
@@ -24,7 +24,6 @@ const allowedImageMimeTypes = new Set([
"image/jpeg",
"image/jpg",
"image/png",
- "image/svg+xml",
"image/webp",
]);
diff --git a/apps/web/src/lib/upload-task-image.ts b/apps/web/src/lib/upload-task-image.ts
index 60b678b80..792c9b6a3 100644
--- a/apps/web/src/lib/upload-task-image.ts
+++ b/apps/web/src/lib/upload-task-image.ts
@@ -11,7 +11,6 @@ const allowedImageMimeTypes = new Set([
"image/jpeg",
"image/jpg",
"image/png",
- "image/svg+xml",
"image/webp",
]);