- Media rows can no longer be created through the CRUD API, updates only take description fields on the caller's own row, and every media read or delete goes through mediaFilePath, which refuses paths outside DATA_DIR or inside the database folder. - Uploads strip the file name and extension to safe characters and keep only title, description and summary from the request's media object. - Folder imports no longer follow symlinks (walkRegularFiles), and the source path is quoted in the computer's shell. - User and bot files are served with nosniff, and HTML, SVG, XML and scripts are sandboxed by CSP. - Sandbox cookies are kept to the computer's prefix, and websocket upgrades on /ws and the computer proxy must come from the app's own address (README and Help note nginx's Host header). - user_types CRUD writes go through the assignment matrix; members only read their own user record; read-only users can read CRUD again; grabUsers builds valid SQL for array search terms and bad pages; an admin media DELETE needs an id; read-only users can't delete media. - Rate limits and the audit log use the connection's address, trusting forwarded headers only from a proxy, and login limits are split per address, per account from that address and per account overall. Co-Authored-By: Claude Opus 5.5 <[email protected]>
83 lines
2.3 KiB
TypeScript
83 lines
2.3 KiB
TypeScript
import mediaFilePath from "@/src/utils/media-file-path";
|
|
import servedFileHeaders from "@/src/utils/served-file-headers";
|
|
import { existsSync } from "node:fs";
|
|
import userAuth from "../../backend/auth/user-auth";
|
|
import grabUserMedia from "../../backend/db/media/grab-user-media";
|
|
|
|
|
|
type Params = {
|
|
req: Request;
|
|
};
|
|
|
|
/**
|
|
* Serves a media file's bytes: `/media/<id>`, and `/media/<id>/thumbnail`
|
|
* for an image's smaller copy.
|
|
*
|
|
* There is no public media. Every URL is private, so the request has to be
|
|
* signed in and the user has to reach the file (`grabUserMedia` decides
|
|
* that: their own upload, any profile photo, bot picture or workspace
|
|
* picture, a bot's or workspace's file they can reach, a file on one of
|
|
* their tasks). Anything else is answered with a 404, never with the file.
|
|
*/
|
|
export default async function handleMediaServer({
|
|
req,
|
|
}: Params): Promise<Response> {
|
|
const url = new URL(req.url);
|
|
|
|
const [_root, media_id, thumbnail] = url.pathname
|
|
.split("/")
|
|
.filter((p) => p.match(/./));
|
|
|
|
if (!media_id) {
|
|
throw new Error(`No media id passed`);
|
|
}
|
|
|
|
const { user } = await userAuth({ req });
|
|
|
|
if (!user?.logged_in_status) {
|
|
throw new Error(`Not logged in`);
|
|
}
|
|
|
|
const media = await grabUserMedia({ media_id, user_id: user.id! });
|
|
|
|
if (!media?.id) {
|
|
throw new Error(`Media record not found!`);
|
|
}
|
|
|
|
const full_media_location = mediaFilePath({
|
|
write_path: thumbnail
|
|
? media.media_thumbnail_write_path
|
|
: media.media_write_path,
|
|
});
|
|
|
|
if (!full_media_location) {
|
|
throw new Error(`No media location found!`);
|
|
}
|
|
|
|
if (!existsSync(full_media_location)) {
|
|
throw new Error(`Media does not exist!`);
|
|
}
|
|
|
|
const file = Bun.file(full_media_location);
|
|
const etag = `"${file.size}-${file.lastModified}"`;
|
|
|
|
const headers = new Headers({
|
|
...servedFileHeaders({ content_type: file.type }),
|
|
ETag: etag,
|
|
/**
|
|
* Private: the response depends on who is signed in, so no shared
|
|
* cache may hold it.
|
|
*/
|
|
"Cache-Control": "private, no-cache",
|
|
});
|
|
|
|
if (req.headers.get("if-none-match") === etag) {
|
|
return new Response(null, {
|
|
status: 304,
|
|
headers,
|
|
});
|
|
}
|
|
|
|
return new Response(file, { headers });
|
|
}
|