@cclabsnz/sf-audit 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,5 +5,13 @@
5
5
  "MEDIUM": 4,
6
6
  "LOW": 1,
7
7
  "INFO": 0
8
+ },
9
+ "checkWeights": {},
10
+ "gradeThresholds": {
11
+ "A": { "minScore": 85, "maxHigh": 0 },
12
+ "B": { "minScore": 70, "maxHigh": 1 },
13
+ "C": { "minScore": 55, "maxHigh": 3 },
14
+ "D": { "minScore": 40, "maxCritical": 0 },
15
+ "F": {}
8
16
  }
9
17
  }
@@ -0,0 +1,42 @@
1
+ {
2
+ "_comment": "Copy this file, adjust values to fit your org's risk appetite, and pass it with --scoring-config <path>. All sections are optional — omit any section to keep the default.",
3
+ "riskScores": {
4
+ "CRITICAL": 10,
5
+ "HIGH": 7,
6
+ "MEDIUM": 4,
7
+ "LOW": 1,
8
+ "INFO": 0
9
+ },
10
+ "checkWeights": {
11
+ "_comment": "Override the weight for a specific check regardless of its riskLevel. To use: remove this _comment key.",
12
+ "apex-sharing": 7,
13
+ "api-limits": 4,
14
+ "audit-trail": 4,
15
+ "code-security": 4,
16
+ "connected-apps": 7,
17
+ "custom-settings": 4,
18
+ "field-level-security": 7,
19
+ "flows-without-sharing": 7,
20
+ "guest-user-access": 10,
21
+ "hardcoded-credentials": 10,
22
+ "health-check": 7,
23
+ "inactive-users": 4,
24
+ "ip-restrictions": 7,
25
+ "login-session": 4,
26
+ "named-credentials": 1,
27
+ "password-session-policy": 7,
28
+ "permissions": 4,
29
+ "public-group-sharing": 4,
30
+ "remote-sites": 4,
31
+ "scheduled-apex": 1,
32
+ "sharing-model": 7,
33
+ "users-and-admins": 10
34
+ },
35
+ "gradeThresholds": {
36
+ "A": { "minScore": 85, "maxHigh": 0 },
37
+ "B": { "minScore": 70, "maxHigh": 1 },
38
+ "C": { "minScore": 55, "maxHigh": 3 },
39
+ "D": { "minScore": 40, "maxCritical": 0 },
40
+ "F": {}
41
+ }
42
+ }
@@ -1,10 +1,12 @@
1
1
  import type { SecurityCheck } from './SecurityCheck.js';
2
2
  import type { AuditContext } from '../context/AuditContext.js';
3
3
  import type { AuditResult } from '../findings/AuditResult.js';
4
+ import type { ScoringConfig } from '../findings/ScoringConfig.js';
4
5
  export declare class CheckEngine {
5
6
  private readonly checks;
6
7
  private readonly ctx;
7
- constructor(checks: SecurityCheck[], ctx: AuditContext);
8
+ private readonly scoringConfig?;
9
+ constructor(checks: SecurityCheck[], ctx: AuditContext, scoringConfig?: ScoringConfig | undefined);
8
10
  run(onProgress?: (current: number, total: number, checkName: string) => void): Promise<AuditResult>;
9
11
  private validateCacheOrdering;
10
12
  }
