Skip to content

Commit 2b6e458

Browse files
committed
refactor(policy): split doctor evaluator modules
1 parent eb1120f commit 2b6e458

15 files changed

Lines changed: 5791 additions & 5555 deletions

‎extensions/policy/src/doctor/access-findings.ts‎

Lines changed: 549 additions & 0 deletions
Large diffs are not rendered by default.

‎extensions/policy/src/doctor/access-shapes.ts‎

Lines changed: 405 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 327 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,327 @@
1+
import type { HealthFinding } from "openclaw/plugin-sdk/health";
2+
import { isRecord } from "openclaw/plugin-sdk/string-coerce-runtime";
3+
import type { PolicyDataHandlingEvidence, PolicyEvidence } from "../policy-state.js";
4+
import { CHECK_IDS } from "./metadata.js";
5+
import { authProfileHasMetadata, requiredAuthProfileMetadata } from "./policy-runtime.js";
6+
import {
7+
agentScopedPolicyTargets,
8+
dataHandlingPolicyHasRules,
9+
scopedAgentIdMatches,
10+
} from "./policy-scope.js";
11+
import {
12+
authProfileAllowModesShapeFindings,
13+
dataHandlingEntries,
14+
dataHandlingFinding,
15+
dataHandlingLabel,
16+
dataHandlingPolicyShapeFindings,
17+
secretPolicyShapeFindings,
18+
} from "./runtime-shapes.js";
19+
import { ocPathSegment, readPolicyBoolean, readStringList } from "./utils.js";
20+
21+
export function secretAuthProvenanceFindings(
22+
policy: unknown,
23+
policyPath: string,
24+
policyDocName: string,
25+
evidence: PolicyEvidence,
26+
): readonly HealthFinding[] {
27+
const secretShapeFindings = secretPolicyShapeFindings(policy, policyPath, policyDocName);
28+
const authShapeFindings = authProfileAllowModesShapeFindings(policy, policyPath, policyDocName);
29+
return [
30+
...(secretShapeFindings.length > 0
31+
? secretShapeFindings
32+
: [
33+
...secretManagedProviderFindings(policy, policyDocName, evidence),
34+
...secretDeniedSourceFindings(policy, policyDocName, evidence),
35+
...secretInsecureProviderFindings(policy, policyDocName, evidence),
36+
]),
37+
...(authShapeFindings.length > 0
38+
? authShapeFindings
39+
: [
40+
...authProfileMetadataFindings(policy, policyDocName, evidence),
41+
...authProfileModeFindings(policy, policyDocName, evidence),
42+
]),
43+
];
44+
}
45+
46+
export function dataHandlingFindings(
47+
policy: unknown,
48+
policyPath: string,
49+
policyDocName: string,
50+
evidence: PolicyEvidence,
51+
): readonly HealthFinding[] {
52+
const shapeFindings = dataHandlingPolicyShapeFindings(policy, policyPath, policyDocName);
53+
if (shapeFindings.length > 0) {
54+
return shapeFindings;
55+
}
56+
const findings: HealthFinding[] = [];
57+
findings.push(
58+
...dataHandlingFindingsForRule(policy, policyDocName, "dataHandling", evidence, () => true),
59+
);
60+
for (const target of agentScopedPolicyTargets(policy)) {
61+
if (!dataHandlingPolicyHasRules(target.overlay.dataHandling)) {
62+
continue;
63+
}
64+
findings.push(
65+
...dataHandlingFindingsForRule(
66+
target.overlay,
67+
policyDocName,
68+
`scopes/${ocPathSegment(target.scopeName)}/dataHandling`,
69+
evidence,
70+
(entry) =>
71+
entry.kind !== "memorySessionTranscriptIndexing" ||
72+
scopedDataHandlingAgentMatches(entry, target.agentId, evidence.dataHandling ?? []),
73+
),
74+
);
75+
}
76+
return findings;
77+
}
78+
79+
function scopedDataHandlingAgentMatches(
80+
entry: PolicyDataHandlingEvidence,
81+
policyAgentId: string,
82+
entries: readonly PolicyDataHandlingEvidence[],
83+
): boolean {
84+
if (entry.id === "memory-qmd-session-transcripts") {
85+
return true;
86+
}
87+
if (scopedAgentIdMatches(entry.agentId, policyAgentId)) {
88+
return true;
89+
}
90+
return (
91+
entry.id === "agents-defaults-memory-session-transcripts" &&
92+
!entries.some(
93+
(candidate) =>
94+
candidate.scope === "agent" &&
95+
candidate.kind === entry.kind &&
96+
scopedAgentIdMatches(candidate.agentId, policyAgentId),
97+
)
98+
);
99+
}
100+
101+
function dataHandlingFindingsForRule(
102+
policy: unknown,
103+
policyDocName: string,
104+
requirementBase: string,
105+
evidence: PolicyEvidence,
106+
evidenceFilter: (entry: PolicyDataHandlingEvidence) => boolean,
107+
): readonly HealthFinding[] {
108+
const dataHandling = isRecord(policy) ? policy.dataHandling : undefined;
109+
if (!isRecord(dataHandling)) {
110+
return [];
111+
}
112+
const findings: HealthFinding[] = [];
113+
if (readPolicyBoolean(dataHandling, ["sensitiveLogging", "requireRedaction"]) === true) {
114+
findings.push(
115+
...dataHandlingEntries(evidence, "sensitiveLoggingRedaction")
116+
.filter(evidenceFilter)
117+
.filter((entry) => entry.value !== true)
118+
.map((entry) =>
119+
dataHandlingFinding(entry, {
120+
checkId: CHECK_IDS.policyDataHandlingRedactionDisabled,
121+
message: "Sensitive logging redaction is disabled.",
122+
requirement: `oc://${policyDocName}/${requirementBase}/sensitiveLogging/requireRedaction`,
123+
fixHint: "Set logging.redactSensitive to tools or update policy after review.",
124+
}),
125+
),
126+
);
127+
}
128+
if (readPolicyBoolean(dataHandling, ["telemetry", "denyContentCapture"]) === true) {
129+
findings.push(
130+
...dataHandlingEntries(evidence, "telemetryContentCapture")
131+
.filter(evidenceFilter)
132+
.filter((entry) => entry.value === true)
133+
.map((entry) =>
134+
dataHandlingFinding(entry, {
135+
checkId: CHECK_IDS.policyDataHandlingTelemetryContentCapture,
136+
message: "Telemetry content capture is enabled.",
137+
requirement: `oc://${policyDocName}/${requirementBase}/telemetry/denyContentCapture`,
138+
fixHint: "Disable diagnostics.otel.captureContent or update policy after review.",
139+
}),
140+
),
141+
);
142+
}
143+
if (readPolicyBoolean(dataHandling, ["retention", "requireSessionMaintenance"]) === true) {
144+
findings.push(
145+
...dataHandlingEntries(evidence, "sessionRetentionMode")
146+
.filter(evidenceFilter)
147+
.filter((entry) => entry.value !== "enforce")
148+
.map((entry) =>
149+
dataHandlingFinding(entry, {
150+
checkId: CHECK_IDS.policyDataHandlingSessionRetentionNotEnforced,
151+
message: `Session retention maintenance mode is '${entry.value ?? "unknown"}'.`,
152+
requirement: `oc://${policyDocName}/${requirementBase}/retention/requireSessionMaintenance`,
153+
fixHint: "Set session.maintenance.mode to enforce or update policy after review.",
154+
}),
155+
),
156+
);
157+
}
158+
if (readPolicyBoolean(dataHandling, ["memory", "denySessionTranscriptIndexing"]) === true) {
159+
findings.push(
160+
...dataHandlingEntries(evidence, "memorySessionTranscriptIndexing")
161+
.filter(evidenceFilter)
162+
.filter((entry) => entry.value === true)
163+
.map((entry) =>
164+
dataHandlingFinding(entry, {
165+
checkId: CHECK_IDS.policyDataHandlingSessionTranscriptMemory,
166+
message: `${dataHandlingLabel(entry)} enables session transcript memory indexing.`,
167+
requirement: `oc://${policyDocName}/${requirementBase}/memory/denySessionTranscriptIndexing`,
168+
fixHint:
169+
"Disable session transcript memory indexing for the matching config surface or update policy after review.",
170+
}),
171+
),
172+
);
173+
}
174+
return findings;
175+
}
176+
177+
function secretManagedProviderFindings(
178+
policy: unknown,
179+
policyDocName: string,
180+
evidence: PolicyEvidence,
181+
): readonly HealthFinding[] {
182+
if (readPolicyBoolean(policy, ["secrets", "requireManagedProviders"]) !== true) {
183+
return [];
184+
}
185+
const secrets = evidence.secrets ?? [];
186+
const providerKeys = new Set(
187+
secrets
188+
.filter((secret) => secret.kind === "provider" && secret.providerSource !== undefined)
189+
.map((secret) => `${secret.providerSource}:${secret.id}`),
190+
);
191+
return secrets
192+
.filter(
193+
(secret) =>
194+
secret.kind === "input" &&
195+
secret.provenance === "secretRef" &&
196+
(secret.refProvider === undefined ||
197+
secret.refSource === undefined ||
198+
!providerKeys.has(`${secret.refSource}:${secret.refProvider}`)),
199+
)
200+
.map((secret): HealthFinding => {
201+
return {
202+
checkId: CHECK_IDS.policySecretsUnmanagedProvider,
203+
severity: "error",
204+
message: `SecretRef uses unmanaged provider '${secret.refProvider ?? "default"}'.`,
205+
source: "policy",
206+
path: "openclaw config",
207+
ocPath: secret.source,
208+
target: secret.source,
209+
requirement: `oc://${policyDocName}/secrets/requireManagedProviders`,
210+
fixHint:
211+
"Declare the referenced provider under secrets.providers or update policy after review.",
212+
};
213+
});
214+
}
215+
216+
function secretDeniedSourceFindings(
217+
policy: unknown,
218+
policyDocName: string,
219+
evidence: PolicyEvidence,
220+
): readonly HealthFinding[] {
221+
const deniedSources = new Set(readStringList(policy, ["secrets", "denySources"]));
222+
if (deniedSources.size === 0) {
223+
return [];
224+
}
225+
return (evidence.secrets ?? [])
226+
.filter((secret) => {
227+
const source = secret.kind === "provider" ? secret.providerSource : secret.refSource;
228+
return source !== undefined && deniedSources.has(source);
229+
})
230+
.map((secret): HealthFinding => {
231+
const source = secret.kind === "provider" ? secret.providerSource : secret.refSource;
232+
return {
233+
checkId: CHECK_IDS.policySecretsDeniedProviderSource,
234+
severity: "error",
235+
message: `Secret ${secret.kind} '${secret.id}' uses denied source '${source}'.`,
236+
source: "policy",
237+
path: "openclaw config",
238+
ocPath: secret.source,
239+
target: secret.source,
240+
requirement: `oc://${policyDocName}/secrets/denySources`,
241+
fixHint: "Move this secret to an approved source or update policy after review.",
242+
};
243+
});
244+
}
245+
246+
function secretInsecureProviderFindings(
247+
policy: unknown,
248+
policyDocName: string,
249+
evidence: PolicyEvidence,
250+
): readonly HealthFinding[] {
251+
if (readPolicyBoolean(policy, ["secrets", "allowInsecureProviders"]) !== false) {
252+
return [];
253+
}
254+
return (evidence.secrets ?? [])
255+
.filter((secret) => secret.kind === "provider" && (secret.insecure?.length ?? 0) > 0)
256+
.map((secret): HealthFinding => {
257+
return {
258+
checkId: CHECK_IDS.policySecretsInsecureProvider,
259+
severity: "error",
260+
message: `Secret provider '${secret.id}' enables insecure posture: ${(secret.insecure ?? []).join(", ")}.`,
261+
source: "policy",
262+
path: "openclaw config",
263+
ocPath: secret.source,
264+
target: secret.source,
265+
requirement: `oc://${policyDocName}/secrets/allowInsecureProviders`,
266+
fixHint: "Remove insecure provider overrides or update policy after review.",
267+
};
268+
});
269+
}
270+
271+
function authProfileMetadataFindings(
272+
policy: unknown,
273+
policyDocName: string,
274+
evidence: PolicyEvidence,
275+
): readonly HealthFinding[] {
276+
const requiredMetadata = requiredAuthProfileMetadata(policy);
277+
if (requiredMetadata.size === 0) {
278+
return [];
279+
}
280+
return (evidence.authProfiles ?? []).flatMap((profile): HealthFinding[] => {
281+
const missing = [...requiredMetadata].filter(
282+
(metadata) => !authProfileHasMetadata(profile, metadata),
283+
);
284+
if (missing.length === 0) {
285+
return [];
286+
}
287+
return [
288+
{
289+
checkId: CHECK_IDS.policyAuthProfileInvalidMetadata,
290+
severity: "error",
291+
message: `Auth profile '${profile.id}' is missing required metadata: ${missing.join(", ")}.`,
292+
source: "policy",
293+
path: "openclaw config",
294+
ocPath: profile.source,
295+
target: profile.source,
296+
requirement: `oc://${policyDocName}/auth/profiles/requireMetadata`,
297+
fixHint: "Set auth.profiles.<id>.provider and a supported auth profile mode.",
298+
},
299+
];
300+
});
301+
}
302+
303+
function authProfileModeFindings(
304+
policy: unknown,
305+
policyDocName: string,
306+
evidence: PolicyEvidence,
307+
): readonly HealthFinding[] {
308+
const allowedModes = new Set(readStringList(policy, ["auth", "profiles", "allowModes"]));
309+
if (allowedModes.size === 0) {
310+
return [];
311+
}
312+
return (evidence.authProfiles ?? [])
313+
.filter((profile) => profile.mode !== undefined && !allowedModes.has(profile.mode))
314+
.map((profile): HealthFinding => {
315+
return {
316+
checkId: CHECK_IDS.policyAuthProfileUnapprovedMode,
317+
severity: "error",
318+
message: `Auth profile '${profile.id}' uses mode '${profile.mode}' outside the policy allowlist.`,
319+
source: "policy",
320+
path: "openclaw config",
321+
ocPath: profile.source,
322+
target: profile.source,
323+
requirement: `oc://${policyDocName}/auth/profiles/allowModes`,
324+
fixHint: "Change the auth profile mode or update policy after review.",
325+
};
326+
});
327+
}

0 commit comments

Comments
 (0)