1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-08-02 19:55:18 +02:00

More security for S3 uploads

This commit is contained in:
Sean Morley 2024-06-13 16:07:17 +00:00
parent 2dfa5674d5
commit 780eff6c4c
2 changed files with 31 additions and 8 deletions

View file

@ -64,9 +64,38 @@ export async function POST(event: RequestEvent): Promise<Response> {
"Content-Type": contentType,
};
const allowedBuckets = ["backgrounds", "profile-pics"];
if (!allowedBuckets.includes(bucket)) {
return new Response(JSON.stringify({ error: "Invalid bucket name" }), {
status: 400,
headers: {
"Content-Type": "application/json",
},
});
}
// Admin only for backgrounds
if (
bucket === "backgrounds" &&
type == "background" &&
event.locals.user.role !== "admin"
) {
return new Response(JSON.stringify({ error: "Unauthorized" }), {
status: 401,
headers: {
"Content-Type": "application/json",
},
});
}
await ensureBucketExists(bucket);
if (event.locals.user?.icon && bucket === "profile-pics") {
if (
event.locals.user?.icon &&
bucket === "profile-pics" &&
type === "profile-pic"
) {
const key: string = event.locals.user.icon.split("/").pop() as string;
await deleteObject(bucket, key);
}
@ -77,13 +106,6 @@ export async function POST(event: RequestEvent): Promise<Response> {
Buffer.from(fileBuffer)
);
if (bucket === "images" && type && type === "background") {
let res = await db.insert(imagesTable).values({
url: objectUrl,
type: "background",
});
}
console.log(`File uploaded to ${objectUrl}`);
return new Response(JSON.stringify({ objectUrl }), {

View file

@ -74,6 +74,7 @@ export const actions: Actions = {
body: profilePicture,
headers: {
bucket: "profile-pics",
type: "profile-pic",
},
});