|
| 1 | +import type { HealthFinding } from "openclaw/plugin-sdk/health"; |
| 2 | +import { isRecord } from "openclaw/plugin-sdk/string-coerce-runtime"; |
| 3 | +import { CHECK_IDS } from "./check-ids.js"; |
| 4 | +import { SUPPORTED_AUTH_PROFILE_METADATA } from "./policy-constants.js"; |
| 5 | +import { isChannelDenyRule } from "./policy-runtime.js"; |
| 6 | +import { unsupportedPolicyKey } from "./shape-helpers.js"; |
| 7 | +import { ocPathSegment } from "./utils.js"; |
| 8 | + |
| 9 | +export function authProfileMetadataRequirementFindings( |
| 10 | + policy: unknown, |
| 11 | + policyPath: string, |
| 12 | + policyDocName: string, |
| 13 | +): readonly HealthFinding[] { |
| 14 | + if ( |
| 15 | + !isRecord(policy) || |
| 16 | + !isRecord(policy.auth) || |
| 17 | + !isRecord(policy.auth.profiles) || |
| 18 | + policy.auth.profiles.requireMetadata === undefined |
| 19 | + ) { |
| 20 | + return []; |
| 21 | + } |
| 22 | + if (!Array.isArray(policy.auth.profiles.requireMetadata)) { |
| 23 | + return [ |
| 24 | + { |
| 25 | + checkId: CHECK_IDS.policyInvalidFile, |
| 26 | + severity: "error", |
| 27 | + message: `${policyPath} auth.profiles.requireMetadata must be an array of metadata keys.`, |
| 28 | + source: "policy", |
| 29 | + path: policyPath, |
| 30 | + target: `oc://${policyDocName}/auth/profiles/requireMetadata`, |
| 31 | + fixHint: `Use supported metadata keys: ${SUPPORTED_AUTH_PROFILE_METADATA.join(", ")}.`, |
| 32 | + }, |
| 33 | + ]; |
| 34 | + } |
| 35 | + const invalidIndex = policy.auth.profiles.requireMetadata.findIndex( |
| 36 | + (entry) => |
| 37 | + typeof entry !== "string" || |
| 38 | + !SUPPORTED_AUTH_PROFILE_METADATA.includes( |
| 39 | + entry.trim().toLowerCase() as (typeof SUPPORTED_AUTH_PROFILE_METADATA)[number], |
| 40 | + ), |
| 41 | + ); |
| 42 | + if (invalidIndex < 0) { |
| 43 | + return []; |
| 44 | + } |
| 45 | + return [ |
| 46 | + { |
| 47 | + checkId: CHECK_IDS.policyInvalidFile, |
| 48 | + severity: "error", |
| 49 | + message: `${policyPath} auth.profiles.requireMetadata[${invalidIndex}] must be a supported metadata key.`, |
| 50 | + source: "policy", |
| 51 | + path: policyPath, |
| 52 | + target: `oc://${policyDocName}/auth/profiles/requireMetadata/#${invalidIndex}`, |
| 53 | + fixHint: `Use supported metadata keys: ${SUPPORTED_AUTH_PROFILE_METADATA.join(", ")}.`, |
| 54 | + }, |
| 55 | + ]; |
| 56 | +} |
| 57 | + |
| 58 | +export function invalidChannelDenyRuleFindings( |
| 59 | + policy: unknown, |
| 60 | + policyPath: string, |
| 61 | + policyDocName: string, |
| 62 | +): readonly HealthFinding[] { |
| 63 | + if (!isRecord(policy) || !isRecord(policy.channels) || policy.channels.denyRules === undefined) { |
| 64 | + return []; |
| 65 | + } |
| 66 | + if (!Array.isArray(policy.channels.denyRules)) { |
| 67 | + return [ |
| 68 | + { |
| 69 | + checkId: CHECK_IDS.policyInvalidFile, |
| 70 | + severity: "error", |
| 71 | + message: `${policyPath} channels.denyRules must be an array.`, |
| 72 | + source: "policy", |
| 73 | + path: policyPath, |
| 74 | + target: `oc://${policyDocName}/channels/denyRules`, |
| 75 | + fixHint: `Fix ${policyPath} so channel deny rules are an array.`, |
| 76 | + }, |
| 77 | + ]; |
| 78 | + } |
| 79 | + for (const [index, rule] of policy.channels.denyRules.entries()) { |
| 80 | + if (!isRecord(rule)) { |
| 81 | + continue; |
| 82 | + } |
| 83 | + const unsupportedRuleKey = unsupportedPolicyKey(rule, ["id", "reason", "when"]); |
| 84 | + if (unsupportedRuleKey !== undefined) { |
| 85 | + return [ |
| 86 | + { |
| 87 | + checkId: CHECK_IDS.policyInvalidFile, |
| 88 | + severity: "error", |
| 89 | + message: `${policyPath} channels.denyRules[${index}].${unsupportedRuleKey} is not supported in channel deny rules.`, |
| 90 | + source: "policy", |
| 91 | + path: policyPath, |
| 92 | + target: `oc://${policyDocName}/channels/denyRules/#${index}/${ocPathSegment(unsupportedRuleKey)}`, |
| 93 | + fixHint: `Remove channels.denyRules[${index}].${unsupportedRuleKey} or use id, when.provider, and reason.`, |
| 94 | + }, |
| 95 | + ]; |
| 96 | + } |
| 97 | + if (isRecord(rule.when)) { |
| 98 | + const unsupportedWhenKey = unsupportedPolicyKey(rule.when, ["provider"]); |
| 99 | + if (unsupportedWhenKey !== undefined) { |
| 100 | + return [ |
| 101 | + { |
| 102 | + checkId: CHECK_IDS.policyInvalidFile, |
| 103 | + severity: "error", |
| 104 | + message: `${policyPath} channels.denyRules[${index}].when.${unsupportedWhenKey} is not supported in channel deny rules.`, |
| 105 | + source: "policy", |
| 106 | + path: policyPath, |
| 107 | + target: `oc://${policyDocName}/channels/denyRules/#${index}/when/${ocPathSegment(unsupportedWhenKey)}`, |
| 108 | + fixHint: `Remove channels.denyRules[${index}].when.${unsupportedWhenKey} or use when.provider.`, |
| 109 | + }, |
| 110 | + ]; |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + const invalid = policy.channels.denyRules.findIndex((rule) => !isChannelDenyRule(rule)); |
| 115 | + if (invalid < 0) { |
| 116 | + return []; |
| 117 | + } |
| 118 | + return [ |
| 119 | + { |
| 120 | + checkId: CHECK_IDS.policyInvalidFile, |
| 121 | + severity: "error", |
| 122 | + message: `${policyPath} channels.denyRules[${invalid}] must define when.provider as a string.`, |
| 123 | + source: "policy", |
| 124 | + path: policyPath, |
| 125 | + target: `oc://${policyDocName}/channels/denyRules/#${invalid}`, |
| 126 | + fixHint: `Fix ${policyPath} so each channel deny rule has a provider match.`, |
| 127 | + }, |
| 128 | + ]; |
| 129 | +} |
0 commit comments