Fix file access, XSS and access-control bugs from a security scan

- 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]>
This commit is contained in:
2026-10-25 03:38:07 +01:00
co-authored by Claude Opus 5.5
parent 2e8e8de976
commit cdb93ac1bc
42 changed files with 817 additions and 204 deletions
@@ -1,10 +1,9 @@
import grabDirNames from "@/src/utils/grab-dir-names";
import mediaFilePath from "@/src/utils/media-file-path";
import servedFileHeaders from "@/src/utils/served-file-headers";
import { existsSync } from "node:fs";
import path from "node:path";
import userAuth from "../../backend/auth/user-auth";
import grabUserMedia from "../../backend/db/media/grab-user-media";
const { DATA_DIR } = grabDirNames();
type Params = {
req: Request;
@@ -45,16 +44,16 @@ export default async function handleMediaServer({
throw new Error(`Media record not found!`);
}
const media_location = thumbnail
? media.media_thumbnail_write_path
: media.media_write_path;
const full_media_location = mediaFilePath({
write_path: thumbnail
? media.media_thumbnail_write_path
: media.media_write_path,
});
if (!media_location) {
if (!full_media_location) {
throw new Error(`No media location found!`);
}
const full_media_location = path.join(DATA_DIR, media_location);
if (!existsSync(full_media_location)) {
throw new Error(`Media does not exist!`);
}
@@ -63,6 +62,7 @@ export default async function handleMediaServer({
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