@astrasyncai/verification-gateway 5.9.0 → 5.11.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/adapters/express.js +19 -1
- package/dist/adapters/express.js.map +1 -1
- package/dist/adapters/express.mjs +19 -1
- package/dist/adapters/express.mjs.map +1 -1
- package/dist/adapters/mcp.js +19 -1
- package/dist/adapters/mcp.js.map +1 -1
- package/dist/adapters/mcp.mjs +19 -1
- package/dist/adapters/mcp.mjs.map +1 -1
- package/dist/adapters/nextjs.js +19 -1
- package/dist/adapters/nextjs.js.map +1 -1
- package/dist/adapters/nextjs.mjs +19 -1
- package/dist/adapters/nextjs.mjs.map +1 -1
- package/dist/adapters/sdk.js +19 -1
- package/dist/adapters/sdk.js.map +1 -1
- package/dist/adapters/sdk.mjs +19 -1
- package/dist/adapters/sdk.mjs.map +1 -1
- package/dist/agent/index.js +1 -1
- package/dist/agent/index.js.map +1 -1
- package/dist/agent/index.mjs +1 -1
- package/dist/agent/index.mjs.map +1 -1
- package/dist/bin/astrasync-claude-hook.js +540 -10
- package/dist/bin/astrasync-codex-hook.js +540 -10
- package/dist/bin/astrasync-guard.js +629 -65
- package/dist/bin/astrasync.js +673 -66
- package/dist/browser/background.js +19 -1
- package/dist/browser/background.js.map +1 -1
- package/dist/browser/background.mjs +19 -1
- package/dist/browser/background.mjs.map +1 -1
- package/dist/cli/index.d.mts +8 -8
- package/dist/cli/index.d.ts +8 -8
- package/dist/cli/index.js +22 -64
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/index.mjs +22 -64
- package/dist/cli/index.mjs.map +1 -1
- package/dist/codex/index.d.mts +5 -0
- package/dist/codex/index.d.ts +5 -0
- package/dist/codex/index.js +556 -18
- package/dist/codex/index.js.map +1 -1
- package/dist/codex/index.mjs +558 -20
- package/dist/codex/index.mjs.map +1 -1
- package/dist/cursor/extension.d.mts +9 -2
- package/dist/cursor/extension.d.ts +9 -2
- package/dist/cursor/extension.js +522 -5
- package/dist/cursor/extension.js.map +1 -1
- package/dist/cursor/extension.mjs +524 -5
- package/dist/cursor/extension.mjs.map +1 -1
- package/dist/edge-config.js +1 -1
- package/dist/edge-config.js.map +1 -1
- package/dist/edge-config.mjs +1 -1
- package/dist/edge-config.mjs.map +1 -1
- package/dist/edge-core/index.js +20 -2
- package/dist/edge-core/index.js.map +1 -1
- package/dist/edge-core/index.mjs +20 -2
- package/dist/edge-core/index.mjs.map +1 -1
- package/dist/enrolment-store-8DyaIOLe.d.mts +26 -0
- package/dist/enrolment-store-8DyaIOLe.d.ts +26 -0
- package/dist/gateway/gateway.js +19 -1
- package/dist/gateway/gateway.js.map +1 -1
- package/dist/gateway/gateway.mjs +19 -1
- package/dist/gateway/gateway.mjs.map +1 -1
- package/dist/index.d.mts +100 -3
- package/dist/index.d.ts +100 -3
- package/dist/index.js +3289 -18
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +3287 -18
- package/dist/index.mjs.map +1 -1
- package/dist/registration/index.js +1 -1
- package/dist/registration/index.js.map +1 -1
- package/dist/registration/index.mjs +1 -1
- package/dist/registration/index.mjs.map +1 -1
- package/dist/transport/index.js +1 -1
- package/dist/transport/index.js.map +1 -1
- package/dist/transport/index.mjs +1 -1
- package/dist/transport/index.mjs.map +1 -1
- package/dist/verify.js +19 -1
- package/dist/verify.js.map +1 -1
- package/dist/verify.mjs +19 -1
- package/dist/verify.mjs.map +1 -1
- package/package.json +1 -1
|
@@ -25,7 +25,8 @@ __export(hook_exports, {
|
|
|
25
25
|
resolveGuardConfig: () => resolveGuardConfig
|
|
26
26
|
});
|
|
27
27
|
module.exports = __toCommonJS(hook_exports);
|
|
28
|
-
var
|
|
28
|
+
var import_os3 = require("os");
|
|
29
|
+
var import_path6 = require("path");
|
|
29
30
|
|
|
30
31
|
// src/gateway/types.ts
|
|
31
32
|
var HIGH_RISK_COMMANDS = [
|
|
@@ -3751,7 +3752,7 @@ function getTrustLevel(score) {
|
|
|
3751
3752
|
}
|
|
3752
3753
|
|
|
3753
3754
|
// src/version.ts
|
|
3754
|
-
var SDK_VERSION = "5.
|
|
3755
|
+
var SDK_VERSION = "5.11.0";
|
|
3755
3756
|
|
|
3756
3757
|
// src/http.ts
|
|
3757
3758
|
var SDK_USER_AGENT = `astrasync-sdk/${SDK_VERSION}`;
|
|
@@ -4070,6 +4071,24 @@ async function callVerifyAccessAPI(config, request) {
|
|
|
4070
4071
|
}
|
|
4071
4072
|
};
|
|
4072
4073
|
}
|
|
4074
|
+
if (response.status === 400 && data?.error === "INVALID_AGENT_ID") {
|
|
4075
|
+
const message = typeof data?.message === "string" ? data.message : "Agent identifier must be a canonical ASTRA-* id.";
|
|
4076
|
+
return {
|
|
4077
|
+
success: true,
|
|
4078
|
+
attemptId: typeof data?.attemptId === "string" ? data.attemptId : void 0,
|
|
4079
|
+
access: {
|
|
4080
|
+
allowed: false,
|
|
4081
|
+
reason: "invalid_agent_id",
|
|
4082
|
+
failures: [
|
|
4083
|
+
{
|
|
4084
|
+
dimension: "identity.invalid_agent_id",
|
|
4085
|
+
message,
|
|
4086
|
+
guidance: typeof data?.guidance === "string" ? data.guidance : "Present your canonical ASTRA-* id (discover it via GET /api/agents/request-registration/{requestId} after owner approval)."
|
|
4087
|
+
}
|
|
4088
|
+
]
|
|
4089
|
+
}
|
|
4090
|
+
};
|
|
4091
|
+
}
|
|
4073
4092
|
if (!response.ok) {
|
|
4074
4093
|
if (response.status === 401) {
|
|
4075
4094
|
return {
|
|
@@ -5151,22 +5170,118 @@ var ClaudeCodeAdapter = class {
|
|
|
5151
5170
|
};
|
|
5152
5171
|
|
|
5153
5172
|
// src/claude-code/guard-config.ts
|
|
5173
|
+
var import_fs5 = require("fs");
|
|
5174
|
+
var import_os2 = require("os");
|
|
5175
|
+
var import_path4 = require("path");
|
|
5176
|
+
|
|
5177
|
+
// src/fleet/enrolment-store.ts
|
|
5154
5178
|
var import_fs4 = require("fs");
|
|
5155
5179
|
var import_os = require("os");
|
|
5156
5180
|
var import_path3 = require("path");
|
|
5157
|
-
function
|
|
5181
|
+
function machineScopeDir(platform = process.platform, env = process.env) {
|
|
5182
|
+
if (platform === "darwin") return "/Library/Application Support/AstraSync";
|
|
5183
|
+
if (platform === "win32") return (0, import_path3.join)(env.ProgramData || "C:\\ProgramData", "AstraSync");
|
|
5184
|
+
return "/etc/astrasync";
|
|
5185
|
+
}
|
|
5186
|
+
function fleetPaths(options = {}) {
|
|
5187
|
+
const home = options.home ?? (0, import_os.homedir)();
|
|
5188
|
+
const machineDir = options.machineDir ?? options.env?.ASTRASYNC_FLEET_MACHINE_DIR ?? machineScopeDir(options.platform, options.env);
|
|
5189
|
+
const userDir = (0, import_path3.join)(home, ".astrasync", "fleet");
|
|
5190
|
+
return {
|
|
5191
|
+
machineDevice: (0, import_path3.join)(machineDir, "fleet.json"),
|
|
5192
|
+
machinePolicy: (0, import_path3.join)(machineDir, "policy.jws"),
|
|
5193
|
+
userDir,
|
|
5194
|
+
userDevice: (0, import_path3.join)(userDir, "device.json"),
|
|
5195
|
+
userPolicy: (0, import_path3.join)(userDir, "policy.jws"),
|
|
5196
|
+
userKeyChain: (0, import_path3.join)(userDir, "keychain.json"),
|
|
5197
|
+
state: (0, import_path3.join)(userDir, "state.json")
|
|
5198
|
+
};
|
|
5199
|
+
}
|
|
5200
|
+
function isEnrolment(value) {
|
|
5201
|
+
const v = value;
|
|
5202
|
+
return !!v && typeof v.deviceId === "string" && typeof v.credential === "string" && typeof v.apiBaseUrl === "string" && !!v.org && typeof v.org.ownerId === "string" && !!v.jwks && Array.isArray(v.jwks.keys);
|
|
5203
|
+
}
|
|
5204
|
+
function loadEnrolment(paths) {
|
|
5205
|
+
const candidates = [
|
|
5206
|
+
[paths.machineDevice, "machine"],
|
|
5207
|
+
[paths.userDevice, "user"]
|
|
5208
|
+
];
|
|
5209
|
+
for (const [path, scope] of candidates) {
|
|
5210
|
+
if (!(0, import_fs4.existsSync)(path)) continue;
|
|
5211
|
+
try {
|
|
5212
|
+
const parsed = JSON.parse((0, import_fs4.readFileSync)(path, "utf-8"));
|
|
5213
|
+
if (isEnrolment(parsed)) return { enrolment: parsed, scope };
|
|
5214
|
+
} catch {
|
|
5215
|
+
}
|
|
5216
|
+
return { corrupt: true, path };
|
|
5217
|
+
}
|
|
5218
|
+
return null;
|
|
5219
|
+
}
|
|
5220
|
+
function writeFileMode(path, content, mode) {
|
|
5221
|
+
(0, import_fs4.mkdirSync)((0, import_path3.dirname)(path), { recursive: true, mode: 448 });
|
|
5222
|
+
(0, import_fs4.writeFileSync)(path, content, { encoding: "utf-8", mode });
|
|
5223
|
+
try {
|
|
5224
|
+
(0, import_fs4.chmodSync)(path, mode);
|
|
5225
|
+
} catch {
|
|
5226
|
+
}
|
|
5227
|
+
}
|
|
5228
|
+
function readCachedPolicies(paths) {
|
|
5229
|
+
const out = [];
|
|
5230
|
+
for (const p of [paths.userPolicy, paths.machinePolicy]) {
|
|
5231
|
+
try {
|
|
5232
|
+
if ((0, import_fs4.existsSync)(p)) out.push((0, import_fs4.readFileSync)(p, "utf-8").trim());
|
|
5233
|
+
} catch {
|
|
5234
|
+
}
|
|
5235
|
+
}
|
|
5236
|
+
return out.filter(Boolean);
|
|
5237
|
+
}
|
|
5238
|
+
function writeCachedPolicy(paths, jws) {
|
|
5239
|
+
writeFileMode(paths.userPolicy, jws, 384);
|
|
5240
|
+
}
|
|
5241
|
+
function readKeyChain(paths) {
|
|
5242
|
+
try {
|
|
5243
|
+
const parsed = JSON.parse((0, import_fs4.readFileSync)(paths.userKeyChain, "utf-8"));
|
|
5244
|
+
return Array.isArray(parsed) ? parsed.filter((j) => typeof j === "string") : [];
|
|
5245
|
+
} catch {
|
|
5246
|
+
return [];
|
|
5247
|
+
}
|
|
5248
|
+
}
|
|
5249
|
+
function writeKeyChain(paths, chain) {
|
|
5250
|
+
writeFileMode(paths.userKeyChain, JSON.stringify(chain), 384);
|
|
5251
|
+
}
|
|
5252
|
+
function readState(paths) {
|
|
5253
|
+
try {
|
|
5254
|
+
const parsed = JSON.parse((0, import_fs4.readFileSync)(paths.state, "utf-8"));
|
|
5255
|
+
const c = parsed.counts;
|
|
5256
|
+
return {
|
|
5257
|
+
...parsed,
|
|
5258
|
+
counts: { allow: c?.allow ?? 0, deny: c?.deny ?? 0, ask: c?.ask ?? 0 }
|
|
5259
|
+
};
|
|
5260
|
+
} catch {
|
|
5261
|
+
return { counts: { allow: 0, deny: 0, ask: 0 } };
|
|
5262
|
+
}
|
|
5263
|
+
}
|
|
5264
|
+
function writeState(paths, state) {
|
|
5265
|
+
try {
|
|
5266
|
+
writeFileMode(paths.state, JSON.stringify(state, null, 2), 384);
|
|
5267
|
+
} catch {
|
|
5268
|
+
}
|
|
5269
|
+
}
|
|
5270
|
+
|
|
5271
|
+
// src/claude-code/guard-config.ts
|
|
5272
|
+
function resolveGuardConfig(env, payloadCwd, home = (0, import_os2.homedir)()) {
|
|
5158
5273
|
const candidateRoots = [env.CLAUDE_PROJECT_DIR, payloadCwd, home].filter(
|
|
5159
5274
|
(root) => typeof root === "string" && root.length > 0
|
|
5160
5275
|
);
|
|
5161
5276
|
for (const root of candidateRoots) {
|
|
5162
|
-
const guardDir = (0,
|
|
5163
|
-
const policyFile = (0,
|
|
5164
|
-
if (!(0,
|
|
5277
|
+
const guardDir = (0, import_path4.join)(root, ".astrasync");
|
|
5278
|
+
const policyFile = (0, import_path4.join)(guardDir, "policy.yaml");
|
|
5279
|
+
if (!(0, import_fs5.existsSync)(policyFile)) continue;
|
|
5165
5280
|
let settings = {};
|
|
5166
|
-
const settingsFile = (0,
|
|
5167
|
-
if ((0,
|
|
5281
|
+
const settingsFile = (0, import_path4.join)(guardDir, "guard.json");
|
|
5282
|
+
if ((0, import_fs5.existsSync)(settingsFile)) {
|
|
5168
5283
|
try {
|
|
5169
|
-
settings = JSON.parse((0,
|
|
5284
|
+
settings = JSON.parse((0, import_fs5.readFileSync)(settingsFile, "utf-8"));
|
|
5170
5285
|
} catch {
|
|
5171
5286
|
settings = {};
|
|
5172
5287
|
}
|
|
@@ -5175,6 +5290,408 @@ function resolveGuardConfig(env, payloadCwd, home = (0, import_os.homedir)()) {
|
|
|
5175
5290
|
}
|
|
5176
5291
|
return null;
|
|
5177
5292
|
}
|
|
5293
|
+
var FLEET_ENROL_INSTRUCTION = "This machine must be enrolled: run `astrasync guard enroll <token>` (token from your organisation admin)";
|
|
5294
|
+
function resolveFleet(env, home = (0, import_os2.homedir)(), paths = fleetPaths({ home, env })) {
|
|
5295
|
+
const loaded = loadEnrolment(paths);
|
|
5296
|
+
if (loaded && "corrupt" in loaded) {
|
|
5297
|
+
return {
|
|
5298
|
+
kind: "fleet-missing",
|
|
5299
|
+
reason: `fleet enrolment at ${loaded.path} is unreadable \u2014 ${FLEET_ENROL_INSTRUCTION}`,
|
|
5300
|
+
paths
|
|
5301
|
+
};
|
|
5302
|
+
}
|
|
5303
|
+
if (loaded) return { kind: "fleet", enrolment: loaded.enrolment, scope: loaded.scope, paths };
|
|
5304
|
+
if (env.ASTRASYNC_FLEET_REQUIRED === "1") {
|
|
5305
|
+
return { kind: "fleet-missing", reason: FLEET_ENROL_INSTRUCTION, paths };
|
|
5306
|
+
}
|
|
5307
|
+
return null;
|
|
5308
|
+
}
|
|
5309
|
+
|
|
5310
|
+
// src/fleet/hook-runner.ts
|
|
5311
|
+
var import_path5 = require("path");
|
|
5312
|
+
|
|
5313
|
+
// src/fleet/client.ts
|
|
5314
|
+
var FleetApiError = class extends Error {
|
|
5315
|
+
constructor(message, status, code) {
|
|
5316
|
+
super(message);
|
|
5317
|
+
this.status = status;
|
|
5318
|
+
this.code = code;
|
|
5319
|
+
this.name = "FleetApiError";
|
|
5320
|
+
}
|
|
5321
|
+
};
|
|
5322
|
+
function base(url) {
|
|
5323
|
+
return url.replace(/\/+$/, "");
|
|
5324
|
+
}
|
|
5325
|
+
async function call(url, init, timeoutMs) {
|
|
5326
|
+
const controller = new AbortController();
|
|
5327
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
5328
|
+
try {
|
|
5329
|
+
const res = await sdkFetch(url, { ...init, signal: controller.signal });
|
|
5330
|
+
const text = await res.text();
|
|
5331
|
+
let body = text;
|
|
5332
|
+
try {
|
|
5333
|
+
body = text ? JSON.parse(text) : null;
|
|
5334
|
+
} catch {
|
|
5335
|
+
}
|
|
5336
|
+
return { status: res.status, body, headers: res.headers };
|
|
5337
|
+
} finally {
|
|
5338
|
+
clearTimeout(timer);
|
|
5339
|
+
}
|
|
5340
|
+
}
|
|
5341
|
+
function apiError(status, body, what) {
|
|
5342
|
+
const b = body ?? {};
|
|
5343
|
+
const code = b.code ?? (typeof b.error === "string" ? b.error : void 0);
|
|
5344
|
+
return new FleetApiError(`${what} failed (${status}${code ? ` ${code}` : ""})`, status, code);
|
|
5345
|
+
}
|
|
5346
|
+
function deviceHeaders(enrolment) {
|
|
5347
|
+
return { Authorization: `Device ${enrolment.credential}` };
|
|
5348
|
+
}
|
|
5349
|
+
function policyEtag(sha256, kid) {
|
|
5350
|
+
return `${sha256}.${kid ?? ""}`;
|
|
5351
|
+
}
|
|
5352
|
+
async function fetchFleetPolicy(enrolment, ifNoneMatch, timeoutMs = 1500) {
|
|
5353
|
+
const headers = deviceHeaders(enrolment);
|
|
5354
|
+
if (ifNoneMatch) headers["If-None-Match"] = `"${ifNoneMatch}"`;
|
|
5355
|
+
const r = await call(`${base(enrolment.apiBaseUrl)}/fleet/policy`, { headers }, timeoutMs);
|
|
5356
|
+
if (r.status === 304) return { status: "unchanged" };
|
|
5357
|
+
if (r.status !== 200) throw apiError(r.status, r.body, "Policy fetch");
|
|
5358
|
+
const data = r.body;
|
|
5359
|
+
if (!data?.policyJws) throw new FleetApiError("Policy response has no policyJws", r.status);
|
|
5360
|
+
return {
|
|
5361
|
+
status: "ok",
|
|
5362
|
+
policyJws: data.policyJws,
|
|
5363
|
+
sha256: data.sha256 ?? "",
|
|
5364
|
+
version: data.version ?? 0,
|
|
5365
|
+
keyChain: Array.isArray(data.keyChain) ? data.keyChain.filter((j) => typeof j === "string") : []
|
|
5366
|
+
};
|
|
5367
|
+
}
|
|
5368
|
+
async function sendHeartbeat(enrolment, request, timeoutMs = 1500) {
|
|
5369
|
+
const r = await call(
|
|
5370
|
+
`${base(enrolment.apiBaseUrl)}/fleet/heartbeat`,
|
|
5371
|
+
{
|
|
5372
|
+
method: "POST",
|
|
5373
|
+
headers: { ...deviceHeaders(enrolment), "Content-Type": "application/json" },
|
|
5374
|
+
body: JSON.stringify(request)
|
|
5375
|
+
},
|
|
5376
|
+
timeoutMs
|
|
5377
|
+
);
|
|
5378
|
+
if (r.status !== 200) throw apiError(r.status, r.body, "Heartbeat");
|
|
5379
|
+
return r.body;
|
|
5380
|
+
}
|
|
5381
|
+
|
|
5382
|
+
// src/fleet/policy-verify.ts
|
|
5383
|
+
var import_crypto = require("crypto");
|
|
5384
|
+
var import_jose2 = require("jose");
|
|
5385
|
+
|
|
5386
|
+
// src/fleet/types.ts
|
|
5387
|
+
var FLEET_POLICY_TYP = "fleet-policy+jwt";
|
|
5388
|
+
var FLEET_POLICY_AUD = "astrasync:fleet-policy";
|
|
5389
|
+
var FLEET_POLICY_ISS = "astrasync.ai";
|
|
5390
|
+
var FLEET_KEYS_TYP = "fleet-keys+jwt";
|
|
5391
|
+
var FLEET_KEYS_AUD = "astrasync:fleet-keys";
|
|
5392
|
+
var DEFAULT_HEARTBEAT_INTERVAL_SEC = 900;
|
|
5393
|
+
|
|
5394
|
+
// src/fleet/policy-verify.ts
|
|
5395
|
+
var FleetPolicyError = class extends Error {
|
|
5396
|
+
constructor(message) {
|
|
5397
|
+
super(message);
|
|
5398
|
+
this.name = "FleetPolicyError";
|
|
5399
|
+
}
|
|
5400
|
+
};
|
|
5401
|
+
function sha256Hex(text) {
|
|
5402
|
+
return (0, import_crypto.createHash)("sha256").update(text, "utf8").digest("hex");
|
|
5403
|
+
}
|
|
5404
|
+
function parseSettings(raw) {
|
|
5405
|
+
if (!raw || typeof raw !== "object") throw new FleetPolicyError("settings missing");
|
|
5406
|
+
const s = raw;
|
|
5407
|
+
if (s.posture !== "active" && s.posture !== "passive") {
|
|
5408
|
+
throw new FleetPolicyError("settings.posture must be active or passive");
|
|
5409
|
+
}
|
|
5410
|
+
if (s.failOpen !== void 0 && typeof s.failOpen !== "boolean") {
|
|
5411
|
+
throw new FleetPolicyError("settings.failOpen must be a boolean");
|
|
5412
|
+
}
|
|
5413
|
+
if (s.governMcp !== void 0 && typeof s.governMcp !== "boolean") {
|
|
5414
|
+
throw new FleetPolicyError("settings.governMcp must be a boolean");
|
|
5415
|
+
}
|
|
5416
|
+
if (s.skipTools !== void 0 && !(Array.isArray(s.skipTools) && s.skipTools.every((t) => typeof t === "string"))) {
|
|
5417
|
+
throw new FleetPolicyError("settings.skipTools must be a string array");
|
|
5418
|
+
}
|
|
5419
|
+
const num = (v) => typeof v === "number" && Number.isFinite(v) && v > 0 ? v : void 0;
|
|
5420
|
+
return {
|
|
5421
|
+
posture: s.posture,
|
|
5422
|
+
failOpen: s.failOpen === true,
|
|
5423
|
+
...typeof s.governMcp === "boolean" && { governMcp: s.governMcp },
|
|
5424
|
+
...Array.isArray(s.skipTools) && { skipTools: s.skipTools },
|
|
5425
|
+
...num(s.heartbeatIntervalSec) && { heartbeatIntervalSec: num(s.heartbeatIntervalSec) },
|
|
5426
|
+
...num(s.staleAfterSec) && { staleAfterSec: num(s.staleAfterSec) }
|
|
5427
|
+
};
|
|
5428
|
+
}
|
|
5429
|
+
async function verifyFleetPolicy(jws, jwks, options) {
|
|
5430
|
+
let payloadBytes;
|
|
5431
|
+
let header;
|
|
5432
|
+
try {
|
|
5433
|
+
const keySet = (0, import_jose2.createLocalJWKSet)(jwks);
|
|
5434
|
+
const result = await (0, import_jose2.compactVerify)(jws, keySet, { algorithms: ["ES256"] });
|
|
5435
|
+
payloadBytes = result.payload;
|
|
5436
|
+
header = result.protectedHeader;
|
|
5437
|
+
} catch (err) {
|
|
5438
|
+
throw new FleetPolicyError(
|
|
5439
|
+
`signature verification failed (${err instanceof Error ? err.message : String(err)})`
|
|
5440
|
+
);
|
|
5441
|
+
}
|
|
5442
|
+
if (header.typ !== FLEET_POLICY_TYP) throw new FleetPolicyError(`unexpected typ ${header.typ}`);
|
|
5443
|
+
let claims;
|
|
5444
|
+
try {
|
|
5445
|
+
claims = JSON.parse(new TextDecoder().decode(payloadBytes));
|
|
5446
|
+
} catch {
|
|
5447
|
+
throw new FleetPolicyError("payload is not JSON");
|
|
5448
|
+
}
|
|
5449
|
+
if (claims.aud !== FLEET_POLICY_AUD) throw new FleetPolicyError(`unexpected aud ${claims.aud}`);
|
|
5450
|
+
if (claims.iss !== FLEET_POLICY_ISS) throw new FleetPolicyError(`unexpected iss ${claims.iss}`);
|
|
5451
|
+
if (!options.expectedSubs.includes(claims.sub)) {
|
|
5452
|
+
throw new FleetPolicyError("policy was issued for a different organisation or device");
|
|
5453
|
+
}
|
|
5454
|
+
if (!Number.isInteger(claims.ver) || claims.ver < 1) {
|
|
5455
|
+
throw new FleetPolicyError("ver must be a positive integer");
|
|
5456
|
+
}
|
|
5457
|
+
if (options.minVer !== void 0 && claims.ver < options.minVer) {
|
|
5458
|
+
throw new FleetPolicyError(
|
|
5459
|
+
`policy version ${claims.ver} is older than the accepted version ${options.minVer} (rollback)`
|
|
5460
|
+
);
|
|
5461
|
+
}
|
|
5462
|
+
if (typeof claims.yaml !== "string" || sha256Hex(claims.yaml) !== claims.sha256) {
|
|
5463
|
+
throw new FleetPolicyError("policy hash mismatch");
|
|
5464
|
+
}
|
|
5465
|
+
const settings = parseSettings(claims.settings);
|
|
5466
|
+
const parsed = parseYaml(claims.yaml);
|
|
5467
|
+
if (!parsed.policy) {
|
|
5468
|
+
const first = parsed.validation.errors[0];
|
|
5469
|
+
throw new FleetPolicyError(
|
|
5470
|
+
`signed policy is invalid${first ? ` (${first.path}: ${first.message})` : ""}`
|
|
5471
|
+
);
|
|
5472
|
+
}
|
|
5473
|
+
return { kid: header.kid, claims: { ...claims, settings }, policy: parsed.policy, settings, jws };
|
|
5474
|
+
}
|
|
5475
|
+
async function resolveFleetJwks(pinned, chain, ownerId) {
|
|
5476
|
+
let trusted = pinned;
|
|
5477
|
+
let lastSeq = 0;
|
|
5478
|
+
for (const link of chain) {
|
|
5479
|
+
try {
|
|
5480
|
+
const { payload, protectedHeader } = await (0, import_jose2.compactVerify)(
|
|
5481
|
+
link,
|
|
5482
|
+
(0, import_jose2.createLocalJWKSet)(trusted),
|
|
5483
|
+
{ algorithms: ["ES256"] }
|
|
5484
|
+
);
|
|
5485
|
+
if (protectedHeader.typ !== FLEET_KEYS_TYP) continue;
|
|
5486
|
+
const c = JSON.parse(new TextDecoder().decode(payload));
|
|
5487
|
+
if (c.aud !== FLEET_KEYS_AUD || c.iss !== FLEET_POLICY_ISS || c.sub !== ownerId) continue;
|
|
5488
|
+
if (!Number.isInteger(c.seq) || c.seq <= lastSeq) continue;
|
|
5489
|
+
const keys = Array.isArray(c.keys) ? c.keys.filter((k) => k && k.kty === "EC" && k.crv === "P-256" && !("d" in k)) : [];
|
|
5490
|
+
if (keys.length === 0) continue;
|
|
5491
|
+
trusted = { keys };
|
|
5492
|
+
lastSeq = c.seq;
|
|
5493
|
+
} catch {
|
|
5494
|
+
}
|
|
5495
|
+
}
|
|
5496
|
+
return trusted;
|
|
5497
|
+
}
|
|
5498
|
+
async function pickNewestValid(candidates, jwks, options) {
|
|
5499
|
+
let best = null;
|
|
5500
|
+
let lastError = new FleetPolicyError("no cached fleet policy");
|
|
5501
|
+
for (const jws of candidates) {
|
|
5502
|
+
try {
|
|
5503
|
+
const v = await verifyFleetPolicy(jws, jwks, options);
|
|
5504
|
+
if (!best || v.claims.ver > best.claims.ver) best = v;
|
|
5505
|
+
} catch (err) {
|
|
5506
|
+
lastError = err instanceof Error ? err : new FleetPolicyError(String(err));
|
|
5507
|
+
}
|
|
5508
|
+
}
|
|
5509
|
+
if (!best) throw lastError;
|
|
5510
|
+
return best;
|
|
5511
|
+
}
|
|
5512
|
+
|
|
5513
|
+
// src/fleet/runtime.ts
|
|
5514
|
+
var FLEET_SYNC_BUDGET_MS = 1500;
|
|
5515
|
+
function expectedSubs(enrolment) {
|
|
5516
|
+
return [enrolment.org.ownerId, enrolment.deviceId];
|
|
5517
|
+
}
|
|
5518
|
+
function fleetJwks(ctx, chain = readKeyChain(ctx.paths)) {
|
|
5519
|
+
return resolveFleetJwks(ctx.enrolment.jwks, chain, ctx.enrolment.org.ownerId);
|
|
5520
|
+
}
|
|
5521
|
+
async function acceptFetched(ctx, fetched, verifyOpts) {
|
|
5522
|
+
const jwks = await fleetJwks(ctx, fetched.keyChain);
|
|
5523
|
+
const verified = await verifyFleetPolicy(fetched.policyJws, jwks, verifyOpts);
|
|
5524
|
+
if (fetched.keyChain.length > 0) writeKeyChain(ctx.paths, fetched.keyChain);
|
|
5525
|
+
writeCachedPolicy(ctx.paths, fetched.policyJws);
|
|
5526
|
+
return verified;
|
|
5527
|
+
}
|
|
5528
|
+
function remember(paths, state, verified) {
|
|
5529
|
+
state.appliedSha256 = verified.claims.sha256;
|
|
5530
|
+
state.maxVer = Math.max(state.maxVer ?? 0, verified.claims.ver);
|
|
5531
|
+
writeState(paths, state);
|
|
5532
|
+
}
|
|
5533
|
+
async function loadFleetPolicy(ctx, options = {}) {
|
|
5534
|
+
const state = readState(ctx.paths);
|
|
5535
|
+
const verifyOpts = { expectedSubs: expectedSubs(ctx.enrolment), minVer: state.maxVer };
|
|
5536
|
+
const cached = readCachedPolicies(ctx.paths);
|
|
5537
|
+
let cacheError = null;
|
|
5538
|
+
if (cached.length > 0) {
|
|
5539
|
+
try {
|
|
5540
|
+
const verified = await pickNewestValid(cached, await fleetJwks(ctx), verifyOpts);
|
|
5541
|
+
if (verified.claims.sha256 !== state.appliedSha256 || verified.claims.ver !== state.maxVer) {
|
|
5542
|
+
remember(ctx.paths, state, verified);
|
|
5543
|
+
}
|
|
5544
|
+
return verified;
|
|
5545
|
+
} catch (err) {
|
|
5546
|
+
cacheError = err instanceof Error ? err : new FleetPolicyError(String(err));
|
|
5547
|
+
}
|
|
5548
|
+
}
|
|
5549
|
+
try {
|
|
5550
|
+
const fetched = await fetchFleetPolicy(
|
|
5551
|
+
ctx.enrolment,
|
|
5552
|
+
null,
|
|
5553
|
+
options.fetchTimeoutMs ?? FLEET_SYNC_BUDGET_MS
|
|
5554
|
+
);
|
|
5555
|
+
if (fetched.status === "ok") {
|
|
5556
|
+
const verified = await acceptFetched(ctx, fetched, verifyOpts);
|
|
5557
|
+
remember(ctx.paths, state, verified);
|
|
5558
|
+
return verified;
|
|
5559
|
+
}
|
|
5560
|
+
} catch (err) {
|
|
5561
|
+
if (err instanceof FleetApiError && err.code === "DEVICE_REVOKED") {
|
|
5562
|
+
state.revoked = true;
|
|
5563
|
+
writeState(ctx.paths, state);
|
|
5564
|
+
}
|
|
5565
|
+
const detail = err instanceof Error ? err.message : String(err);
|
|
5566
|
+
throw new FleetPolicyError(
|
|
5567
|
+
cacheError ? `${cacheError.message}; refresh failed (${detail})` : `no fleet policy (${detail})`
|
|
5568
|
+
);
|
|
5569
|
+
}
|
|
5570
|
+
throw cacheError ?? new FleetPolicyError("no fleet policy available");
|
|
5571
|
+
}
|
|
5572
|
+
async function recordDecisionAndHeartbeat(ctx, applied, decision, options) {
|
|
5573
|
+
const now = options.now ?? Date.now;
|
|
5574
|
+
const state = readState(ctx.paths);
|
|
5575
|
+
if (decision) state.counts[decision] += 1;
|
|
5576
|
+
const intervalSec = applied.settings.heartbeatIntervalSec ?? ctx.enrolment.heartbeatIntervalSec ?? DEFAULT_HEARTBEAT_INTERVAL_SEC;
|
|
5577
|
+
const last = state.lastHeartbeatAt ? Date.parse(state.lastHeartbeatAt) : 0;
|
|
5578
|
+
if (now() - last < intervalSec * 1e3) {
|
|
5579
|
+
writeState(ctx.paths, state);
|
|
5580
|
+
return "throttled";
|
|
5581
|
+
}
|
|
5582
|
+
const deadline = now() + (options.budgetMs ?? FLEET_SYNC_BUDGET_MS);
|
|
5583
|
+
const remaining = () => Math.max(1, deadline - now());
|
|
5584
|
+
state.lastHeartbeatAt = new Date(now()).toISOString();
|
|
5585
|
+
let outcome = "sent";
|
|
5586
|
+
try {
|
|
5587
|
+
const hb = await sendHeartbeat(
|
|
5588
|
+
ctx.enrolment,
|
|
5589
|
+
{
|
|
5590
|
+
appliedSha256: applied.claims.sha256,
|
|
5591
|
+
guardVersion: options.guardVersion,
|
|
5592
|
+
adapters: [{ kind: options.adapterKind, managed: true }],
|
|
5593
|
+
posture: applied.settings.posture,
|
|
5594
|
+
decisionCounts: { ...state.counts }
|
|
5595
|
+
},
|
|
5596
|
+
remaining()
|
|
5597
|
+
);
|
|
5598
|
+
state.counts = { allow: 0, deny: 0, ask: 0 };
|
|
5599
|
+
state.revoked = false;
|
|
5600
|
+
delete state.lastError;
|
|
5601
|
+
const policyChanged = !!hb.activeSha256 && hb.activeSha256 !== applied.claims.sha256;
|
|
5602
|
+
const keyChanged = !!hb.activeKid && !!applied.kid && hb.activeKid !== applied.kid;
|
|
5603
|
+
if (policyChanged || keyChanged) {
|
|
5604
|
+
const fetched = await fetchFleetPolicy(
|
|
5605
|
+
ctx.enrolment,
|
|
5606
|
+
policyEtag(applied.claims.sha256, applied.kid),
|
|
5607
|
+
remaining()
|
|
5608
|
+
);
|
|
5609
|
+
if (fetched.status === "ok") {
|
|
5610
|
+
const verified = await acceptFetched(ctx, fetched, {
|
|
5611
|
+
expectedSubs: expectedSubs(ctx.enrolment),
|
|
5612
|
+
minVer: Math.max(state.maxVer ?? 0, applied.claims.ver)
|
|
5613
|
+
});
|
|
5614
|
+
state.appliedSha256 = verified.claims.sha256;
|
|
5615
|
+
state.maxVer = Math.max(state.maxVer ?? 0, verified.claims.ver);
|
|
5616
|
+
outcome = "refreshed";
|
|
5617
|
+
}
|
|
5618
|
+
}
|
|
5619
|
+
} catch (err) {
|
|
5620
|
+
if (err instanceof FleetApiError && err.code === "DEVICE_REVOKED") {
|
|
5621
|
+
state.revoked = true;
|
|
5622
|
+
outcome = "revoked";
|
|
5623
|
+
} else {
|
|
5624
|
+
state.lastError = err instanceof Error ? err.message : String(err);
|
|
5625
|
+
outcome = "failed";
|
|
5626
|
+
}
|
|
5627
|
+
}
|
|
5628
|
+
writeState(ctx.paths, state);
|
|
5629
|
+
return outcome;
|
|
5630
|
+
}
|
|
5631
|
+
|
|
5632
|
+
// src/fleet/hook-runner.ts
|
|
5633
|
+
var PREFIX = "AstraSync Local Guard (organisation fleet policy)";
|
|
5634
|
+
async function runFleetHook(args) {
|
|
5635
|
+
if (args.resolution.kind === "fleet-missing") {
|
|
5636
|
+
return { output: args.denyOutput(`${PREFIX}: ${args.resolution.reason}`), exitCode: 0 };
|
|
5637
|
+
}
|
|
5638
|
+
const ctx = { enrolment: args.resolution.enrolment, paths: args.resolution.paths };
|
|
5639
|
+
let verified;
|
|
5640
|
+
try {
|
|
5641
|
+
verified = await loadFleetPolicy(ctx);
|
|
5642
|
+
} catch (err) {
|
|
5643
|
+
const detail = err instanceof Error ? err.message : String(err);
|
|
5644
|
+
return {
|
|
5645
|
+
output: args.denyOutput(`${PREFIX}: no valid signed policy \u2014 ${detail}`),
|
|
5646
|
+
exitCode: 0
|
|
5647
|
+
};
|
|
5648
|
+
}
|
|
5649
|
+
const { settings } = verified;
|
|
5650
|
+
const decide = async (output) => {
|
|
5651
|
+
try {
|
|
5652
|
+
const outcome = await recordDecisionAndHeartbeat(ctx, verified, args.classify(output), {
|
|
5653
|
+
guardVersion: SDK_VERSION,
|
|
5654
|
+
adapterKind: args.adapterKind,
|
|
5655
|
+
now: args.now
|
|
5656
|
+
});
|
|
5657
|
+
args.onHeartbeat?.(outcome);
|
|
5658
|
+
} catch {
|
|
5659
|
+
}
|
|
5660
|
+
return { output, exitCode: 0 };
|
|
5661
|
+
};
|
|
5662
|
+
let payload;
|
|
5663
|
+
try {
|
|
5664
|
+
payload = JSON.parse(args.payloadJson);
|
|
5665
|
+
} catch {
|
|
5666
|
+
if (settings.failOpen) return decide(null);
|
|
5667
|
+
return decide(args.denyOutput(`${PREFIX}: could not parse hook payload`));
|
|
5668
|
+
}
|
|
5669
|
+
try {
|
|
5670
|
+
const gatewayConfig = {
|
|
5671
|
+
mode: "local",
|
|
5672
|
+
policy: verified.policy,
|
|
5673
|
+
posture: settings.posture,
|
|
5674
|
+
traceEnabled: true,
|
|
5675
|
+
tracePath: (0, import_path5.join)(args.resolution.paths.userDir, "traces"),
|
|
5676
|
+
debug: false
|
|
5677
|
+
};
|
|
5678
|
+
const gateway = args.gatewayFactory ? args.gatewayFactory(gatewayConfig) : new AstraSyncGateway(gatewayConfig);
|
|
5679
|
+
await gateway.initialize();
|
|
5680
|
+
const adapter = args.createAdapter({
|
|
5681
|
+
...settings.skipTools && { skipTools: settings.skipTools },
|
|
5682
|
+
...typeof settings.governMcp === "boolean" && { governMcp: settings.governMcp }
|
|
5683
|
+
});
|
|
5684
|
+
await adapter.initialize({ gateway, adapterOptions: {} });
|
|
5685
|
+
const output = await adapter.evaluatePayload(payload);
|
|
5686
|
+
await adapter.shutdown();
|
|
5687
|
+
await gateway.shutdown();
|
|
5688
|
+
return decide(output);
|
|
5689
|
+
} catch (err) {
|
|
5690
|
+
if (settings.failOpen) return decide(null);
|
|
5691
|
+
const detail = err instanceof Error ? err.message : String(err);
|
|
5692
|
+
return decide(args.denyOutput(`${PREFIX}: policy evaluation failed (${detail})`));
|
|
5693
|
+
}
|
|
5694
|
+
}
|
|
5178
5695
|
|
|
5179
5696
|
// src/claude-code/hook.ts
|
|
5180
5697
|
var MAX_STDIN_BYTES = 1024 * 1024;
|
|
@@ -5192,6 +5709,19 @@ function denyOutput(reason) {
|
|
|
5192
5709
|
}
|
|
5193
5710
|
async function processHookPayload(payloadJson, options = {}) {
|
|
5194
5711
|
const env = options.env ?? process.env;
|
|
5712
|
+
const fleet = resolveFleet(env, options.home ?? (0, import_os3.homedir)(), options.fleetPaths);
|
|
5713
|
+
if (fleet) {
|
|
5714
|
+
return runFleetHook({
|
|
5715
|
+
resolution: fleet,
|
|
5716
|
+
payloadJson,
|
|
5717
|
+
adapterKind: "claude-code",
|
|
5718
|
+
createAdapter: (adapterOptions) => new ClaudeCodeAdapter(adapterOptions),
|
|
5719
|
+
denyOutput,
|
|
5720
|
+
classify: (output) => output?.hookSpecificOutput?.permissionDecision === "deny" ? "deny" : output?.hookSpecificOutput?.permissionDecision === "ask" ? "ask" : "allow",
|
|
5721
|
+
gatewayFactory: options.gatewayFactory,
|
|
5722
|
+
now: options.now
|
|
5723
|
+
});
|
|
5724
|
+
}
|
|
5195
5725
|
if (env.ASTRASYNC_GUARD_DISABLE === "1") {
|
|
5196
5726
|
return { output: null, exitCode: 0 };
|
|
5197
5727
|
}
|
|
@@ -5224,7 +5754,7 @@ async function processHookPayload(payloadJson, options = {}) {
|
|
|
5224
5754
|
policyFile: resolved.policyFile,
|
|
5225
5755
|
posture: resolved.settings.posture ?? "active",
|
|
5226
5756
|
traceEnabled: true,
|
|
5227
|
-
tracePath: (0,
|
|
5757
|
+
tracePath: (0, import_path6.join)(resolved.guardDir, "traces"),
|
|
5228
5758
|
debug: false,
|
|
5229
5759
|
...mode === "hybrid" && {
|
|
5230
5760
|
apiBaseUrl: resolved.settings.apiBaseUrl,
|