mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2025-08-04 04:35:19 +02:00
More security for S3 uploads
This commit is contained in:
parent
2dfa5674d5
commit
780eff6c4c
2 changed files with 31 additions and 8 deletions
|
@ -64,9 +64,38 @@ export async function POST(event: RequestEvent): Promise<Response> {
|
||||||
"Content-Type": contentType,
|
"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);
|
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;
|
const key: string = event.locals.user.icon.split("/").pop() as string;
|
||||||
await deleteObject(bucket, key);
|
await deleteObject(bucket, key);
|
||||||
}
|
}
|
||||||
|
@ -77,13 +106,6 @@ export async function POST(event: RequestEvent): Promise<Response> {
|
||||||
Buffer.from(fileBuffer)
|
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}`);
|
console.log(`File uploaded to ${objectUrl}`);
|
||||||
|
|
||||||
return new Response(JSON.stringify({ objectUrl }), {
|
return new Response(JSON.stringify({ objectUrl }), {
|
||||||
|
|
|
@ -74,6 +74,7 @@ export const actions: Actions = {
|
||||||
body: profilePicture,
|
body: profilePicture,
|
||||||
headers: {
|
headers: {
|
||||||
bucket: "profile-pics",
|
bucket: "profile-pics",
|
||||||
|
type: "profile-pic",
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue