feat(server): serve non-image attachments as downloads
Content-Disposition: attachment for any attachment whose extension is not an image extension, so uploaded content is never rendered inline on the same-origin asset route. Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
1 parent
1d934d2255
commit
9b89ef9698
2 files changed
+22
-4
No files matched your search
@@ -36,6 +36,7 @@ import {
|
||||
} from "../auth/utils.ts";
|
||||
import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
|
||||
import { resolveAttachmentPathById } from "../attachmentStore.ts";
|
||||
import { SAFE_IMAGE_FILE_EXTENSIONS } from "../imageMime.ts";
|
||||
import * as ServerConfig from "../config.ts";
|
||||
import * as ProjectFaviconResolver from "../project/ProjectFaviconResolver.ts";
|
||||
import * as WorkspacePaths from "../workspace/WorkspacePaths.ts";
|
||||
@@ -91,7 +92,11 @@ const AssetClaimsJson = Schema.fromJsonString(AssetClaimsSchema);
|
||||
const decodeAssetClaims = Schema.decodeUnknownOption(AssetClaimsJson);
|
||||
const encodeAssetClaims = Schema.encodeSync(AssetClaimsJson);
|
||||
|
||||
export type ResolvedAsset = { readonly kind: "file"; readonly path: string };
|
||||
export type ResolvedAsset = {
|
||||
readonly kind: "file";
|
||||
readonly path: string;
|
||||
readonly download?: boolean;
|
||||
};
|
||||
|
||||
function decodeClaims(encodedPayload: string): AssetClaims | null {
|
||||
try {
|
||||
@@ -383,9 +388,17 @@ export const resolveAsset = Effect.fn("AssetAccess.resolveAsset")(function* (
|
||||
),
|
||||
Effect.orElseSucceed(() => Option.none()),
|
||||
);
|
||||
return Option.isSome(info) && info.value.type === "File"
|
||||
? ({ kind: "file", path: attachmentPath } satisfies ResolvedAsset)
|
||||
: null;
|
||||
if (!Option.isSome(info) || info.value.type !== "File") {
|
||||
return null;
|
||||
}
|
||||
// Non-image attachments are downloaded rather than rendered inline so the
|
||||
// same-origin asset route never interprets uploaded content (e.g. HTML/XML).
|
||||
const extension = attachmentPath.slice(attachmentPath.lastIndexOf(".")).toLowerCase();
|
||||
return {
|
||||
kind: "file",
|
||||
path: attachmentPath,
|
||||
download: !SAFE_IMAGE_FILE_EXTENSIONS.has(extension),
|
||||
} satisfies ResolvedAsset;
|
||||
}
|
||||
|
||||
if (claims.kind === "project-favicon") {
|
||||
|
||||
@@ -197,6 +197,11 @@ export const assetRouteLayer = HttpRouter.add(
|
||||
headers: {
|
||||
"Cache-Control": "private, max-age=3600",
|
||||
"X-Content-Type-Options": "nosniff",
|
||||
...(asset.download
|
||||
? {
|
||||
"Content-Disposition": `attachment; filename="${asset.path.replace(/^.*[/\\]/, "")}"`,
|
||||
}
|
||||
: {}),
|
||||
},
|
||||
}).pipe(
|
||||
Effect.orElseSucceed(() => HttpServerResponse.text("Internal Server Error", { status: 500 })),
|
||||
|
||||
Reference in new issue
Block a user