963 lines
48 KiB
JavaScript
963 lines
48 KiB
JavaScript
import express from "express";
|
|
import { createHash, randomUUID } from "node:crypto";
|
|
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
|
import { SSEServerTransport } from "@modelcontextprotocol/sdk/server/sse.js";
|
|
import { isInitializeRequest } from "@modelcontextprotocol/sdk/types.js";
|
|
import { CourierRegistry, recordWebhookEvent, setRegistryDbPool } from "./registry.js";
|
|
import { loadConfig, validateCarrierBaseUrl } from "./config.js";
|
|
import { buildMcpServer, executeToolCall, TOOLS } from "./server.js";
|
|
import { createRateLimiter } from "./middleware/rate_limit.js";
|
|
import { optionalAuthMiddleware, requireMcpAuth, isActionPermitted, resolveAuthContext, authenticateWebhook, validateSecurityConfig, recordAuditLog, initDynamicAuthStore, upsertDynamicKeyHash, setTenantDynamicKeyStatus, listDynamicKeySummaries, } from "./middleware/auth.js";
|
|
import { registerOAuthRoutes, buildWwwAuthenticateHeader } from "./middleware/oauth.js";
|
|
import { McpToolError, wrapError, wrapSuccess, maskPhone } from "./envelope.js";
|
|
import { APP_VERSION } from "./version.js";
|
|
import path from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
import pg from "pg";
|
|
/**
|
|
* F02 Fix: Remove hardcoded plaintext password.
|
|
* Connection string is read strictly from process.env.DATABASE_URL.
|
|
*/
|
|
const dbUrl = process.env.DATABASE_URL;
|
|
let dbPool = null;
|
|
if (process.env.NODE_ENV !== "test" && dbUrl) {
|
|
try {
|
|
dbPool = new pg.Pool({ connectionString: dbUrl, max: 4, idleTimeoutMillis: 30000 });
|
|
setRegistryDbPool(dbPool);
|
|
initDynamicAuthStore(dbPool).catch(() => { });
|
|
}
|
|
catch (err) {
|
|
console.warn("[PostgreSQL] Connection initialization skipped:", err?.message);
|
|
}
|
|
}
|
|
/**
|
|
* NEW-01 Fix: Automated Data Retention Engine (Daily Sweep) aligned to live PostgreSQL `courier_parcels` schema.
|
|
*/
|
|
export async function runDataRetentionSweep(pool) {
|
|
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 pruned = await runDataRetentionSweep(dbPool);
|
|
if (pruned > 0) {
|
|
console.log(`[Data Retention Engine] Anonymized ${pruned} legacy order records older than 90 days.`);
|
|
}
|
|
}
|
|
catch (err) {
|
|
console.warn("[Data Retention Engine] Sweep error:", err?.message);
|
|
}
|
|
}
|
|
}, 24 * 60 * 60 * 1000).unref();
|
|
const PORT = Number(process.env.PORT || 8080);
|
|
const APP_DIR = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
|
|
const LANDING = path.join(APP_DIR, "index.html");
|
|
const PRIVACY_HTML = path.join(APP_DIR, "privacy.html");
|
|
const TERMS_HTML = path.join(APP_DIR, "terms.html");
|
|
const HEALTH_HTML = path.join(APP_DIR, "health.html");
|
|
const MCP_EXPLORER_HTML = path.join(APP_DIR, "mcp_explorer.html");
|
|
const GUIDE_HTML = path.join(APP_DIR, "guide.html");
|
|
const SIMULATOR_HTML = path.join(APP_DIR, "simulator.html");
|
|
const ASSETS_DIR = path.join(APP_DIR, "assets");
|
|
const CONFIGS_DIR = path.join(APP_DIR, "configs");
|
|
/**
|
|
* F04 Helper: HTML entity escaping to prevent stored/reflected XSS.
|
|
*/
|
|
function escapeHtml(str) {
|
|
if (str == null)
|
|
return "";
|
|
return String(str)
|
|
.replace(/&/g, "&")
|
|
.replace(/</g, "<")
|
|
.replace(/>/g, ">")
|
|
.replace(/"/g, """)
|
|
.replace(/'/g, "'");
|
|
}
|
|
/**
|
|
* F03 Helper: Redact recipient street address to District/City level for public tracking.
|
|
*/
|
|
function redactAddressToDistrict(addr) {
|
|
if (!addr)
|
|
return "Bangladesh";
|
|
const parts = addr.split(",").map((p) => p.trim()).filter(Boolean);
|
|
if (parts.length > 1) {
|
|
return parts.slice(-2).join(", ");
|
|
}
|
|
return addr;
|
|
}
|
|
/**
|
|
* P0-03 & P1-01 Fix: Pull authenticated tenant BYOK keys strictly from request headers.
|
|
* Arbitrary caller base-URL headers are validated against the official HTTPS carrier allowlist
|
|
* (rejecting any http:, IP literal, private/internal host, or unlisted domain).
|
|
*/
|
|
export function keysFromRequest(req) {
|
|
const h = req.headers;
|
|
const isByokRequest = Boolean(req.auraAuth?.isByok ||
|
|
h["x-steadfast-api-key"] ||
|
|
h["x-pathao-client-id"] ||
|
|
h["x-redx-api-token"] ||
|
|
h["x-paperfly-api-key"]);
|
|
const isPublicFraudCheck = req.auraAuth?.tenantId === "public_ro" &&
|
|
req.body?.method === "tools/call" &&
|
|
req.body?.params?.name === "check_fraud_risk";
|
|
const allowMasterEnvFallback = (req.auraAuth?.tenantId === "master" || isPublicFraudCheck) && !isByokRequest;
|
|
const pick = (header, envVar, allowNonMasterEnv = false) => {
|
|
const hv = h[header];
|
|
const v = Array.isArray(hv) ? hv[0] : hv;
|
|
if (v && typeof v === "string" && v.trim())
|
|
return v.trim();
|
|
if (envVar && (allowMasterEnvFallback || allowNonMasterEnv)) {
|
|
return process.env[envVar];
|
|
}
|
|
return undefined;
|
|
};
|
|
// Validate any caller-supplied base URL headers immediately against the strict SSRF allowlist
|
|
const pickBaseUrl = (provider, header, envVar, defaultUrl) => {
|
|
const hv = h[header];
|
|
const headerVal = (Array.isArray(hv) ? hv[0] : hv) || "";
|
|
if (typeof headerVal === "string" && headerVal.trim()) {
|
|
return validateCarrierBaseUrl(provider, headerVal.trim(), defaultUrl);
|
|
}
|
|
return validateCarrierBaseUrl(provider, process.env[envVar], defaultUrl);
|
|
};
|
|
return {
|
|
STEADFAST_API_KEY: pick("x-steadfast-api-key", "STEADFAST_API_KEY"),
|
|
STEADFAST_SECRET_KEY: pick("x-steadfast-secret-key", "STEADFAST_SECRET_KEY"),
|
|
STEADFAST_EMAIL: pick("x-steadfast-email", "STEADFAST_EMAIL"),
|
|
STEADFAST_PASSWORD: pick("x-steadfast-password", "STEADFAST_PASSWORD"),
|
|
STEADFAST_BASE_URL: pickBaseUrl("steadfast", "x-steadfast-base-url", "STEADFAST_BASE_URL", "https://portal.packzy.com/api/v1"),
|
|
PATHAO_CLIENT_ID: pick("x-pathao-client-id", "PATHAO_CLIENT_ID"),
|
|
PATHAO_CLIENT_SECRET: pick("x-pathao-client-secret", "PATHAO_CLIENT_SECRET"),
|
|
PATHAO_USERNAME: pick("x-pathao-username", "PATHAO_USERNAME"),
|
|
PATHAO_PASSWORD: pick("x-pathao-password", "PATHAO_PASSWORD"),
|
|
PATHAO_STORE_ID: pick("x-pathao-store-id", "PATHAO_STORE_ID"),
|
|
PATHAO_BASE_URL: pickBaseUrl("pathao", "x-pathao-base-url", "PATHAO_BASE_URL", "https://api-hermes.pathao.com"),
|
|
REDX_API_TOKEN: pick("x-redx-api-token", "REDX_API_TOKEN"),
|
|
REDX_PHONE: pick("x-redx-phone", "REDX_PHONE"),
|
|
REDX_PASSWORD: pick("x-redx-password", "REDX_PASSWORD"),
|
|
REDX_BASE_URL: pickBaseUrl("redx", "x-redx-base-url", "REDX_BASE_URL", "https://openapi.redx.com.bd/v1.0.0-beta"),
|
|
REDX_PICKUP_STORE_ID: pick("x-redx-pickup-store-id", "REDX_PICKUP_STORE_ID"),
|
|
PAPERFLY_API_KEY: pick("x-paperfly-api-key", "PAPERFLY_API_KEY"),
|
|
PAPERFLY_USERNAME: pick("x-paperfly-username", "PAPERFLY_USERNAME"),
|
|
PAPERFLY_PASSWORD: pick("x-paperfly-password", "PAPERFLY_PASSWORD"),
|
|
PAPERFLY_STORE_NAME: pick("x-paperfly-store-name", "PAPERFLY_STORE_NAME"),
|
|
PAPERFLY_BASE_URL: pickBaseUrl("paperfly", "x-paperfly-base-url", "PAPERFLY_BASE_URL", "https://api.paperfly.com.bd"),
|
|
GOOGLE_MAPS_API_KEY: pick("x-google-maps-key", "GOOGLE_MAPS_API_KEY", true),
|
|
};
|
|
}
|
|
export const app = express();
|
|
app.set("trust proxy", 1);
|
|
app.use(express.json({
|
|
limit: "1mb",
|
|
verify: (req, _res, buf) => {
|
|
req.rawBody = buf;
|
|
},
|
|
}));
|
|
app.use(express.urlencoded({ extended: false, limit: "1mb" }));
|
|
// Robust Malformed JSON Syntax Error Guard
|
|
app.use((err, _req, res, next) => {
|
|
if (err && (err.type === "entity.parse.failed" || err.status === 400)) {
|
|
return res.status(400).json(wrapError(new McpToolError({
|
|
code: "INVALID_INPUT",
|
|
message: "Malformed JSON payload in request body.",
|
|
retryable: false,
|
|
}), "validator"));
|
|
}
|
|
next(err);
|
|
});
|
|
// Controlled CORS policy: respects CORS_ORIGIN allowlist when configured; excludes arbitrary carrier base-URL headers.
|
|
app.use((req, res, next) => {
|
|
const configuredOrigins = (process.env.CORS_ORIGIN || "")
|
|
.split(",")
|
|
.map((s) => s.trim())
|
|
.filter(Boolean);
|
|
const reqOrigin = req.headers.origin;
|
|
if (configuredOrigins.length > 0) {
|
|
if (reqOrigin && configuredOrigins.includes(reqOrigin)) {
|
|
res.header("Access-Control-Allow-Origin", reqOrigin);
|
|
res.header("Vary", "Origin");
|
|
}
|
|
}
|
|
else {
|
|
res.header("Access-Control-Allow-Origin", "*");
|
|
}
|
|
res.header("Access-Control-Allow-Methods", "GET, POST, DELETE, OPTIONS, HEAD, PATCH, PUT");
|
|
res.header("Access-Control-Allow-Headers", "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);
|
|
}
|
|
next();
|
|
});
|
|
// P1-01 Early Anti-SSRF Header Guard: reject any disallowed carrier base-URL header before routing
|
|
app.use((req, res, next) => {
|
|
const checks = [
|
|
["steadfast", "x-steadfast-base-url", "https://portal.packzy.com/api/v1"],
|
|
["pathao", "x-pathao-base-url", "https://api-hermes.pathao.com"],
|
|
["redx", "x-redx-base-url", "https://openapi.redx.com.bd/v1.0.0-beta"],
|
|
["paperfly", "x-paperfly-base-url", "https://api.paperfly.com.bd"],
|
|
];
|
|
for (const [prov, hdr, def] of checks) {
|
|
const hv = req.headers[hdr];
|
|
const val = Array.isArray(hv) ? hv[0] : hv;
|
|
if (typeof val === "string" && val.trim()) {
|
|
try {
|
|
validateCarrierBaseUrl(prov, val.trim(), def);
|
|
}
|
|
catch (err) {
|
|
return res.status(400).json(wrapError(new McpToolError({
|
|
code: "INVALID_INPUT",
|
|
message: err?.message || `Disallowed ${prov} base URL header.`,
|
|
retryable: false,
|
|
}), "validator"));
|
|
}
|
|
}
|
|
}
|
|
next();
|
|
});
|
|
// Static assets (official logo, favicons, branding) & agent MCP configs
|
|
app.use("/assets", express.static(ASSETS_DIR, { maxAge: "7d" }));
|
|
app.use("/configs", express.static(CONFIGS_DIR, { maxAge: "1d" }));
|
|
app.get("/favicon.ico", (_req, res) => {
|
|
res.sendFile(path.join(ASSETS_DIR, "courier-logo.jpg"), (err) => {
|
|
if (err && !res.headersSent)
|
|
res.status(204).end();
|
|
});
|
|
});
|
|
// Standard MCP OAuth 2.1 + PKCE (S256) + RFC 9728 + RFC 8414 + RFC 7591 Dynamic Client Registration
|
|
registerOAuthRoutes(app);
|
|
app.use(optionalAuthMiddleware);
|
|
// Dynamic Tenant API Key Sync & Revocation Routes (Called by Snehalata / Admin Master Key)
|
|
app.get("/admin/tokens", (req, res) => {
|
|
if (!req.auraAuth?.authenticated || !req.auraAuth?.isAdmin) {
|
|
return res.status(401).json(wrapError(new McpToolError({ code: "UNAUTHORIZED", message: "Master admin token required." }), "validator"));
|
|
}
|
|
return res.json({ ok: true, tokens: listDynamicKeySummaries() });
|
|
});
|
|
app.post("/admin/tokens/register", async (req, res) => {
|
|
if (!req.auraAuth?.authenticated || !req.auraAuth?.isAdmin) {
|
|
return res.status(401).json(wrapError(new McpToolError({ code: "UNAUTHORIZED", message: "Master admin token required." }), "validator"));
|
|
}
|
|
const b = req.body || {};
|
|
if ((!b.tokenHash && !b.rawToken) || !b.tenantId) {
|
|
return res.status(400).json(wrapError(new McpToolError({ code: "INVALID_INPUT", message: "tenantId and tokenHash (or rawToken) required." }), "validator"));
|
|
}
|
|
try {
|
|
const rec = await upsertDynamicKeyHash({
|
|
tokenHash: b.tokenHash,
|
|
rawToken: b.rawToken,
|
|
tenantId: String(b.tenantId),
|
|
storeName: b.storeName ? String(b.storeName) : undefined,
|
|
scopes: Array.isArray(b.scopes) ? b.scopes : ["read", "write"],
|
|
isAdmin: Boolean(b.isAdmin),
|
|
status: String(b.status || "ACTIVE"),
|
|
});
|
|
return res.json({ ok: true, tenantId: rec.tenantId, status: rec.status, hashPrefix: rec.tokenHash.slice(0, 10) });
|
|
}
|
|
catch (err) {
|
|
return res.status(500).json(wrapError(err, "validator"));
|
|
}
|
|
});
|
|
app.post("/admin/tokens/revoke", async (req, res) => {
|
|
if (!req.auraAuth?.authenticated || !req.auraAuth?.isAdmin) {
|
|
return res.status(401).json(wrapError(new McpToolError({ code: "UNAUTHORIZED", message: "Master admin token required." }), "validator"));
|
|
}
|
|
const b = req.body || {};
|
|
if (!b.tenantId) {
|
|
return res.status(400).json(wrapError(new McpToolError({ code: "INVALID_INPUT", message: "tenantId required." }), "validator"));
|
|
}
|
|
const count = await setTenantDynamicKeyStatus(String(b.tenantId), String(b.status || "PAYMENT_DUE"));
|
|
return res.json({ ok: true, tenantId: b.tenantId, updatedKeys: count, status: b.status || "PAYMENT_DUE" });
|
|
});
|
|
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" });
|
|
const sessions = new Map();
|
|
const sseSessions = new Map();
|
|
const SESSION_TTL_MS = 30 * 60 * 1000;
|
|
setInterval(() => {
|
|
const now = Date.now();
|
|
for (const [sid, entry] of sessions.entries()) {
|
|
if (now - entry.lastActiveAt > SESSION_TTL_MS) {
|
|
sessions.delete(sid);
|
|
}
|
|
}
|
|
for (const [sid, entry] of sseSessions.entries()) {
|
|
if (now - entry.lastActiveAt > SESSION_TTL_MS) {
|
|
sseSessions.delete(sid);
|
|
}
|
|
}
|
|
}, 60 * 1000).unref();
|
|
/**
|
|
* AI Agent Connector Accept Normalizer:
|
|
* Ensures clients sending Accept: application/json or wildcard do not get HTTP 406 from StreamableHTTPServerTransport.
|
|
*/
|
|
app.use("/mcp", (req, _res, next) => {
|
|
const accept = req.headers["accept"] || "";
|
|
if (!accept.includes("text/event-stream")) {
|
|
req.headers["accept"] = accept ? `${accept}, text/event-stream` : "application/json, text/event-stream";
|
|
}
|
|
next();
|
|
});
|
|
/**
|
|
* POST /mcp - MCP Protocol Gateway
|
|
*/
|
|
app.post("/mcp", mcpLimiter, requireMcpAuth, async (req, res) => {
|
|
const sessionId = req.headers["mcp-session-id"];
|
|
const tenantId = req.auraAuth?.tenantId || "default";
|
|
let transport;
|
|
// F05: Verify MCP Session belongs strictly to caller's tenant
|
|
if (sessionId && sessions.has(sessionId)) {
|
|
const entry = sessions.get(sessionId);
|
|
if (entry.tenantId !== tenantId && req.auraAuth?.tenantId !== "master") {
|
|
res.status(403).json(wrapError(new McpToolError({
|
|
code: "UNAUTHORIZED",
|
|
message: "MCP Session belongs to another tenant.",
|
|
retryable: false,
|
|
source: "validator",
|
|
}), "validator"));
|
|
return;
|
|
}
|
|
entry.lastActiveAt = Date.now();
|
|
transport = entry.transport;
|
|
}
|
|
else if (!sessionId && isInitializeRequest(req.body)) {
|
|
const registry = new CourierRegistry(loadConfig(keysFromRequest(req)));
|
|
const server = buildMcpServer(registry, (toolName) => isActionPermitted(req, toolName), tenantId);
|
|
transport = new StreamableHTTPServerTransport({
|
|
sessionIdGenerator: () => randomUUID(),
|
|
enableJsonResponse: true,
|
|
onsessioninitialized: (sid) => {
|
|
sessions.set(sid, { transport, lastActiveAt: Date.now(), tenantId });
|
|
},
|
|
});
|
|
transport.onclose = () => {
|
|
if (transport.sessionId)
|
|
sessions.delete(transport.sessionId);
|
|
};
|
|
await server.connect(transport);
|
|
}
|
|
else if (req.body && (req.body.method === "tools/list" || req.body.method === "tools/call")) {
|
|
// Stateless JSON-RPC 2.0 Direct Execution Mode
|
|
const rpcId = req.body.id ?? 1;
|
|
const registry = new CourierRegistry(loadConfig(keysFromRequest(req)));
|
|
// F14: Allow unauthenticated tools/list discovery
|
|
if (req.body.method === "tools/list") {
|
|
res.status(200).json({ jsonrpc: "2.0", id: rpcId, result: { tools: TOOLS } });
|
|
return;
|
|
}
|
|
const toolName = String(req.body.params?.name || "");
|
|
const args = (req.body.params?.arguments || {});
|
|
const auth = isActionPermitted(req, toolName, args);
|
|
if (!auth.permitted) {
|
|
const errEnvelope = wrapError(new McpToolError({
|
|
code: "UNAUTHORIZED",
|
|
message: auth.reason || "Unauthorized tool invocation.",
|
|
retryable: false,
|
|
}), "validator");
|
|
res.status(200).json({
|
|
jsonrpc: "2.0",
|
|
id: rpcId,
|
|
result: {
|
|
isError: true,
|
|
content: [{ type: "text", text: JSON.stringify(errEnvelope, null, 2) }],
|
|
},
|
|
});
|
|
return;
|
|
}
|
|
const { isError, envelope } = await executeToolCall(registry, toolName, args, tenantId);
|
|
recordAuditLog(tenantId, toolName, args, isError ? "error" : "ok");
|
|
res.status(200).json({
|
|
jsonrpc: "2.0",
|
|
id: rpcId,
|
|
result: {
|
|
isError,
|
|
content: [{ type: "text", text: JSON.stringify(envelope, null, 2) }],
|
|
},
|
|
});
|
|
return;
|
|
}
|
|
else {
|
|
res.status(400).json({
|
|
jsonrpc: "2.0",
|
|
error: { code: -32000, message: "Bad Request: missing or invalid session. Send an initialize request first." },
|
|
id: null,
|
|
});
|
|
return;
|
|
}
|
|
try {
|
|
await transport.handleRequest(req, res, req.body);
|
|
}
|
|
catch (err) {
|
|
if (!res.headersSent) {
|
|
res.status(500).json({
|
|
jsonrpc: "2.0",
|
|
error: { code: -32603, message: `Internal error: ${err?.message || String(err)}` },
|
|
id: null,
|
|
});
|
|
}
|
|
}
|
|
});
|
|
// GET (server->client SSE stream or Browser Explorer)
|
|
app.get("/mcp", mcpLimiter, async (req, res) => {
|
|
const sessionId = req.headers["mcp-session-id"];
|
|
if (!sessionId || !sessions.has(sessionId)) {
|
|
if (req.headers.accept?.includes("text/html")) {
|
|
return res.sendFile(MCP_EXPLORER_HTML, (err) => {
|
|
if (err && !res.headersSent) {
|
|
res.status(400).send("Invalid or missing session ID. Visit https://courier.auraajenticai.cloud for instructions.");
|
|
}
|
|
});
|
|
}
|
|
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.",
|
|
}), "validator"));
|
|
}
|
|
res.status(400).send("Invalid or missing session ID");
|
|
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.tenantId;
|
|
if (entry.tenantId !== callerTenant && callerTenant !== "master") {
|
|
return res.status(403).json(wrapError(new McpToolError({
|
|
code: "UNAUTHORIZED",
|
|
message: "MCP Session belongs to another tenant.",
|
|
retryable: false,
|
|
}), "validator"));
|
|
}
|
|
entry.lastActiveAt = Date.now();
|
|
await entry.transport.handleRequest(req, res);
|
|
});
|
|
// DELETE (end session) for an existing session.
|
|
app.delete("/mcp", requireMcpAuth, async (req, res) => {
|
|
const sessionId = req.headers["mcp-session-id"];
|
|
if (!sessionId || !sessions.has(sessionId)) {
|
|
res.status(400).send("Invalid or missing session ID");
|
|
return;
|
|
}
|
|
const entry = sessions.get(sessionId);
|
|
const callerTenant = req.auraAuth?.tenantId || "default";
|
|
if (entry.tenantId !== callerTenant && callerTenant !== "master") {
|
|
res.status(403).json(wrapError(new McpToolError({
|
|
code: "UNAUTHORIZED",
|
|
message: "MCP Session belongs to another tenant.",
|
|
retryable: false,
|
|
}), "validator"));
|
|
return;
|
|
}
|
|
entry.lastActiveAt = Date.now();
|
|
await entry.transport.handleRequest(req, res);
|
|
});
|
|
/**
|
|
* GET /sse - Authenticated MCP Server-Sent Events Endpoint for AI Agent Connectors
|
|
*/
|
|
app.get("/sse", mcpLimiter, requireMcpAuth, async (req, res) => {
|
|
const tenantId = req.auraAuth?.tenantId || "default";
|
|
const token = req.query.token || req.query.key || req.query.apiKey || req.query.api_key;
|
|
const endpointUrl = token
|
|
? `/messages?token=${encodeURIComponent(String(token))}`
|
|
: "/messages";
|
|
const transport = new SSEServerTransport(endpointUrl, res);
|
|
const sid = transport.sessionId;
|
|
sseSessions.set(sid, { transport, tenantId, lastActiveAt: Date.now() });
|
|
transport.onclose = () => {
|
|
sseSessions.delete(sid);
|
|
};
|
|
const registry = new CourierRegistry(loadConfig(keysFromRequest(req)));
|
|
const server = buildMcpServer(registry, (toolName) => isActionPermitted(req, toolName), tenantId);
|
|
await server.connect(transport);
|
|
});
|
|
/**
|
|
* POST /messages - Authenticated MCP JSON-RPC Message Ingestion Endpoint for SSE Transports
|
|
*/
|
|
app.post("/messages", mcpLimiter, requireMcpAuth, async (req, res) => {
|
|
const sessionId = (req.query.sessionId || req.headers["mcp-session-id"]);
|
|
if (!sessionId || !sseSessions.has(sessionId)) {
|
|
return res.status(400).send("Invalid or missing sessionId");
|
|
}
|
|
const entry = sseSessions.get(sessionId);
|
|
const callerTenant = req.auraAuth?.tenantId || "anonymous";
|
|
if (entry.tenantId !== callerTenant && callerTenant !== "master") {
|
|
return res.status(403).json(wrapError(new McpToolError({
|
|
code: "UNAUTHORIZED",
|
|
message: "SSE Session belongs to another tenant.",
|
|
retryable: false,
|
|
}), "validator"));
|
|
}
|
|
entry.lastActiveAt = Date.now();
|
|
await entry.transport.handlePostMessage(req, res, req.body);
|
|
});
|
|
// P0-02 Replay Protection Cache (10-minute sliding window, bounded size)
|
|
const WEBHOOK_REPLAY_TTL_MS = 10 * 60 * 1000;
|
|
const webhookReplayCache = new Map();
|
|
export function clearWebhookReplayCache() {
|
|
webhookReplayCache.clear();
|
|
}
|
|
function checkAndMarkWebhookReplay(key) {
|
|
const now = Date.now();
|
|
const prev = webhookReplayCache.get(key);
|
|
if (prev && now - prev < WEBHOOK_REPLAY_TTL_MS) {
|
|
return true;
|
|
}
|
|
webhookReplayCache.set(key, now);
|
|
if (webhookReplayCache.size > 5000) {
|
|
for (const [k, ts] of webhookReplayCache.entries()) {
|
|
if (now - ts >= WEBHOOK_REPLAY_TTL_MS || webhookReplayCache.size > 4000) {
|
|
webhookReplayCache.delete(k);
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
/**
|
|
* P0-02 Hardened — Unified Authenticated Webhook Handler for Steadfast, Pathao, RedX, and Paperfly
|
|
* - Rejects unauthenticated/arbitrary merchant_token path segments with 401
|
|
* - Never discloses PATHAO_CLIENT_SECRET or AURA_WEBHOOK_SECRET in any response header
|
|
* - Derives tenantId strictly from server-side authenticated identity
|
|
* - Enforces optional timestamp skew guard (±10 min) and event replay deduplication (409)
|
|
*/
|
|
function handleCourierWebhook(provider) {
|
|
return (req, res) => {
|
|
const whAuth = authenticateWebhook(req, provider);
|
|
if (!whAuth.valid) {
|
|
return res.status(401).json(wrapError(new McpToolError({
|
|
code: "UNAUTHORIZED",
|
|
message: `Unauthorized ${provider} webhook request: missing or invalid signature/secret.`,
|
|
courier: provider,
|
|
retryable: false,
|
|
}), "validator"));
|
|
}
|
|
// Optional webhook timestamp freshness check (±10 minutes)
|
|
const rawTs = (req.headers["x-webhook-timestamp"] || req.headers["x-timestamp"] || "");
|
|
if (rawTs) {
|
|
const parsedMs = /^\d+$/.test(rawTs.trim())
|
|
? (rawTs.trim().length <= 10 ? Number(rawTs.trim()) * 1000 : Number(rawTs.trim()))
|
|
: Date.parse(rawTs.trim());
|
|
if (!Number.isFinite(parsedMs) || Math.abs(Date.now() - parsedMs) > WEBHOOK_REPLAY_TTL_MS) {
|
|
return res.status(401).json(wrapError(new McpToolError({
|
|
code: "UNAUTHORIZED",
|
|
message: "Stale or invalid webhook timestamp (outside ±10 minute replay window).",
|
|
courier: provider,
|
|
retryable: false,
|
|
}), "validator"));
|
|
}
|
|
}
|
|
const payload = req.body || {};
|
|
if (provider === "pathao" && String(payload.event || "").toLowerCase() === "webhook_integration") {
|
|
// P0-02 Fix: NEVER return PATHAO_CLIENT_SECRET or AURA_WEBHOOK_SECRET in any HTTP response.
|
|
return res.status(202).json({ ok: true, status: "webhook_verified" });
|
|
}
|
|
const consignmentId = payload.consignment_id ||
|
|
payload.merchant_order_id ||
|
|
payload.tracking_code ||
|
|
payload.tracking_id ||
|
|
payload.order_id;
|
|
if (provider === "steadfast" && !consignmentId) {
|
|
return res.status(400).json(wrapError(new McpToolError({
|
|
code: "INVALID_INPUT",
|
|
message: "Invalid or missing consignment_id in webhook payload.",
|
|
courier: provider,
|
|
}), "validator"));
|
|
}
|
|
const status = String(payload.status || payload.order_status || payload.event || "received");
|
|
const eventType = String(payload.notification_type || payload.event || "delivery_status");
|
|
const invoice = payload.invoice || payload.merchant_order_id;
|
|
// P0-02 Fix: Tenant identity comes STRICTLY from server-side verified webhook auth, never arbitrary URL path.
|
|
const tenantId = whAuth.tenantId;
|
|
// Replay deduplication check (header event-id or payload body fingerprint)
|
|
const explicitEventId = String(req.headers["x-webhook-id"] ||
|
|
req.headers["x-event-id"] ||
|
|
payload.event_id ||
|
|
payload.webhook_event_id ||
|
|
"").trim();
|
|
const rawBodyHash = createHash("sha256")
|
|
.update(req.rawBody ? String(req.rawBody) : JSON.stringify(payload))
|
|
.digest("hex")
|
|
.slice(0, 24);
|
|
const replayKey = explicitEventId
|
|
? `${provider}:${tenantId}:id:${explicitEventId}`
|
|
: `${provider}:${tenantId}:hash:${consignmentId || "none"}:${status}:${eventType}:${rawBodyHash}`;
|
|
if (checkAndMarkWebhookReplay(replayKey)) {
|
|
return res.status(409).json({
|
|
ok: false,
|
|
duplicate: true,
|
|
courier: provider,
|
|
message: "Duplicate webhook event ignored (replay protection).",
|
|
});
|
|
}
|
|
const record = {
|
|
id: randomUUID(),
|
|
courier: provider,
|
|
consignment_id: String(consignmentId || "UNKNOWN"),
|
|
invoice: invoice ? String(invoice) : undefined,
|
|
status,
|
|
event_type: eventType,
|
|
received_at: new Date().toISOString(),
|
|
payload,
|
|
};
|
|
// F06: Record webhook event scoped by server-verified tenantId
|
|
recordWebhookEvent(record, tenantId);
|
|
// Forward verified Pathao financial events best-effort to Snehalata Financial Manager
|
|
if (provider === "pathao" && process.env.NODE_ENV !== "test" && /deliver|return|paid|settlement|invoice/i.test(`${status} ${eventType}`)) {
|
|
const fwdUrl = process.env.SNEHALATA_WEBHOOK_URL || "https://www.snehalata.com/api/webhooks/pathao";
|
|
const sig = String(req.headers["x-pathao-signature"] || "");
|
|
if (fwdUrl && sig) {
|
|
fetch(fwdUrl, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json", "X-Pathao-Signature": sig },
|
|
body: JSON.stringify(payload),
|
|
}).catch(() => { });
|
|
}
|
|
}
|
|
return res.status(200).json(wrapSuccess({
|
|
status: "success",
|
|
courier: provider,
|
|
tenant_scope: tenantId,
|
|
consignment_id: record.consignment_id,
|
|
event_id: record.id,
|
|
message: `${provider} webhook verified and recorded for tenant '${tenantId}'.`,
|
|
}, "live_api"));
|
|
};
|
|
}
|
|
app.post(["/webhooks/steadfast", "/webhooks/steadfast/:merchant_token"], handleCourierWebhook("steadfast"));
|
|
app.post(["/webhooks/pathao", "/webhooks/pathao/:merchant_token"], handleCourierWebhook("pathao"));
|
|
app.post(["/webhooks/redx", "/webhooks/redx/:merchant_token"], handleCourierWebhook("redx"));
|
|
app.post(["/webhooks/paperfly", "/webhooks/paperfly/:merchant_token"], handleCourierWebhook("paperfly"));
|
|
/**
|
|
* GET /track/:code - Hardened Public & Authenticated Tracking Gateway
|
|
* F03: PII Masking for unauthenticated callers.
|
|
* F04: HTML entity escaping for XSS prevention.
|
|
*/
|
|
app.get("/track/:code", trackingLimiter, async (req, res) => {
|
|
const code = req.params.code;
|
|
const format = req.query.format;
|
|
const isAuth = Boolean(req.auraAuth?.authenticated);
|
|
const callerTenant = req.auraAuth?.tenantId;
|
|
let courier = (req.query.courier || "").toLowerCase();
|
|
if (!courier) {
|
|
if (code.toUpperCase().startsWith("DS") || code.toUpperCase().startsWith("DB"))
|
|
courier = "pathao";
|
|
else if (code.toUpperCase().startsWith("SF") || /^\d{8,12}$/.test(code))
|
|
courier = "steadfast";
|
|
else if (code.toUpperCase().startsWith("REDX") || code.toUpperCase().startsWith("RX"))
|
|
courier = "redx";
|
|
else if (code.toUpperCase().startsWith("PF"))
|
|
courier = "paperfly";
|
|
else
|
|
courier = "pathao";
|
|
}
|
|
let dbParcel = null;
|
|
if (dbPool) {
|
|
try {
|
|
const qRes = await dbPool.query("SELECT * FROM courier_parcels WHERE tracking_code = $1 LIMIT 1", [code]);
|
|
if (qRes.rows && qRes.rows.length > 0) {
|
|
dbParcel = qRes.rows[0];
|
|
}
|
|
}
|
|
catch (_) { }
|
|
}
|
|
const registry = new CourierRegistry(loadConfig(keysFromRequest(req)));
|
|
let trackResult = null;
|
|
let errorObj = null;
|
|
try {
|
|
trackResult = await registry.trackParcel(code, courier);
|
|
}
|
|
catch (err) {
|
|
errorObj = err;
|
|
}
|
|
// F03: If caller is unauthenticated or not owner, sanitize/redact PII
|
|
const isOwner = isAuth && (callerTenant === "master" || callerTenant === dbParcel?.tenant_id || callerTenant === dbParcel?.tenant);
|
|
if (format === "json" || (!req.headers.accept?.includes("text/html") && format !== "html")) {
|
|
if (trackResult) {
|
|
if (!isOwner) {
|
|
// P1-02 Fix: Minimal public DTO — strips raw_response, phone, full address, customer name, and COD value
|
|
const publicDto = {
|
|
success: true,
|
|
courier: trackResult.courier,
|
|
tracking_code: trackResult.tracking_code || code,
|
|
status: trackResult.status,
|
|
updated_at: trackResult.updated_at,
|
|
destination_area: trackResult.recipient_address
|
|
? redactAddressToDistrict(trackResult.recipient_address)
|
|
: "Bangladesh",
|
|
privacy_mode: "public_masked",
|
|
};
|
|
return res.json(wrapSuccess(publicDto, "live_api"));
|
|
}
|
|
return res.json(wrapSuccess(trackResult, "live_api"));
|
|
}
|
|
if (dbParcel) {
|
|
if (!isOwner) {
|
|
const publicDbDto = {
|
|
success: true,
|
|
parcel: {
|
|
courier: dbParcel.courier || courier,
|
|
tracking_code: dbParcel.tracking_code || code,
|
|
status: dbParcel.status || "booked",
|
|
updated_at: dbParcel.updated_at || dbParcel.created_at,
|
|
destination_area: redactAddressToDistrict(dbParcel.recipient_address || dbParcel.address || ""),
|
|
privacy_mode: "public_masked",
|
|
},
|
|
};
|
|
return res.json(wrapSuccess(publicDbDto, "cache"));
|
|
}
|
|
return res.json(wrapSuccess({ success: true, parcel: dbParcel }, "cache"));
|
|
}
|
|
return res.status(404).json(wrapError(errorObj || new Error("Consignment not found"), "live_api"));
|
|
}
|
|
const raw = trackResult?.raw_response?.data || trackResult?.raw_response || (dbParcel?.raw?.data || dbParcel?.raw) || {};
|
|
let recipientName = dbParcel?.customer_name || raw.recipient_name || raw.customer_name || raw.name || "Verified Customer";
|
|
let recipientAddress = dbParcel?.address || raw.recipient_address || raw.address || "Bangladesh";
|
|
// P1-02 Fix: Strict PII & COD redaction for unauthenticated/non-owner HTML tracker view
|
|
if (!isOwner) {
|
|
recipientAddress = redactAddressToDistrict(recipientAddress);
|
|
recipientName = "Verified Recipient";
|
|
}
|
|
const codAmount = dbParcel?.cod_amount || raw.order_amount || raw.amount_to_collect || raw.cod_amount || raw.collectable_amount || 0;
|
|
const codDisplay = isOwner ? `৳ ${Number(codAmount).toLocaleString()}` : "Protected (Merchant Only)";
|
|
const statusText = trackResult?.status || raw.order_status || (dbParcel?.status ? dbParcel.status.charAt(0).toUpperCase() + dbParcel.status.slice(1) : "Not Found");
|
|
const resolvedCourier = (dbParcel?.courier || courier || "pathao").toLowerCase();
|
|
const carrierName = resolvedCourier === "pathao" ? "Pathao Express" : (resolvedCourier === "steadfast" ? "Steadfast Courier" : resolvedCourier === "redx" ? "RedX Delivery" : resolvedCourier === "paperfly" ? "Paperfly Go" : resolvedCourier.toUpperCase());
|
|
const shortLink = raw.short_link || (carrierName.toLowerCase().includes("pathao") ? `https://merchant.pathao.com/tracking?consignment_id=${code}` : (carrierName.toLowerCase().includes("steadfast") ? `https://steadfast.com.bd/t/${code}` : ""));
|
|
const updatedAt = raw.order_status_updated_at || raw.updated_at || trackResult?.updated_at || (dbParcel?.updated_at ? new Date(dbParcel.updated_at).toLocaleString("en-GB") : new Date().toLocaleString());
|
|
const isDelivered = String(statusText).toLowerCase().includes("deliv");
|
|
const isAtHub = String(statusText).toLowerCase().includes("hub");
|
|
const isTransit = String(statusText).toLowerCase().includes("way") || String(statusText).toLowerCase().includes("transit") || String(statusText).toLowerCase().includes("progress");
|
|
const isPickedUp = isDelivered || isAtHub || isTransit || String(statusText).toLowerCase().includes("pick");
|
|
// F04: All interpolated variables are strictly escaped via escapeHtml()
|
|
res.type("html").send(`<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
<title>Aura Live Tracking — ${escapeHtml(code)}</title>
|
|
<link rel="preconnect" href="https://fonts.googleapis.com">
|
|
<link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;600;700;800&family=JetBrains+Mono:wght@500;700&display=swap" rel="stylesheet">
|
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" />
|
|
<style>
|
|
:root { --bg: #07090e; --card: rgba(13, 17, 28, 0.85); --border: rgba(0, 240, 255, 0.18); --cyan: #00f0ff; --purple: #818cf8; --green: #10b981; --text: #f8fafc; --text-dim: #94a3b8; }
|
|
* { box-sizing: border-box; margin: 0; padding: 0; }
|
|
body { background: var(--bg); color: var(--text); font-family: 'Plus Jakarta Sans', sans-serif; min-height: 100vh; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 20px; position: relative; overflow-x: hidden; }
|
|
.track-card { width: 100%; max-width: 580px; background: var(--card); backdrop-filter: blur(20px); border: 1px solid var(--border); border-radius: 28px; padding: 32px; box-shadow: 0 20px 50px rgba(0,0,0,0.5); position: relative; z-index: 10; }
|
|
.header { display: flex; align-items: center; justify-content: space-between; border-bottom: 1px solid rgba(255,255,255,0.08); padding-bottom: 20px; margin-bottom: 24px; }
|
|
.brand { display: flex; align-items: center; gap: 12px; }
|
|
.brand-icon { width: 44px; height: 44px; border-radius: 14px; background: linear-gradient(135deg, #00f0ff, #818cf8); display: grid; place-items: center; font-size: 20px; color: #07090e; }
|
|
.brand-text h1 { font-size: 18px; font-weight: 800; }
|
|
.brand-text p { font-size: 11px; color: var(--text-dim); }
|
|
.status-badge { display: inline-flex; align-items: center; gap: 8px; padding: 6px 14px; border-radius: 20px; font-size: 12px; font-weight: 700; background: rgba(0, 240, 255, 0.1); color: var(--cyan); border: 1px solid rgba(0, 240, 255, 0.3); }
|
|
.tracking-code-box { background: rgba(0,0,0,0.35); border: 1px dashed rgba(255,255,255,0.15); border-radius: 16px; padding: 14px 18px; display: flex; justify-content: space-between; align-items: center; margin-bottom: 24px; }
|
|
.code-label { font-size: 11px; color: var(--text-dim); text-transform: uppercase; font-weight: 700; }
|
|
.code-val { font-family: 'JetBrains Mono', monospace; font-size: 16px; font-weight: 700; color: #fff; }
|
|
.timeline { position: relative; padding-left: 28px; margin-bottom: 24px; }
|
|
.timeline::before { content: ''; position: absolute; left: 8px; top: 8px; bottom: 8px; width: 2px; background: rgba(255,255,255,0.1); }
|
|
.timeline-step { position: relative; margin-bottom: 20px; }
|
|
.step-dot { position: absolute; left: -28px; top: 3px; width: 18px; height: 18px; border-radius: 50%; display: grid; place-items: center; font-size: 10px; background: #1e293b; border: 2px solid #64748b; color: #fff; }
|
|
.step-dot.done { background: var(--cyan); border-color: var(--cyan); color: #07090e; }
|
|
.step-title { font-size: 14px; font-weight: 700; color: #fff; }
|
|
.step-sub { font-size: 12px; color: var(--text-dim); margin-top: 2px; }
|
|
.info-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 24px; }
|
|
.info-item { background: rgba(255,255,255,0.03); border: 1px solid rgba(255,255,255,0.06); border-radius: 14px; padding: 12px 14px; }
|
|
.info-item-label { font-size: 11px; color: var(--text-dim); margin-bottom: 4px; }
|
|
.info-item-val { font-size: 13px; font-weight: 700; color: #fff; }
|
|
.actions { display: flex; gap: 10px; }
|
|
.btn { flex: 1; padding: 12px; border-radius: 14px; font-weight: 700; font-size: 13px; display: flex; align-items: center; justify-content: center; gap: 8px; text-decoration: none; cursor: pointer; }
|
|
.btn-primary { background: linear-gradient(135deg, #00f0ff, #38bdf8); color: #07090e; border: none; font-weight: 800; }
|
|
.btn-secondary { background: rgba(255,255,255,0.06); color: #fff; border: 1px solid rgba(255,255,255,0.12); }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="track-card">
|
|
<div class="header">
|
|
<div class="brand">
|
|
<div class="brand-icon"><i class="fa-solid fa-truck-fast"></i></div>
|
|
<div class="brand-text">
|
|
<h1>Aura Live Tracking</h1>
|
|
<p>${escapeHtml(carrierName)} • Direct Live Telemetry</p>
|
|
</div>
|
|
</div>
|
|
<div class="status-badge"><span>${escapeHtml(statusText)}</span></div>
|
|
</div>
|
|
<div class="tracking-code-box">
|
|
<div><div class="code-label">Consignment Tracking ID</div><div class="code-val">${escapeHtml(code)}</div></div>
|
|
<div style="text-align: right;"><div class="code-label">Carrier</div><div style="font-weight: 700; color: var(--cyan); font-size: 13px;">${escapeHtml(carrierName)}</div></div>
|
|
</div>
|
|
<div class="timeline">
|
|
<div class="timeline-step"><div class="step-dot done"><i class="fa-solid fa-check"></i></div><div class="step-title">Order Booked & Confirmed</div><div class="step-sub">Aura Dispatch Engine initialized booking.</div></div>
|
|
<div class="timeline-step"><div class="step-dot ${isPickedUp ? "done" : ""}"><i class="fa-solid fa-check"></i></div><div class="step-title">Picked Up by Rider</div><div class="step-sub">Central Merchant Fulfillment Hub</div></div>
|
|
<div class="timeline-step"><div class="step-dot ${isDelivered ? "done" : ""}"><i class="fa-solid fa-arrow-right"></i></div><div class="step-title">${escapeHtml(statusText)}</div><div class="step-sub">Live Telemetry: ${escapeHtml(updatedAt)}</div></div>
|
|
<div class="timeline-step"><div class="step-dot ${isDelivered ? "done" : ""}"><i class="fa-solid fa-house"></i></div><div class="step-title">Destination Delivery</div><div class="step-sub">Destination: ${escapeHtml(recipientAddress)}</div></div>
|
|
</div>
|
|
<div class="info-grid">
|
|
<div class="info-item"><div class="info-item-label">Customer</div><div class="info-item-val">${escapeHtml(recipientName)}</div></div>
|
|
<div class="info-item"><div class="info-item-label">Cash on Delivery (COD)</div><div class="info-item-val">${escapeHtml(codDisplay)}</div></div>
|
|
</div>
|
|
<div class="actions">
|
|
${shortLink ? `<a href="${escapeHtml(shortLink)}" target="_blank" class="btn btn-primary"><i class="fa-solid fa-location-arrow"></i> Official Carrier Map Tracker</a>` : ""}
|
|
<button onclick="window.location.reload()" class="btn btn-secondary"><i class="fa-solid fa-rotate-right"></i> Refresh Live Status</button>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</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, res) => {
|
|
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));
|
|
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,
|
|
courier_breakdown: result.courier_breakdown || [],
|
|
recommendation: result.recommendation,
|
|
};
|
|
return res.json(wrapSuccess(safeDto, "live_api"));
|
|
}
|
|
catch (err) {
|
|
return res.status(502).json(wrapError(err, "live_api"));
|
|
}
|
|
});
|
|
/**
|
|
* Task T9 — Health & Live Telemetry Endpoint
|
|
*/
|
|
app.get("/health", (req, res) => {
|
|
const auth = req.auraAuth || resolveAuthContext(req);
|
|
const isAdmin = auth.authenticated && auth.isAdmin;
|
|
if (!isAdmin) {
|
|
if (req.query.ui === "1" && req.headers.accept?.includes("text/html")) {
|
|
return res.sendFile(HEALTH_HTML, (err) => {
|
|
if (err && !res.headersSent)
|
|
res.json({ ok: true, version: APP_VERSION });
|
|
});
|
|
}
|
|
return res.json({
|
|
ok: true,
|
|
version: APP_VERSION,
|
|
});
|
|
}
|
|
const mem = process.memoryUsage();
|
|
const detailed = {
|
|
ok: true,
|
|
service: "aura-courier-mcp",
|
|
version: APP_VERSION,
|
|
tools_count: TOOLS.length,
|
|
spatial_engine: "Google Maps Platform (gmp_git_agentskills_v1)",
|
|
fraud_engine: "Pathao Nationwide v2 + Steadfast Nationwide API + BD Prefix Validator",
|
|
webhooks: {
|
|
steadfast: "/webhooks/steadfast",
|
|
pathao: "/webhooks/pathao",
|
|
redx: "/webhooks/redx",
|
|
paperfly: "/webhooks/paperfly",
|
|
},
|
|
performance: {
|
|
sessions_active: sessions.size,
|
|
heap_used_mb: Math.round((mem.heapUsed / 1024 / 1024) * 10) / 10,
|
|
heap_total_mb: Math.round((mem.heapTotal / 1024 / 1024) * 10) / 10,
|
|
rss_mb: Math.round((mem.rss / 1024 / 1024) * 10) / 10,
|
|
uptime_seconds: Math.round(process.uptime()),
|
|
},
|
|
};
|
|
return res.json(detailed);
|
|
});
|
|
app.get("/health.html", (_req, res) => {
|
|
res.sendFile(HEALTH_HTML, (err) => {
|
|
if (err && !res.headersSent)
|
|
res.json({ ok: true, version: APP_VERSION });
|
|
});
|
|
});
|
|
app.get("/privacy", (_req, res) => {
|
|
res.sendFile(PRIVACY_HTML, (err) => {
|
|
if (err && !res.headersSent) {
|
|
res.redirect("/");
|
|
}
|
|
});
|
|
});
|
|
app.get("/terms", (_req, res) => {
|
|
res.sendFile(TERMS_HTML, (err) => {
|
|
if (err && !res.headersSent) {
|
|
res.redirect("/");
|
|
}
|
|
});
|
|
});
|
|
app.get("/guide", (_req, res) => {
|
|
res.sendFile(GUIDE_HTML, (err) => {
|
|
if (err && !res.headersSent) {
|
|
res.redirect("/");
|
|
}
|
|
});
|
|
});
|
|
app.get("/simulator", (_req, res) => {
|
|
res.sendFile(SIMULATOR_HTML, (err) => {
|
|
if (err && !res.headersSent) {
|
|
res.redirect("/guide");
|
|
}
|
|
});
|
|
});
|
|
app.get("/", (_req, res) => {
|
|
res.sendFile(LANDING, (err) => {
|
|
if (err && !res.headersSent) {
|
|
res
|
|
.type("html")
|
|
.send(`<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Aura Courier MCP</title>` +
|
|
`<style>body{font-family:system-ui,sans-serif;background:#07080a;color:#f4f5f7;margin:0;display:grid;place-items:center;min-height:100vh}.b{max-width:640px;padding:40px;text-align:center}h1{font-size:28px;margin:0 0 10px}code{background:#0f1115;padding:2px 8px;border-radius:6px;color:#00d4ff;font-size:13px}a{color:#00d4ff}p{line-height:1.6;color:#a4a8b3}</style></head>` +
|
|
`<body><div class="b"><h1>🚚 Aura Courier MCP v${APP_VERSION}</h1><p>Enterprise Bangladesh courier MCP & Google Maps Platform spatial hub.</p>` +
|
|
`<p>Connect your AI to <code>POST /mcp</code> with Streamable-HTTP.</p>` +
|
|
`<p>Webhooks: <code>POST /webhooks/steadfast</code> · <code>POST /webhooks/pathao</code></p>` +
|
|
`<p>By <a href="https://auraajenticai.cloud">Aura Ajentic AI</a> · <a href="https://courier.auraajenticai.cloud">docs & setup</a></p></div></body></html>`);
|
|
}
|
|
});
|
|
});
|
|
if (process.env.NODE_ENV !== "test") {
|
|
const sec = validateSecurityConfig();
|
|
if (!sec.ok) {
|
|
console.error("[FATAL SECURITY CONFIG] Refusing to start Aura Courier MCP without strong secrets:");
|
|
for (const e of sec.errors) {
|
|
console.error(` - ${e}`);
|
|
}
|
|
process.exit(1);
|
|
}
|
|
app.listen(PORT, () => {
|
|
console.log(`Aura Courier MCP v${APP_VERSION} (HTTP) listening on :${PORT} — POST /mcp`);
|
|
});
|
|
}
|