@@ -3,6 +3,7 @@ function buildErrorFinding(check, err) {
3
3
  const msg = err instanceof Error ? err.message : String(err);
4
4
  return {
5
5
  id: `${check.id}-error`,
6
+ checkId: check.id,
6
7
  category: check.category,
7
8
  riskLevel: 'INFO',
8
9
  title: `${check.name}: check failed`,
@@ -13,9 +14,11 @@ function buildErrorFinding(check, err) {
13
14
  export class CheckEngine {
14
15
  checks;
15
16
  ctx;
16
- constructor(checks, ctx) {
17
+ scoringConfig;
18
+ constructor(checks, ctx, scoringConfig) {
17
19
  this.checks = checks;
18
20
  this.ctx = ctx;
21
+ this.scoringConfig = scoringConfig;
19
22
  this.validateCacheOrdering();
20
23
  }
21
24
  async run(onProgress) {
@@ -27,7 +30,7 @@ export class CheckEngine {
27
30
  onProgress?.(i + 1, total, check.name);
28
31
  try {
29
32
  const result = await check.run(this.ctx);
30
- findings.push(...result.findings);
33
+ findings.push(...result.findings.map((f) => ({ ...f, checkId: check.id })));
31
34
  if (result.metrics) {
32
35
  metrics = { ...metrics, ...result.metrics };
33
36
  }
@@ -36,7 +39,7 @@ export class CheckEngine {
36
39
  findings.push(buildErrorFinding(check, err));
37
40
  }
38
41
  }
39
- return buildAuditResult(this.ctx, findings, metrics);
42
+ return buildAuditResult(this.ctx, findings, metrics, this.scoringConfig);
40
43
  }
41
44
  validateCacheOrdering() {
42
45
  const populated = new Set();
@@ -10,6 +10,7 @@ export default class SecurityAuditCommand extends SfCommand<AuditResult> {
10
10
  output: import("@oclif/core/interfaces").OptionFlag<string, import("@oclif/core/interfaces").CustomOptions>;
11
11
  'fail-on': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
12
12
  checks: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
13
+ 'scoring-config': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
13
14
  };
14
15
  run(): Promise<AuditResult>;
15
16
  private printSummary;
@@ -9,6 +9,7 @@ import { JsonRenderer } from '../../renderers/JsonRenderer.js';
9
9
  import { HtmlRenderer } from '../../renderers/HtmlRenderer.js';
10
10
  import { MarkdownRenderer } from '../../renderers/MarkdownRenderer.js';
11
11
  import { buildAuditContext, resolveOrgInfo } from '../../lib/wire.js';
12
+ import { loadScoringConfig } from '../../findings/loadScoringConfig.js';
12
13
  const RENDERERS = {
13
14
  html: new HtmlRenderer(),
14
15
  md: new MarkdownRenderer(),
@@ -42,6 +43,10 @@ export default class SecurityAuditCommand extends SfCommand {
42
43
  summary: 'Comma-separated check IDs to run. Omit to run all checks.',
43
44
  helpValue: 'hardcoded-credentials,apex-sharing',
44
45
  }),
46
+ 'scoring-config': Flags.string({
47
+ summary: 'Path to a custom scoring config JSON file. Merges with defaults.',
48
+ helpValue: './hnz-scoring.json',
49
+ }),
45
50
  };
46
51
  async run() {
47
52
  const { flags } = await this.parse(SecurityAuditCommand);
@@ -61,10 +66,12 @@ export default class SecurityAuditCommand extends SfCommand {
61
66
  return CHECKS.filter((c) => ids.has(c.id));
62
67
  })()
63
68
  : CHECKS;
69
+ const knownCheckIds = new Set(CHECKS.map((c) => c.id));
70
+ const scoringConfig = loadScoringConfig(flags['scoring-config'], knownCheckIds, (msg) => this.warn(msg));
64
71
  this.log(`Auditing org: ${orgInfo.name} (${orgInfo.id})`);
65
72
  if (flags.checks)
66
73
  this.log(`Running ${checksToRun.length} of ${CHECKS.length} checks`);
67
- const engine = new CheckEngine(checksToRun, ctx);
74
+ const engine = new CheckEngine(checksToRun, ctx, scoringConfig);
68
75
  const result = await engine.run((current, total, checkName) => {
69
76
  this.log(`[${String(current).padStart(2)}/${total}] ${checkName}`);
70
77
  });
@@ -6,6 +6,16 @@ export interface AffectedItem {
6
6
  }
7
7
  export interface Finding {
8
8
  id: string;
9
+ /**
10
+ * The ID of the check that produced this finding.
11
+ *
12
+ * This field is optional on the interface so that the 22+ individual check implementations
13
+ * can construct `Finding` objects without declaring it — doing so would otherwise cause
14
+ * TypeScript compile errors throughout the codebase. The `CheckEngine` is the single place
15
+ * that sets this value after each check runs. Consumers of `AuditResult.findings` can
16
+ * therefore rely on `checkId` being present on every finding in a completed audit result.
17
+ */
18
+ checkId?: string;
9
19
  category: string;
10
20
  riskLevel: RiskLevel;
11
21
  title: string;
@@ -0,0 +1,266 @@
1
+ import { z } from 'zod';
2
+ import type { RiskLevel } from './RiskLevel.js';
3
+ export declare const scoringConfigSchema: z.ZodObject<{
4
+ /** When provided, ALL five risk levels (CRITICAL, HIGH, MEDIUM, LOW, INFO) must be present.
5
+ * The entire object is replaced atomically during merge, not key-by-key. */
6
+ riskScores: z.ZodOptional<z.ZodObject<{
7
+ CRITICAL: z.ZodNumber;
8
+ HIGH: z.ZodNumber;
9
+ MEDIUM: z.ZodNumber;
10
+ LOW: z.ZodNumber;
11
+ INFO: z.ZodNumber;
12
+ }, "strip", z.ZodTypeAny, {
13
+ CRITICAL: number;
14
+ HIGH: number;
15
+ MEDIUM: number;
16
+ LOW: number;
17
+ INFO: number;
18
+ }, {
19
+ CRITICAL: number;
20
+ HIGH: number;
21
+ MEDIUM: number;
22
+ LOW: number;
23
+ INFO: number;
24
+ }>>;
25
+ checkWeights: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodNumber>>;
26
+ gradeThresholds: z.ZodOptional<z.ZodObject<{
27
+ A: z.ZodOptional<z.ZodObject<{
28
+ minScore: z.ZodOptional<z.ZodNumber>;
29
+ maxCritical: z.ZodOptional<z.ZodNumber>;
30
+ maxHigh: z.ZodOptional<z.ZodNumber>;
31
+ maxMedium: z.ZodOptional<z.ZodNumber>;
32
+ }, "strip", z.ZodTypeAny, {
33
+ minScore?: number | undefined;
34
+ maxCritical?: number | undefined;
35
+ maxHigh?: number | undefined;
36
+ maxMedium?: number | undefined;
37
+ }, {
38
+ minScore?: number | undefined;
39
+ maxCritical?: number | undefined;
40
+ maxHigh?: number | undefined;
41
+ maxMedium?: number | undefined;
42
+ }>>;
43
+ B: z.ZodOptional<z.ZodObject<{
44
+ minScore: z.ZodOptional<z.ZodNumber>;
45
+ maxCritical: z.ZodOptional<z.ZodNumber>;
46
+ maxHigh: z.ZodOptional<z.ZodNumber>;
47
+ maxMedium: z.ZodOptional<z.ZodNumber>;
48
+ }, "strip", z.ZodTypeAny, {
49
+ minScore?: number | undefined;
50
+ maxCritical?: number | undefined;
51
+ maxHigh?: number | undefined;
52
+ maxMedium?: number | undefined;
53
+ }, {
54
+ minScore?: number | undefined;
55
+ maxCritical?: number | undefined;
56
+ maxHigh?: number | undefined;
57
+ maxMedium?: number | undefined;
58
+ }>>;
59
+ C: z.ZodOptional<z.ZodObject<{
60
+ minScore: z.ZodOptional<z.ZodNumber>;
61
+ maxCritical: z.ZodOptional<z.ZodNumber>;
62
+ maxHigh: z.ZodOptional<z.ZodNumber>;
63
+ maxMedium: z.ZodOptional<z.ZodNumber>;
64
+ }, "strip", z.ZodTypeAny, {
65
+ minScore?: number | undefined;
66
+ maxCritical?: number | undefined;
67
+ maxHigh?: number | undefined;
68
+ maxMedium?: number | undefined;
69
+ }, {
70
+ minScore?: number | undefined;
71
+ maxCritical?: number | undefined;
72
+ maxHigh?: number | undefined;
73
+ maxMedium?: number | undefined;
74
+ }>>;
75
+ D: z.ZodOptional<z.ZodObject<{
76
+ minScore: z.ZodOptional<z.ZodNumber>;
77
+ maxCritical: z.ZodOptional<z.ZodNumber>;
78
+ maxHigh: z.ZodOptional<z.ZodNumber>;
79
+ maxMedium: z.ZodOptional<z.ZodNumber>;
80
+ }, "strip", z.ZodTypeAny, {
81
+ minScore?: number | undefined;
82
+ maxCritical?: number | undefined;
83
+ maxHigh?: number | undefined;
84
+ maxMedium?: number | undefined;
85
+ }, {
86
+ minScore?: number | undefined;
87
+ maxCritical?: number | undefined;
88
+ maxHigh?: number | undefined;
89
+ maxMedium?: number | undefined;
90
+ }>>;
91
+ F: z.ZodOptional<z.ZodObject<{
92
+ minScore: z.ZodOptional<z.ZodNumber>;
93
+ maxCritical: z.ZodOptional<z.ZodNumber>;
94
+ maxHigh: z.ZodOptional<z.ZodNumber>;
95
+ maxMedium: z.ZodOptional<z.ZodNumber>;
96
+ }, "strip", z.ZodTypeAny, {
97
+ minScore?: number | undefined;
98
+ maxCritical?: number | undefined;
99
+ maxHigh?: number | undefined;
100
+ maxMedium?: number | undefined;
101
+ }, {
102
+ minScore?: number | undefined;
103
+ maxCritical?: number | undefined;
104
+ maxHigh?: number | undefined;
105
+ maxMedium?: number | undefined;
106
+ }>>;
107
+ }, "strip", z.ZodTypeAny, {
108
+ A?: {
109
+ minScore?: number | undefined;
110
+ maxCritical?: number | undefined;
111
+ maxHigh?: number | undefined;
112
+ maxMedium?: number | undefined;
113
+ } | undefined;
114
+ B?: {
115
+ minScore?: number | undefined;
116
+ maxCritical?: number | undefined;
117
+ maxHigh?: number | undefined;
118
+ maxMedium?: number | undefined;
119
+ } | undefined;
120
+ C?: {
121
+ minScore?: number | undefined;
122
+ maxCritical?: number | undefined;
123
+ maxHigh?: number | undefined;
124
+ maxMedium?: number | undefined;
125
+ } | undefined;
126
+ D?: {
127
+ minScore?: number | undefined;
128
+ maxCritical?: number | undefined;
129
+ maxHigh?: number | undefined;
130
+ maxMedium?: number | undefined;
131
+ } | undefined;
132
+ F?: {
133
+ minScore?: number | undefined;
134
+ maxCritical?: number | undefined;
135
+ maxHigh?: number | undefined;
136
+ maxMedium?: number | undefined;
137
+ } | undefined;
138
+ }, {
139
+ A?: {
140
+ minScore?: number | undefined;
141
+ maxCritical?: number | undefined;
142
+ maxHigh?: number | undefined;
143
+ maxMedium?: number | undefined;
144
+ } | undefined;
145
+ B?: {
146
+ minScore?: number | undefined;
147
+ maxCritical?: number | undefined;
148
+ maxHigh?: number | undefined;
149
+ maxMedium?: number | undefined;
150
+ } | undefined;
151
+ C?: {
152
+ minScore?: number | undefined;
153
+ maxCritical?: number | undefined;
154
+ maxHigh?: number | undefined;
155
+ maxMedium?: number | undefined;
156
+ } | undefined;
157
+ D?: {
158
+ minScore?: number | undefined;
159
+ maxCritical?: number | undefined;
160
+ maxHigh?: number | undefined;
161
+ maxMedium?: number | undefined;
162
+ } | undefined;
163
+ F?: {
164
+ minScore?: number | undefined;
165
+ maxCritical?: number | undefined;
166
+ maxHigh?: number | undefined;
167
+ maxMedium?: number | undefined;
168
+ } | undefined;
169
+ }>>;
170
+ }, "strip", z.ZodTypeAny, {
171
+ riskScores?: {
172
+ CRITICAL: number;
173
+ HIGH: number;
174
+ MEDIUM: number;
175
+ LOW: number;
176
+ INFO: number;
177
+ } | undefined;
178
+ checkWeights?: Record<string, number> | undefined;
179
+ gradeThresholds?: {
180
+ A?: {
181
+ minScore?: number | undefined;
182
+ maxCritical?: number | undefined;
183
+ maxHigh?: number | undefined;
184
+ maxMedium?: number | undefined;
185
+ } | undefined;
186
+ B?: {
187
+ minScore?: number | undefined;
188
+ maxCritical?: number | undefined;
189
+ maxHigh?: number | undefined;
190
+ maxMedium?: number | undefined;
191
+ } | undefined;
192
+ C?: {
193
+ minScore?: number | undefined;
194
+ maxCritical?: number | undefined;
195
+ maxHigh?: number | undefined;
196
+ maxMedium?: number | undefined;
197
+ } | undefined;
198
+ D?: {
199
+ minScore?: number | undefined;
200
+ maxCritical?: number | undefined;
201
+ maxHigh?: number | undefined;
202
+ maxMedium?: number | undefined;
203
+ } | undefined;
204
+ F?: {
205
+ minScore?: number | undefined;
206
+ maxCritical?: number | undefined;
207
+ maxHigh?: number | undefined;
208
+ maxMedium?: number | undefined;
209
+ } | undefined;
210
+ } | undefined;
211
+ }, {
212
+ riskScores?: {
213
+ CRITICAL: number;
214
+ HIGH: number;
215
+ MEDIUM: number;
216
+ LOW: number;
217
+ INFO: number;
218
+ } | undefined;
219
+ checkWeights?: Record<string, number> | undefined;
220
+ gradeThresholds?: {
221
+ A?: {
222
+ minScore?: number | undefined;
223
+ maxCritical?: number | undefined;
224
+ maxHigh?: number | undefined;
225
+ maxMedium?: number | undefined;
226
+ } | undefined;
227
+ B?: {
228
+ minScore?: number | undefined;
229
+ maxCritical?: number | undefined;
230
+ maxHigh?: number | undefined;
231
+ maxMedium?: number | undefined;
232
+ } | undefined;
233
+ C?: {
234
+ minScore?: number | undefined;
235
+ maxCritical?: number | undefined;
236
+ maxHigh?: number | undefined;
237
+ maxMedium?: number | undefined;
238
+ } | undefined;
239
+ D?: {
240
+ minScore?: number | undefined;
241
+ maxCritical?: number | undefined;
242
+ maxHigh?: number | undefined;
243
+ maxMedium?: number | undefined;
244
+ } | undefined;
245
+ F?: {
246
+ minScore?: number | undefined;
247
+ maxCritical?: number | undefined;
248
+ maxHigh?: number | undefined;
249
+ maxMedium?: number | undefined;
250
+ } | undefined;
251
+ } | undefined;
252
+ }>;
253
+ export type ScoringConfigInput = z.infer<typeof scoringConfigSchema>;
254
+ export interface GradeConditions {
255
+ minScore?: number;
256
+ maxCritical?: number;
257
+ maxHigh?: number;
258
+ maxMedium?: number;
259
+ }
260
+ export interface ScoringConfig {
261
+ riskScores: Record<RiskLevel, number>;
262
+ checkWeights: Record<string, number>;
263
+ gradeThresholds: Record<'A' | 'B' | 'C' | 'D' | 'F', GradeConditions>;
264
+ }
265
+ export declare const DEFAULT_SCORING_CONFIG: ScoringConfig;
266
+ //# sourceMappingURL=ScoringConfig.d.ts.map
@@ -0,0 +1,43 @@
1
+ import { z } from 'zod';
2
+ const nonNegativeInt = z.number().int().nonnegative();
3
+ const gradeConditionsSchema = z.object({
4
+ minScore: nonNegativeInt.optional(),
5
+ maxCritical: nonNegativeInt.optional(),
6
+ maxHigh: nonNegativeInt.optional(),
7
+ maxMedium: nonNegativeInt.optional(),
8
+ });
9
+ export const scoringConfigSchema = z.object({
10
+ /** When provided, ALL five risk levels (CRITICAL, HIGH, MEDIUM, LOW, INFO) must be present.
11
+ * The entire object is replaced atomically during merge, not key-by-key. */
12
+ riskScores: z
13
+ .object({
14
+ CRITICAL: nonNegativeInt,
15
+ HIGH: nonNegativeInt,
16
+ MEDIUM: nonNegativeInt,
17
+ LOW: nonNegativeInt,
18
+ INFO: nonNegativeInt,
19
+ })
20
+ .optional(),
21
+ checkWeights: z.record(z.string(), nonNegativeInt).optional(),
22
+ gradeThresholds: z
23
+ .object({
24
+ A: gradeConditionsSchema.optional(),
25
+ B: gradeConditionsSchema.optional(),
26
+ C: gradeConditionsSchema.optional(),
27
+ D: gradeConditionsSchema.optional(),
28
+ F: gradeConditionsSchema.optional(),
29
+ })
30
+ .optional(),
31
+ });
32
+ export const DEFAULT_SCORING_CONFIG = {
33
+ riskScores: { CRITICAL: 10, HIGH: 7, MEDIUM: 4, LOW: 1, INFO: 0 },
34
+ checkWeights: {},
35
+ gradeThresholds: {
36
+ A: { minScore: 85, maxHigh: 0 },
37
+ B: { minScore: 70, maxHigh: 1 },
38
+ C: { minScore: 55, maxHigh: 3 },
39
+ D: { minScore: 40, maxCritical: 0 },
40
+ F: {},
41
+ },
42
+ };
43
+ //# sourceMappingURL=ScoringConfig.js.map
@@ -0,0 +1,3 @@
1
+ import type { ScoringConfig } from './ScoringConfig.js';
2
+ export declare function loadScoringConfig(filePath: string | undefined, knownCheckIds: Set<string>, warn: (msg: string) => void): ScoringConfig;
3
+ //# sourceMappingURL=loadScoringConfig.d.ts.map
@@ -0,0 +1,37 @@
1
+ import * as fs from 'node:fs';
2
+ import { scoringConfigSchema, DEFAULT_SCORING_CONFIG } from './ScoringConfig.js';
3
+ export function loadScoringConfig(filePath, knownCheckIds, warn) {
4
+ if (!filePath)
5
+ return DEFAULT_SCORING_CONFIG;
6
+ let raw;
7
+ try {
8
+ raw = fs.readFileSync(filePath, 'utf-8');
9
+ }
10
+ catch (err) {
11
+ throw new Error(`Cannot read scoring config file '${filePath}': ${err instanceof Error ? err.message : String(err)}`);
12
+ }
13
+ const parsed = JSON.parse(raw);
14
+ const validated = scoringConfigSchema.parse(parsed); // throws ZodError on failure
15
+ // Warn on unknown check IDs and drop them
16
+ const safeCheckWeights = {};
17
+ for (const [id, weight] of Object.entries(validated.checkWeights ?? {})) {
18
+ if (!knownCheckIds.has(id)) {
19
+ warn(`Unknown check ID in --scoring-config checkWeights: '${id}'. Run 'sf audit list' to see valid IDs. Skipping.`);
20
+ }
21
+ else {
22
+ safeCheckWeights[id] = weight;
23
+ }
24
+ }
25
+ return {
26
+ riskScores: validated.riskScores ?? DEFAULT_SCORING_CONFIG.riskScores,
27
+ checkWeights: safeCheckWeights,
28
+ gradeThresholds: {
29
+ A: { ...DEFAULT_SCORING_CONFIG.gradeThresholds.A, ...(validated.gradeThresholds?.A ?? {}) },
30
+ B: { ...DEFAULT_SCORING_CONFIG.gradeThresholds.B, ...(validated.gradeThresholds?.B ?? {}) },
31
+ C: { ...DEFAULT_SCORING_CONFIG.gradeThresholds.C, ...(validated.gradeThresholds?.C ?? {}) },
32
+ D: { ...DEFAULT_SCORING_CONFIG.gradeThresholds.D, ...(validated.gradeThresholds?.D ?? {}) },
33
+ F: { ...DEFAULT_SCORING_CONFIG.gradeThresholds.F, ...(validated.gradeThresholds?.F ?? {}) },
34
+ },
35
+ };
36
+ }
37
+ //# sourceMappingURL=loadScoringConfig.js.map
@@ -2,5 +2,6 @@ import type { Finding } from './Finding.js';
2
2
  import type { OrgMetrics } from '../context/OrgMetrics.js';
3
3
  import type { AuditResult } from './AuditResult.js';
4
4
  import type { AuditContext } from '../context/AuditContext.js';
5
- export declare function buildAuditResult(ctx: AuditContext, findings: Finding[], metrics: Partial<OrgMetrics>): AuditResult;
5
+ import type { ScoringConfig } from './ScoringConfig.js';
6
+ export declare function buildAuditResult(ctx: AuditContext, findings: Finding[], metrics: Partial<OrgMetrics>, config?: ScoringConfig): AuditResult;
6
7
  //# sourceMappingURL=scoring.d.ts.map
@@ -1,49 +1,30 @@
1
- import * as fs from 'node:fs';
2
- import * as path from 'node:path';
3
- import { fileURLToPath } from 'node:url';
4
1
  import { EMPTY_METRICS } from '../context/OrgMetrics.js';
5
- const pluginRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..');
6
- const scoringConfig = JSON.parse(fs.readFileSync(path.join(pluginRoot, 'config', 'scoring.json'), 'utf-8'));
7
- const RISK_SCORES = scoringConfig.riskScores;
8
- export function buildAuditResult(ctx, findings, metrics) {
9
- const totalScore = findings.reduce((sum, f) => sum + RISK_SCORES[f.riskLevel], 0);
2
+ import { DEFAULT_SCORING_CONFIG } from './ScoringConfig.js';
3
+ function meetsConditions(conditions, healthScore, criticalCount, highCount, mediumCount) {
4
+ if (conditions.minScore !== undefined && healthScore < conditions.minScore)
5
+ return false;
6
+ if (conditions.maxCritical !== undefined && criticalCount > conditions.maxCritical)
7
+ return false;
8
+ if (conditions.maxHigh !== undefined && highCount > conditions.maxHigh)
9
+ return false;
10
+ if (conditions.maxMedium !== undefined && mediumCount > conditions.maxMedium)
11
+ return false;
12
+ return true;
13
+ }
14
+ export function buildAuditResult(ctx, findings, metrics, config = DEFAULT_SCORING_CONFIG) {
15
+ const totalScore = findings.reduce((sum, f) => sum + (f.checkId !== undefined ? (config.checkWeights[f.checkId] ?? config.riskScores[f.riskLevel]) : config.riskScores[f.riskLevel]), 0);
10
16
  const maxPossible = findings.length * 10;
11
17
  const healthScore = Math.max(0, 100 - Math.round((totalScore / Math.max(maxPossible, 1)) * 100));
12
18
  const criticalCount = findings.filter((f) => f.riskLevel === 'CRITICAL').length;
13
19
  const highCount = findings.filter((f) => f.riskLevel === 'HIGH').length;
14
20
  const mediumCount = findings.filter((f) => f.riskLevel === 'MEDIUM').length;
15
- const totalFindings = findings.length;
16
- let grade;
17
- if (criticalCount > 0) {
18
- grade = 'F';
19
- }
20
- else if (healthScore < 40 && totalFindings > 5) {
21
- // Many findings with very low health score = F
22
- grade = 'F';
23
- }
24
- else if (highCount > 3) {
25
- grade = 'D';
26
- }
27
- else if (highCount > 1) {
28
- grade = 'C';
29
- }
30
- else if (mediumCount > 3) {
31
- grade = 'B';
32
- }
33
- else if (healthScore < 40) {
34
- grade = 'F';
35
- }
36
- else if (healthScore < 55) {
37
- grade = 'D';
38
- }
39
- else if (healthScore < 70) {
40
- grade = 'C';
41
- }
42
- else if (healthScore < 85) {
43
- grade = 'B';
44
- }
45
- else {
46
- grade = 'A';
21
+ const grades = ['A', 'B', 'C', 'D'];
22
+ let grade = 'F';
23
+ for (const g of grades) {
24
+ if (meetsConditions(config.gradeThresholds[g], healthScore, criticalCount, highCount, mediumCount)) {
25
+ grade = g;
26
+ break;
27
+ }
47
28
  }
48
29
  return {
49
30
  generatedAt: new Date(),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cclabsnz/sf-audit",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "CloudCounsel Salesforce Security Audit — native sf plugin",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",