refactor(auth): standardize session management and security

- Centralize secret retrieval using `getAdminSessionSecret`
- Replace manual session parsing with `verifyRequestAdminSession` utility
- Tighten API validation and improve security headers handling
- Enforce environment variable security for production deployments
This commit is contained in:
Team Dark Devil committed 2026-11-18 05:18:55 +06:00
1 parent 34519c07df
commit 177ed4a844
78 files changed
+6748 -3157

No files matched your search

+69 -11
View File
@@ -1,8 +1,11 @@
import { AuthService } from "../auth/jwt";
import { AuthService, resolveDefaultJwtSecret } from "../auth/jwt";
import { DataBufferEngine } from "../data/buffer";
import { DatabaseEngine } from "../Database/database";
import { SDKManager } from "../sdk/sdkManager";
import { SecurityEngine } from "../Security/security";
import {
SecurityEngine,
getInternalIngestionHeaders,
} from "../Security/security";
import { CSVStorageManager } from "../Storage/csvStorage";
export interface AdminSettings {
@@ -35,9 +38,7 @@ function getDefaultSettings(): AdminSettings {
batchFlushThreshold: 500,
batchFlushIntervalMs: 2000,
fileRotationMb: 100,
jwtSecret:
process.env.DATASTACK_JWT_SECRET ||
"datastack-jwt-default-secret-key-32-chars-long",
jwtSecret: resolveDefaultJwtSecret(),
enableIdempotency: true,
autoPruneDays: 30,
updatedAt: new Date().toISOString(),
@@ -62,7 +63,9 @@ export class SettingsManager {
AuthService.setSecret(settings.jwtSecret);
}
CSVStorageManager.maxFileSizeBytes = settings.fileRotationMb * 1024 * 1024;
CSVStorageManager.pruneExpiredPartitions(settings.autoPruneDays);
if (settings.autoPruneDays > 0) {
CSVStorageManager.pruneExpiredPartitions(settings.autoPruneDays);
}
}
private static async syncFromDatabase(): Promise<AdminSettings | null> {
@@ -83,6 +86,10 @@ export class SettingsManager {
if (result.rowCount && result.rowCount > 0) {
const row = result.rows[0];
const parsedPruneDays =
row.auto_prune_days !== undefined && row.auto_prune_days !== null
? Number(row.auto_prune_days)
: NaN;
const loaded: AdminSettings = {
serverPort: Number(row.server_port) || 4000,
webPort: Number(row.web_port) || 3000,
@@ -91,12 +98,9 @@ export class SettingsManager {
batchFlushThreshold: Number(row.batch_flush_threshold) || 500,
batchFlushIntervalMs: Number(row.batch_flush_interval_ms) || 2000,
fileRotationMb: Number(row.file_rotation_mb) || 100,
jwtSecret:
String(row.jwt_secret || "") ||
process.env.DATASTACK_JWT_SECRET ||
"datastack-jwt-default-secret-key-32-chars-long",
jwtSecret: String(row.jwt_secret || "").trim() || resolveDefaultJwtSecret(),
enableIdempotency: Boolean(row.enable_idempotency),
autoPruneDays: Number(row.auto_prune_days) || 30,
autoPruneDays: Number.isFinite(parsedPruneDays) ? parsedPruneDays : 30,
updatedAt: row.updated_at
? new Date(row.updated_at as string | Date).toISOString()
: new Date().toISOString(),
@@ -214,8 +218,46 @@ export class SettingsManager {
};
}
private static async notifyIngestionSettingsSync(
settings: AdminSettings,
authJwtSecret: string,
jwtSecretChanged: boolean,
): Promise<void> {
const headers = getInternalIngestionHeaders(authJwtSecret);
if (!headers["x-datastack-internal-secret"]) return;
const ingestionBaseUrl = (
process.env.INGESTION_INTERNAL_URL || "http://127.0.0.1:4000"
).replace(/\/$/, "");
try {
await fetch(`${ingestionBaseUrl}/internal/settings-sync`, {
method: "POST",
headers: {
"Content-Type": "application/json",
...headers,
},
body: JSON.stringify({
maxEventsPerSec: settings.maxEventsPerSec,
maxPayloadSizeKb: settings.maxPayloadSizeKb,
batchFlushThreshold: settings.batchFlushThreshold,
batchFlushIntervalMs: settings.batchFlushIntervalMs,
fileRotationMb: settings.fileRotationMb,
enableIdempotency: settings.enableIdempotency,
autoPruneDays: settings.autoPruneDays,
...(jwtSecretChanged ? { jwtSecret: settings.jwtSecret } : {}),
}),
cache: "no-store",
signal: AbortSignal.timeout(1500),
});
} catch {
// Port 4000 may not be running in isolated single-process dev mode
}
}
public static updateSettings(updates: Partial<AdminSettings>): AdminSettings {
const current = getSettingsState();
const previousJwtSecret = current.jwtSecret;
const jwtSecretChanged =
Boolean(updates.jwtSecret) && updates.jwtSecret !== current.jwtSecret;
const nextSettings: AdminSettings = {
@@ -229,6 +271,13 @@ export class SettingsManager {
SDKManager.resignAllClientTokens();
}
void this.persistSettingsToDb(nextSettings);
void this.notifyIngestionSettingsSync(
nextSettings,
previousJwtSecret,
jwtSecretChanged,
).then(() =>
jwtSecretChanged ? SDKManager.syncAllClientsToIngestionAsync() : undefined,
);
return { ...nextSettings };
}
@@ -236,6 +285,7 @@ export class SettingsManager {
updates: Partial<AdminSettings>,
): Promise<AdminSettings> {
const current = await this.getSettingsAsync();
const previousJwtSecret = current.jwtSecret;
const jwtSecretChanged =
Boolean(updates.jwtSecret) && updates.jwtSecret !== current.jwtSecret;
const nextSettings: AdminSettings = {
@@ -249,6 +299,14 @@ export class SettingsManager {
await SDKManager.resignAllClientTokensAsync();
}
await this.persistSettingsToDb(nextSettings);
await this.notifyIngestionSettingsSync(
nextSettings,
previousJwtSecret,
jwtSecretChanged,
);
if (jwtSecretChanged) {
await SDKManager.syncAllClientsToIngestionAsync();
}
return { ...nextSettings };
}
}