-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsuppressions.ts
More file actions
45 lines (41 loc) · 1.37 KB
/
Copy pathsuppressions.ts
File metadata and controls
45 lines (41 loc) · 1.37 KB
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
// Suppression pattern per interfaces.md §2. A suppression silences one rule
// for one scope with a required reason and (when severity ≥ high) a
// required expiry. The evaluator is called by policy rules before reporting.
export interface Suppression {
ruleId: string;
reason: string;
/** Glob-like URN prefix the suppression applies to. `*` matches any suffix. */
urnScope?: string;
expiresAt?: string;
}
export interface SuppressionMatch {
suppressed: boolean;
reason?: string;
}
function urnMatches(scope: string, urn: string): boolean {
if (scope === urn) return true;
if (scope.endsWith("*")) {
return urn.startsWith(scope.slice(0, -1));
}
return false;
}
export function matchSuppression(
ruleId: string,
urn: string,
suppressions: readonly Suppression[] | undefined,
now: Date = new Date(),
): SuppressionMatch {
if (!suppressions || suppressions.length === 0) return { suppressed: false };
for (const s of suppressions) {
if (s.ruleId !== ruleId) continue;
if (s.urnScope !== undefined && !urnMatches(s.urnScope, urn)) continue;
if (s.expiresAt !== undefined) {
const expiry = new Date(s.expiresAt);
if (Number.isFinite(expiry.getTime()) && expiry.getTime() < now.getTime()) {
continue;
}
}
return { suppressed: true, ...(s.reason ? { reason: s.reason } : {}) };
}
return { suppressed: false };
}