- 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]>
88 lines
2.8 KiB
TypeScript
88 lines
2.8 KiB
TypeScript
import { AppData } from "@/src/data/app-data";
|
|
|
|
type Params = {
|
|
headers: Headers;
|
|
/**
|
|
* The computer's own address, e.g. `http://cobble-computer:8080`.
|
|
*/
|
|
base_url: string;
|
|
/**
|
|
* Where the computer is served, e.g. `/computers/3`.
|
|
*/
|
|
prefix: string;
|
|
};
|
|
|
|
/**
|
|
* Headers that would stop the chat from showing the computer in a frame,
|
|
* or that no longer fit the body (fetch has already decoded it).
|
|
*/
|
|
const Dropped = ["x-frame-options", "content-encoding", "content-length", "transfer-encoding", "connection"];
|
|
|
|
/**
|
|
* The headers a computer's response goes back with: framing allowed, and
|
|
* redirects pointed back under the computer's prefix.
|
|
*/
|
|
export default function computerProxyResponseHeaders({ headers, base_url, prefix }: Params) {
|
|
const out = new Headers(headers);
|
|
|
|
for (const name of Dropped) out.delete(name);
|
|
|
|
/**
|
|
* The sandbox is served on the app's own origin, so a cookie it sets
|
|
* would land beside the app's session: each one is kept to the
|
|
* computer's own prefix, and one named like the app's session cookies
|
|
* is dropped.
|
|
*/
|
|
out.delete("set-cookie");
|
|
|
|
for (const cookie of computerProxyCookies({ cookies: headers.getSetCookie(), prefix })) {
|
|
out.append("set-cookie", cookie);
|
|
}
|
|
|
|
const csp = out.get("content-security-policy");
|
|
|
|
if (csp?.includes("frame-ancestors")) {
|
|
out.set(
|
|
"content-security-policy",
|
|
csp
|
|
.split(";")
|
|
.filter((d) => !d.trim().startsWith("frame-ancestors"))
|
|
.join(";"),
|
|
);
|
|
}
|
|
|
|
const location = out.get("location");
|
|
|
|
if (location) {
|
|
const base = base_url.replace(/\/+$/, "");
|
|
|
|
if (location.startsWith(base)) {
|
|
out.set("location", `${prefix}${location.slice(base.length) || "/"}`);
|
|
} else if (location.startsWith("/") && !location.startsWith("//") && !location.startsWith(`${prefix}/`)) {
|
|
out.set("location", `${prefix}${location}`);
|
|
}
|
|
}
|
|
|
|
return out;
|
|
}
|
|
|
|
/**
|
|
* A computer's `Set-Cookie` values, each limited to `Path=<prefix>` (any
|
|
* `Path` or `Domain` it named removed), without the app's own cookie
|
|
* names.
|
|
*/
|
|
export function computerProxyCookies({ cookies, prefix }: { cookies: string[]; prefix: string }) {
|
|
const reserved: string[] = [AppData.AuthKeyCookieName, AppData.AuthCSRFCookieName, AppData.BuildCookieName];
|
|
|
|
return cookies
|
|
.filter((cookie) => !reserved.includes(cookie.split("=")[0]!.trim()))
|
|
.map((cookie) => {
|
|
const [pair, ...attributes] = cookie.split(";");
|
|
const kept = attributes
|
|
.map((attribute) => attribute.trim())
|
|
.filter((attribute) => attribute && !/^(path|domain)\s*=/i.test(attribute));
|
|
|
|
return [pair!.trim(), ...kept, `Path=${prefix}/`].join("; ");
|
|
});
|
|
}
|