ai-shield-core 0.1.0 → 0.3.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/dist/audit/logger.d.ts.map +1 -1
- package/dist/audit/logger.js +13 -14
- package/dist/audit/types.js +1 -2
- package/dist/cache/lru.js +1 -5
- package/dist/canary/memory.d.ts +75 -0
- package/dist/canary/memory.d.ts.map +1 -0
- package/dist/canary/memory.js +194 -0
- package/dist/context/wrap-context.d.ts +169 -0
- package/dist/context/wrap-context.d.ts.map +1 -0
- package/dist/context/wrap-context.js +278 -0
- package/dist/cost/anomaly.js +1 -4
- package/dist/cost/pricing.d.ts.map +1 -1
- package/dist/cost/pricing.js +26 -19
- package/dist/cost/tracker.d.ts +19 -1
- package/dist/cost/tracker.d.ts.map +1 -1
- package/dist/cost/tracker.js +27 -10
- package/dist/index.d.ts +34 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +55 -37
- package/dist/judge/async-judge.d.ts +85 -0
- package/dist/judge/async-judge.d.ts.map +1 -0
- package/dist/judge/async-judge.js +146 -0
- package/dist/policy/circuit-breaker.d.ts +70 -0
- package/dist/policy/circuit-breaker.d.ts.map +1 -0
- package/dist/policy/circuit-breaker.js +376 -0
- package/dist/policy/engine.js +1 -5
- package/dist/policy/tools.js +4 -8
- package/dist/scanner/canary.js +4 -8
- package/dist/scanner/chain.js +1 -5
- package/dist/scanner/heuristic.d.ts +27 -0
- package/dist/scanner/heuristic.d.ts.map +1 -1
- package/dist/scanner/heuristic.js +118 -7
- package/dist/scanner/ingestion.d.ts +147 -0
- package/dist/scanner/ingestion.d.ts.map +1 -0
- package/dist/scanner/ingestion.js +520 -0
- package/dist/scanner/output.d.ts +73 -0
- package/dist/scanner/output.d.ts.map +1 -0
- package/dist/scanner/output.js +297 -0
- package/dist/scanner/pii.d.ts.map +1 -1
- package/dist/scanner/pii.js +24 -12
- package/dist/shield.d.ts.map +1 -1
- package/dist/shield.js +34 -26
- package/dist/types.d.ts +156 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +1 -2
- package/package.json +4 -3
- package/src/audit/logger.ts +6 -1
- package/src/canary/memory.ts +259 -0
- package/src/context/wrap-context.ts +475 -0
- package/src/cost/pricing.ts +21 -9
- package/src/cost/tracker.ts +35 -1
- package/src/index.ts +113 -2
- package/src/judge/async-judge.ts +254 -0
- package/src/policy/circuit-breaker.ts +449 -0
- package/src/scanner/heuristic.ts +125 -2
- package/src/scanner/ingestion.ts +624 -0
- package/src/scanner/output.ts +386 -0
- package/src/scanner/pii.ts +21 -7
- package/src/shield.ts +15 -2
- package/src/types.ts +194 -2
- package/tsconfig.json +2 -1
- package/dist/audit/logger.js.map +0 -1
- package/dist/audit/types.js.map +0 -1
- package/dist/cache/lru.js.map +0 -1
- package/dist/cost/anomaly.js.map +0 -1
- package/dist/cost/pricing.js.map +0 -1
- package/dist/cost/tracker.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/policy/engine.js.map +0 -1
- package/dist/policy/tools.js.map +0 -1
- package/dist/scanner/canary.js.map +0 -1
- package/dist/scanner/chain.js.map +0 -1
- package/dist/scanner/heuristic.js.map +0 -1
- package/dist/scanner/pii.js.map +0 -1
- package/dist/shield.js.map +0 -1
- package/dist/types.js.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../src/audit/logger.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AACxE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAO7C,MAAM,WAAW,iBAAiB;IAChC,KAAK,EAAE,UAAU,CAAC;IAClB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,eAAe,CAAC,EAAE,MAAM,CAAC;CAC1B;AAED,qBAAa,WAAW;IACtB,OAAO,CAAC,KAAK,CAAa;IAC1B,OAAO,CAAC,M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AK,IAAI,OAAO,CAAC,IAAI,CAAC;IACtB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAE5B,OAAO,CAAC,KAAK;CAUd;AAID,qBAAa,gBAAiB,YAAW,UAAU;IACjD,OAAO,EAAE,WAAW,EAAE,CAAM;IAEtB,KAAK,CAAC,MAAM,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;IAIzC,UAAU,CAAC,OAAO,EAAE,WAAW,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAIjD,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IACtB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAC7B"}
|
package/dist/audit/logger.js
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const node_crypto_1 = require("node:crypto");
|
|
5
|
-
const node_crypto_2 = require("node:crypto");
|
|
6
|
-
class AuditLogger {
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
export class AuditLogger {
|
|
7
4
|
store;
|
|
8
5
|
buffer = [];
|
|
9
6
|
batchSize;
|
|
@@ -15,19 +12,24 @@ class AuditLogger {
|
|
|
15
12
|
this.flushTimer = setInterval(() => {
|
|
16
13
|
void this.flush();
|
|
17
14
|
}, flushMs);
|
|
15
|
+
// Allow the Node process to exit even if the timer is pending —
|
|
16
|
+
// clean shutdown should go through close(), not rely on the timer.
|
|
17
|
+
if (typeof this.flushTimer.unref === "function") {
|
|
18
|
+
this.flushTimer.unref();
|
|
19
|
+
}
|
|
18
20
|
}
|
|
19
21
|
/** Log a scan result */
|
|
20
22
|
async log(input, result, context = {}, extra = {}) {
|
|
21
23
|
const record = {
|
|
22
|
-
id:
|
|
24
|
+
id: randomUUID(),
|
|
23
25
|
timestamp: new Date(),
|
|
24
26
|
sessionId: context.sessionId,
|
|
25
27
|
agentId: context.agentId,
|
|
26
28
|
userIdHash: context.userId
|
|
27
|
-
?
|
|
29
|
+
? createHash("sha256").update(context.userId).digest("hex").substring(0, 32)
|
|
28
30
|
: undefined,
|
|
29
31
|
requestType: context.tools?.length ? "tool_call" : "chat",
|
|
30
|
-
inputHash:
|
|
32
|
+
inputHash: createHash("sha256").update(input).digest("hex"),
|
|
31
33
|
inputTokenCount: Math.ceil(input.length / 4), // rough estimate
|
|
32
34
|
model: extra.model,
|
|
33
35
|
securityDecision: result.decision,
|
|
@@ -62,9 +64,8 @@ class AuditLogger {
|
|
|
62
64
|
await this.store.close();
|
|
63
65
|
}
|
|
64
66
|
}
|
|
65
|
-
exports.AuditLogger = AuditLogger;
|
|
66
67
|
// --- Console Store (for development) ---
|
|
67
|
-
class ConsoleAuditStore {
|
|
68
|
+
export class ConsoleAuditStore {
|
|
68
69
|
async write(record) {
|
|
69
70
|
this.print(record);
|
|
70
71
|
}
|
|
@@ -83,9 +84,8 @@ class ConsoleAuditStore {
|
|
|
83
84
|
process.stderr.write(`[AI-Shield] ${icon} | ${record.scanDurationMs.toFixed(1)}ms | agent=${record.agentId ?? "-"} | ${record.inputHash.substring(0, 8)}...${violations}\n`);
|
|
84
85
|
}
|
|
85
86
|
}
|
|
86
|
-
exports.ConsoleAuditStore = ConsoleAuditStore;
|
|
87
87
|
// --- Memory Store (for testing) ---
|
|
88
|
-
class MemoryAuditStore {
|
|
88
|
+
export class MemoryAuditStore {
|
|
89
89
|
records = [];
|
|
90
90
|
async write(record) {
|
|
91
91
|
this.records.push(record);
|
|
@@ -96,5 +96,4 @@ class MemoryAuditStore {
|
|
|
96
96
|
async flush() { }
|
|
97
97
|
async close() { }
|
|
98
98
|
}
|
|
99
|
-
exports.MemoryAuditStore = MemoryAuditStore;
|
|
100
99
|
//# sourceMappingURL=logger.js.map
|
package/dist/audit/types.js
CHANGED
package/dist/cache/lru.js
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
// ============================================================
|
|
3
2
|
// LRU Cache — O(1) scan result caching with TTL
|
|
4
3
|
// Uses Map insertion-order for LRU eviction
|
|
5
4
|
// ============================================================
|
|
6
|
-
|
|
7
|
-
exports.ScanLRUCache = void 0;
|
|
8
|
-
class ScanLRUCache {
|
|
5
|
+
export class ScanLRUCache {
|
|
9
6
|
cache = new Map();
|
|
10
7
|
maxSize;
|
|
11
8
|
ttlMs;
|
|
@@ -70,5 +67,4 @@ class ScanLRUCache {
|
|
|
70
67
|
return removed;
|
|
71
68
|
}
|
|
72
69
|
}
|
|
73
|
-
exports.ScanLRUCache = ScanLRUCache;
|
|
74
70
|
//# sourceMappingURL=lru.js.map
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type { MemoryCanaryEntry, MemoryCanaryVerification } from "../types.js";
|
|
2
|
+
export interface MintMemoryCanaryOptions {
|
|
3
|
+
/**
|
|
4
|
+
* Override token byte length. Minimum 8 (16 hex chars). Default 16.
|
|
5
|
+
* Longer = stronger guessing resistance, but the hex string lives in
|
|
6
|
+
* sidecar storage so the overhead is negligible.
|
|
7
|
+
*/
|
|
8
|
+
tokenBytes?: number;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Mint a canary for a memory entry. Call at write time, persist the
|
|
12
|
+
* returned `MemoryCanaryEntry` alongside (or in place of) the raw entry.
|
|
13
|
+
*
|
|
14
|
+
* @param id Stable identifier of the memory entry.
|
|
15
|
+
* @param content The content being stored.
|
|
16
|
+
* @param tenantId Optional tenant scope.
|
|
17
|
+
*/
|
|
18
|
+
export declare function mintMemoryCanary(id: string, content: string, tenantId?: string, options?: MintMemoryCanaryOptions): MemoryCanaryEntry;
|
|
19
|
+
/**
|
|
20
|
+
* Verify a previously minted canary against the content read back from
|
|
21
|
+
* storage. Returns `valid: true` only if both the content matches what
|
|
22
|
+
* was sealed and the tenant binding (if any) matches.
|
|
23
|
+
*
|
|
24
|
+
* Use case 1 — mutation detection:
|
|
25
|
+
* ```ts
|
|
26
|
+
* const entry = mintMemoryCanary("fact:42", "Sky is blue.");
|
|
27
|
+
* await db.write({ ...entry });
|
|
28
|
+
*
|
|
29
|
+
* // ... later ...
|
|
30
|
+
* const stored = await db.read("fact:42");
|
|
31
|
+
* const ver = verifyMemoryCanary(stored, stored.content);
|
|
32
|
+
* if (!ver.valid) {
|
|
33
|
+
* logger.security("Memory poisoning suspected", { id, reason: ver.reason });
|
|
34
|
+
* }
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* Use case 2 — cross-tenant leak detection:
|
|
38
|
+
* ```ts
|
|
39
|
+
* // tenant A reads what should be a tenant-B-only entry
|
|
40
|
+
* const ver = verifyMemoryCanary(entry, entry.content, { tenantId: "tenant-A" });
|
|
41
|
+
* // ver.reason === "tenant_mismatch"
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export declare function verifyMemoryCanary(entry: MemoryCanaryEntry, observedContent: string, options?: {
|
|
45
|
+
tenantId?: string;
|
|
46
|
+
}): MemoryCanaryVerification;
|
|
47
|
+
/**
|
|
48
|
+
* Re-mint a canary after legitimate content edit. Returns a new
|
|
49
|
+
* sealed entry that supersedes the old one. The old `canaryToken`
|
|
50
|
+
* is rotated so a replay of the previous hash is also invalidated.
|
|
51
|
+
*/
|
|
52
|
+
export declare function rotateMemoryCanary(prev: MemoryCanaryEntry, newContent: string): MemoryCanaryEntry;
|
|
53
|
+
/**
|
|
54
|
+
* Inject a *sentinel* memory entry — a decoy fact whose mutation would
|
|
55
|
+
* indicate the store was tampered with. Pair with `findSentinelMutations()`
|
|
56
|
+
* in a periodic sweep over the memory store.
|
|
57
|
+
*
|
|
58
|
+
* Returns a `MemoryCanaryEntry` with deterministic content that callers
|
|
59
|
+
* can recognise. The content includes the canary token so even content
|
|
60
|
+
* inspection (not just hash compare) catches mutation.
|
|
61
|
+
*/
|
|
62
|
+
export declare function buildSentinelEntry(scope: string, tenantId?: string): MemoryCanaryEntry;
|
|
63
|
+
/**
|
|
64
|
+
* Bulk verify a set of stored entries against their canaries. Returns
|
|
65
|
+
* the IDs that failed verification, with the reason.
|
|
66
|
+
*/
|
|
67
|
+
export declare function bulkVerify(entries: Array<{
|
|
68
|
+
canary: MemoryCanaryEntry;
|
|
69
|
+
observedContent: string;
|
|
70
|
+
expectedTenantId?: string;
|
|
71
|
+
}>): Array<{
|
|
72
|
+
id: string;
|
|
73
|
+
reason: NonNullable<MemoryCanaryVerification["reason"]>;
|
|
74
|
+
}>;
|
|
75
|
+
//# sourceMappingURL=memory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory.d.ts","sourceRoot":"","sources":["../../src/canary/memory.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,iBAAiB,EACjB,wBAAwB,EACzB,MAAM,aAAa,CAAC;AAgCrB,MAAM,WAAW,uBAAuB;IACtC;;;;OAIG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAC9B,EAAE,EAAE,MAAM,EACV,OAAO,EAAE,MAAM,EACf,QAAQ,CAAC,EAAE,MAAM,EACjB,OAAO,GAAE,uBAA4B,GACpC,iBAAiB,CAsBnB;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,iBAAiB,EACxB,eAAe,EAAE,MAAM,EACvB,OAAO,GAAE;IAAE,QAAQ,CAAC,EAAE,MAAM,CAAA;CAAO,GAClC,wBAAwB,CAuD1B;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAChC,IAAI,EAAE,iBAAiB,EACvB,UAAU,EAAE,MAAM,GACjB,iBAAiB,CAEnB;AAED;;;;;;;;GAQG;AACH,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,MAAM,EACb,QAAQ,CAAC,EAAE,MAAM,GAChB,iBAAiB,CAQnB;AAED;;;GAGG;AACH,wBAAgB,UAAU,CACxB,OAAO,EAAE,KAAK,CAAC;IACb,MAAM,EAAE,iBAAiB,CAAC;IAC1B,eAAe,EAAE,MAAM,CAAC;IACxB,gBAAgB,CAAC,EAAE,MAAM,CAAC;CAC3B,CAAC,GACD,KAAK,CAAC;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,WAAW,CAAC,wBAAwB,CAAC,QAAQ,CAAC,CAAC,CAAA;CAAE,CAAC,CAchF"}
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { randomBytes, createHash, timingSafeEqual } from "node:crypto";
|
|
2
|
+
// ============================================================
|
|
3
|
+
// Memory Canary — Persistence-Poisoning Detection
|
|
4
|
+
//
|
|
5
|
+
// Existing canary tokens defend the *system prompt*: inject a sentinel
|
|
6
|
+
// + check the response for leakage. That works for prompt-extraction
|
|
7
|
+
// attacks but does nothing for the broader class of *persistence
|
|
8
|
+
// poisoning*: an attacker mutates a stored memory entry, knowledge-graph
|
|
9
|
+
// fact, or RAG document so that the next retrieval steers the model.
|
|
10
|
+
//
|
|
11
|
+
// MemoryCanary seals each write with:
|
|
12
|
+
// 1. A random sentinel token bound to the entry (`canaryToken`).
|
|
13
|
+
// 2. A SHA-256 hash over `id || content || canaryToken` so any silent
|
|
14
|
+
// mutation of `content` between write and read is detectable.
|
|
15
|
+
// 3. Optional `tenantId` binding so a cross-tenant leak surfaces as
|
|
16
|
+
// an explicit `tenant_mismatch` reason.
|
|
17
|
+
//
|
|
18
|
+
// Storage of the canary metadata is the caller's responsibility — the
|
|
19
|
+
// returned `MemoryCanaryEntry` is JSON-serialisable and meant to be
|
|
20
|
+
// persisted alongside the memory entry (separate column / sidecar key).
|
|
21
|
+
//
|
|
22
|
+
// The library does not store secrets and does not need a key. The
|
|
23
|
+
// security property is integrity (mutation detection), not
|
|
24
|
+
// confidentiality.
|
|
25
|
+
// ============================================================
|
|
26
|
+
/** Minimum allowed canary-token length in bytes (= 16 hex chars). */
|
|
27
|
+
const MIN_TOKEN_BYTES = 8;
|
|
28
|
+
/** Default canary-token length: 16 random bytes -> 32 hex chars. */
|
|
29
|
+
const DEFAULT_TOKEN_BYTES = 16;
|
|
30
|
+
/**
|
|
31
|
+
* Mint a canary for a memory entry. Call at write time, persist the
|
|
32
|
+
* returned `MemoryCanaryEntry` alongside (or in place of) the raw entry.
|
|
33
|
+
*
|
|
34
|
+
* @param id Stable identifier of the memory entry.
|
|
35
|
+
* @param content The content being stored.
|
|
36
|
+
* @param tenantId Optional tenant scope.
|
|
37
|
+
*/
|
|
38
|
+
export function mintMemoryCanary(id, content, tenantId, options = {}) {
|
|
39
|
+
if (typeof id !== "string" || id.length === 0) {
|
|
40
|
+
throw new TypeError("mintMemoryCanary: 'id' must be a non-empty string");
|
|
41
|
+
}
|
|
42
|
+
if (typeof content !== "string") {
|
|
43
|
+
throw new TypeError("mintMemoryCanary: 'content' must be a string");
|
|
44
|
+
}
|
|
45
|
+
const tokenBytes = Math.max(MIN_TOKEN_BYTES, options.tokenBytes ?? DEFAULT_TOKEN_BYTES);
|
|
46
|
+
const canaryToken = randomBytes(tokenBytes).toString("hex");
|
|
47
|
+
const contentHash = computeHash(id, content, canaryToken, tenantId);
|
|
48
|
+
return {
|
|
49
|
+
id,
|
|
50
|
+
content,
|
|
51
|
+
contentHash,
|
|
52
|
+
canaryToken,
|
|
53
|
+
createdAt: new Date(),
|
|
54
|
+
tenantId,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Verify a previously minted canary against the content read back from
|
|
59
|
+
* storage. Returns `valid: true` only if both the content matches what
|
|
60
|
+
* was sealed and the tenant binding (if any) matches.
|
|
61
|
+
*
|
|
62
|
+
* Use case 1 — mutation detection:
|
|
63
|
+
* ```ts
|
|
64
|
+
* const entry = mintMemoryCanary("fact:42", "Sky is blue.");
|
|
65
|
+
* await db.write({ ...entry });
|
|
66
|
+
*
|
|
67
|
+
* // ... later ...
|
|
68
|
+
* const stored = await db.read("fact:42");
|
|
69
|
+
* const ver = verifyMemoryCanary(stored, stored.content);
|
|
70
|
+
* if (!ver.valid) {
|
|
71
|
+
* logger.security("Memory poisoning suspected", { id, reason: ver.reason });
|
|
72
|
+
* }
|
|
73
|
+
* ```
|
|
74
|
+
*
|
|
75
|
+
* Use case 2 — cross-tenant leak detection:
|
|
76
|
+
* ```ts
|
|
77
|
+
* // tenant A reads what should be a tenant-B-only entry
|
|
78
|
+
* const ver = verifyMemoryCanary(entry, entry.content, { tenantId: "tenant-A" });
|
|
79
|
+
* // ver.reason === "tenant_mismatch"
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
export function verifyMemoryCanary(entry, observedContent, options = {}) {
|
|
83
|
+
if (!entry || typeof entry !== "object") {
|
|
84
|
+
return { valid: false, reason: "canary_missing" };
|
|
85
|
+
}
|
|
86
|
+
if (typeof entry.canaryToken !== "string" ||
|
|
87
|
+
entry.canaryToken.length < MIN_TOKEN_BYTES * 2) {
|
|
88
|
+
return { valid: false, reason: "canary_missing" };
|
|
89
|
+
}
|
|
90
|
+
if (typeof observedContent !== "string") {
|
|
91
|
+
return { valid: false, reason: "content_mutated" };
|
|
92
|
+
}
|
|
93
|
+
// Tenant binding is fail-closed. If the entry was minted with a
|
|
94
|
+
// tenantId, the caller MUST supply the same tenantId. A caller that
|
|
95
|
+
// omits `options.tenantId` against a tenant-bound entry surfaces as
|
|
96
|
+
// a leak rather than a silent pass (Critic C2 from round 1 review).
|
|
97
|
+
if (entry.tenantId !== undefined) {
|
|
98
|
+
if (options.tenantId === undefined ||
|
|
99
|
+
options.tenantId !== entry.tenantId) {
|
|
100
|
+
return {
|
|
101
|
+
valid: false,
|
|
102
|
+
reason: "tenant_mismatch",
|
|
103
|
+
observed: observedContent,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
else if (options.tenantId !== undefined &&
|
|
108
|
+
entry.tenantId !== options.tenantId) {
|
|
109
|
+
// Caller passed a tenantId but the entry has none — also a mismatch.
|
|
110
|
+
return {
|
|
111
|
+
valid: false,
|
|
112
|
+
reason: "tenant_mismatch",
|
|
113
|
+
observed: observedContent,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
const expectedHash = computeHash(entry.id, observedContent, entry.canaryToken, entry.tenantId);
|
|
117
|
+
if (!hashesEqual(expectedHash, entry.contentHash)) {
|
|
118
|
+
return {
|
|
119
|
+
valid: false,
|
|
120
|
+
reason: observedContent === entry.content ? "hash_mismatch" : "content_mutated",
|
|
121
|
+
observed: observedContent,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
return { valid: true };
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Re-mint a canary after legitimate content edit. Returns a new
|
|
128
|
+
* sealed entry that supersedes the old one. The old `canaryToken`
|
|
129
|
+
* is rotated so a replay of the previous hash is also invalidated.
|
|
130
|
+
*/
|
|
131
|
+
export function rotateMemoryCanary(prev, newContent) {
|
|
132
|
+
return mintMemoryCanary(prev.id, newContent, prev.tenantId);
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Inject a *sentinel* memory entry — a decoy fact whose mutation would
|
|
136
|
+
* indicate the store was tampered with. Pair with `findSentinelMutations()`
|
|
137
|
+
* in a periodic sweep over the memory store.
|
|
138
|
+
*
|
|
139
|
+
* Returns a `MemoryCanaryEntry` with deterministic content that callers
|
|
140
|
+
* can recognise. The content includes the canary token so even content
|
|
141
|
+
* inspection (not just hash compare) catches mutation.
|
|
142
|
+
*/
|
|
143
|
+
export function buildSentinelEntry(scope, tenantId) {
|
|
144
|
+
// ID combines timestamp (for ordering) + random suffix (so enumeration
|
|
145
|
+
// by approximate-time guessing is infeasible — Critic M2 round 1).
|
|
146
|
+
const idSuffix = randomBytes(4).toString("hex");
|
|
147
|
+
const id = `ai-shield-sentinel:${scope}:${Date.now().toString(36)}-${idSuffix}`;
|
|
148
|
+
const nonce = randomBytes(8).toString("hex");
|
|
149
|
+
const content = `[AI-Shield sentinel — do not modify] scope=${scope} nonce=${nonce}`;
|
|
150
|
+
return mintMemoryCanary(id, content, tenantId);
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Bulk verify a set of stored entries against their canaries. Returns
|
|
154
|
+
* the IDs that failed verification, with the reason.
|
|
155
|
+
*/
|
|
156
|
+
export function bulkVerify(entries) {
|
|
157
|
+
const failures = [];
|
|
158
|
+
for (const e of entries) {
|
|
159
|
+
const v = verifyMemoryCanary(e.canary, e.observedContent, {
|
|
160
|
+
tenantId: e.expectedTenantId,
|
|
161
|
+
});
|
|
162
|
+
if (!v.valid && v.reason) {
|
|
163
|
+
failures.push({ id: e.canary.id, reason: v.reason });
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return failures;
|
|
167
|
+
}
|
|
168
|
+
// --- Internal helpers ---
|
|
169
|
+
function computeHash(id, content, token, tenantId) {
|
|
170
|
+
return createHash("sha256")
|
|
171
|
+
.update(id)
|
|
172
|
+
.update("\0")
|
|
173
|
+
.update(content)
|
|
174
|
+
.update("\0")
|
|
175
|
+
.update(token)
|
|
176
|
+
.update("\0")
|
|
177
|
+
.update(tenantId ?? "")
|
|
178
|
+
.digest("hex");
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Constant-time hex string compare. Falls back to a non-timing-safe
|
|
182
|
+
* direct compare only when lengths differ (which is itself the leak
|
|
183
|
+
* we want surfaced as a `false`, so this is fine).
|
|
184
|
+
*/
|
|
185
|
+
function hashesEqual(a, b) {
|
|
186
|
+
if (typeof a !== "string" || typeof b !== "string")
|
|
187
|
+
return false;
|
|
188
|
+
if (a.length !== b.length)
|
|
189
|
+
return false;
|
|
190
|
+
// Safe — both buffers have identical length, both come from
|
|
191
|
+
// hex(SHA-256), no untrusted input.
|
|
192
|
+
return timingSafeEqual(Buffer.from(a, "hex"), Buffer.from(b, "hex"));
|
|
193
|
+
}
|
|
194
|
+
//# sourceMappingURL=memory.js.map
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import type { TrustTier, WrappedContext, ScanDecision, Violation } from "../types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Input shape for `wrapContext()`. Each named field is conventional;
|
|
4
|
+
* pass only what applies.
|
|
5
|
+
*/
|
|
6
|
+
export interface WrapContextInput {
|
|
7
|
+
/** Developer-controlled prompt. Always `trust: "system"`. */
|
|
8
|
+
system?: string;
|
|
9
|
+
/** Direct user message(s). `trust: "untrusted"`, `source: "user"`. */
|
|
10
|
+
user?: string | string[];
|
|
11
|
+
/** Retrieved documents. `trust: "untrusted"`, `source: "rag"`. */
|
|
12
|
+
retrieved?: Array<{
|
|
13
|
+
content: string;
|
|
14
|
+
label?: string;
|
|
15
|
+
} | string>;
|
|
16
|
+
/** MCP / function tool descriptions about to be exposed to the model. */
|
|
17
|
+
tools?: Array<{
|
|
18
|
+
content: string;
|
|
19
|
+
label?: string;
|
|
20
|
+
} | string>;
|
|
21
|
+
/** Stored memory facts. `trust: "untrusted"`, `source: "memory"`. */
|
|
22
|
+
memory?: Array<{
|
|
23
|
+
content: string;
|
|
24
|
+
label?: string;
|
|
25
|
+
} | string>;
|
|
26
|
+
/** Scraped / fetched web content. */
|
|
27
|
+
web?: Array<{
|
|
28
|
+
content: string;
|
|
29
|
+
label?: string;
|
|
30
|
+
} | string>;
|
|
31
|
+
/** Output from another agent (multi-agent pipelines). */
|
|
32
|
+
agentOutput?: Array<{
|
|
33
|
+
content: string;
|
|
34
|
+
label?: string;
|
|
35
|
+
} | string>;
|
|
36
|
+
/**
|
|
37
|
+
* Promote specific named segments to `"trusted"` (e.g. an internal
|
|
38
|
+
* knowledge base whose contents you control end-to-end).
|
|
39
|
+
* Match is by `label` substring, case-insensitive.
|
|
40
|
+
*/
|
|
41
|
+
trustedLabels?: string[];
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Build a `WrappedContext` from typed inputs.
|
|
45
|
+
*
|
|
46
|
+
* Trust assignment:
|
|
47
|
+
* - `system` -> system
|
|
48
|
+
* - `retrieved`/`tools`/`memory`/`web`/`agent-output` -> untrusted
|
|
49
|
+
* - `user` -> untrusted (a user is not trusted in this threat model — they
|
|
50
|
+
* can also inject; the `untrusted` label means "scan aggressively")
|
|
51
|
+
* - any segment whose `label` matches one of `trustedLabels` -> trusted
|
|
52
|
+
*
|
|
53
|
+
* Trust does NOT mean "skip scanning". It only governs how
|
|
54
|
+
* `assemblePrompt()` and the per-segment policy decide whether to
|
|
55
|
+
* include the segment in the final assembled prompt.
|
|
56
|
+
*/
|
|
57
|
+
export declare function wrapContext(input: WrapContextInput): WrappedContext;
|
|
58
|
+
/**
|
|
59
|
+
* Scan every segment with the source-specific ingestion profile.
|
|
60
|
+
* Mutates `ctx` in place by attaching `scanResults` + `decision`,
|
|
61
|
+
* AND returns the same object for chaining.
|
|
62
|
+
*/
|
|
63
|
+
export declare function scanWrappedContext(ctx: WrappedContext, options?: {
|
|
64
|
+
strictness?: "low" | "medium" | "high";
|
|
65
|
+
}): Promise<WrappedContext>;
|
|
66
|
+
/**
|
|
67
|
+
* Assemble a prompt string respecting tier boundaries.
|
|
68
|
+
*
|
|
69
|
+
* Order: `system` → `trusted` retrieved/memory/tool-desc → `user`
|
|
70
|
+
* → all remaining `untrusted` segments wrapped in fenced markers.
|
|
71
|
+
*
|
|
72
|
+
* Why `trusted` before `user`? Putting developer-marked trusted
|
|
73
|
+
* context above the user message reduces the chance an untrusted user
|
|
74
|
+
* prompt re-frames the trusted reference material below it.
|
|
75
|
+
*
|
|
76
|
+
* Untrusted segments are wrapped in an explicit fence so a downstream
|
|
77
|
+
* model has a chance to attend to provenance. This is not a guarantee
|
|
78
|
+
* (no in-band marker is) but it is the single highest-leverage
|
|
79
|
+
* mitigation we can apply at the toolkit layer per Anthropic +
|
|
80
|
+
* OpenAI Model Spec guidance.
|
|
81
|
+
*
|
|
82
|
+
* Pass `strictMode: true` to OMIT blocked segments entirely. Default
|
|
83
|
+
* keeps them but fences them with a `<BLOCKED>` marker so an auditor
|
|
84
|
+
* can see what was tried.
|
|
85
|
+
*/
|
|
86
|
+
export interface AssembleOptions {
|
|
87
|
+
strictMode?: boolean;
|
|
88
|
+
/** Custom fence labels. Defaults are sensible. */
|
|
89
|
+
fences?: {
|
|
90
|
+
untrusted?: {
|
|
91
|
+
open: string;
|
|
92
|
+
close: string;
|
|
93
|
+
};
|
|
94
|
+
blocked?: {
|
|
95
|
+
open: string;
|
|
96
|
+
close: string;
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
export declare function assemblePrompt(ctx: WrappedContext, options?: AssembleOptions): string;
|
|
101
|
+
/**
|
|
102
|
+
* Convenience aggregator: violations across all scanned segments.
|
|
103
|
+
*/
|
|
104
|
+
export declare function flattenViolations(ctx: WrappedContext): Violation[];
|
|
105
|
+
export interface AgentHop {
|
|
106
|
+
/** The agent that PRODUCED the payload entering this hop. */
|
|
107
|
+
agentId: string;
|
|
108
|
+
/** Trust tier the payload was treated as at this hop. */
|
|
109
|
+
trust: TrustTier;
|
|
110
|
+
/** Scan decision for this hop's payload. */
|
|
111
|
+
decision: ScanDecision;
|
|
112
|
+
/** Violations found at this hop. */
|
|
113
|
+
violations: Violation[];
|
|
114
|
+
}
|
|
115
|
+
export interface PropagateTrustOptions {
|
|
116
|
+
/**
|
|
117
|
+
* Trust tier of the producing agent's output. Defaults to `untrusted` —
|
|
118
|
+
* agent output is attacker-influenceable by construction. Only set to
|
|
119
|
+
* `trusted` for an agent whose output you control end-to-end.
|
|
120
|
+
*/
|
|
121
|
+
fromTrust?: TrustTier;
|
|
122
|
+
/**
|
|
123
|
+
* Chain returned by an earlier `propagateTrust()` call. Pass it to keep
|
|
124
|
+
* contamination sticky across A→B→C. Omit for the first link.
|
|
125
|
+
*/
|
|
126
|
+
priorChain?: AgentHop[];
|
|
127
|
+
/** Ingestion-scanner strictness for the contagion scan. Default `high`. */
|
|
128
|
+
strictness?: "low" | "medium" | "high";
|
|
129
|
+
}
|
|
130
|
+
export interface TrustPropagationResult {
|
|
131
|
+
/** No contamination anywhere in the chain (including prior hops). */
|
|
132
|
+
safe: boolean;
|
|
133
|
+
/** Worst decision across the whole chain — sticky (once block, stays). */
|
|
134
|
+
decision: ScanDecision;
|
|
135
|
+
/**
|
|
136
|
+
* Trust tier the RECEIVING agent should treat the payload as. Degrades to
|
|
137
|
+
* `untrusted` the moment this hop — or any prior hop — warns or blocks.
|
|
138
|
+
*/
|
|
139
|
+
effectiveTrust: TrustTier;
|
|
140
|
+
/** Full chain including this hop. Feed back as `priorChain` for the next. */
|
|
141
|
+
hops: AgentHop[];
|
|
142
|
+
/** Every violation across the chain, newest hop last. */
|
|
143
|
+
violations: Violation[];
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Scan one agent-to-agent hand-off and propagate trust along the chain.
|
|
147
|
+
*
|
|
148
|
+
* @param payload The producing agent's output (= consuming agent's input).
|
|
149
|
+
* @param fromAgentId Agent that produced `payload`.
|
|
150
|
+
* @param toAgentId Agent about to consume `payload`.
|
|
151
|
+
*
|
|
152
|
+
* @example
|
|
153
|
+
* ```ts
|
|
154
|
+
* import { propagateTrust } from "ai-shield-core";
|
|
155
|
+
*
|
|
156
|
+
* // A → B
|
|
157
|
+
* let chain = await propagateTrust(aOutput, "researcher", "planner");
|
|
158
|
+
* // B → C, contamination at A stays sticky through to C
|
|
159
|
+
* chain = await propagateTrust(bOutput, "planner", "executor", {
|
|
160
|
+
* priorChain: chain.hops,
|
|
161
|
+
* });
|
|
162
|
+
* if (chain.effectiveTrust !== "trusted" && !chain.safe) {
|
|
163
|
+
* // an upstream agent was poisoned — do not let the executor act on it
|
|
164
|
+
* haltPipeline(chain.violations);
|
|
165
|
+
* }
|
|
166
|
+
* ```
|
|
167
|
+
*/
|
|
168
|
+
export declare function propagateTrust(payload: string, fromAgentId: string, toAgentId: string, options?: PropagateTrustOptions): Promise<TrustPropagationResult>;
|
|
169
|
+
//# sourceMappingURL=wrap-context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wrap-context.d.ts","sourceRoot":"","sources":["../../src/context/wr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