- 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]>
60 lines
1.7 KiB
TypeScript
60 lines
1.7 KiB
TypeScript
type Params = {
|
|
/**
|
|
* The connection's own address (`server.requestIP`).
|
|
*/
|
|
peer?: string | null;
|
|
/**
|
|
* The request's `X-Forwarded-For`.
|
|
*/
|
|
forwarded_for?: string | null;
|
|
/**
|
|
* The request's `X-Real-IP`.
|
|
*/
|
|
real_ip?: string | null;
|
|
};
|
|
|
|
/**
|
|
* Whether an address is this machine or a private network: where a reverse
|
|
* proxy or tunnel in front of the app connects from.
|
|
*/
|
|
export function isProxyAddress({ ip }: { ip: string }) {
|
|
const address = ip.replace(/^::ffff:/i, "").toLowerCase();
|
|
|
|
return (
|
|
address == "::1" ||
|
|
address.startsWith("127.") ||
|
|
address.startsWith("10.") ||
|
|
address.startsWith("192.168.") ||
|
|
/^172\.(1[6-9]|2\d|3[01])\./.test(address) ||
|
|
address.startsWith("fc") ||
|
|
address.startsWith("fd") ||
|
|
address.startsWith("fe80:")
|
|
);
|
|
}
|
|
|
|
/**
|
|
* The client's address, for rate limits and the audit log. The forwarded
|
|
* headers are only believed when the connection came from a proxy
|
|
* (`isProxyAddress`), and then only the last `X-Forwarded-For` entry,
|
|
* the one that proxy added itself: anything before it is whatever the
|
|
* client chose to send. Otherwise the connection's own address wins, so a
|
|
* client can't pick a fresh address per request to dodge a limit.
|
|
*/
|
|
export default function clientIP({ peer, forwarded_for, real_ip }: Params) {
|
|
const behind_proxy = !peer || isProxyAddress({ ip: peer });
|
|
|
|
if (behind_proxy) {
|
|
const forwarded = (forwarded_for || "")
|
|
.split(",")
|
|
.map((entry) => entry.trim())
|
|
.filter(Boolean)
|
|
.at(-1);
|
|
|
|
if (forwarded) return forwarded;
|
|
|
|
if (real_ip?.trim()) return real_ip.trim();
|
|
}
|
|
|
|
return peer || undefined;
|
|
}
|