release(v2.7.3): tenant SQL scoping, spoof-proof rate limits, safe fraud demo, non-root Docker & full parity

This commit is contained in:
Amirul Islam Redwan71
2026-11-27 04:47:06 +06:00
parent c827fdfee6
commit a3392ae69f
65 changed files with 911 additions and 1228 deletions
+83 -12
View File
@@ -44,19 +44,34 @@ if (process.env.NODE_ENV !== "test" && dbUrl) {
}
}
// Automated Data Retention Engine (Daily Midnight Sweep)
/**
* NEW-01 Fix: Automated Data Retention Engine (Daily Sweep) aligned to live PostgreSQL `courier_parcels` schema.
*/
export async function runDataRetentionSweep(pool: any): Promise<number> {
if (!pool) return 0;
const pruneRes = await pool.query(
`UPDATE courier_parcels
SET customer_name = 'ANONYMIZED',
phone = 'ANONYMIZED',
address = 'ANONYMIZED'
WHERE created_at < NOW() - INTERVAL '90 days'
AND (customer_name IS DISTINCT FROM 'ANONYMIZED'
OR phone IS DISTINCT FROM 'ANONYMIZED'
OR address IS DISTINCT FROM 'ANONYMIZED')`
);
return pruneRes?.rowCount || 0;
}
setInterval(async () => {
if (dbPool) {
try {
const pruneRes = await dbPool.query(
`UPDATE courier_parcels
SET recipient_name = 'ANONYMIZED', recipient_phone = 'ANONYMIZED', recipient_address = 'ANONYMIZED'
WHERE created_at < NOW() - INTERVAL '90 days' AND recipient_name != 'ANONYMIZED'`
);
if (pruneRes.rowCount && pruneRes.rowCount > 0) {
console.log(`[Data Retention Engine] Anonymized ${pruneRes.rowCount} legacy order records older than 90 days.`);
const pruned = await runDataRetentionSweep(dbPool);
if (pruned > 0) {
console.log(`[Data Retention Engine] Anonymized ${pruned} legacy order records older than 90 days.`);
}
} catch (_) {}
} catch (err: any) {
console.warn("[Data Retention Engine] Sweep error:", err?.message);
}
}
}, 24 * 60 * 60 * 1000).unref();
@@ -216,6 +231,8 @@ app.use((req, res, next) => {
"Content-Type, Accept, Authorization, mcp-session-id, mcp-protocol-version, x-aura-signature, x-steadfast-signature, x-pathao-signature, x-redx-signature, x-paperfly-signature, x-webhook-secret, x-steadfast-api-key, x-steadfast-secret-key, x-pathao-client-id, x-pathao-client-secret, x-pathao-username, x-pathao-password, x-pathao-store-id, x-redx-api-token, x-redx-pickup-store-id, x-paperfly-api-key, x-paperfly-username, x-paperfly-password, x-paperfly-store-name, x-google-maps-key"
);
res.header("Access-Control-Expose-Headers", "mcp-session-id, WWW-Authenticate");
res.header("Referrer-Policy", "no-referrer");
res.header("X-Content-Type-Options", "nosniff");
if (req.method === "OPTIONS") {
return res.sendStatus(204);
}
@@ -312,8 +329,9 @@ app.post("/admin/tokens/revoke", async (req: Request, res: Response) => {
return res.json({ ok: true, tenantId: b.tenantId, updatedKeys: count, status: b.status || "PAYMENT_DUE" });
});
const mcpLimiter = createRateLimiter({ windowMs: 60 * 1000, max: 60, name: "MCP Protocol Gateway" });
const mcpLimiter = createRateLimiter({ windowMs: 60 * 1000, max: 120, name: "MCP Protocol Gateway" });
const trackingLimiter = createRateLimiter({ windowMs: 60 * 1000, max: 120, name: "Tracking Gateway" });
const publicDemoLimiter = createRateLimiter({ windowMs: 60 * 1000, max: 10, name: "Public Fraud Demo" });
/**
* F05 Fix: MCP Session Entry with Cryptographic Tenant Isolation
@@ -500,9 +518,22 @@ app.get("/mcp", mcpLimiter, async (req: Request, res: Response) => {
return;
}
if (!req.auraAuth?.authenticated) {
res.setHeader("WWW-Authenticate", buildWwwAuthenticateHeader(req, req.auraAuth?.tenantId === "invalid"));
return res.status(401).json(
wrapError(
new McpToolError({
code: "UNAUTHORIZED",
message: "Missing or invalid Authorization: Bearer <AURA_API_KEY> header for active MCP session.",
}),
"validator"
)
);
}
const entry = sessions.get(sessionId)!;
const callerTenant = req.auraAuth?.authenticated ? req.auraAuth.tenantId : entry.tenantId;
if (entry.tenantId !== callerTenant && callerTenant !== "master" && entry.tenantId !== "default" && entry.tenantId !== "anonymous") {
const callerTenant = req.auraAuth.tenantId;
if (entry.tenantId !== callerTenant && callerTenant !== "master") {
return res.status(403).json(
wrapError(
new McpToolError({
@@ -956,6 +987,46 @@ app.get("/track/:code", trackingLimiter, async (req: Request, res: Response) =>
</html>`);
});
/**
* NEW-03 Fix: Safe Server-Side Public Fraud Demo Endpoint (Zero Browser API Key Exposure).
* Strictly limited to 10 requests/minute/IP and scoped exclusively to check_fraud_risk on valid BD mobile numbers.
*/
app.post("/api/public/fraud-demo", publicDemoLimiter, async (req: Request, res: Response) => {
const rawPhone = String(req.body?.phone || "").trim().replace(/[\s\-()]/g, "").replace(/^\+?88/, "");
if (!/^01[3-9]\d{8}$/.test(rawPhone)) {
return res.status(400).json(
wrapError(
new McpToolError({
code: "INVALID_INPUT",
message: "Valid 11-digit Bangladesh mobile number (013-019) is required.",
retryable: false,
}),
"validator"
)
);
}
try {
const registry = new CourierRegistry(loadConfig(process.env as EnvSource));
const result = await registry.checkFraudRisk(rawPhone);
const safeDto = {
phone_masked: maskPhone(rawPhone),
risk_level: result.risk_level,
delivery_success_rate: result.delivery_success_rate,
cancellation_rate: result.cancellation_rate,
is_verified_buyer: result.is_verified_buyer,
total_parcels: result.total_parcels,
total_delivered: result.total_delivered,
total_cancelled: result.total_cancelled,
sample_size: result.sample_size,
data_sources: result.data_sources,
recommendation: result.recommendation,
};
return res.json(wrapSuccess(safeDto, "live_api"));
} catch (err: any) {
return res.status(502).json(wrapError(err, "live_api"));
}
});
/**
* Task T9 — Health & Live Telemetry Endpoint
*/