sigild 0.0.11 → 0.0.13
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 +43 -13
- package/SECURITY.md +69 -0
- package/THREAT_MODEL.md +5 -3
- package/dist/src/audit/index.d.ts +2 -1
- package/dist/src/audit/index.d.ts.map +1 -1
- package/dist/src/audit/index.js +1 -0
- package/dist/src/audit/index.js.map +1 -1
- package/dist/src/audit/log.d.ts +37 -3
- package/dist/src/audit/log.d.ts.map +1 -1
- package/dist/src/audit/log.js +227 -41
- package/dist/src/audit/log.js.map +1 -1
- package/dist/src/bin/sigil-mcp.js +23 -6
- package/dist/src/bin/sigil-mcp.js.map +1 -1
- package/dist/src/cli/args.d.ts.map +1 -1
- package/dist/src/cli/args.js.map +1 -1
- package/dist/src/cli/index.d.ts +1 -1
- package/dist/src/cli/index.d.ts.map +1 -1
- package/dist/src/cli/index.js +1 -1
- package/dist/src/cli/index.js.map +1 -1
- package/dist/src/cli/main.d.ts.map +1 -1
- package/dist/src/cli/main.js +76 -4
- package/dist/src/cli/main.js.map +1 -1
- package/dist/src/cli/paths.d.ts +2 -0
- package/dist/src/cli/paths.d.ts.map +1 -1
- package/dist/src/cli/paths.js +1 -0
- package/dist/src/cli/paths.js.map +1 -1
- package/dist/src/cli/portal.d.ts.map +1 -1
- package/dist/src/cli/portal.js +17 -3
- package/dist/src/cli/portal.js.map +1 -1
- package/dist/src/cli/rpc.d.ts +24 -0
- package/dist/src/cli/rpc.d.ts.map +1 -0
- package/dist/src/cli/rpc.js +68 -0
- package/dist/src/cli/rpc.js.map +1 -0
- package/dist/src/cli/status.d.ts.map +1 -1
- package/dist/src/cli/status.js +1 -1
- package/dist/src/cli/status.js.map +1 -1
- package/dist/src/cli/unlock.d.ts.map +1 -1
- package/dist/src/cli/unlock.js +1 -1
- package/dist/src/cli/unlock.js.map +1 -1
- package/dist/src/confirm/config.d.ts.map +1 -1
- package/dist/src/confirm/config.js.map +1 -1
- package/dist/src/confirm/index.d.ts +2 -2
- package/dist/src/confirm/index.d.ts.map +1 -1
- package/dist/src/confirm/index.js +1 -1
- package/dist/src/confirm/index.js.map +1 -1
- package/dist/src/confirm/ntfy.d.ts.map +1 -1
- package/dist/src/confirm/ntfy.js +5 -5
- package/dist/src/confirm/ntfy.js.map +1 -1
- package/dist/src/control/broadcast.d.ts.map +1 -1
- package/dist/src/control/broadcast.js +7 -4
- package/dist/src/control/broadcast.js.map +1 -1
- package/dist/src/control/client.d.ts.map +1 -1
- package/dist/src/control/client.js +5 -2
- package/dist/src/control/client.js.map +1 -1
- package/dist/src/control/protocol.d.ts.map +1 -1
- package/dist/src/control/protocol.js.map +1 -1
- package/dist/src/control/server.d.ts.map +1 -1
- package/dist/src/control/server.js +17 -4
- package/dist/src/control/server.js.map +1 -1
- package/dist/src/crypto/aead.d.ts.map +1 -1
- package/dist/src/crypto/aead.js.map +1 -1
- package/dist/src/crypto/index.d.ts +1 -1
- package/dist/src/crypto/index.d.ts.map +1 -1
- package/dist/src/crypto/index.js +1 -1
- package/dist/src/crypto/index.js.map +1 -1
- package/dist/src/crypto/keyfile.d.ts.map +1 -1
- package/dist/src/crypto/keyfile.js +1 -0
- package/dist/src/crypto/keyfile.js.map +1 -1
- package/dist/src/daemon/handles.d.ts.map +1 -1
- package/dist/src/daemon/handles.js +1 -1
- package/dist/src/daemon/handles.js.map +1 -1
- package/dist/src/daemon/index.d.ts +1 -1
- package/dist/src/daemon/index.d.ts.map +1 -1
- package/dist/src/daemon/index.js +1 -1
- package/dist/src/daemon/index.js.map +1 -1
- package/dist/src/daemon/methods.d.ts +8 -1
- package/dist/src/daemon/methods.d.ts.map +1 -1
- package/dist/src/daemon/methods.js +133 -13
- package/dist/src/daemon/methods.js.map +1 -1
- package/dist/src/daemon/passphrase.d.ts.map +1 -1
- package/dist/src/daemon/passphrase.js.map +1 -1
- package/dist/src/eth/address.d.ts.map +1 -1
- package/dist/src/eth/address.js +1 -1
- package/dist/src/eth/address.js.map +1 -1
- package/dist/src/eth/index.d.ts +2 -2
- package/dist/src/eth/index.d.ts.map +1 -1
- package/dist/src/eth/index.js +2 -2
- package/dist/src/eth/index.js.map +1 -1
- package/dist/src/eth/rlp.js +1 -1
- package/dist/src/eth/rlp.js.map +1 -1
- package/dist/src/eth/secp.d.ts.map +1 -1
- package/dist/src/eth/secp.js.map +1 -1
- package/dist/src/eth/sign-message.d.ts.map +1 -1
- package/dist/src/eth/sign-message.js.map +1 -1
- package/dist/src/eth/sign-tx.d.ts.map +1 -1
- package/dist/src/eth/sign-tx.js +13 -4
- package/dist/src/eth/sign-tx.js.map +1 -1
- package/dist/src/eth/sign-typed.d.ts.map +1 -1
- package/dist/src/eth/sign-typed.js.map +1 -1
- package/dist/src/fs/index.d.ts +2 -0
- package/dist/src/fs/index.d.ts.map +1 -0
- package/dist/src/fs/index.js +2 -0
- package/dist/src/fs/index.js.map +1 -0
- package/dist/src/fs/lock.d.ts +84 -0
- package/dist/src/fs/lock.d.ts.map +1 -0
- package/dist/src/fs/lock.js +329 -0
- package/dist/src/fs/lock.js.map +1 -0
- package/dist/src/hooks/bip39-wordlist.d.ts +12 -0
- package/dist/src/hooks/bip39-wordlist.d.ts.map +1 -0
- package/dist/src/hooks/bip39-wordlist.js +2061 -0
- package/dist/src/hooks/bip39-wordlist.js.map +1 -0
- package/dist/src/hooks/bip39.d.ts +19 -0
- package/dist/src/hooks/bip39.d.ts.map +1 -0
- package/dist/src/hooks/bip39.js +101 -0
- package/dist/src/hooks/bip39.js.map +1 -0
- package/dist/src/hooks/command-scanner.d.ts.map +1 -1
- package/dist/src/hooks/command-scanner.js +45 -9
- package/dist/src/hooks/command-scanner.js.map +1 -1
- package/dist/src/hooks/index.d.ts +1 -1
- package/dist/src/hooks/index.d.ts.map +1 -1
- package/dist/src/hooks/index.js +1 -1
- package/dist/src/hooks/index.js.map +1 -1
- package/dist/src/hooks/install.js +1 -1
- package/dist/src/hooks/install.js.map +1 -1
- package/dist/src/hooks/protocol.d.ts.map +1 -1
- package/dist/src/hooks/protocol.js +1 -1
- package/dist/src/hooks/protocol.js.map +1 -1
- package/dist/src/hooks/redactor.d.ts +10 -1
- package/dist/src/hooks/redactor.d.ts.map +1 -1
- package/dist/src/hooks/redactor.js +34 -3
- package/dist/src/hooks/redactor.js.map +1 -1
- package/dist/src/mcp/protocol.d.ts.map +1 -1
- package/dist/src/mcp/protocol.js +5 -1
- package/dist/src/mcp/protocol.js.map +1 -1
- package/dist/src/mcp/tools.d.ts.map +1 -1
- package/dist/src/mcp/tools.js +6 -3
- package/dist/src/mcp/tools.js.map +1 -1
- package/dist/src/policy/evaluate.d.ts.map +1 -1
- package/dist/src/policy/evaluate.js +71 -6
- package/dist/src/policy/evaluate.js.map +1 -1
- package/dist/src/policy/index.d.ts +3 -1
- package/dist/src/policy/index.d.ts.map +1 -1
- package/dist/src/policy/index.js +3 -1
- package/dist/src/policy/index.js.map +1 -1
- package/dist/src/policy/ledger.d.ts +83 -0
- package/dist/src/policy/ledger.d.ts.map +1 -0
- package/dist/src/policy/ledger.js +250 -0
- package/dist/src/policy/ledger.js.map +1 -0
- package/dist/src/policy/loader.d.ts.map +1 -1
- package/dist/src/policy/loader.js +45 -0
- package/dist/src/policy/loader.js.map +1 -1
- package/dist/src/policy/template.d.ts +2 -2
- package/dist/src/policy/template.d.ts.map +1 -1
- package/dist/src/policy/template.js +32 -0
- package/dist/src/policy/template.js.map +1 -1
- package/dist/src/policy/types.d.ts +26 -0
- package/dist/src/policy/types.d.ts.map +1 -1
- package/dist/src/policy/types.js.map +1 -1
- package/dist/src/policy/window.d.ts +22 -0
- package/dist/src/policy/window.d.ts.map +1 -0
- package/dist/src/policy/window.js +66 -0
- package/dist/src/policy/window.js.map +1 -0
- package/dist/src/qr/encode.d.ts +10 -1
- package/dist/src/qr/encode.d.ts.map +1 -1
- package/dist/src/qr/encode.js +2 -2
- package/dist/src/qr/encode.js.map +1 -1
- package/dist/src/qr/index.d.ts +1 -1
- package/dist/src/qr/index.d.ts.map +1 -1
- package/dist/src/qr/index.js.map +1 -1
- package/dist/src/rpc/fill.d.ts.map +1 -1
- package/dist/src/rpc/fill.js.map +1 -1
- package/dist/src/rpc/server.d.ts.map +1 -1
- package/dist/src/rpc/server.js +4 -2
- package/dist/src/rpc/server.js.map +1 -1
- package/dist/src/rpc/upstream.d.ts.map +1 -1
- package/dist/src/rpc/upstream.js.map +1 -1
- package/dist/src/svm/transaction.d.ts.map +1 -1
- package/dist/src/svm/transaction.js +13 -4
- package/dist/src/svm/transaction.js.map +1 -1
- package/package.json +16 -7
|
@@ -47,13 +47,15 @@ export function evaluate(request, policy) {
|
|
|
47
47
|
kind: 'deny',
|
|
48
48
|
reason: 'personal_sign denied — strict mode + allow_message_signing=false',
|
|
49
49
|
};
|
|
50
|
-
case 'typed_data':
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
: {
|
|
50
|
+
case 'typed_data': {
|
|
51
|
+
if (!policy.allowTypedData) {
|
|
52
|
+
return {
|
|
54
53
|
kind: 'deny',
|
|
55
54
|
reason: 'EIP-712 typed-data denied — strict mode + allow_typed_data=false',
|
|
56
55
|
};
|
|
56
|
+
}
|
|
57
|
+
return evaluateTypedDataStrict(request.typedData, policy) ?? { kind: 'allow' };
|
|
58
|
+
}
|
|
57
59
|
case 'svm_message':
|
|
58
60
|
return policy.allowSvmMessageSigning
|
|
59
61
|
? { kind: 'allow' }
|
|
@@ -180,7 +182,7 @@ function evaluateTransactionStrict(tx, policy) {
|
|
|
180
182
|
};
|
|
181
183
|
}
|
|
182
184
|
// 5. function selector allowlist (only for txs with calldata)
|
|
183
|
-
const dataHex = typeof tx.data === 'string' ? tx.data :
|
|
185
|
+
const dataHex = typeof tx.data === 'string' ? tx.data : '0x' + tx.data.toString('hex');
|
|
184
186
|
if (dataHex.length > 2) {
|
|
185
187
|
if (dataHex.length < 10) {
|
|
186
188
|
return { kind: 'deny', reason: `tx denied — calldata too short to extract selector` };
|
|
@@ -220,7 +222,7 @@ function txSummary(tx) {
|
|
|
220
222
|
return `${formatWei(BigInt(tx.value))} ETH → ${dest} on chain ${BigInt(tx.chainId)}`;
|
|
221
223
|
}
|
|
222
224
|
function initcodeBytes(tx) {
|
|
223
|
-
const dataHex = typeof tx.data === 'string' ? tx.data :
|
|
225
|
+
const dataHex = typeof tx.data === 'string' ? tx.data : '0x' + tx.data.toString('hex');
|
|
224
226
|
return (dataHex.length - 2) / 2;
|
|
225
227
|
}
|
|
226
228
|
function shortAddr(addr) {
|
|
@@ -238,4 +240,67 @@ function formatWei(v) {
|
|
|
238
240
|
const fracStr = frac.toString().padStart(18, '0').slice(0, 6).replace(/0+$/, '');
|
|
239
241
|
return fracStr === '' ? whole.toString() : `${whole}.${fracStr}`;
|
|
240
242
|
}
|
|
243
|
+
/**
|
|
244
|
+
* Strict-mode EIP-712 checks, applied once allow_typed_data has passed.
|
|
245
|
+
* A typed-data signature can move funds (Permit, Permit2, exchange orders),
|
|
246
|
+
* so the domain gets the same treatment a transaction's `to` does:
|
|
247
|
+
* 1. domain.chainId must be present and in chain_ids — a signature for
|
|
248
|
+
* another chain is exactly the kind of thing an injected prompt asks
|
|
249
|
+
* for, and a chain-less domain can't be checked at all.
|
|
250
|
+
* 2. domain.verifyingContract must be allowlisted when the list is set.
|
|
251
|
+
* 3. primaryType must be allowlisted when the list is set.
|
|
252
|
+
* Domain fields are read defensively: the evaluator runs before sign-typed.ts
|
|
253
|
+
* validates the structure, and a malformed domain must deny, not throw.
|
|
254
|
+
*/
|
|
255
|
+
function evaluateTypedDataStrict(td, policy) {
|
|
256
|
+
if (typeof td.domain !== 'object' || td.domain === null || Array.isArray(td.domain)) {
|
|
257
|
+
return { kind: 'deny', reason: 'EIP-712 denied — domain must be an object' };
|
|
258
|
+
}
|
|
259
|
+
const domain = td.domain;
|
|
260
|
+
const chainId = domain['chainId'];
|
|
261
|
+
if (chainId === undefined) {
|
|
262
|
+
// A chain-less domain can't be checked against chain_ids, and a
|
|
263
|
+
// signature over one is valid wherever that domain is accepted. Strict
|
|
264
|
+
// mode therefore requires the domain to be chain-bound.
|
|
265
|
+
return {
|
|
266
|
+
kind: 'deny',
|
|
267
|
+
reason: 'EIP-712 denied — strict mode requires domain.chainId (chain-less domains cannot be checked against chain_ids)',
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
const id = exactChainId(chainId);
|
|
271
|
+
if (id === null || !policy.chainIds.some((c) => BigInt(c) === id)) {
|
|
272
|
+
return {
|
|
273
|
+
kind: 'deny',
|
|
274
|
+
reason: `EIP-712 denied — domain.chainId ${String(chainId)} not in chain_ids ${JSON.stringify(policy.chainIds)}`,
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
if (policy.typedDataVerifyingContracts.length > 0) {
|
|
278
|
+
const vc = domain['verifyingContract'];
|
|
279
|
+
const norm = typeof vc === 'string' ? vc.toLowerCase() : undefined;
|
|
280
|
+
if (norm === undefined || !policy.typedDataVerifyingContracts.includes(norm)) {
|
|
281
|
+
return {
|
|
282
|
+
kind: 'deny',
|
|
283
|
+
reason: `EIP-712 denied — domain.verifyingContract ${norm ?? '(absent)'} not in typed_data_verifying_contracts`,
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
if (policy.typedDataPrimaryTypes.length > 0) {
|
|
288
|
+
const pt = td.primaryType;
|
|
289
|
+
if (typeof pt !== 'string' || !policy.typedDataPrimaryTypes.includes(pt)) {
|
|
290
|
+
return {
|
|
291
|
+
kind: 'deny',
|
|
292
|
+
reason: `EIP-712 denied — primaryType ${typeof pt === 'string' ? pt : '(absent)'} not in typed_data_primary_types`,
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
return null;
|
|
297
|
+
}
|
|
298
|
+
/** Exact integer chain id as a bigint, or null if it isn't a safe integer / bigint. */
|
|
299
|
+
function exactChainId(v) {
|
|
300
|
+
if (typeof v === 'bigint')
|
|
301
|
+
return v >= 0n ? v : null;
|
|
302
|
+
if (typeof v === 'number')
|
|
303
|
+
return Number.isSafeInteger(v) && v >= 0 ? BigInt(v) : null;
|
|
304
|
+
return null;
|
|
305
|
+
}
|
|
241
306
|
//# sourceMappingURL=evaluate.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"evaluate.js","sourceRoot":"","sources":["../../../src/policy/evaluate.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,QAAQ,CAAC,OAAsB,EAAE,MAAc;IAC7D,IAAI,MAAM,CAAC,IAAI,KAAK,YAAY,EAAE,CAAC;QACjC,IAAI,OAAO,CAAC,IAAI,KAAK,aAAa,EAAE,CAAC;YACnC,MAAM,OAAO,GAAG,YAAY,CAAC,OAAO,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;YACjD,IAAI,OAAO;gBAAE,OAAO,OAAO,CAAC;QAC9B,CAAC;QACD,IAAI,OAAO,CAAC,IAAI,KAAK,iBAAiB,EAAE,CAAC;YACvC,MAAM,OAAO,GAAG,eAAe,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;YACjD,IAAI,OAAO;gBAAE,OAAO,OAAO,CAAC;QAC9B,CAAC;QACD,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC;IAC3B,CAAC;IACD,QAAQ,OAAO,CAAC,IAAI,EAAE,CAAC;QACrB,KAAK,aAAa,CAAC,CAAC,CAAC;YACnB,MAAM,QAAQ,GAAG,yBAAyB,CAAC,OAAO,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;YAC/D,IAAI,QAAQ;gBAAE,OAAO,QAAQ,CAAC;YAC9B,MAAM,OAAO,GAAG,YAAY,CAAC,OAAO,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;YACjD,IAAI,OAAO;gBAAE,OAAO,OAAO,CAAC;YAC5B,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC;QAC3B,CAAC;QACD,KAAK,SAAS;YACZ,OAAO,MAAM,CAAC,mBAAmB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,MAAM,CAAC,2BAA2B,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;YAC7E,OAAO;gBACL,IAAI,EAAE,MAAM;gBACZ,MAAM,EAAE,6CAA6C,IAAI,IAAI,UAAU,wCAAwC;aAChH,CAAC;QACJ,CAAC;IACH,CAAC;IACD,IAAI,MAAM,CAAC,qBAAqB,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC5C,MAAM,EAAE,GAAG,EAAE,CAAC,WAAW,CAAC;QAC1B,IAAI,OAAO,EAAE,KAAK,QAAQ,IAAI,CAAC,MAAM,CAAC,qBAAqB,CAAC,QAAQ,CAAC,EAAE,CAAC,EAAE,CAAC;YACzE,OAAO;gBACL,IAAI,EAAE,MAAM;gBACZ,MAAM,EAAE,gCAAgC,OAAO,EAAE,KAAK,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,UAAU,kCAAkC;aACnH,CAAC;QACJ,CAAC;IACH,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED,uFAAuF;AACvF,SAAS,YAAY,CAAC,CAAU;IAC9B,IAAI,OAAO,CAAC,KAAK,QAAQ;QAAE,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;IACrD,IAAI,OAAO,CAAC,KAAK,QAAQ;QAAE,OAAO,MAAM,CAAC,aAAa,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;IACvF,OAAO,IAAI,CAAC;AACd,CAAC"}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export { type Policy, type PolicyRequest, type PolicyDecision, type PolicyResolver, PolicyLoadError, } from './types.js';
|
|
2
|
-
export { parsePolicy, FileSystemPolicyResolver, permissivePolicyResolver
|
|
2
|
+
export { parsePolicy, FileSystemPolicyResolver, permissivePolicyResolver } from './loader.js';
|
|
3
3
|
export { evaluate } from './evaluate.js';
|
|
4
4
|
export { type PolicyMode, PERMISSIVE_TEMPLATE, STRICT_TEMPLATE, policyTemplate, } from './template.js';
|
|
5
|
+
export { type SpendAsset, type WindowCap, HOUR_MS, DAY_MS, windowCapsFor, checkWindowCaps, } from './window.js';
|
|
6
|
+
export { type SpendLedger, type SpendLedgerOpts, SpendLedgerError, MemorySpendLedger, FileSpendLedger, _ledgerTestHooks, } from './ledger.js';
|
|
5
7
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/policy/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,MAAM,EACX,KAAK,aAAa,EAClB,KAAK,cAAc,EACnB,KAAK,cAAc,EACnB,eAAe,GAChB,MAAM,YAAY,CAAC;AACpB,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/policy/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,MAAM,EACX,KAAK,aAAa,EAClB,KAAK,cAAc,EACnB,KAAK,cAAc,EACnB,eAAe,GAChB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,WAAW,EAAE,wBAAwB,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAC;AAC9F,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EACL,KAAK,UAAU,EACf,mBAAmB,EACnB,eAAe,EACf,cAAc,GACf,MAAM,eAAe,CAAC;AACvB,OAAO,EACL,KAAK,UAAU,EACf,KAAK,SAAS,EACd,OAAO,EACP,MAAM,EACN,aAAa,EACb,eAAe,GAChB,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,KAAK,WAAW,EAChB,KAAK,eAAe,EACpB,gBAAgB,EAChB,iBAAiB,EACjB,eAAe,EACf,gBAAgB,GACjB,MAAM,aAAa,CAAC"}
|
package/dist/src/policy/index.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export { PolicyLoadError, } from './types.js';
|
|
2
|
-
export { parsePolicy, FileSystemPolicyResolver, permissivePolicyResolver
|
|
2
|
+
export { parsePolicy, FileSystemPolicyResolver, permissivePolicyResolver } from './loader.js';
|
|
3
3
|
export { evaluate } from './evaluate.js';
|
|
4
4
|
export { PERMISSIVE_TEMPLATE, STRICT_TEMPLATE, policyTemplate, } from './template.js';
|
|
5
|
+
export { HOUR_MS, DAY_MS, windowCapsFor, checkWindowCaps, } from './window.js';
|
|
6
|
+
export { SpendLedgerError, MemorySpendLedger, FileSpendLedger, _ledgerTestHooks, } from './ledger.js';
|
|
5
7
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/policy/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,eAAe,GAChB,MAAM,YAAY,CAAC;AACpB,OAAO,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/policy/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,eAAe,GAChB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,WAAW,EAAE,wBAAwB,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAC;AAC9F,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAEL,mBAAmB,EACnB,eAAe,EACf,cAAc,GACf,MAAM,eAAe,CAAC;AACvB,OAAO,EAGL,OAAO,EACP,MAAM,EACN,aAAa,EACb,eAAe,GAChB,MAAM,aAAa,CAAC;AACrB,OAAO,EAGL,gBAAgB,EAChB,iBAAiB,EACjB,eAAe,EACf,gBAAgB,GACjB,MAAM,aAAa,CAAC"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { type SpendAsset, type WindowCap } from './window.js';
|
|
2
|
+
/**
|
|
3
|
+
* What the sign path needs from the spend ledger. `reserve` is the only
|
|
4
|
+
* mutating call: it checks every cap against what is already recorded and,
|
|
5
|
+
* only if all hold, records the new amount — atomically with respect to
|
|
6
|
+
* other sigil-mcp processes, so two windows can't both squeeze under a cap
|
|
7
|
+
* in the same instant.
|
|
8
|
+
*
|
|
9
|
+
* The ledger owns the clock. Time is sampled *inside* the critical section
|
|
10
|
+
* so a process that read the clock, got descheduled, and then took the lock
|
|
11
|
+
* cannot judge a competitor's later spend as "in the future" and ignore it.
|
|
12
|
+
* Entries dated after now (another process's later sample, or a clock that
|
|
13
|
+
* stepped back) are always counted: a spend that has happened has happened.
|
|
14
|
+
*/
|
|
15
|
+
export interface SpendLedger {
|
|
16
|
+
/** Total recorded for (handle, asset) with ts > now - windowMs, including any ts > now. */
|
|
17
|
+
spent(handle: string, asset: SpendAsset, windowMs: number): bigint;
|
|
18
|
+
/**
|
|
19
|
+
* Check-then-record. Returns null on success (amount recorded), or the
|
|
20
|
+
* deny reason (nothing recorded). A zero amount is a no-op success.
|
|
21
|
+
*/
|
|
22
|
+
reserve(handle: string, asset: SpendAsset, amount: bigint, caps: readonly WindowCap[]): string | null;
|
|
23
|
+
}
|
|
24
|
+
export declare class SpendLedgerError extends Error {
|
|
25
|
+
constructor(message: string);
|
|
26
|
+
}
|
|
27
|
+
export interface SpendLedgerOpts {
|
|
28
|
+
/** Clock override for tests. Defaults to Date.now. */
|
|
29
|
+
now?: () => number;
|
|
30
|
+
}
|
|
31
|
+
/** In-memory ledger for tests and for callers that don't persist. */
|
|
32
|
+
export declare class MemorySpendLedger implements SpendLedger {
|
|
33
|
+
#private;
|
|
34
|
+
constructor(opts?: SpendLedgerOpts);
|
|
35
|
+
spent(handle: string, asset: SpendAsset, windowMs: number): bigint;
|
|
36
|
+
reserve(handle: string, asset: SpendAsset, amount: bigint, caps: readonly WindowCap[]): string | null;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* File-backed ledger: one append-only JSONL file per portal at
|
|
40
|
+
* `<stateDir>/<handle>.ledger`, lines of `{"ts":…,"asset":"wei","amount":"…"}`.
|
|
41
|
+
*
|
|
42
|
+
* Every read and write runs under the cross-process file lock, the clock is
|
|
43
|
+
* sampled inside it, and each `reserve` re-reads the file before deciding,
|
|
44
|
+
* so the decision is always made against the union of every window's
|
|
45
|
+
* spends.
|
|
46
|
+
*
|
|
47
|
+
* Integrity is fail-closed: a line that does not parse makes every reserve
|
|
48
|
+
* for that portal deny with a message naming the file, until a human fixes
|
|
49
|
+
* or removes it. Skipping bad lines would silently loosen the cap, which is
|
|
50
|
+
* the one thing a rate limit must not do. A crash can leave a torn final
|
|
51
|
+
* line (writes are complete and fsynced, so only power loss does this); the
|
|
52
|
+
* next append starts on a fresh line so the torn fragment stays isolated —
|
|
53
|
+
* and still trips the check, on purpose.
|
|
54
|
+
*
|
|
55
|
+
* Entries older than the longest window sigil supports (24h) can never
|
|
56
|
+
* influence a decision again; when more than COMPACT_AFTER of them have
|
|
57
|
+
* piled up the file is rewritten (tmp + fsync + rename + directory fsync,
|
|
58
|
+
* under the same lock) with only the live tail. Entries dated in the future
|
|
59
|
+
* are always kept.
|
|
60
|
+
*
|
|
61
|
+
* Threat model: an attacker who can delete or rewrite this file can also
|
|
62
|
+
* rewrite the policy file beside it and remove the cap outright, so the
|
|
63
|
+
* ledger earns no extra protection beyond its 0600 mode inside the 0700
|
|
64
|
+
* sigil home; a missing file is an empty ledger. What it does defend
|
|
65
|
+
* against is the prompt-injected *agent*, which never touches the
|
|
66
|
+
* filesystem directly. The hash-chained audit log is the record.
|
|
67
|
+
*/
|
|
68
|
+
export declare class FileSpendLedger implements SpendLedger {
|
|
69
|
+
#private;
|
|
70
|
+
static readonly COMPACT_AFTER = 256;
|
|
71
|
+
constructor(stateDir: string, opts?: SpendLedgerOpts);
|
|
72
|
+
pathFor(handle: string): string;
|
|
73
|
+
spent(handle: string, asset: SpendAsset, windowMs: number): bigint;
|
|
74
|
+
reserve(handle: string, asset: SpendAsset, amount: bigint, caps: readonly WindowCap[]): string | null;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Test-only seam: replaces the directory fsync so a test can make it fail.
|
|
78
|
+
* Never set by production code.
|
|
79
|
+
*/
|
|
80
|
+
export declare const _ledgerTestHooks: {
|
|
81
|
+
fsyncDir?: (dir: string) => void;
|
|
82
|
+
};
|
|
83
|
+
//# sourceMappingURL=ledger.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ledger.d.ts","sourceRoot":"","sources":["../../../src/policy/ledger.ts"],"names":[],"mappings":"AAGA,OAAO,EAA2B,KAAK,UAAU,EAAE,KAAK,SAAS,EAAE,MAAM,aAAa,CAAC;AAEvF;;;;;;;;;;;;GAYG;AACH,MAAM,WAAW,WAAW;IAC1B,2FAA2F;IAC3F,KAAK,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAAC;IACnE;;;OAGG;IACH,OAAO,CACL,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,UAAU,EACjB,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,SAAS,SAAS,EAAE,GACzB,MAAM,GAAG,IAAI,CAAC;CAClB;AAED,qBAAa,gBAAiB,SAAQ,KAAK;gBAC7B,OAAO,EAAE,MAAM;CAI5B;AAKD,MAAM,WAAW,eAAe;IAC9B,sDAAsD;IACtD,GAAG,CAAC,EAAE,MAAM,MAAM,CAAC;CACpB;AAsBD,qEAAqE;AACrE,qBAAa,iBAAkB,YAAW,WAAW;;gBAIvC,IAAI,GAAE,eAAoB;IAatC,KAAK,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM;IAIlE,OAAO,CACL,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,UAAU,EACjB,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,SAAS,SAAS,EAAE,GACzB,MAAM,GAAG,IAAI;CASjB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,qBAAa,eAAgB,YAAW,WAAW;;IACjD,MAAM,CAAC,QAAQ,CAAC,aAAa,OAAO;gBAIxB,QAAQ,EAAE,MAAM,EAAE,IAAI,GAAE,eAAoB;IAKxD,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM;IAe/B,KAAK,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM;IAQlE,OAAO,CACL,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,UAAU,EACjB,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,SAAS,SAAS,EAAE,GACzB,MAAM,GAAG,IAAI;CA4CjB;AAED;;;GAGG;AACH,eAAO,MAAM,gBAAgB,EAAE;IAAE,QAAQ,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAA;CAAO,CAAC"}
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import { closeSync, fsyncSync, mkdirSync, openSync, readFileSync, renameSync } from 'node:fs';
|
|
2
|
+
import { dirname, join } from 'node:path';
|
|
3
|
+
import { withFileLock, writeAllSync } from '../fs/index.js';
|
|
4
|
+
import { checkWindowCaps, DAY_MS } from './window.js';
|
|
5
|
+
export class SpendLedgerError extends Error {
|
|
6
|
+
constructor(message) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.name = 'SpendLedgerError';
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
/** A negative spend is a caller bug, never a credit: refuse without touching the file. */
|
|
12
|
+
const NEGATIVE_REASON = 'denied — negative amount is not a valid spend';
|
|
13
|
+
function sumWindow(entries, asset, windowMs, now) {
|
|
14
|
+
const floor = now - windowMs;
|
|
15
|
+
let total = 0n;
|
|
16
|
+
for (const e of entries) {
|
|
17
|
+
if (e.asset === asset && e.ts > floor)
|
|
18
|
+
total += e.amount;
|
|
19
|
+
}
|
|
20
|
+
return total;
|
|
21
|
+
}
|
|
22
|
+
/** In-memory ledger for tests and for callers that don't persist. */
|
|
23
|
+
export class MemorySpendLedger {
|
|
24
|
+
#entries = new Map();
|
|
25
|
+
#now;
|
|
26
|
+
constructor(opts = {}) {
|
|
27
|
+
this.#now = opts.now ?? (() => Date.now());
|
|
28
|
+
}
|
|
29
|
+
#for(handle) {
|
|
30
|
+
let list = this.#entries.get(handle);
|
|
31
|
+
if (!list) {
|
|
32
|
+
list = [];
|
|
33
|
+
this.#entries.set(handle, list);
|
|
34
|
+
}
|
|
35
|
+
return list;
|
|
36
|
+
}
|
|
37
|
+
spent(handle, asset, windowMs) {
|
|
38
|
+
return sumWindow(this.#for(handle), asset, windowMs, this.#now());
|
|
39
|
+
}
|
|
40
|
+
reserve(handle, asset, amount, caps) {
|
|
41
|
+
if (amount < 0n)
|
|
42
|
+
return NEGATIVE_REASON;
|
|
43
|
+
const list = this.#for(handle);
|
|
44
|
+
const now = this.#now();
|
|
45
|
+
const reason = checkWindowCaps(caps, amount, (w) => sumWindow(list, asset, w, now), asset);
|
|
46
|
+
if (reason !== null)
|
|
47
|
+
return reason;
|
|
48
|
+
if (amount > 0n)
|
|
49
|
+
list.push({ ts: now, asset, amount });
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* File-backed ledger: one append-only JSONL file per portal at
|
|
55
|
+
* `<stateDir>/<handle>.ledger`, lines of `{"ts":…,"asset":"wei","amount":"…"}`.
|
|
56
|
+
*
|
|
57
|
+
* Every read and write runs under the cross-process file lock, the clock is
|
|
58
|
+
* sampled inside it, and each `reserve` re-reads the file before deciding,
|
|
59
|
+
* so the decision is always made against the union of every window's
|
|
60
|
+
* spends.
|
|
61
|
+
*
|
|
62
|
+
* Integrity is fail-closed: a line that does not parse makes every reserve
|
|
63
|
+
* for that portal deny with a message naming the file, until a human fixes
|
|
64
|
+
* or removes it. Skipping bad lines would silently loosen the cap, which is
|
|
65
|
+
* the one thing a rate limit must not do. A crash can leave a torn final
|
|
66
|
+
* line (writes are complete and fsynced, so only power loss does this); the
|
|
67
|
+
* next append starts on a fresh line so the torn fragment stays isolated —
|
|
68
|
+
* and still trips the check, on purpose.
|
|
69
|
+
*
|
|
70
|
+
* Entries older than the longest window sigil supports (24h) can never
|
|
71
|
+
* influence a decision again; when more than COMPACT_AFTER of them have
|
|
72
|
+
* piled up the file is rewritten (tmp + fsync + rename + directory fsync,
|
|
73
|
+
* under the same lock) with only the live tail. Entries dated in the future
|
|
74
|
+
* are always kept.
|
|
75
|
+
*
|
|
76
|
+
* Threat model: an attacker who can delete or rewrite this file can also
|
|
77
|
+
* rewrite the policy file beside it and remove the cap outright, so the
|
|
78
|
+
* ledger earns no extra protection beyond its 0600 mode inside the 0700
|
|
79
|
+
* sigil home; a missing file is an empty ledger. What it does defend
|
|
80
|
+
* against is the prompt-injected *agent*, which never touches the
|
|
81
|
+
* filesystem directly. The hash-chained audit log is the record.
|
|
82
|
+
*/
|
|
83
|
+
export class FileSpendLedger {
|
|
84
|
+
static COMPACT_AFTER = 256;
|
|
85
|
+
#dir;
|
|
86
|
+
#now;
|
|
87
|
+
constructor(stateDir, opts = {}) {
|
|
88
|
+
this.#dir = stateDir;
|
|
89
|
+
this.#now = opts.now ?? (() => Date.now());
|
|
90
|
+
}
|
|
91
|
+
pathFor(handle) {
|
|
92
|
+
return join(this.#dir, `${handle}.ledger`);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Create the state dir and make its directory entry durable. Not cached:
|
|
96
|
+
* a sync that failed must not be remembered as done, and a reservation
|
|
97
|
+
* from another daemon must not mistake the directory's existence for
|
|
98
|
+
* established durability. One mkdir + one fsync per reserve is cheap.
|
|
99
|
+
*/
|
|
100
|
+
#ensureDir() {
|
|
101
|
+
mkdirSync(this.#dir, { recursive: true, mode: 0o700 });
|
|
102
|
+
fsyncDirOrFail(dirname(this.#dir));
|
|
103
|
+
}
|
|
104
|
+
spent(handle, asset, windowMs) {
|
|
105
|
+
const path = this.pathFor(handle);
|
|
106
|
+
mkdirSync(this.#dir, { recursive: true, mode: 0o700 });
|
|
107
|
+
return withFileLock(path, () => sumWindow(readEntries(path).entries, asset, windowMs, this.#now()));
|
|
108
|
+
}
|
|
109
|
+
reserve(handle, asset, amount, caps) {
|
|
110
|
+
if (amount < 0n)
|
|
111
|
+
return NEGATIVE_REASON;
|
|
112
|
+
this.#ensureDir();
|
|
113
|
+
const path = this.pathFor(handle);
|
|
114
|
+
return withFileLock(path, () => {
|
|
115
|
+
const { entries, endsWithNewline, size } = readEntries(path);
|
|
116
|
+
const now = this.#now();
|
|
117
|
+
const reason = checkWindowCaps(caps, amount, (w) => sumWindow(entries, asset, w, now), asset);
|
|
118
|
+
if (reason !== null)
|
|
119
|
+
return reason;
|
|
120
|
+
if (amount === 0n)
|
|
121
|
+
return null;
|
|
122
|
+
const entry = { ts: now, asset, amount };
|
|
123
|
+
const stale = entries.filter((e) => e.ts <= now - DAY_MS).length;
|
|
124
|
+
if (stale > FileSpendLedger.COMPACT_AFTER) {
|
|
125
|
+
const live = entries.filter((e) => e.ts > now - DAY_MS);
|
|
126
|
+
live.push(entry);
|
|
127
|
+
const tmp = `${path}.tmp`;
|
|
128
|
+
const fd = openSync(tmp, 'w', 0o600);
|
|
129
|
+
try {
|
|
130
|
+
writeAllSync(fd, live.map(serialize).join(''));
|
|
131
|
+
fsyncSync(fd);
|
|
132
|
+
}
|
|
133
|
+
finally {
|
|
134
|
+
closeSync(fd);
|
|
135
|
+
}
|
|
136
|
+
renameSync(tmp, path);
|
|
137
|
+
fsyncDirOrFail(dirname(path));
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
const fd = openSync(path, 'a', 0o600);
|
|
141
|
+
try {
|
|
142
|
+
// Never merge with a torn fragment: start on a fresh line.
|
|
143
|
+
writeAllSync(fd, (size > 0 && !endsWithNewline ? '\n' : '') + serialize(entry));
|
|
144
|
+
fsyncSync(fd);
|
|
145
|
+
}
|
|
146
|
+
finally {
|
|
147
|
+
closeSync(fd);
|
|
148
|
+
}
|
|
149
|
+
// The directory entry needs its own fsync, or a power cut right after
|
|
150
|
+
// a signature can lose a brand-new ledger and hand back a fresh
|
|
151
|
+
// allowance on reboot. Done on every append rather than only the
|
|
152
|
+
// first: if a previous attempt's sync failed, the file's mere
|
|
153
|
+
// existence proves nothing about durability.
|
|
154
|
+
fsyncDirOrFail(dirname(path));
|
|
155
|
+
}
|
|
156
|
+
return null;
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Test-only seam: replaces the directory fsync so a test can make it fail.
|
|
162
|
+
* Never set by production code.
|
|
163
|
+
*/
|
|
164
|
+
export const _ledgerTestHooks = {};
|
|
165
|
+
/**
|
|
166
|
+
* fsync a directory, converting any failure into a SpendLedgerError. The
|
|
167
|
+
* spend may already be on disk at this point; refusing to sign then errs
|
|
168
|
+
* on the side of over-counting, never under-counting. Nothing is swallowed:
|
|
169
|
+
* a filesystem that can't confirm durability can't back a rate limit.
|
|
170
|
+
*/
|
|
171
|
+
function fsyncDirOrFail(dir) {
|
|
172
|
+
try {
|
|
173
|
+
if (_ledgerTestHooks.fsyncDir) {
|
|
174
|
+
_ledgerTestHooks.fsyncDir(dir);
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
const fd = openSync(dir, 'r');
|
|
178
|
+
try {
|
|
179
|
+
fsyncSync(fd);
|
|
180
|
+
}
|
|
181
|
+
finally {
|
|
182
|
+
closeSync(fd);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
catch (err) {
|
|
186
|
+
throw new SpendLedgerError(`spend ledger: could not make ${dir} durable (${err.message}) — refusing to sign`);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
function serialize(e) {
|
|
190
|
+
return JSON.stringify({ ts: e.ts, asset: e.asset, amount: e.amount.toString() }) + '\n';
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Parse the ledger. Any line that does not parse — including a torn final
|
|
194
|
+
* line — throws SpendLedgerError naming the file: the cap cannot be
|
|
195
|
+
* evaluated against unknown history, so every reserve denies until a human
|
|
196
|
+
* repairs or removes the file. A missing file is an empty ledger.
|
|
197
|
+
*/
|
|
198
|
+
function readEntries(path) {
|
|
199
|
+
let text;
|
|
200
|
+
try {
|
|
201
|
+
text = readFileSync(path, 'utf8');
|
|
202
|
+
}
|
|
203
|
+
catch (err) {
|
|
204
|
+
if (err.code === 'ENOENT') {
|
|
205
|
+
return { entries: [], endsWithNewline: true, size: 0 };
|
|
206
|
+
}
|
|
207
|
+
throw err;
|
|
208
|
+
}
|
|
209
|
+
const out = [];
|
|
210
|
+
const lines = text.split('\n');
|
|
211
|
+
const endsWithNewline = text === '' || text.endsWith('\n');
|
|
212
|
+
for (let i = 0; i < lines.length; i++) {
|
|
213
|
+
const line = lines[i];
|
|
214
|
+
if (line === '') {
|
|
215
|
+
// Blank lines (the split's trailing empty string, or a separator we
|
|
216
|
+
// wrote to isolate a torn fragment) carry no information.
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
const e = parseEntry(line);
|
|
220
|
+
if (!e) {
|
|
221
|
+
throw new SpendLedgerError(`spend ledger ${path} has an unreadable line ${i + 1} — refusing to sign against ` +
|
|
222
|
+
`unknown history; inspect and repair or remove the file (this resets the allowance)`);
|
|
223
|
+
}
|
|
224
|
+
out.push(e);
|
|
225
|
+
}
|
|
226
|
+
return { entries: out, endsWithNewline, size: Buffer.byteLength(text, 'utf8') };
|
|
227
|
+
}
|
|
228
|
+
function parseEntry(line) {
|
|
229
|
+
let obj;
|
|
230
|
+
try {
|
|
231
|
+
obj = JSON.parse(line);
|
|
232
|
+
}
|
|
233
|
+
catch {
|
|
234
|
+
return null;
|
|
235
|
+
}
|
|
236
|
+
if (typeof obj !== 'object' || obj === null)
|
|
237
|
+
return null;
|
|
238
|
+
const r = obj;
|
|
239
|
+
const ts = r['ts'];
|
|
240
|
+
const asset = r['asset'];
|
|
241
|
+
const amount = r['amount'];
|
|
242
|
+
if (typeof ts !== 'number' || !Number.isFinite(ts))
|
|
243
|
+
return null;
|
|
244
|
+
if (asset !== 'wei' && asset !== 'lamports')
|
|
245
|
+
return null;
|
|
246
|
+
if (typeof amount !== 'string' || !/^[0-9]+$/.test(amount))
|
|
247
|
+
return null;
|
|
248
|
+
return { ts, asset, amount: BigInt(amount) };
|
|
249
|
+
}
|
|
250
|
+
//# sourceMappingURL=ledger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ledger.js","sourceRoot":"","sources":["../../../src/policy/ledger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,SAAS,EAAE,QAAQ,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAC9F,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC5D,OAAO,EAAE,eAAe,EAAE,MAAM,EAAmC,MAAM,aAAa,CAAC;AA8BvF,MAAM,OAAO,gBAAiB,SAAQ,KAAK;IACzC,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,kBAAkB,CAAC;IACjC,CAAC;CACF;AAED,0FAA0F;AAC1F,MAAM,eAAe,GAAG,+CAA+C,CAAC;AAaxE,SAAS,SAAS,CAChB,OAA+B,EAC/B,KAAiB,EACjB,QAAgB,EAChB,GAAW;IAEX,MAAM,KAAK,GAAG,GAAG,GAAG,QAAQ,CAAC;IAC7B,IAAI,KAAK,GAAG,EAAE,CAAC;IACf,KAAK,MAAM,CAAC,IAAI,OAAO,EAAE,CAAC;QACxB,IAAI,CAAC,CAAC,KAAK,KAAK,KAAK,IAAI,CAAC,CAAC,EAAE,GAAG,KAAK;YAAE,KAAK,IAAI,CAAC,CAAC,MAAM,CAAC;I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C,QAAQ,CAAC,EAAE,CAAC;QAAE,OAAO,IAAI,CAAC;IAChE,IAAI,KAAK,KAAK,KAAK,IAAI,KAAK,KAAK,UAAU;QAAE,OAAO,IAAI,CAAC;IACzD,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC;QAAE,OAAO,IAAI,CAAC;IACxE,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC;AAC/C,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loader.d.ts","sourceRoot":"","sources":["../../../src/policy/loader.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,MAAM,EAAmB,KAAK,cAAc,EAAE,MAAM,YAAY,CAAC;AAM/E;;;;;;;;;;;;GAYG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,
|
|
1
|
+
{"version":3,"file":"loader.d.ts","sourceRoot":"","sources":["../../../src/policy/loader.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,MAAM,EAAmB,KAAK,cAAc,EAAE,MAAM,YAAY,CAAC;AAM/E;;;;;;;;;;;;GAYG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CA6KlD;AAmCD;;;;GAIG;AACH,wBAAgB,wBAAwB,IAAI,cAAc,CAiBzD;AAED;;;;;GAKG;AACH,qBAAa,wBAAyB,YAAW,cAAc;;gBAEjD,SAAS,EAAE,MAAM;IAG7B,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM;CAehC"}
|
|
@@ -35,6 +35,8 @@ export function parsePolicy(source) {
|
|
|
35
35
|
// that applies whether or not the rest of the file is strict.
|
|
36
36
|
const requireConfirmAboveWei = parseOptionalWei(raw['require_confirm_above_wei'], 'require_confirm_above_wei');
|
|
37
37
|
const requireConfirmAboveLamports = parseOptionalDec(raw['require_confirm_above_lamports'], 'require_confirm_above_lamports');
|
|
38
|
+
// Rolling-window caps are mode-independent too.
|
|
39
|
+
const windows = parseWindowCaps(raw);
|
|
38
40
|
if (mode === 'permissive') {
|
|
39
41
|
return {
|
|
40
42
|
mode: 'permissive',
|
|
@@ -48,8 +50,11 @@ export function parsePolicy(source) {
|
|
|
48
50
|
allowSvmMessageSigning: true,
|
|
49
51
|
svmAllowTo: [],
|
|
50
52
|
svmMaxLamports: 0n,
|
|
53
|
+
typedDataVerifyingContracts: [],
|
|
54
|
+
typedDataPrimaryTypes: [],
|
|
51
55
|
...(requireConfirmAboveWei !== undefined ? { requireConfirmAboveWei } : {}),
|
|
52
56
|
...(requireConfirmAboveLamports !== undefined ? { requireConfirmAboveLamports } : {}),
|
|
57
|
+
...windows,
|
|
53
58
|
};
|
|
54
59
|
}
|
|
55
60
|
// strict mode — every field is consulted; apply defaults for absent ones.
|
|
@@ -77,6 +82,18 @@ export function parsePolicy(source) {
|
|
|
77
82
|
const allowContractCreation = asBool(raw['allow_contract_creation'], 'allow_contract_creation', false);
|
|
78
83
|
const allowMessageSigning = asBool(raw['allow_message_signing'], 'allow_message_signing', false);
|
|
79
84
|
const allowTypedData = asBool(raw['allow_typed_data'], 'allow_typed_data', false);
|
|
85
|
+
const typedDataVerifyingContracts = asStringArray(raw['typed_data_verifying_contracts'], 'typed_data_verifying_contracts').map((s, i) => {
|
|
86
|
+
if (!ADDR_RE.test(s)) {
|
|
87
|
+
throw new PolicyLoadError(`policy.typed_data_verifying_contracts[${i}] must be 0x-prefixed 20-byte address`);
|
|
88
|
+
}
|
|
89
|
+
return s.toLowerCase();
|
|
90
|
+
});
|
|
91
|
+
const typedDataPrimaryTypes = asStringArray(raw['typed_data_primary_types'], 'typed_data_primary_types').map((s, i) => {
|
|
92
|
+
if (s.trim() === '') {
|
|
93
|
+
throw new PolicyLoadError(`policy.typed_data_primary_types[${i}] must be a non-empty string`);
|
|
94
|
+
}
|
|
95
|
+
return s;
|
|
96
|
+
});
|
|
80
97
|
const allowSvmMessageSigning = asBool(raw['allow_svm_message_signing'], 'allow_svm_message_signing', false);
|
|
81
98
|
const svmAllowToRaw = asStringArray(raw['svm_allow_to'], 'svm_allow_to');
|
|
82
99
|
const svmAllowTo = svmAllowToRaw.map((s, i) => {
|
|
@@ -122,8 +139,34 @@ export function parsePolicy(source) {
|
|
|
122
139
|
allowSvmMessageSigning,
|
|
123
140
|
svmAllowTo,
|
|
124
141
|
svmMaxLamports,
|
|
142
|
+
typedDataVerifyingContracts,
|
|
143
|
+
typedDataPrimaryTypes,
|
|
125
144
|
...(requireConfirmAboveWei !== undefined ? { requireConfirmAboveWei } : {}),
|
|
126
145
|
...(requireConfirmAboveLamports !== undefined ? { requireConfirmAboveLamports } : {}),
|
|
146
|
+
...windows,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Parse the four optional rolling-window caps. An hourly cap above the daily
|
|
151
|
+
* cap for the same asset can never bind, so it is rejected as a
|
|
152
|
+
* misconfiguration rather than silently ignored.
|
|
153
|
+
*/
|
|
154
|
+
function parseWindowCaps(raw) {
|
|
155
|
+
const hourWei = parseOptionalDec(raw['max_value_per_hour_wei'], 'max_value_per_hour_wei');
|
|
156
|
+
const dayWei = parseOptionalDec(raw['max_value_per_day_wei'], 'max_value_per_day_wei');
|
|
157
|
+
const hourLam = parseOptionalDec(raw['svm_max_lamports_per_hour'], 'svm_max_lamports_per_hour');
|
|
158
|
+
const dayLam = parseOptionalDec(raw['svm_max_lamports_per_day'], 'svm_max_lamports_per_day');
|
|
159
|
+
if (hourWei !== undefined && dayWei !== undefined && hourWei > dayWei) {
|
|
160
|
+
throw new PolicyLoadError(`policy.max_value_per_hour_wei (${hourWei}) must not exceed max_value_per_day_wei (${dayWei})`);
|
|
161
|
+
}
|
|
162
|
+
if (hourLam !== undefined && dayLam !== undefined && hourLam > dayLam) {
|
|
163
|
+
throw new PolicyLoadError(`policy.svm_max_lamports_per_hour (${hourLam}) must not exceed svm_max_lamports_per_day (${dayLam})`);
|
|
164
|
+
}
|
|
165
|
+
return {
|
|
166
|
+
...(hourWei !== undefined ? { maxValuePerHourWei: hourWei } : {}),
|
|
167
|
+
...(dayWei !== undefined ? { maxValuePerDayWei: dayWei } : {}),
|
|
168
|
+
...(hourLam !== undefined ? { svmMaxLamportsPerHour: hourLam } : {}),
|
|
169
|
+
...(dayLam !== undefined ? { svmMaxLamportsPerDay: dayLam } : {}),
|
|
127
170
|
};
|
|
128
171
|
}
|
|
129
172
|
/**
|
|
@@ -144,6 +187,8 @@ export function permissivePolicyResolver() {
|
|
|
144
187
|
allowSvmMessageSigning: true,
|
|
145
188
|
svmAllowTo: [],
|
|
146
189
|
svmMaxLamports: 0n,
|
|
190
|
+
typedDataVerifyingContracts: [],
|
|
191
|
+
typedDataPrimaryTypes: [],
|
|
147
192
|
};
|
|
148
193
|
return { resolve: () => policy };
|
|
149
194
|
}
|