Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

fix: malformed pptx name (#2168)

Marc Seitzab70c47950cf
diff --git a/lib/files/put-file-server.ts b/lib/files/put-file-server.ts
index 44c085fc..20440572 100644
--- a/lib/files/put-file-server.ts
+++ b/lib/files/put-file-server.ts
@@ -5,7 +5,7 @@ import path from "node:path";
import { match } from "ts-pattern";
import { newId } from "@/lib/id-helper";
-import { safeSlugify } from "@/lib/utils";
+import { buildContentDisposition, safeSlugify } from "@/lib/utils";
import { SUPPORTED_DOCUMENT_MIME_TYPES } from "../constants";
import { getTeamS3ClientAndConfig } from "./aws-client";
@@ -102,7 +102,10 @@ const putFileInS3Server = async ({
Key: key,
Body: file.buffer,
ContentType: file.type,
- ContentDisposition: `attachment; filename="${slugifiedName}"; filename*=UTF-8''${encodeURIComponent(originalFileName)}`,
+ ContentDisposition: buildContentDisposition(
+ originalFileName,
+ slugifiedName,
+ ),
};
// Create a new instance of the PutObjectCommand with the parameters
diff --git a/lib/files/stream-file-server.ts b/lib/files/stream-file-server.ts
index 27cd5bf0..15d6b731 100644
--- a/lib/files/stream-file-server.ts
+++ b/lib/files/stream-file-server.ts
@@ -3,7 +3,7 @@ import { DocumentStorageType } from "@prisma/client";
import path from "node:path";
import { Readable } from "stream";
-import { safeSlugify } from "@/lib/utils";
+import { buildContentDisposition, safeSlugify } from "@/lib/utils";
import { getTeamS3ClientAndConfig } from "./aws-client";
@@ -38,7 +38,10 @@ export const streamFileServer = async ({
Key: key,
Body: file.stream,
ContentType: file.type,
- ContentDisposition: `attachment; filename="${slugifiedName}"; filename*=UTF-8''${encodeURIComponent(originalFileName)}`,
+ ContentDisposition: buildContentDisposition(
+ originalFileName,
+ slugifiedName,
+ ),
},
};
diff --git a/lib/utils.ts b/lib/utils.ts
index 0813eff0..1d436c32 100644
--- a/lib/utils.ts
+++ b/lib/utils.ts
@@ -363,6 +363,34 @@ export function safeSlugify(input: string): string {
return slugify(transliterate(input)) || nanoid();
}
+/**
+ * RFC 5987 percent-encoder for Content-Disposition `filename*` values.
+ *
+ * `encodeURIComponent` leaves `! * ' ( ) ~` unencoded, but RFC 5987's
+ * `attr-char` set does not allow any of those, and strict parsers
+ * (e.g. Go's `mime.ParseMediaType` used by Gotenberg) reject them with
+ * `mime: invalid media parameter`.
+ */
+export function encodeRFC5987(value: string): string {
+ return encodeURIComponent(value).replace(
+ /['()*!~]/g,
+ (c) => "%" + c.charCodeAt(0).toString(16).toUpperCase(),
+ );
+}
+
+/**
+ * Build a strictly RFC 5987-compliant Content-Disposition header so downstream
+ * tooling (Gotenberg/LibreOffice, etc.) can parse it without errors. The
+ * `filename` fallback uses the slugified ASCII name; `filename*` carries the
+ * original (possibly Unicode) name, percent-encoded per RFC 5987.
+ */
+export function buildContentDisposition(
+ originalFileName: string,
+ slugifiedName: string,
+): string {
+ return `attachment; filename="${slugifiedName}"; filename*=UTF-8''${encodeRFC5987(originalFileName)}`;
+}
+
export const daysLeft = (
accountCreationDate: Date,
maxDays: number,
diff --git a/pages/api/file/s3/get-presigned-post-url.ts b/pages/api/file/s3/get-presigned-post-url.ts
index 33fa7fa6..1e01d074 100644
--- a/pages/api/file/s3/get-presigned-post-url.ts
+++ b/pages/api/file/s3/get-presigned-post-url.ts
@@ -7,7 +7,7 @@ import path from "node:path";
import { ONE_HOUR, ONE_SECOND } from "@/lib/constants";
import { getTeamS3ClientAndConfig } from "@/lib/files/aws-client";
-import { safeSlugify } from "@/lib/utils";
+import { buildContentDisposition, safeSlugify } from "@/lib/utils";
import prisma from "@/lib/prisma";
import { CustomUser } from "@/lib/types";
@@ -56,7 +56,10 @@ export default async function handler(
const slugifiedName = safeSlugify(name) + ext;
const originalFileName = `${name}${ext}`;
const key = `${team.id}/${docId}/${slugifiedName}`;
- const contentDisposition = `attachment; filename="${slugifiedName}"; filename*=UTF-8''${encodeURIComponent(originalFileName)}`;
+ const contentDisposition = buildContentDisposition(
+ originalFileName,
+ slugifiedName,
+ );
const { client, config } = await getTeamS3ClientAndConfig(team.id);
diff --git a/pages/api/file/s3/multipart.ts b/pages/api/file/s3/multipart.ts
index d4b41b82..63f9bbfc 100644
--- a/pages/api/file/s3/multipart.ts
+++ b/pages/api/file/s3/multipart.ts
@@ -12,7 +12,7 @@ import path from "node:path";
import { ONE_HOUR, ONE_SECOND } from "@/lib/constants";
import { getTeamS3ClientAndConfig } from "@/lib/files/aws-client";
-import { safeSlugify } from "@/lib/utils";
+import { buildContentDisposition, safeSlugify } from "@/lib/utils";
import prisma from "@/lib/prisma";
import { CustomUser } from "@/lib/types";
import { MultipartUploadSchema } from "@/lib/zod/schemas/multipart";
@@ -81,7 +81,10 @@ export default async function handler(
Bucket: config.bucket,
Key: key,
ContentType: contentType,
- ContentDisposition: `attachment; filename="${slugifiedName}"; filename*=UTF-8''${encodeURIComponent(originalFileName)}`,
+ ContentDisposition: buildContentDisposition(
+ originalFileName,
+ slugifiedName,
+ ),
});
const createResponse = await client.send(createCommand);
diff --git a/pages/api/file/tus-viewer/[[...file]].ts b/pages/api/file/tus-viewer/[[...file]].ts
index 7fc90d77..16b22c68 100644
--- a/pages/api/file/tus-viewer/[[...file]].ts
+++ b/pages/api/file/tus-viewer/[[...file]].ts
@@ -7,7 +7,7 @@ import path from "node:path";
import { verifyDataroomSessionInPagesRouter } from "@/lib/auth/dataroom-auth";
import { getTeamS3ClientAndConfig } from "@/lib/files/aws-client";
-import { safeSlugify } from "@/lib/utils";
+import { buildContentDisposition, safeSlugify } from "@/lib/utils";
import { RedisLocker } from "@/lib/files/tus-redis-locker";
import { newId } from "@/lib/id-helper";
import prisma from "@/lib/prisma";
@@ -148,7 +148,10 @@ const tusServer = new Server({
const contentType = metadata.contentType || "application/octet-stream";
const { name, ext } = path.parse(metadata.fileName!);
const originalFileName = `${name}${ext}`;
- const contentDisposition = `attachment; filename="${safeSlugify(name)}${ext}"; filename*=UTF-8''${encodeURIComponent(originalFileName)}`;
+ const contentDisposition = buildContentDisposition(
+ originalFileName,
+ `${safeSlugify(name)}${ext}`,
+ );
// The Key (object path) where the file was uploaded
const objectKey = upload.id;
diff --git a/pages/api/file/tus/[[...file]].ts b/pages/api/file/tus/[[...file]].ts
index cb961aaf..a007337b 100644
--- a/pages/api/file/tus/[[...file]].ts
+++ b/pages/api/file/tus/[[...file]].ts
@@ -14,7 +14,7 @@ import { newId } from "@/lib/id-helper";
import prisma from "@/lib/prisma";
import { lockerRedisClient } from "@/lib/redis";
import { CustomUser } from "@/lib/types";
-import { log, safeSlugify } from "@/lib/utils";
+import { buildContentDisposition, log, safeSlugify } from "@/lib/utils";
import {
getFileSizeLimit,
getFileSizeLimits,
@@ -229,7 +229,10 @@ const tusServer = new Server({
const contentType = metadata.contentType || "application/octet-stream";
const { name, ext } = path.parse(metadata.fileName!);
const originalFileName = `${name}${ext}`;
- const contentDisposition = `attachment; filename="${safeSlugify(name)}${ext}"; filename*=UTF-8''${encodeURIComponent(originalFileName)}`;
+ const contentDisposition = buildContentDisposition(
+ originalFileName,
+ `${safeSlugify(name)}${ext}`,
+ );
// The Key (object path) where the file was uploaded
const objectKey = upload.id;