Initial upload to Gitea
This commit is contained in:
1 parent
78193254ad
commit
8478998cae
1485 files changed
+461025
-42348
No files matched your search
@@ -0,0 +1,201 @@
|
||||
"use strict";
|
||||
|
||||
// Wire-level privacy redaction for data that must stay fully readable
|
||||
// server-side (session audit records, remember-login registry) but must not
|
||||
// ship to browsers in raw form. Every function here is fail-closed: input
|
||||
// that cannot be parsed confidently is replaced by a fixed placeholder,
|
||||
// never echoed back.
|
||||
|
||||
const net = require("net");
|
||||
|
||||
// Loopback and other placeholder inputs that carry no network identity.
|
||||
const NON_IP_PLACEHOLDER = "unknown";
|
||||
|
||||
function maskIPv4(raw) {
|
||||
const octets = raw.split(".");
|
||||
// net.isIPv4() guarantees four 0-255 decimal octets; re-validate anyway so
|
||||
// this function is safe to call standalone.
|
||||
if (octets.length !== 4 || octets.some((o) => !/^\d{1,3}$/.test(o) || Number(o) > 255)) {
|
||||
return NON_IP_PLACEHOLDER;
|
||||
}
|
||||
return `${octets[0]}.${octets[1]}.${octets[2]}.0`;
|
||||
}
|
||||
|
||||
// Expands an IPv6 string (already validated by net.isIPv6) into eight numeric
|
||||
// hextets. Embedded IPv4 tails ("::ffff:192.0.2.1") become two hextets.
|
||||
function expandIPv6Groups(raw) {
|
||||
const parts = raw.split("::");
|
||||
if (parts.length > 2) return null;
|
||||
|
||||
const parseGroup = (group) => {
|
||||
if (/^\d{1,3}(\.\d{1,3}){3}$/.test(group)) {
|
||||
const octets = group.split(".").map(Number);
|
||||
if (octets.some((o) => o > 255)) return null;
|
||||
return [(octets[0] << 8) | octets[1], (octets[2] << 8) | octets[3]];
|
||||
}
|
||||
if (!/^[0-9a-f]{1,4}$/.test(group)) return null;
|
||||
return [parseInt(group, 16)];
|
||||
};
|
||||
|
||||
const headGroups = [];
|
||||
for (const group of (parts[0] === "" ? [] : parts[0].split(":"))) {
|
||||
const parsed = parseGroup(group);
|
||||
if (!parsed) return null;
|
||||
headGroups.push(...parsed);
|
||||
}
|
||||
|
||||
let tailGroups = null;
|
||||
if (parts.length === 2) {
|
||||
tailGroups = [];
|
||||
for (const group of (parts[1] === "" ? [] : parts[1].split(":"))) {
|
||||
const parsed = parseGroup(group);
|
||||
if (!parsed) return null;
|
||||
tailGroups.push(...parsed);
|
||||
}
|
||||
}
|
||||
|
||||
if (tailGroups === null) {
|
||||
return headGroups.length === 8 ? headGroups : null;
|
||||
}
|
||||
|
||||
const fill = 8 - headGroups.length - tailGroups.length;
|
||||
if (fill < 0) return null;
|
||||
return [...headGroups, ...new Array(fill).fill(0), ...tailGroups];
|
||||
}
|
||||
|
||||
function maskIPv6(raw) {
|
||||
const groups = expandIPv6Groups(raw);
|
||||
if (!groups || groups.length !== 8) return NON_IP_PLACEHOLDER;
|
||||
|
||||
// IPv4-mapped (::ffff:a.b.c.d): the real client network is IPv4, so mask
|
||||
// the embedded address as IPv4 instead of hiding it inside "::ffff:0.0.0".
|
||||
if (groups.slice(0, 5).every((g) => g === 0) && groups[5] === 0xffff) {
|
||||
return maskIPv4(`${groups[6] >> 8}.${groups[6] & 0xff}.${groups[7] >> 8}.${groups[7] & 0xff}`);
|
||||
}
|
||||
|
||||
// /48 network prefix: first three hextets, with trailing zero hextets
|
||||
// folded into the "::" suffix for readable output ("fe80::", "::").
|
||||
const prefixGroups = [groups[0], groups[1], groups[2]];
|
||||
while (prefixGroups.length > 0 && prefixGroups[prefixGroups.length - 1] === 0) {
|
||||
prefixGroups.pop();
|
||||
}
|
||||
if (prefixGroups.length === 0) return "::";
|
||||
return `${prefixGroups.map((g) => g.toString(16)).join(":")}::`;
|
||||
}
|
||||
|
||||
// Masks an IP address for display: /24 for IPv4, /48 for IPv6. Anything that
|
||||
// does not parse as an IP becomes "unknown" — the raw input is never returned.
|
||||
function maskIpAddress(value) {
|
||||
let raw = String(value || "").trim().toLowerCase();
|
||||
if (!raw) return "";
|
||||
|
||||
// Tolerate proxy/store artifacts: "[2001:db8::1]" and "192.0.2.10:52341".
|
||||
if (raw.startsWith("[")) {
|
||||
const close = raw.indexOf("]");
|
||||
if (close > 0) raw = raw.slice(1, close);
|
||||
} else if (/^\d{1,3}(\.\d{1,3}){3}:\d{1,5}$/.test(raw)) {
|
||||
raw = raw.slice(0, raw.lastIndexOf(":"));
|
||||
}
|
||||
|
||||
// Link-local addresses may carry a zone index ("fe80::1%eth0").
|
||||
const zone = raw.indexOf("%");
|
||||
if (zone > 0) raw = raw.slice(0, zone);
|
||||
|
||||
if (net.isIPv4(raw)) return maskIPv4(raw);
|
||||
if (net.isIPv6(raw)) return maskIPv6(raw);
|
||||
return NON_IP_PLACEHOLDER;
|
||||
}
|
||||
|
||||
// Order matters: Chromium-derived browsers advertise Chrome/ too, and iOS
|
||||
// variants advertise Safari/, so specific families are matched first.
|
||||
const UA_BROWSER_PATTERNS = [
|
||||
{ re: /edg(?:a|ios)?[ /](\d{1,3})/i, name: "Edge" },
|
||||
{ re: /opr\/(\d{1,3})/i, name: "Opera" },
|
||||
{ re: /opera[ /](\d{1,3})/i, name: "Opera" },
|
||||
{ re: /samsungbrowser\/(\d{1,3})/i, name: "Samsung Internet" },
|
||||
{ re: /crios\/(\d{1,3})/i, name: "Chrome" },
|
||||
{ re: /fxios\/(\d{1,3})/i, name: "Firefox" },
|
||||
{ re: /firefox\/(\d{1,3})/i, name: "Firefox" },
|
||||
{ re: /chrome\/(\d{1,3})/i, name: "Chrome" },
|
||||
{ re: /version\/(\d{1,3})[\d.]*.*safari/i, name: "Safari" },
|
||||
// Bare "Safari/605" tokens are engine build numbers, not product versions.
|
||||
{ re: /safari\/\d/i, name: "Safari" },
|
||||
];
|
||||
|
||||
const UA_AUTOMATED_PATTERNS = [
|
||||
[/curl\/(\d{1,3})/i, "curl"],
|
||||
[/wget\/(\d{1,3})/i, "wget"],
|
||||
[/python-requests\/(\d{1,3})/i, "python-requests"],
|
||||
[/python-urllib\/(\d{1,3})/i, "python-urllib"],
|
||||
[/go-http-client\/(\d{1,3})/i, "go-http-client"],
|
||||
[/node(?:-fetch|\/v?\d)/i, "node"],
|
||||
[/java\/(\d{1,3})/i, "java"],
|
||||
[/axios\/(\d{1,3})/i, "axios"],
|
||||
[/postmanruntime\/(\d{1,3})/i, "postman"],
|
||||
];
|
||||
|
||||
function describeOsFromUserAgent(ua) {
|
||||
const nt = ua.match(/windows nt (\d{1,2})\.(\d{1,2})/i);
|
||||
if (nt) {
|
||||
const version = `${nt[1]}.${nt[2]}`;
|
||||
if (version === "10.0") return "Windows 10/11";
|
||||
if (version === "11.0") return "Windows 11";
|
||||
if (version === "6.1") return "Windows 7";
|
||||
if (version === "6.2") return "Windows 8";
|
||||
if (version === "6.3") return "Windows 8.1";
|
||||
return "Windows";
|
||||
}
|
||||
if (/windows phone/i.test(ua)) return "Windows Phone";
|
||||
if (/windows/i.test(ua)) return "Windows";
|
||||
|
||||
const android = ua.match(/android[ /]?(\d{1,3})/i);
|
||||
if (android) return `Android ${android[1]}`;
|
||||
if (/android/i.test(ua)) return "Android";
|
||||
|
||||
const ios = ua.match(/(?:iphone|ipad|ipod)[^)]*?os (\d{1,3}(?:[._]\d{1,3})*)/i);
|
||||
if (ios) return `iOS ${ios[1].replace(/_/g, ".")}`;
|
||||
if (/iphone|ipad|ipod/i.test(ua)) return "iOS";
|
||||
|
||||
if (/cros/i.test(ua)) return "ChromeOS";
|
||||
if (/macintosh|mac os x/i.test(ua)) return "macOS";
|
||||
if (/x11|linux/i.test(ua)) return "Linux";
|
||||
return null;
|
||||
}
|
||||
|
||||
// Converts a raw User-Agent into a short human-readable label such as
|
||||
// "Chrome 126 on Windows 10/11". The output is assembled exclusively from
|
||||
// fixed labels plus digit groups captured by strict regexes — no substring
|
||||
// of the raw input is ever included, so the result cannot leak fingerprint
|
||||
// detail (build numbers, device models, extensions) or carry injected
|
||||
// content. Returns "" for empty input so callers can apply their own
|
||||
// "not detected" wording.
|
||||
function describeUserAgent(value) {
|
||||
const ua = String(value || "").trim().slice(0, 512);
|
||||
if (!ua) return "";
|
||||
|
||||
for (const [re, label] of UA_AUTOMATED_PATTERNS) {
|
||||
const match = ua.match(re);
|
||||
if (match) {
|
||||
const version = match[1] ? ` ${match[1]}` : "";
|
||||
return `Automated client (${label}${version})`;
|
||||
}
|
||||
}
|
||||
|
||||
let browser = null;
|
||||
for (const { re, name } of UA_BROWSER_PATTERNS) {
|
||||
const match = ua.match(re);
|
||||
if (match) {
|
||||
browser = match[1] ? `${name} ${match[1]}` : name;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const os = describeOsFromUserAgent(ua);
|
||||
|
||||
if (!browser && !os) return "Unknown client";
|
||||
if (!browser) return `Unknown browser on ${os}`;
|
||||
if (!os) return `${browser} on unknown OS`;
|
||||
return `${browser} on ${os}`.slice(0, 80);
|
||||
}
|
||||
|
||||
module.exports = { maskIpAddress, describeUserAgent };
|
||||
Reference in new issue
Block a user