@clawbureau/clawverify-core 0.1.0 → 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.
- package/README.md +12 -0
- package/dist/badge-health.d.ts +40 -0
- package/dist/badge-health.d.ts.map +1 -0
- package/dist/badge-health.js +56 -0
- package/dist/badge-health.js.map +1 -0
- package/dist/compliance.d.ts +106 -0
- package/dist/compliance.d.ts.map +1 -0
- package/dist/compliance.js +356 -0
- package/dist/compliance.js.map +1 -0
- package/dist/hashcash.d.ts +44 -0
- package/dist/hashcash.d.ts.map +1 -0
- package/dist/hashcash.js +97 -0
- package/dist/hashcash.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -1
- package/dist/policy-evaluator.d.ts +119 -0
- package/dist/policy-evaluator.d.ts.map +1 -0
- package/dist/policy-evaluator.js +452 -0
- package/dist/policy-evaluator.js.map +1 -0
- package/dist/schema-validators.generated.d.ts.map +1 -1
- package/dist/schema-validators.generated.js +2121 -1102
- package/dist/schema-validators.generated.js.map +1 -1
- package/dist/trace-compiler.d.ts +7 -0
- package/dist/trace-compiler.d.ts.map +1 -0
- package/dist/trace-compiler.js +46 -0
- package/dist/trace-compiler.js.map +1 -0
- package/dist/types.d.ts +68 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/verify-causal-integrity.d.ts +68 -0
- package/dist/verify-causal-integrity.d.ts.map +1 -0
- package/dist/verify-causal-integrity.js +186 -0
- package/dist/verify-causal-integrity.js.map +1 -0
- package/package.json +2 -2
package/dist/hashcash.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* hashcash.ts — Proof-of-Work for VaaS DoS protection
|
|
3
|
+
*
|
|
4
|
+
* Red Team Fix #8: Requires unauthenticated API callers to present a
|
|
5
|
+
* valid PoW token, making volumetric DoS economically infeasible.
|
|
6
|
+
*
|
|
7
|
+
* Uses WebCrypto (crypto.subtle) for Cloudflare Workers compatibility.
|
|
8
|
+
*
|
|
9
|
+
* Challenge format: `${dateHourUTC}:${clientIdentifier}`
|
|
10
|
+
* The client must find a nonce such that SHA-256(challenge + ":" + nonce)
|
|
11
|
+
* has `difficulty` leading zero hex characters.
|
|
12
|
+
*
|
|
13
|
+
* Default difficulty: 4 (approx 65,536 attempts, ~100ms on modern hardware).
|
|
14
|
+
*/
|
|
15
|
+
/** Default number of leading zero hex chars required. */
|
|
16
|
+
export const DEFAULT_POW_DIFFICULTY = 4;
|
|
17
|
+
/**
|
|
18
|
+
* Generate the current date-hour challenge component.
|
|
19
|
+
* Format: YYYYMMDDHH (UTC).
|
|
20
|
+
*/
|
|
21
|
+
export function getDateHourUTC(date) {
|
|
22
|
+
const d = date ?? new Date();
|
|
23
|
+
const year = d.getUTCFullYear();
|
|
24
|
+
const month = String(d.getUTCMonth() + 1).padStart(2, '0');
|
|
25
|
+
const day = String(d.getUTCDate()).padStart(2, '0');
|
|
26
|
+
const hour = String(d.getUTCHours()).padStart(2, '0');
|
|
27
|
+
return `${year}${month}${day}${hour}`;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Build a challenge string from the date-hour and client identifier.
|
|
31
|
+
*/
|
|
32
|
+
export function buildChallenge(clientIdentifier, date) {
|
|
33
|
+
return `${getDateHourUTC(date)}:${clientIdentifier}`;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Compute SHA-256 of a string and return hex digest.
|
|
37
|
+
* Uses WebCrypto for CF Workers compatibility.
|
|
38
|
+
*/
|
|
39
|
+
async function sha256Hex(input) {
|
|
40
|
+
const data = new TextEncoder().encode(input);
|
|
41
|
+
const hashBuffer = await crypto.subtle.digest('SHA-256', data);
|
|
42
|
+
const hashArray = new Uint8Array(hashBuffer);
|
|
43
|
+
return Array.from(hashArray)
|
|
44
|
+
.map((b) => b.toString(16).padStart(2, '0'))
|
|
45
|
+
.join('');
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Check if a hex string has the required number of leading zero characters.
|
|
49
|
+
*/
|
|
50
|
+
function hasLeadingZeros(hex, difficulty) {
|
|
51
|
+
if (hex.length < difficulty)
|
|
52
|
+
return false;
|
|
53
|
+
for (let i = 0; i < difficulty; i++) {
|
|
54
|
+
if (hex[i] !== '0')
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Find a nonce such that SHA-256(challenge + ":" + nonce) has `difficulty`
|
|
61
|
+
* leading zero hex characters.
|
|
62
|
+
*
|
|
63
|
+
* @param challenge The challenge string (e.g. "2026021223:192.168.1.1")
|
|
64
|
+
* @param difficulty Number of leading zero hex chars required (default: 4)
|
|
65
|
+
* @returns The nonce string that satisfies the PoW
|
|
66
|
+
*/
|
|
67
|
+
export async function generatePoW(challenge, difficulty = DEFAULT_POW_DIFFICULTY) {
|
|
68
|
+
if (difficulty < 1 || difficulty > 16) {
|
|
69
|
+
throw new Error(`PoW difficulty must be between 1 and 16, got ${difficulty}`);
|
|
70
|
+
}
|
|
71
|
+
for (let nonce = 0;; nonce++) {
|
|
72
|
+
const candidate = `${challenge}:${nonce}`;
|
|
73
|
+
const hex = await sha256Hex(candidate);
|
|
74
|
+
if (hasLeadingZeros(hex, difficulty)) {
|
|
75
|
+
return String(nonce);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Verify that a nonce satisfies the PoW requirement.
|
|
81
|
+
*
|
|
82
|
+
* @param challenge The challenge string
|
|
83
|
+
* @param nonce The claimed nonce
|
|
84
|
+
* @param difficulty Number of leading zero hex chars required (default: 4)
|
|
85
|
+
* @returns true if the PoW is valid
|
|
86
|
+
*/
|
|
87
|
+
export async function verifyPoW(challenge, nonce, difficulty = DEFAULT_POW_DIFFICULTY) {
|
|
88
|
+
if (difficulty < 1 || difficulty > 16)
|
|
89
|
+
return false;
|
|
90
|
+
// Reject non-numeric or excessively long nonces
|
|
91
|
+
if (!/^\d{1,15}$/.test(nonce))
|
|
92
|
+
return false;
|
|
93
|
+
const candidate = `${challenge}:${nonce}`;
|
|
94
|
+
const hex = await sha256Hex(candidate);
|
|
95
|
+
return hasLeadingZeros(hex, difficulty);
|
|
96
|
+
}
|
|
97
|
+
//# sourceMappingURL=hashcash.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hashcash.js","sourceRoot":"","sources":["../src/hashcash.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,yDAAyD;AACzD,MAAM,CAAC,MAAM,sBAAsB,GAAG,CAAC,CAAC;AAExC;;;GAGG;AACH,MAAM,UAAU,cAAc,CAAC,IAAW;IACxC,MAAM,CAAC,GAAG,IAAI,IAAI,IAAI,IAAI,EAAE,CAAC;IAC7B,MAAM,IAAI,GAAG,CAAC,CAAC,cAAc,EAAE,CAAC;IAChC,MAAM,KAAK,GAAG,MAAM,CAAC,CAAC,CAAC,WAAW,EAAE,GAAG,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;IAC3D,MAAM,GAAG,GAAG,MAAM,CAAC,CAAC,CAAC,UAAU,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;IACpD,MAAM,IAAI,GAAG,MAAM,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;IACtD,OAAO,GAAG,IAAI,GAAG,KAAK,GAAG,GAAG,GAAG,IAAI,EAAE,CAAC;AACxC,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,cAAc,CAC5B,gBAAwB,EACxB,IAAW;IAEX,OAAO,GAAG,cAAc,CAAC,IAAI,CAAC,IAAI,gBAAgB,EAAE,CAAC;AACvD,CAAC;AAED;;;GAGG;AACH,KAAK,UAAU,SAAS,CAAC,KAA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eAAe,CAAC,GAAG,EAAE,UAAU,CAAC,EAAE,CAAC;YACrC,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC;QACvB,CAAC;IACH,CAAC;AACH,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,SAAS,CAC7B,SAAiB,EACjB,KAAa,EACb,aAAqB,sBAAsB;IAE3C,IAAI,UAAU,GAAG,CAAC,IAAI,UAAU,GAAG,EAAE;QAAE,OAAO,KAAK,CAAC;IAEpD,gDAAgD;IAChD,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,KAAK,CAAC;QAAE,OAAO,KAAK,CAAC;IAE5C,MAAM,SAAS,GAAG,GAAG,SAAS,IAAI,KAAK,EAAE,CAAC;IAC1C,MAAM,GAAG,GAAG,MAAM,SAAS,CAAC,SAAS,CAAC,CAAC;IACvC,OAAO,eAAe,CAAC,GAAG,EAAE,UAAU,CAAC,CAAC;AAC1C,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -24,4 +24,14 @@ export { verifyAuditResultAttestation } from './verify-audit-result-attestation.
|
|
|
24
24
|
export { verifyLogInclusionProof } from './verify-log-inclusion-proof.js';
|
|
25
25
|
export { base64UrlDecode, base64UrlEncode, computeHash, extractPublicKeyFromDidKey, verifySignature, } from './crypto.js';
|
|
26
26
|
export { jcsCanonicalize } from './jcs.js';
|
|
27
|
+
export { mapToSOC2, mapToISO27001, mapToEUAIAct, generateComplianceReport, } from './compliance.js';
|
|
28
|
+
export type { ComplianceFramework, ControlStatus, EvidenceType, ControlResult, ComplianceGap, ComplianceReport, ComplianceBundleInput, CompliancePolicyInput, } from './compliance.js';
|
|
29
|
+
export { evaluatePolicy, evaluatePolicyBatch, convertV1toV2, } from './policy-evaluator.js';
|
|
30
|
+
export type { WPCv1, WPCv2, WPC, PolicyStatement, PolicyConditions, ConditionMap, PolicyContext, PolicyDecision, PolicyDecisionEffect, PolicyResolver, } from './policy-evaluator.js';
|
|
31
|
+
export { verifyCausalIntegrity } from './verify-causal-integrity.js';
|
|
32
|
+
export type { CausalIntegrityBundleInput, CausalIntegritySeverity, CausalIntegrityFinding, CausalIntegrityResult, } from './verify-causal-integrity.js';
|
|
33
|
+
export { generatePoW, verifyPoW, buildChallenge, getDateHourUTC, DEFAULT_POW_DIFFICULTY, } from './hashcash.js';
|
|
34
|
+
export { computeBadgeStatus } from './badge-health.js';
|
|
35
|
+
export type { BadgeColor, BadgeStats, BadgeStatus, } from './badge-health.js';
|
|
36
|
+
export { compileSemanticTrace } from './trace-compiler.js';
|
|
27
37
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,cAAc,YAAY,CAAC;AAE3B,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAC7D,YAAY,EAAE,0BAA0B,EAAE,MAAM,0BAA0B,CAAC;AAE3E,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAC/D,YAAY,EAAE,yBAAyB,EAAE,MAAM,2BAA2B,CAAC;AAE3E,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,YAAY,EAAE,sBAAsB,EAAE,MAAM,qBAAqB,CAAC;AAElE,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAE3D,OAAO,EAAE,0BAA0B,EAAE,MAAM,mCAAmC,CAAC;AAC/E,OAAO,EAAE,2BAA2B,EAAE,MAAM,oCAAoC,CAAC;AACjF,OAAO,EAAE,4BAA4B,EAAE,MAAM,sCAAsC,CAAC;AACpF,OAAO,EAAE,uBAAuB,EAAE,MAAM,iCAAiC,CAAC;AAE1E,OAAO,EACL,eAAe,EACf,eAAe,EACf,WAAW,EACX,0BAA0B,EAC1B,eAAe,GAChB,MAAM,aAAa,CAAC;AAErB,OAAO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,GAAG,EACH,eAAe,EACf,gBAAgB,EAChB,YAAY,EACZ,aAAa,EACb,cAAc,EACd,oBAAoB,EACpB,cAAc,GACf,MAAM,uBAAuB,CAAC;AAG/B,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AACrE,YAAY,EACV,0BAA0B,EAC1B,uBAAuB,EACvB,sBAAsB,EACtB,qBAAqB,GACtB,MAAM,8BAA8B,CAAC;AAGtC,OAAO,EACL,WAAW,EACX,SAAS,EACT,cAAc,EACd,cAAc,EACd,sBAAsB,GACvB,MAAM,eAAe,CAAC;AAGvB,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AACvD,YAAY,EACV,UAAU,EACV,UAAU,EACV,WAAW,GACZ,MAAM,mBAAmB,CAAC;AAG3B,OAAO,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -21,4 +21,14 @@ export { verifyAuditResultAttestation } from './verify-audit-result-attestation.
|
|
|
21
21
|
export { verifyLogInclusionProof } from './verify-log-inclusion-proof.js';
|
|
22
22
|
export { base64UrlDecode, base64UrlEncode, computeHash, extractPublicKeyFromDidKey, verifySignature, } from './crypto.js';
|
|
23
23
|
export { jcsCanonicalize } from './jcs.js';
|
|
24
|
+
export { mapToSOC2, mapToISO27001, mapToEUAIAct, generateComplianceReport, } from './compliance.js';
|
|
25
|
+
export { evaluatePolicy, evaluatePolicyBatch, convertV1toV2, } from './policy-evaluator.js';
|
|
26
|
+
// Red Team Fix #11: TOCTOU causal integrity verification
|
|
27
|
+
export { verifyCausalIntegrity } from './verify-causal-integrity.js';
|
|
28
|
+
// Red Team Fix #8: Hashcash PoW for VaaS DoS protection
|
|
29
|
+
export { generatePoW, verifyPoW, buildChallenge, getDateHourUTC, DEFAULT_POW_DIFFICULTY, } from './hashcash.js';
|
|
30
|
+
// Red Team Fix #9: Heartbeat Badge status computation
|
|
31
|
+
export { computeBadgeStatus } from './badge-health.js';
|
|
32
|
+
// Sentinel trace compiler
|
|
33
|
+
export { compileSemanticTrace } from './trace-compiler.js';
|
|
24
34
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,cAAc,YAAY,CAAC;AAE3B,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAG7D,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAG/D,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAGpD,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAE3D,OAAO,EAAE,0BAA0B,EAAE,MAAM,mCAAmC,CAAC;AAC/E,OAAO,EAAE,2BAA2B,EAAE,MAAM,oCAAoC,CAAC;AACjF,OAAO,EAAE,4BAA4B,EAAE,MAAM,sCAAsC,CAAC;AACpF,OAAO,EAAE,uBAAuB,EAAE,MAAM,iCAAiC,CAAC;AAE1E,OAAO,EACL,eAAe,EACf,eAAe,EACf,WAAW,EACX,0BAA0B,EAC1B,eAAe,GAChB,MAAM,aAAa,CAAC;AAErB,OAAO,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,cAAc,YAAY,CAAC;AAE3B,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAG7D,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAG/D,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAGpD,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAE3D,OAAO,EAAE,0BAA0B,EAAE,MAAM,mCAAmC,CAAC;AAC/E,OAAO,EAAE,2BAA2B,EAAE,MAAM,oCAAoC,CAAC;AACjF,OAAO,EAAE,4BAA4B,EAAE,MAAM,sCAAsC,CAAC;AACpF,OAAO,EAAE,uBAAuB,EAAE,MAAM,iCAAiC,CAAC;AAE1E,OAAO,EACL,eAAe,EACf,eAAe,EACf,WAAW,EACX,0BAA0B,EAC1B,eAAe,GAChB,MAAM,aAAa,CAAC;AAErB,OAAO,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAE3C,OAAO,EACL,SAAS,EACT,aAAa,EACb,YAAY,EACZ,wBAAwB,GACzB,MAAM,iBAAiB,CAAC;AAYzB,OAAO,EACL,cAAc,EACd,mBAAmB,EACnB,aAAa,GACd,MAAM,uBAAuB,CAAC;AAc/B,yDAAyD;AACzD,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AAQrE,wDAAwD;AACxD,OAAO,EACL,WAAW,EACX,SAAS,EACT,cAAc,EACd,cAAc,EACd,sBAAsB,GACvB,MAAM,eAAe,CAAC;AAEvB,sDAAsD;AACtD,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAOvD,0BAA0B;AAC1B,OAAO,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC"}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WPC v2 Policy Evaluator — IAM-style policy engine.
|
|
3
|
+
*
|
|
4
|
+
* Evaluation rules (matching AWS IAM semantics):
|
|
5
|
+
* 1. Default deny: if no statement explicitly allows, the action is denied.
|
|
6
|
+
* 2. Explicit Deny always wins over Allow.
|
|
7
|
+
* 3. Strict Intersection: when `inherits` is set, parent AND child must both allow.
|
|
8
|
+
*
|
|
9
|
+
* Pure TypeScript, zero external dependencies, deterministic, offline.
|
|
10
|
+
* Designed to run in Cloudflare Workers (<5ms for 20 statements).
|
|
11
|
+
*/
|
|
12
|
+
/** WPC v1 schema (for backward compatibility). */
|
|
13
|
+
export interface WPCv1 {
|
|
14
|
+
policy_version: '1';
|
|
15
|
+
policy_id: string;
|
|
16
|
+
issuer_did: string;
|
|
17
|
+
allowed_providers?: string[];
|
|
18
|
+
allowed_models?: string[];
|
|
19
|
+
minimum_model_identity_tier?: string;
|
|
20
|
+
egress_allowlist?: string[];
|
|
21
|
+
redaction_rules?: unknown[];
|
|
22
|
+
receipt_privacy_mode?: string;
|
|
23
|
+
required_audit_packs?: string[];
|
|
24
|
+
metadata?: Record<string, unknown>;
|
|
25
|
+
}
|
|
26
|
+
/** Condition operator map: context-key -> expected value. */
|
|
27
|
+
export type ConditionMap = Record<string, string>;
|
|
28
|
+
/** Condition operators supported by the evaluator. */
|
|
29
|
+
export interface PolicyConditions {
|
|
30
|
+
StringEquals?: ConditionMap;
|
|
31
|
+
StringNotEquals?: ConditionMap;
|
|
32
|
+
StringLike?: ConditionMap;
|
|
33
|
+
StringNotLike?: ConditionMap;
|
|
34
|
+
NumericEquals?: ConditionMap;
|
|
35
|
+
NumericLessThan?: ConditionMap;
|
|
36
|
+
NumericGreaterThan?: ConditionMap;
|
|
37
|
+
Bool?: ConditionMap;
|
|
38
|
+
IpAddress?: ConditionMap;
|
|
39
|
+
}
|
|
40
|
+
/** A single IAM-style policy statement. */
|
|
41
|
+
export interface PolicyStatement {
|
|
42
|
+
sid: string;
|
|
43
|
+
effect: 'Allow' | 'Deny';
|
|
44
|
+
actions: string[];
|
|
45
|
+
resources: string[];
|
|
46
|
+
conditions?: PolicyConditions;
|
|
47
|
+
}
|
|
48
|
+
/** WPC v2 schema. */
|
|
49
|
+
export interface WPCv2 {
|
|
50
|
+
policy_version: '2';
|
|
51
|
+
policy_id: string;
|
|
52
|
+
issuer_did: string;
|
|
53
|
+
inherits?: string;
|
|
54
|
+
statements: PolicyStatement[];
|
|
55
|
+
metadata?: Record<string, unknown>;
|
|
56
|
+
}
|
|
57
|
+
/** Union type for any WPC version. */
|
|
58
|
+
export type WPC = WPCv1 | WPCv2;
|
|
59
|
+
/** Context keys available during policy evaluation. */
|
|
60
|
+
export interface PolicyContext {
|
|
61
|
+
/** Agent's DID (Agent:DID) */
|
|
62
|
+
'Agent:DID'?: string;
|
|
63
|
+
/** Model provider (Model:Provider) */
|
|
64
|
+
'Model:Provider'?: string;
|
|
65
|
+
/** Model name (Model:Name) */
|
|
66
|
+
'Model:Name'?: string;
|
|
67
|
+
/** Proof tier from receipt (Receipt:ProofTier) */
|
|
68
|
+
'Receipt:ProofTier'?: string;
|
|
69
|
+
/** Whether human approval was obtained (Request:HasHumanApproval) */
|
|
70
|
+
'Request:HasHumanApproval'?: string;
|
|
71
|
+
/** Target domain for network egress (SideEffect:TargetDomain) */
|
|
72
|
+
'SideEffect:TargetDomain'?: string;
|
|
73
|
+
/** Tool name being invoked (Tool:Name) */
|
|
74
|
+
'Tool:Name'?: string;
|
|
75
|
+
/** Hour of day 0-23 (Context:Hour) */
|
|
76
|
+
'Context:Hour'?: string;
|
|
77
|
+
/** Allow additional context keys. */
|
|
78
|
+
[key: string]: string | undefined;
|
|
79
|
+
}
|
|
80
|
+
export type PolicyDecisionEffect = 'ALLOW' | 'DENY';
|
|
81
|
+
/** Result of evaluating a policy against an action + context. */
|
|
82
|
+
export interface PolicyDecision {
|
|
83
|
+
effect: PolicyDecisionEffect;
|
|
84
|
+
/** Human-readable reason for the decision. */
|
|
85
|
+
reason: string;
|
|
86
|
+
/** Statement IDs that contributed to the decision. */
|
|
87
|
+
matched_statements: string[];
|
|
88
|
+
}
|
|
89
|
+
/** Resolver for parent policies (used in Strict Intersection). */
|
|
90
|
+
export type PolicyResolver = (policyId: string) => WPCv2 | WPC | null;
|
|
91
|
+
/**
|
|
92
|
+
* Convert a WPC v1 to v2 statements for evaluation.
|
|
93
|
+
* This mirrors the logic in the migration helper but is kept inline
|
|
94
|
+
* to avoid circular deps.
|
|
95
|
+
*/
|
|
96
|
+
export declare function convertV1toV2(v1: WPCv1): WPCv2;
|
|
97
|
+
/**
|
|
98
|
+
* Evaluate a WPC policy against an action, resource, and context.
|
|
99
|
+
*
|
|
100
|
+
* Supports both WPC v1 (auto-converted) and v2 policies.
|
|
101
|
+
* When `inherits` is set and a `resolver` is provided, performs
|
|
102
|
+
* Strict Intersection (parent AND child must both allow).
|
|
103
|
+
*
|
|
104
|
+
* @param policy - The WPC policy (v1 or v2)
|
|
105
|
+
* @param action - The action being requested (e.g., "model:invoke")
|
|
106
|
+
* @param resource - The resource being acted upon (e.g., "src/index.ts")
|
|
107
|
+
* @param context - Context keys from the proof bundle / runtime
|
|
108
|
+
* @param resolver - Optional resolver for parent policies (Strict Intersection)
|
|
109
|
+
*/
|
|
110
|
+
export declare function evaluatePolicy(policy: WPC, action: string, resource: string, context: PolicyContext, resolver?: PolicyResolver): PolicyDecision;
|
|
111
|
+
/**
|
|
112
|
+
* Convenience: evaluate multiple actions against a policy.
|
|
113
|
+
* Returns a map of action -> PolicyDecision.
|
|
114
|
+
*/
|
|
115
|
+
export declare function evaluatePolicyBatch(policy: WPC, requests: Array<{
|
|
116
|
+
action: string;
|
|
117
|
+
resource: string;
|
|
118
|
+
}>, context: PolicyContext, resolver?: PolicyResolver): Map<string, PolicyDecision>;
|
|
119
|
+
//# sourceMappingURL=policy-evaluator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"policy-evaluator.d.ts","sourceRoot":"","sources":["../src/policy-evaluator.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAMH,kDAAkD;AAClD,MAAM,WAAW,KAAK;IACpB,cAAc,EAAE,GAAG,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE,MAAM,CAAC;IACnB,iBAAiB,CAAC,EAAE,MAAM,EAAE,CAAC;IAC7B,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;IAC1B,2BAA2B,CAAC,EAAE,MAAM,CAAC;IACrC,gBAAgB,CAAC,EAAE,MAAM,EAAE,CAAC;IAC5B,eAAe,CAAC,EAAE,OAAO,EAAE,CAAC;IAC5B,oBAAoB,C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