feat(client): implement per-client storage and security controls

- Added storage quota, IP allowlisting, and expiration logic to client management APIs.
- Updated CSV storage manager to support auto-compression and improved partition file parsing.
- Integrated security checks into event ingestion and data retrieval routes.
This commit is contained in:
Team Dark Devil committed 2026-11-19 23:02:27 +06:00
1 parent 89e4d68690
commit 2873b9904a
32 files changed
+6075 -241

No files matched your search

+219
View File
@@ -0,0 +1,219 @@
import net from "node:net";
/**
* Per-Client IP / CIDR Allowlist & Token Expiration Validator (V2-Phase-02)
*/
export function stripIpv6MappedPrefix(ip: string): string {
const trimmed = String(ip || "").trim();
if (trimmed.toLowerCase().startsWith("::ffff:")) {
const candidate = trimmed.slice(7);
if (net.isIPv4(candidate)) {
return candidate;
}
}
return trimmed;
}
export function ipv4ToUint32(ip: string): number | null {
const clean = stripIpv6MappedPrefix(ip);
if (!net.isIPv4(clean)) return null;
const parts = clean.split(".").map((octet) => Number(octet));
if (
parts.length !== 4 ||
parts.some((n) => !Number.isInteger(n) || n < 0 || n > 255)
) {
return null;
}
return (
(((parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8) | parts[3]) >>> 0)
);
}
export function parseAllowedIpsList(raw?: string | null): string[] {
if (!raw || typeof raw !== "string") return [];
return raw
.split(/[\s,;]+/)
.map((entry) => entry.trim())
.filter(Boolean);
}
export function validateAllowedIpsInput(
raw: unknown,
): { valid: true; normalized: string } | { valid: false; error: string } {
if (raw === undefined || raw === null || raw === "") {
return { valid: true, normalized: "" };
}
if (typeof raw !== "string") {
return { valid: false, error: "allowedIps must be a string." };
}
if (raw.length > 2048) {
return {
valid: false,
error: "allowedIps cannot exceed 2048 characters.",
};
}
const entries = parseAllowedIpsList(raw);
if (entries.length === 0) {
return { valid: true, normalized: "" };
}
if (entries.length > 64) {
return {
valid: false,
error: "allowedIps cannot contain more than 64 IP/CIDR entries.",
};
}
const normalizedEntries: string[] = [];
for (const entry of entries) {
if (entry === "*") {
normalizedEntries.push("*");
continue;
}
if (entry.includes("/")) {
const [ipPart, prefixPart, ...extra] = entry.split("/");
if (extra.length > 0 || !ipPart || !prefixPart || !/^\d+$/.test(prefixPart)) {
return {
valid: false,
error: `Invalid CIDR entry '${entry}'. Expected format like 203.0.113.0/24.`,
};
}
const prefix = Number(prefixPart);
const cleanIp = stripIpv6MappedPrefix(ipPart);
if (net.isIPv4(cleanIp)) {
if (prefix < 0 || prefix > 32) {
return {
valid: false,
error: `Invalid IPv4 CIDR prefix in '${entry}' (must be 0..32).`,
};
}
normalizedEntries.push(`${cleanIp}/${prefix}`);
continue;
}
if (net.isIPv6(cleanIp)) {
if (prefix < 0 || prefix > 128) {
return {
valid: false,
error: `Invalid IPv6 CIDR prefix in '${entry}' (must be 0..128).`,
};
}
normalizedEntries.push(`${cleanIp.toLowerCase()}/${prefix}`);
continue;
}
return {
valid: false,
error: `Invalid IP address in CIDR entry '${entry}'.`,
};
}
const cleanIp = stripIpv6MappedPrefix(entry);
const ipVersion = net.isIP(cleanIp);
if (ipVersion === 4) {
normalizedEntries.push(cleanIp);
} else if (ipVersion === 6) {
normalizedEntries.push(cleanIp.toLowerCase());
} else {
return {
valid: false,
error: `Invalid IP or CIDR entry '${entry}'.`,
};
}
}
return {
valid: true,
normalized: Array.from(new Set(normalizedEntries)).join(", "),
};
}
export function isClientIpAllowed(
clientIp: string,
allowedIpsRaw?: string | null,
): boolean {
const entries = parseAllowedIpsList(allowedIpsRaw);
if (entries.length === 0) {
return true;
}
const cleanClientIp = stripIpv6MappedPrefix(clientIp);
if (!cleanClientIp || cleanClientIp === "unknown") {
return entries.includes("*");
}
const clientIpv4Int = ipv4ToUint32(cleanClientIp);
const isLoopbackClient =
cleanClientIp === "127.0.0.1" || cleanClientIp.toLowerCase() === "::1";
for (const rule of entries) {
if (rule === "*") return true;
if (rule.includes("/")) {
const [ruleIpRaw, prefixRaw] = rule.split("/");
const ruleIp = stripIpv6MappedPrefix(ruleIpRaw);
const prefix = Number(prefixRaw);
const ruleIpv4Int = ipv4ToUint32(ruleIp);
if (clientIpv4Int !== null && ruleIpv4Int !== null && prefix >= 0 && prefix <= 32) {
if (prefix === 0) return true;
const mask = prefix === 32 ? 0xffffffff : (~((1 << (32 - prefix)) - 1)) >>> 0;
if ((clientIpv4Int & mask) >>> 0 === (ruleIpv4Int & mask) >>> 0) {
return true;
}
}
continue;
}
const cleanRule = stripIpv6MappedPrefix(rule);
if (cleanRule.toLowerCase() === cleanClientIp.toLowerCase()) {
return true;
}
if (
isLoopbackClient &&
(cleanRule === "127.0.0.1" || cleanRule.toLowerCase() === "::1")
) {
return true;
}
}
return false;
}
export function validateClientExpiresAtInput(
raw: unknown,
): { valid: true; iso: string | null } | { valid: false; error: string } {
if (raw === undefined || raw === null || raw === "") {
return { valid: true, iso: null };
}
if (typeof raw !== "string") {
return {
valid: false,
error: "expiresAt must be an ISO timestamp string or null.",
};
}
const trimmed = raw.trim();
if (!trimmed) {
return { valid: true, iso: null };
}
const parsedMs = Date.parse(trimmed);
if (!Number.isFinite(parsedMs)) {
return {
valid: false,
error: "expiresAt must be a valid ISO-8601 date or timestamp.",
};
}
return { valid: true, iso: new Date(parsedMs).toISOString() };
}
export function isClientTokenExpired(
expiresAt?: string | null,
nowMs = Date.now(),
): boolean {
if (!expiresAt || typeof expiresAt !== "string") return false;
const trimmed = expiresAt.trim();
if (!trimmed) return false;
const parsedMs = Date.parse(trimmed);
if (!Number.isFinite(parsedMs)) return false;
return nowMs >= parsedMs;
}
+152
View File
@@ -0,0 +1,152 @@
import {
createCipheriv,
createDecipheriv,
hkdfSync,
randomBytes,
} from "node:crypto";
const DEV_DEFAULT_SESSION_SECRET =
"datastack-admin-session-default-secret-32-chars";
const VAULT_PREFIX = "v1";
const HKDF_SALT = Buffer.from("datastack-vault-salt-v1", "utf8");
const HKDF_INFO = Buffer.from("datastack-aes-256-gcm-v1", "utf8");
export type SecretVaultErrorCode =
| "SECRET_VAULT_KEY_UNAVAILABLE"
| "SECRET_VAULT_DECRYPT_FAILED"
| "SECRET_VAULT_INVALID_INPUT";
export class SecretVaultError extends Error {
public readonly code: SecretVaultErrorCode;
constructor(code: SecretVaultErrorCode, message: string) {
super(`${code}: ${message}`);
this.name = "SecretVaultError";
this.code = code;
}
}
export function resolveVaultMasterSecret(secretOverride?: string): string | null {
const candidate = (
secretOverride ??
process.env.DATASTACK_ADMIN_SESSION_SECRET ??
""
).trim();
if (process.env.NODE_ENV === "production") {
return candidate.length >= 32 ? candidate : null;
}
if (candidate.length > 0) {
return candidate.length >= 32 ? candidate : null;
}
return DEV_DEFAULT_SESSION_SECRET;
}
function deriveAesKey(secretOverride?: string): Buffer {
const master = resolveVaultMasterSecret(secretOverride);
if (!master) {
throw new SecretVaultError(
"SECRET_VAULT_KEY_UNAVAILABLE",
"DATASTACK_ADMIN_SESSION_SECRET (>= 32 characters) is required to encrypt or decrypt stored secrets.",
);
}
const derived = hkdfSync(
"sha256",
Buffer.from(master, "utf8"),
HKDF_SALT,
HKDF_INFO,
32,
);
return Buffer.from(derived);
}
export function encryptSecret(
plaintext: string,
secretOverride?: string,
): string {
if (typeof plaintext !== "string") {
throw new SecretVaultError(
"SECRET_VAULT_INVALID_INPUT",
"Secret value must be a string.",
);
}
if (!plaintext) {
return "";
}
const key = deriveAesKey(secretOverride);
const iv = randomBytes(12);
const cipher = createCipheriv("aes-256-gcm", key, iv);
const encrypted = Buffer.concat([
cipher.update(plaintext, "utf8"),
cipher.final(),
]);
const authTag = cipher.getAuthTag();
return [
VAULT_PREFIX,
iv.toString("base64url"),
authTag.toString("base64url"),
encrypted.toString("base64url"),
].join(":");
}
export function decryptSecret(
ciphertext: string,
secretOverride?: string,
): string {
if (!ciphertext || typeof ciphertext !== "string") {
return "";
}
const parts = ciphertext.trim().split(":");
if (parts.length !== 4 || parts[0] !== VAULT_PREFIX) {
throw new SecretVaultError(
"SECRET_VAULT_DECRYPT_FAILED",
"Stored secret ciphertext has an unrecognized format.",
);
}
const key = deriveAesKey(secretOverride);
try {
const iv = Buffer.from(parts[1], "base64url");
const authTag = Buffer.from(parts[2], "base64url");
const payload = Buffer.from(parts[3], "base64url");
if (iv.length !== 12 || authTag.length !== 16) {
throw new Error("Invalid IV or auth tag length");
}
const decipher = createDecipheriv("aes-256-gcm", key, iv);
decipher.setAuthTag(authTag);
const decrypted = Buffer.concat([
decipher.update(payload),
decipher.final(),
]);
return decrypted.toString("utf8");
} catch (err: unknown) {
if (err instanceof SecretVaultError) {
throw err;
}
throw new SecretVaultError(
"SECRET_VAULT_DECRYPT_FAILED",
"Unable to decrypt stored secret. Verify DATASTACK_ADMIN_SESSION_SECRET matches the key used to encrypt it.",
);
}
}
export function maskSecretHint(plaintext: string): string {
const trimmed = String(plaintext || "").trim();
if (!trimmed) return "";
if (trimmed.length >= 8) {
return `••••${trimmed.slice(-4)}`;
}
return "••••••••";
}
export function getMaskedHintFromEncrypted(
ciphertext: string,
secretOverride?: string,
): string {
if (!ciphertext || !ciphertext.trim()) return "";
try {
const plain = decryptSecret(ciphertext, secretOverride);
return maskSecretHint(plain);
} catch {
return "••••(locked)";
}
}