feat(auth): harden session management and security
- Implement server-side session revocation on logout - Add IP extraction utility and admin setup error handling - Improve error codes and response consistency for auth endpoints - Refine landing page redirect logic to handle transient DB outages - Update deployment documentation and configuration defaults
This commit is contained in:
1 parent
935f12a1e9
commit
89e4d68690
81 files changed
+11530
-6916
No files matched your search
@@ -34,6 +34,11 @@ export class ApiEngine {
|
||||
).replace(/\/$/, '');
|
||||
const internalHeaders = getInternalIngestionHeaders();
|
||||
|
||||
let ingestionBridgeStatus: 'online' | 'auth_error' | 'error' | 'offline' = 'offline';
|
||||
let ingestionBridgeError: string | undefined;
|
||||
let liveServerPort = settings.serverPort || 4000;
|
||||
let liveWebPort = settings.webPort || 3000;
|
||||
|
||||
try {
|
||||
const res = await fetch(`${ingestionBaseUrl}/internal/metrics`, {
|
||||
headers: internalHeaders,
|
||||
@@ -42,8 +47,17 @@ export class ApiEngine {
|
||||
});
|
||||
if (res.ok) {
|
||||
const data = await res.json();
|
||||
ingestionBridgeStatus = 'online';
|
||||
activeSockets = Number(data.activeSockets) || 0;
|
||||
ingestionRedisConnected = Boolean(data.redisConnected);
|
||||
if (Number.isFinite(Number(data.serverPort)) && Number(data.serverPort) > 0) {
|
||||
liveServerPort = Number(data.serverPort);
|
||||
} else if (Number.isFinite(Number(data.port)) && Number(data.port) > 0) {
|
||||
liveServerPort = Number(data.port);
|
||||
}
|
||||
if (Number.isFinite(Number(data.webPort)) && Number(data.webPort) > 0) {
|
||||
liveWebPort = Number(data.webPort);
|
||||
}
|
||||
if (data.localBufferSize !== undefined) {
|
||||
ingestionLocalBufferSize = Number(data.localBufferSize) || 0;
|
||||
ingestionRedisBufferSize = Number(data.redisBufferSize) || 0;
|
||||
@@ -53,14 +67,36 @@ export class ApiEngine {
|
||||
if (data.securityStats && typeof data.securityStats === 'object') {
|
||||
remoteSecurityStats = data.securityStats;
|
||||
}
|
||||
// BUG-02: Auto-reconcile Port 4000 cold start whenever metrics observe unsynced registry
|
||||
if (data.hasSyncedInMemoryClients === false && clients.length > 0) {
|
||||
await SDKManager.syncAllClientsToIngestionAsync();
|
||||
}
|
||||
} else {
|
||||
ingestionBridgeStatus = res.status === 401 || res.status === 503 ? 'auth_error' : 'error';
|
||||
ingestionBridgeError = `HTTP ${res.status}`;
|
||||
console.warn(`[DataStack Bridge] /internal/metrics failed with ${ingestionBridgeError}`);
|
||||
}
|
||||
} catch {
|
||||
} catch (err: unknown) {
|
||||
// Ingestion server may not be running in single-process dev mode
|
||||
activeSockets = 0;
|
||||
ingestionLocalBufferSize = 0;
|
||||
ingestionRedisBufferSize = 0;
|
||||
ingestionBridgeStatus = 'offline';
|
||||
ingestionBridgeError = err instanceof Error ? err.message : 'Unreachable';
|
||||
}
|
||||
|
||||
const effectiveRedisBufferSize = Math.max(
|
||||
localBufferStats.redisBufferSize || 0,
|
||||
ingestionRedisBufferSize,
|
||||
);
|
||||
const redisConnected = localBufferStats.redisConnected || ingestionRedisConnected;
|
||||
const securityMode: 'redis_shared' | 'bridge_coordinated' | 'degraded_local' =
|
||||
redisConnected
|
||||
? 'redis_shared'
|
||||
: ingestionBridgeStatus === 'online'
|
||||
? 'bridge_coordinated'
|
||||
: 'degraded_local';
|
||||
|
||||
const localSecurityStats = SecurityEngine.getStats();
|
||||
const securityStats = {
|
||||
activeIdempotencyKeys:
|
||||
@@ -74,15 +110,12 @@ export class ApiEngine {
|
||||
Number(remoteSecurityStats?.maxPayloadSizeBytes) ||
|
||||
localSecurityStats.maxPayloadSizeBytes,
|
||||
),
|
||||
mode: securityMode,
|
||||
};
|
||||
|
||||
const effectiveRedisBufferSize = Math.max(
|
||||
localBufferStats.redisBufferSize || 0,
|
||||
ingestionRedisBufferSize,
|
||||
);
|
||||
const bufferStats = {
|
||||
...localBufferStats,
|
||||
redisConnected: localBufferStats.redisConnected || ingestionRedisConnected,
|
||||
redisConnected,
|
||||
redisBufferSize: effectiveRedisBufferSize,
|
||||
currentBufferSize:
|
||||
localBufferStats.localBufferSize +
|
||||
@@ -98,8 +131,11 @@ export class ApiEngine {
|
||||
storageStats,
|
||||
securityStats,
|
||||
runtime: {
|
||||
serverPort: settings.serverPort || 4000,
|
||||
webPort: settings.webPort || 3000,
|
||||
serverPort: liveServerPort,
|
||||
webPort: liveWebPort,
|
||||
ingestionBridgeStatus,
|
||||
...(ingestionBridgeError ? { ingestionBridgeError } : {}),
|
||||
securityMode,
|
||||
uptimeSeconds: Math.floor(process.uptime()),
|
||||
memoryUsageMb: (process.memoryUsage().rss / (1024 * 1024)).toFixed(1),
|
||||
nodeVersion: process.version,
|
||||
@@ -107,23 +143,35 @@ export class ApiEngine {
|
||||
};
|
||||
}
|
||||
|
||||
private static filterFilesByClientId<T extends { clientId: string }>(
|
||||
files: T[],
|
||||
clientId?: string,
|
||||
): T[] {
|
||||
if (!clientId || clientId === 'all') return files;
|
||||
const normalized = SDKManager.normalizeClientId(clientId);
|
||||
if (!normalized) return files;
|
||||
const sanitized = CSVStorageManager.sanitizeClientId(normalized);
|
||||
return files.filter((f) => {
|
||||
const folderNorm = SDKManager.normalizeClientId(f.clientId);
|
||||
return (
|
||||
folderNorm === normalized ||
|
||||
f.clientId === normalized ||
|
||||
f.clientId === sanitized
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters and lists stored CSV files for export
|
||||
*/
|
||||
public static getExportFiles(clientId?: string) {
|
||||
const allFiles = CSVStorageManager.listAllFiles();
|
||||
if (clientId && clientId !== 'all') {
|
||||
return allFiles.filter((f) => f.clientId === clientId);
|
||||
}
|
||||
return allFiles;
|
||||
return this.filterFilesByClientId(allFiles, clientId);
|
||||
}
|
||||
|
||||
public static async getExportFilesAsync(clientId?: string) {
|
||||
const allFiles = await CSVStorageManager.listAllFilesAsync();
|
||||
if (clientId && clientId !== 'all') {
|
||||
return allFiles.filter((f) => f.clientId === clientId);
|
||||
}
|
||||
return allFiles;
|
||||
return this.filterFilesByClientId(allFiles, clientId);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user