Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/auth/sso.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,22 @@ export class SsoAuthenticator {
}

/**
* Creates an HMAC-SHA256 signed session JWT.
* Creates a cryptographically signed session JWT using PBKDF2 key derivation.
*/
public createSessionToken(user: SsoUser): string {
const header = Buffer.from(JSON.stringify({ alg: "HS256", typ: "JWT" })).toString("base64url");
const header = Buffer.from(JSON.stringify({ alg: "PBKDF2-SHA256", typ: "JWT" })).toString(
"base64url",
);
const payload = Buffer.from(JSON.stringify(user)).toString("base64url");
const signature = crypto
.createHmac("sha256", this.signingKey)
.update(`${header}.${payload}`)
.digest("base64url");
.pbkdf2Sync(`${header}.${payload}`, this.signingKey, 10000, 32, "sha256")
.toString("base64url");

return `${header}.${payload}.${signature}`;
}

/**
* Verifies an HMAC-SHA256 signed session JWT or provider ID token.
* Verifies an HMAC/PBKDF2 signed session JWT or provider ID token.
*/
public verifySessionToken(token: string): SsoValidationResult {
if (!token || typeof token !== "string") {
Expand All @@ -120,9 +121,8 @@ export class SsoAuthenticator {

const [headerB64, payloadB64, sig] = parts;
const expectedSig = crypto
.createHmac("sha256", this.signingKey)
.update(`${headerB64}.${payloadB64}`)
.digest("base64url");
.pbkdf2Sync(`${headerB64}.${payloadB64}`, this.signingKey, 10000, 32, "sha256")
.toString("base64url");

const validSignature =
sig.length === expectedSig.length &&
Expand Down
38 changes: 32 additions & 6 deletions src/dashboard/server.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import crypto from "node:crypto";
import http from "node:http";
import type { AddressInfo } from "node:net";
import { ChatOpsNotifier } from "../approval/chatops.js";
Expand All @@ -24,6 +25,7 @@ export class DashboardServer {
private readonly ssoAuthenticator?: SsoAuthenticator;
private readonly complianceReporter: ComplianceReporter;
private readonly chatOpsNotifier: ChatOpsNotifier;
private readonly sessionStore = new Map<string, string>();
private sseClients: Set<http.ServerResponse> = new Set();
private listenPort = 3100;

Expand Down Expand Up @@ -137,7 +139,8 @@ export class DashboardServer {
const parts = cookieHeader.split(";").map((p) => p.trim());
for (const p of parts) {
if (p.startsWith("dbshield_session=")) {
return p.slice("dbshield_session=".length).trim();
const rawId = p.slice("dbshield_session=".length).trim();
return this.sessionStore.get(rawId) || rawId;
}
}
}
Expand Down Expand Up @@ -244,8 +247,11 @@ export class DashboardServer {
return;
}

const sessionId = `sess_${crypto.randomBytes(32).toString("hex")}`;
this.sessionStore.set(sessionId, callbackResult.sessionToken);

res.writeHead(302, {
"Set-Cookie": `dbshield_session=${callbackResult.sessionToken}; Path=/; HttpOnly; Secure; SameSite=Lax`,
"Set-Cookie": `dbshield_session=${sessionId}; Path=/; HttpOnly; Secure; SameSite=Lax`,
Location: "/",
});
res.end();
Expand All @@ -262,6 +268,16 @@ export class DashboardServer {

// SSO: Logout
if (pathname === "/auth/logout") {
const cookieHeader = req.headers.cookie;
if (cookieHeader) {
const parts = cookieHeader.split(";").map((p) => p.trim());
for (const p of parts) {
if (p.startsWith("dbshield_session=")) {
const rawId = p.slice("dbshield_session=".length).trim();
this.sessionStore.delete(rawId);
}
}
}
res.writeHead(302, {
"Set-Cookie":
"dbshield_session=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT; HttpOnly; Secure; SameSite=Lax",
Expand All @@ -285,17 +301,27 @@ export class DashboardServer {
return;
}

const framework = (url.searchParams.get("framework") as "soc2" | "iso27001") || "soc2";
const format = (url.searchParams.get("format") as "json" | "html" | "pdf") || "json";
const rawFramework = url.searchParams.get("framework");
const framework = rawFramework === "iso27001" ? "iso27001" : "soc2";
const rawFormat = url.searchParams.get("format");
const format = rawFormat === "html" ? "html" : rawFormat === "pdf" ? "pdf" : "json";

const rawCompany = url.searchParams.get("company");
const safeCompany = rawCompany
? escapeServerHtml(rawCompany.replace(/[^\w\s.-]/g, ""))
: undefined;
const rawEnv = url.searchParams.get("env");
const safeEnv = rawEnv ? escapeServerHtml(rawEnv.replace(/[^\w\s.-]/g, "")) : undefined;

const report = await this.complianceReporter.generateReport({
framework,
format,
companyName: url.searchParams.get("company") || undefined,
environment: url.searchParams.get("env") || undefined,
companyName: safeCompany,
environment: safeEnv,
});

if (format === "html") {
res.setHeader("Content-Security-Policy", "default-src 'none'; style-src 'unsafe-inline';");
res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
res.end(report.rawReport);
} else if (format === "pdf") {
Expand Down
Loading