Swadhin Biswas feat(api): add runner, analytics, packages, and security services · 3/6/2026
28f7954
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { getDatabase, schema } from "@/db";
import type { NodePgDatabase } from "drizzle-orm/node-postgres";
import { eq } from "drizzle-orm";
import { generateId } from "./utils";
export interface ResolvedSecurityPolicy {
enforcementMode: "warn" | "block";
secretBlockedTypes: string[];
secretMinSeverity: "CRITICAL" | "HIGH" | "MEDIUM" | "LOW" | "UNKNOWN";
licenseAllowedTypes: string[];
licenseBlockedLicenses: string[];
isEnabled: boolean;
}
export const DEFAULT_SECURITY_POLICY: ResolvedSecurityPolicy = {
enforcementMode: "warn",
secretBlockedTypes: [],
secretMinSeverity: "HIGH",
licenseAllowedTypes: ["permissive"],
licenseBlockedLicenses: [],
isEnabled: true,
};
const severityRank: Record<string, number> = {
CRITICAL: 5,
HIGH: 4,
MEDIUM: 3,
LOW: 2,
UNKNOWN: 1,
};
function toStringArray(input: unknown): string[] {
if (!Array.isArray(input)) return [];
return input.filter((entry): entry is string => typeof entry === "string");
}
function parseJsonArray(raw: string | null | undefined): string[] {
if (!raw) return [];
try {
return toStringArray(JSON.parse(raw));
} catch {
return [];
}
}
function parseSeverity(raw: string | null | undefined): ResolvedSecurityPolicy["secretMinSeverity"] {
const normalized = (raw || "").toUpperCase();
if (normalized === "CRITICAL" || normalized === "HIGH" || normalized === "MEDIUM" || normalized === "LOW" || normalized === "UNKNOWN") {
return normalized;
}
return "HIGH";
}
function parseMode(raw: string | null | undefined): ResolvedSecurityPolicy["enforcementMode"] {
return raw === "block" ? "block" : "warn";
}
export async function getRepositorySecurityPolicy(repositoryId: string): Promise<ResolvedSecurityPolicy> {
const db = getDatabase() as NodePgDatabase<typeof schema>;
const row = await db.query.securityPolicies.findFirst({
where: eq(schema.securityPolicies.repositoryId, repositoryId),
});
if (!row) {
return { ...DEFAULT_SECURITY_POLICY };
}
return {
enforcementMode: parseMode(row.enforcementMode),
secretBlockedTypes: parseJsonArray(row.secretBlockedTypes),
secretMinSeverity: parseSeverity(row.secretMinSeverity),
licenseAllowedTypes: parseJsonArray(row.licenseAllowedTypes),
licenseBlockedLicenses: parseJsonArray(row.licenseBlockedLicenses),
isEnabled: row.isEnabled,
};
}
export async function upsertRepositorySecurityPolicy(options: {
repositoryId: string;
userId: string;
enforcementMode: "warn" | "block";
secretBlockedTypes: string[];
secretMinSeverity: "CRITICAL" | "HIGH" | "MEDIUM" | "LOW" | "UNKNOWN";
licenseAllowedTypes: string[];
licenseBlockedLicenses: string[];
isEnabled: boolean;
}): Promise<ResolvedSecurityPolicy> {
const db = getDatabase() as NodePgDatabase<typeof schema>;
const existing = await db.query.securityPolicies.findFirst({
where: eq(schema.securityPolicies.repositoryId, options.repositoryId),
});
const payload = {
enforcementMode: options.enforcementMode,
secretBlockedTypes: JSON.stringify(options.secretBlockedTypes),
secretMinSeverity: options.secretMinSeverity,
licenseAllowedTypes: JSON.stringify(options.licenseAllowedTypes),
licenseBlockedLicenses: JSON.stringify(options.licenseBlockedLicenses),
isEnabled: options.isEnabled,
updatedById: options.userId,
updatedAt: new Date(),
};
if (existing) {
await db.update(schema.securityPolicies).set(payload).where(eq(schema.securityPolicies.id, existing.id));
} else {
await db.insert(schema.securityPolicies).values({
id: generateId(),
repositoryId: options.repositoryId,
createdAt: new Date(),
...payload,
});
}
return getRepositorySecurityPolicy(options.repositoryId);
}
function extractLicenseName(vulnerabilityId: string | null | undefined, title: string | null | undefined): string {
if (vulnerabilityId?.startsWith("LICENSE-")) {
return vulnerabilityId.slice("LICENSE-".length).trim();
}
if (title?.startsWith("License:")) {
return title.replace("License:", "").trim();
}
return "";
}
export function evaluateSecretPolicy(
policy: ResolvedSecurityPolicy,
secretType: string | null | undefined,
severity: string | null | undefined
): { violated: boolean; reason?: string } {
if (!policy.isEnabled) return { violated: false };
const normalizedSeverity = (severity || "UNKNOWN").toUpperCase();
const meetsSeverity =
(severityRank[normalizedSeverity] || 0) >= severityRank[policy.secretMinSeverity];
const normalizedType = (secretType || "").toLowerCase();
const normalizedBlocked = policy.secretBlockedTypes.map((t) => t.toLowerCase());
const blockedByType = normalizedBlocked.length > 0 && normalizedBlocked.includes(normalizedType);
if (blockedByType) {
return { violated: true, reason: `Secret type "${secretType}" is blocked by policy` };
}
if (meetsSeverity) {
return { violated: true, reason: `Secret severity ${normalizedSeverity} meets or exceeds policy threshold ${policy.secretMinSeverity}` };
}
return { violated: false };
}
export function evaluateLicensePolicy(
policy: ResolvedSecurityPolicy,
licenseType: string | null | undefined,
vulnerabilityId: string | null | undefined,
title: string | null | undefined
): { violated: boolean; reason?: string } {
if (!policy.isEnabled) return { violated: false };
const normalizedType = (licenseType || "unknown").toLowerCase();
const allowedTypes = policy.licenseAllowedTypes.map((t) => t.toLowerCase());
const licenseName = extractLicenseName(vulnerabilityId, title);
const blockedLicenses = policy.licenseBlockedLicenses.map((l) => l.toLowerCase());
if (blockedLicenses.includes(licenseName.toLowerCase())) {
return { violated: true, reason: `License "${licenseName}" is blocked by policy` };
}
if (allowedTypes.length > 0 && !allowedTypes.includes(normalizedType)) {
return { violated: true, reason: `License type "${normalizedType}" is not allowed by policy` };
}
return { violated: false };
}