@astrasyncai/verification-gateway 5.10.0 → 5.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -0
- package/dist/adapters/express.d.mts +1 -1
- package/dist/adapters/express.d.ts +1 -1
- package/dist/adapters/express.js +359 -30
- package/dist/adapters/express.js.map +1 -1
- package/dist/adapters/express.mjs +359 -30
- package/dist/adapters/express.mjs.map +1 -1
- package/dist/adapters/mcp.d.mts +5 -1
- package/dist/adapters/mcp.d.ts +5 -1
- package/dist/adapters/mcp.js +168 -41
- package/dist/adapters/mcp.js.map +1 -1
- package/dist/adapters/mcp.mjs +168 -41
- package/dist/adapters/mcp.mjs.map +1 -1
- package/dist/adapters/nextjs.d.mts +1 -1
- package/dist/adapters/nextjs.d.ts +1 -1
- package/dist/adapters/nextjs.js +352 -13
- package/dist/adapters/nextjs.js.map +1 -1
- package/dist/adapters/nextjs.mjs +352 -13
- package/dist/adapters/nextjs.mjs.map +1 -1
- package/dist/adapters/sdk.d.mts +9 -1
- package/dist/adapters/sdk.d.ts +9 -1
- package/dist/adapters/sdk.js +100 -8
- package/dist/adapters/sdk.js.map +1 -1
- package/dist/adapters/sdk.mjs +100 -8
- 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 +805 -16
- package/dist/bin/astrasync-codex-hook.js +805 -16
- package/dist/bin/astrasync-guard.js +894 -71
- package/dist/bin/astrasync.js +951 -74
- package/dist/browser/background.js +284 -7
- package/dist/browser/background.js.map +1 -1
- package/dist/browser/background.mjs +284 -7
- 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 +821 -24
- package/dist/codex/index.js.map +1 -1
- package/dist/codex/index.mjs +823 -26
- 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 +787 -11
- package/dist/cursor/extension.js.map +1 -1
- package/dist/cursor/extension.mjs +789 -11
- package/dist/cursor/extension.mjs.map +1 -1
- package/dist/edge-config.d.mts +1 -1
- package/dist/edge-config.d.ts +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.d.mts +128 -13
- package/dist/edge-core/index.d.ts +128 -13
- package/dist/edge-core/index.js +337 -55
- package/dist/edge-core/index.js.map +1 -1
- package/dist/edge-core/index.mjs +334 -55
- 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 +284 -7
- package/dist/gateway/gateway.js.map +1 -1
- package/dist/gateway/gateway.mjs +284 -7
- package/dist/gateway/gateway.mjs.map +1 -1
- package/dist/git-trigger/git-hooks.d.mts +1 -1
- package/dist/git-trigger/git-hooks.d.ts +1 -1
- package/dist/index.d.mts +215 -5
- package/dist/index.d.ts +215 -5
- package/dist/index.js +3756 -80
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +3748 -80
- package/dist/index.mjs.map +1 -1
- package/dist/platform-signatures.js +4 -1
- package/dist/platform-signatures.js.map +1 -1
- package/dist/platform-signatures.mjs +4 -1
- package/dist/platform-signatures.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/{types-CTmUpNpS.d.mts → types-BCkwBA84.d.mts} +12 -0
- package/dist/{types-CTmUpNpS.d.ts → types-BCkwBA84.d.ts} +12 -0
- package/dist/{types-CtFXmDzm.d.ts → types-C_b4QTyO.d.ts} +18 -0
- package/dist/{types-DQrn_kei.d.mts → types-DlshIIR-.d.mts} +18 -0
- package/dist/ui/index.d.mts +1 -1
- package/dist/ui/index.d.ts +1 -1
- package/dist/verify.d.mts +35 -4
- package/dist/verify.d.ts +35 -4
- package/dist/verify.js +130 -16
- package/dist/verify.js.map +1 -1
- package/dist/verify.mjs +129 -16
- package/dist/verify.mjs.map +1 -1
- package/package.json +1 -1
package/dist/cursor/extension.js
CHANGED
|
@@ -24,6 +24,8 @@ __export(extension_exports, {
|
|
|
24
24
|
deactivate: () => deactivate
|
|
25
25
|
});
|
|
26
26
|
module.exports = __toCommonJS(extension_exports);
|
|
27
|
+
var import_os3 = require("os");
|
|
28
|
+
var import_path4 = require("path");
|
|
27
29
|
|
|
28
30
|
// src/adapter-interface/interface.ts
|
|
29
31
|
var ADAPTER_INTERFACE_VERSION = 1;
|
|
@@ -3992,7 +3994,7 @@ function getTrustLevel(score) {
|
|
|
3992
3994
|
}
|
|
3993
3995
|
|
|
3994
3996
|
// src/version.ts
|
|
3995
|
-
var SDK_VERSION = "5.
|
|
3997
|
+
var SDK_VERSION = "5.12.0";
|
|
3996
3998
|
|
|
3997
3999
|
// src/http.ts
|
|
3998
4000
|
var SDK_USER_AGENT = `astrasync-sdk/${SDK_VERSION}`;
|
|
@@ -4019,6 +4021,14 @@ function getCachedWellKnownUrls(apiBaseUrl) {
|
|
|
4019
4021
|
return cache.get(apiBaseUrl)?.data;
|
|
4020
4022
|
}
|
|
4021
4023
|
|
|
4024
|
+
// src/failure-posture.ts
|
|
4025
|
+
var REJECTED_IDENTITY_DIMENSIONS = /* @__PURE__ */ new Set([
|
|
4026
|
+
"identity.invalid_agent_id",
|
|
4027
|
+
"identity.unknown_agent",
|
|
4028
|
+
"agent.lookup"
|
|
4029
|
+
]);
|
|
4030
|
+
var UNKNOWN_AGENT_GUIDANCE = "Register your agent with AstraSync (or, inside an MCP host, add the AstraSync MCP connector for keyless, human-approved registration), then present the canonical ASTRA-* id issued after owner approval.";
|
|
4031
|
+
|
|
4022
4032
|
// src/verify.ts
|
|
4023
4033
|
var DEFAULT_CONFIG = {
|
|
4024
4034
|
apiBaseUrl: "https://astrasync.ai/api",
|
|
@@ -4230,11 +4240,44 @@ function createGuidanceResponse(_config, reason, options = {}) {
|
|
|
4230
4240
|
// coercing the absent recommendation to a policy 'deny'. Misconfig is
|
|
4231
4241
|
// deterministic and intentionally NOT marked retryable.
|
|
4232
4242
|
...isApiError && { retryable: true },
|
|
4243
|
+
...options.httpStatus !== void 0 && { httpStatus: options.httpStatus },
|
|
4244
|
+
...options.retryAfterMs !== void 0 && { retryAfterMs: options.retryAfterMs },
|
|
4233
4245
|
correlationId: options.correlationId,
|
|
4234
4246
|
attemptId: options.attemptId,
|
|
4235
4247
|
verifiedAt: /* @__PURE__ */ new Date()
|
|
4236
4248
|
};
|
|
4237
4249
|
}
|
|
4250
|
+
async function readJsonSafe(response) {
|
|
4251
|
+
try {
|
|
4252
|
+
const parsed = await response.json();
|
|
4253
|
+
return parsed && typeof parsed === "object" ? parsed : {};
|
|
4254
|
+
} catch {
|
|
4255
|
+
return {};
|
|
4256
|
+
}
|
|
4257
|
+
}
|
|
4258
|
+
function rejectedIdentityGuidance(failures) {
|
|
4259
|
+
const f = failures?.find((x) => REJECTED_IDENTITY_DIMENSIONS.has(x.dimension));
|
|
4260
|
+
if (!f) return void 0;
|
|
4261
|
+
if (f.dimension === "agent.lookup") {
|
|
4262
|
+
return `The presented agent identifier is not registered with AstraSync. ${UNKNOWN_AGENT_GUIDANCE}`;
|
|
4263
|
+
}
|
|
4264
|
+
return f.guidance ? `${f.message} ${f.guidance}` : f.message;
|
|
4265
|
+
}
|
|
4266
|
+
var MAX_RETRY_AFTER_MS = 15 * 60 * 1e3;
|
|
4267
|
+
function parseRetryAfterMs(response) {
|
|
4268
|
+
const raw = response.headers?.get?.("retry-after");
|
|
4269
|
+
if (!raw) return void 0;
|
|
4270
|
+
const trimmed = raw.trim();
|
|
4271
|
+
let ms;
|
|
4272
|
+
if (/^\d+(\.\d+)?$/.test(trimmed)) {
|
|
4273
|
+
ms = Number(trimmed) * 1e3;
|
|
4274
|
+
} else {
|
|
4275
|
+
const at = Date.parse(trimmed);
|
|
4276
|
+
if (Number.isNaN(at)) return void 0;
|
|
4277
|
+
ms = at - Date.now();
|
|
4278
|
+
}
|
|
4279
|
+
return Math.min(MAX_RETRY_AFTER_MS, Math.max(0, Math.round(ms)));
|
|
4280
|
+
}
|
|
4238
4281
|
async function callVerifyAccessAPI(config, request) {
|
|
4239
4282
|
const { credentials, ...requestData } = request;
|
|
4240
4283
|
const body = {
|
|
@@ -4291,7 +4334,8 @@ async function callVerifyAccessAPI(config, request) {
|
|
|
4291
4334
|
body: JSON.stringify(body),
|
|
4292
4335
|
signal: AbortSignal.timeout(config.verifyTimeoutMs ?? 1e4)
|
|
4293
4336
|
});
|
|
4294
|
-
const data = await response
|
|
4337
|
+
const data = await readJsonSafe(response);
|
|
4338
|
+
const retryAfterMs = parseRetryAfterMs(response);
|
|
4295
4339
|
if (response.status === 410) {
|
|
4296
4340
|
return {
|
|
4297
4341
|
success: true,
|
|
@@ -4329,6 +4373,23 @@ async function callVerifyAccessAPI(config, request) {
|
|
|
4329
4373
|
}
|
|
4330
4374
|
};
|
|
4331
4375
|
}
|
|
4376
|
+
if (response.status === 404 && credentials.astraId && (data?.error === "AGENT_NOT_FOUND" || typeof data?.message === "string" && /agent/i.test(data.message))) {
|
|
4377
|
+
return {
|
|
4378
|
+
success: true,
|
|
4379
|
+
attemptId: typeof data?.attemptId === "string" ? data.attemptId : void 0,
|
|
4380
|
+
access: {
|
|
4381
|
+
allowed: false,
|
|
4382
|
+
reason: "unknown_agent",
|
|
4383
|
+
failures: [
|
|
4384
|
+
{
|
|
4385
|
+
dimension: "identity.unknown_agent",
|
|
4386
|
+
message: "The presented agent identifier is not registered with AstraSync.",
|
|
4387
|
+
guidance: UNKNOWN_AGENT_GUIDANCE
|
|
4388
|
+
}
|
|
4389
|
+
]
|
|
4390
|
+
}
|
|
4391
|
+
};
|
|
4392
|
+
}
|
|
4332
4393
|
if (!response.ok) {
|
|
4333
4394
|
if (response.status === 401) {
|
|
4334
4395
|
return {
|
|
@@ -4343,9 +4404,11 @@ async function callVerifyAccessAPI(config, request) {
|
|
|
4343
4404
|
}
|
|
4344
4405
|
return {
|
|
4345
4406
|
success: false,
|
|
4346
|
-
error: data.message || data.error || `API returned ${response.status}`,
|
|
4407
|
+
error: typeof data?.message === "string" && data.message || typeof data?.error === "string" && data.error || `API returned ${response.status}`,
|
|
4347
4408
|
correlationId: typeof data?.correlationId === "string" ? data.correlationId : void 0,
|
|
4348
|
-
attemptId: typeof data?.attemptId === "string" ? data.attemptId : void 0
|
|
4409
|
+
attemptId: typeof data?.attemptId === "string" ? data.attemptId : void 0,
|
|
4410
|
+
httpStatus: response.status,
|
|
4411
|
+
...retryAfterMs !== void 0 && { retryAfterMs }
|
|
4349
4412
|
};
|
|
4350
4413
|
}
|
|
4351
4414
|
return data;
|
|
@@ -4417,12 +4480,14 @@ async function verify(config, request, options) {
|
|
|
4417
4480
|
correlationId: apiResponse.correlationId,
|
|
4418
4481
|
attemptId: apiResponse.attemptId,
|
|
4419
4482
|
remediation: apiResponse.remediation,
|
|
4483
|
+
httpStatus: apiResponse.httpStatus,
|
|
4484
|
+
retryAfterMs: apiResponse.retryAfterMs,
|
|
4420
4485
|
urls
|
|
4421
4486
|
});
|
|
4422
4487
|
}
|
|
4423
4488
|
if (!apiResponse.access?.allowed) {
|
|
4424
4489
|
const aggregatedFailures = apiResponse.access?.failures;
|
|
4425
|
-
const idVerifiedFromBackend = apiResponse.verificationContext?.idVerified === true;
|
|
4490
|
+
const idVerifiedFromBackend = apiResponse.verificationContext?.idVerified === true && !aggregatedFailures?.some((f) => REJECTED_IDENTITY_DIMENSIONS.has(f.dimension));
|
|
4426
4491
|
const result2 = {
|
|
4427
4492
|
identityVerified: idVerifiedFromBackend,
|
|
4428
4493
|
policyAllowed: false,
|
|
@@ -4434,7 +4499,10 @@ async function verify(config, request, options) {
|
|
|
4434
4499
|
requiresStepUp: apiResponse.access?.requiresStepUp,
|
|
4435
4500
|
requiresApproval: apiResponse.access?.requiresApproval,
|
|
4436
4501
|
guidance: {
|
|
4437
|
-
|
|
4502
|
+
// 5.12.0: a presented-but-rejected id (malformed / unknown) carries
|
|
4503
|
+
// actionable registration guidance on its failure — surface it
|
|
4504
|
+
// instead of the bare machine reason ('unknown_agent').
|
|
4505
|
+
message: rejectedIdentityGuidance(aggregatedFailures) ?? (apiResponse.access?.reason || "Access denied by PDLSS policy"),
|
|
4438
4506
|
registrationUrl: urls?.registrationUrl ?? "",
|
|
4439
4507
|
documentationUrl: urls?.documentationUrl ?? "",
|
|
4440
4508
|
// 5.1.0: anonymous callers land on this branch — steer MCP-hosted
|
|
@@ -4534,7 +4602,7 @@ async function verify(config, request, options) {
|
|
|
4534
4602
|
registrationUrl: urls?.registrationUrl ?? "",
|
|
4535
4603
|
documentationUrl: urls?.documentationUrl ?? ""
|
|
4536
4604
|
};
|
|
4537
|
-
} else if (result.recommendation === "step_up_required") {
|
|
4605
|
+
} else if (result.recommendation === "step_up_required" && result.identityVerified) {
|
|
4538
4606
|
result.requiresStepUp = true;
|
|
4539
4607
|
result.denialReasons = serverDenialReasons ?? result.recommendationReasons ?? ["Step-up verification required"];
|
|
4540
4608
|
}
|
|
@@ -4563,6 +4631,217 @@ var DEFAULT_EDGE_CONFIG = Object.freeze({
|
|
|
4563
4631
|
var DEFAULT_TTL_MS = 60 * 1e3;
|
|
4564
4632
|
var DEFAULT_MAX_STALE_MS = 24 * 60 * 60 * 1e3;
|
|
4565
4633
|
|
|
4634
|
+
// src/edge-core/events.ts
|
|
4635
|
+
async function withBudget(promise, ms) {
|
|
4636
|
+
let timer;
|
|
4637
|
+
try {
|
|
4638
|
+
return await Promise.race([
|
|
4639
|
+
promise,
|
|
4640
|
+
new Promise((resolve) => {
|
|
4641
|
+
timer = setTimeout(() => resolve(void 0), ms);
|
|
4642
|
+
})
|
|
4643
|
+
]);
|
|
4644
|
+
} catch {
|
|
4645
|
+
return void 0;
|
|
4646
|
+
} finally {
|
|
4647
|
+
if (timer) clearTimeout(timer);
|
|
4648
|
+
}
|
|
4649
|
+
}
|
|
4650
|
+
var destKeyOf = (d) => `${d.apiBaseUrl}\0${d.apiKey ?? ""}\0${d.counterparty}\0${d.adapter ?? ""}`;
|
|
4651
|
+
var BeaconBuffer = class {
|
|
4652
|
+
constructor(options = {}) {
|
|
4653
|
+
this.queue = [];
|
|
4654
|
+
this.droppedSinceLast = 0;
|
|
4655
|
+
this.droppedTotal = 0;
|
|
4656
|
+
this.deliveredTotal = 0;
|
|
4657
|
+
this.backoffUntil = 0;
|
|
4658
|
+
this.collapse = /* @__PURE__ */ new Map();
|
|
4659
|
+
/** Keys the backend answered 401 for — later batches go unauthenticated. */
|
|
4660
|
+
this.rejectedKeys = /* @__PURE__ */ new Set();
|
|
4661
|
+
this.opts = {
|
|
4662
|
+
maxItems: options.maxItems ?? 1e3,
|
|
4663
|
+
maxBatch: options.maxBatch ?? 100,
|
|
4664
|
+
maxAttempts: options.maxAttempts ?? 3,
|
|
4665
|
+
collapseWindowMs: options.collapseWindowMs ?? 6e4,
|
|
4666
|
+
collapseMaxKeys: options.collapseMaxKeys ?? 1e3,
|
|
4667
|
+
requestTimeoutMs: options.requestTimeoutMs ?? 1e4,
|
|
4668
|
+
retryBackoffMs: options.retryBackoffMs ?? 1e3,
|
|
4669
|
+
now: options.now ?? Date.now
|
|
4670
|
+
};
|
|
4671
|
+
}
|
|
4672
|
+
/**
|
|
4673
|
+
* Queue one beacon. With a `collapseKey`, a repeat inside the window is
|
|
4674
|
+
* merged into a held aggregate (sent when the window closes) instead of
|
|
4675
|
+
* becoming its own item.
|
|
4676
|
+
*/
|
|
4677
|
+
enqueue(dest, item, collapseKey) {
|
|
4678
|
+
const now = this.opts.now();
|
|
4679
|
+
if (collapseKey) {
|
|
4680
|
+
const entry = this.collapse.get(collapseKey);
|
|
4681
|
+
if (entry && now - entry.firstSeenAt < this.opts.collapseWindowMs) {
|
|
4682
|
+
for (const target of [entry.first, entry.held]) {
|
|
4683
|
+
if (target && !target.inFlight && this.queue.includes(target)) {
|
|
4684
|
+
target.item.repeatCount = (target.item.repeatCount ?? 1) + 1;
|
|
4685
|
+
return;
|
|
4686
|
+
}
|
|
4687
|
+
}
|
|
4688
|
+
const held = this.push(
|
|
4689
|
+
dest,
|
|
4690
|
+
{ ...item, repeatCount: 1 },
|
|
4691
|
+
entry.firstSeenAt + this.opts.collapseWindowMs
|
|
4692
|
+
);
|
|
4693
|
+
entry.held = held;
|
|
4694
|
+
return;
|
|
4695
|
+
}
|
|
4696
|
+
this.collapse.delete(collapseKey);
|
|
4697
|
+
if (this.collapse.size >= this.opts.collapseMaxKeys) {
|
|
4698
|
+
const oldest = this.collapse.keys().next().value;
|
|
4699
|
+
if (oldest !== void 0) this.collapse.delete(oldest);
|
|
4700
|
+
}
|
|
4701
|
+
const fresh = { firstSeenAt: now };
|
|
4702
|
+
this.collapse.set(collapseKey, fresh);
|
|
4703
|
+
fresh.first = this.push(dest, item, now);
|
|
4704
|
+
return;
|
|
4705
|
+
}
|
|
4706
|
+
this.push(dest, item, now);
|
|
4707
|
+
}
|
|
4708
|
+
/**
|
|
4709
|
+
* Deliver every eligible queued item. Concurrent callers share ONE
|
|
4710
|
+
* in-flight drain (it survives the caller's budget and a Lambda freeze),
|
|
4711
|
+
* so nothing in flight is ever sent twice; items queued while it runs are
|
|
4712
|
+
* picked up by the same drain.
|
|
4713
|
+
*/
|
|
4714
|
+
flush() {
|
|
4715
|
+
if (!this.inFlight) {
|
|
4716
|
+
this.inFlight = this.drain().finally(() => {
|
|
4717
|
+
this.inFlight = void 0;
|
|
4718
|
+
});
|
|
4719
|
+
}
|
|
4720
|
+
return this.inFlight;
|
|
4721
|
+
}
|
|
4722
|
+
/** Counters for tests and diagnostics. */
|
|
4723
|
+
stats() {
|
|
4724
|
+
return {
|
|
4725
|
+
queueDepth: this.queue.length,
|
|
4726
|
+
droppedSinceLast: this.droppedSinceLast,
|
|
4727
|
+
droppedTotal: this.droppedTotal,
|
|
4728
|
+
deliveredTotal: this.deliveredTotal,
|
|
4729
|
+
flushing: this.inFlight !== void 0
|
|
4730
|
+
};
|
|
4731
|
+
}
|
|
4732
|
+
push(dest, item, eligibleAt) {
|
|
4733
|
+
while (this.queue.length >= this.opts.maxItems) this.dropOldest();
|
|
4734
|
+
const entry = {
|
|
4735
|
+
dest,
|
|
4736
|
+
destKey: destKeyOf(dest),
|
|
4737
|
+
item,
|
|
4738
|
+
attempts: 0,
|
|
4739
|
+
eligibleAt,
|
|
4740
|
+
inFlight: false
|
|
4741
|
+
};
|
|
4742
|
+
this.queue.push(entry);
|
|
4743
|
+
return entry;
|
|
4744
|
+
}
|
|
4745
|
+
dropOldest() {
|
|
4746
|
+
if (this.queue.shift()) this.countDropped(1);
|
|
4747
|
+
}
|
|
4748
|
+
countDropped(n) {
|
|
4749
|
+
this.droppedSinceLast += n;
|
|
4750
|
+
this.droppedTotal += n;
|
|
4751
|
+
}
|
|
4752
|
+
async drain() {
|
|
4753
|
+
for (; ; ) {
|
|
4754
|
+
const now = this.opts.now();
|
|
4755
|
+
if (now < this.backoffUntil) return;
|
|
4756
|
+
const batch = this.takeBatch(now);
|
|
4757
|
+
if (batch.length === 0) return;
|
|
4758
|
+
const ok = await this.send(batch);
|
|
4759
|
+
if (!ok) return;
|
|
4760
|
+
}
|
|
4761
|
+
}
|
|
4762
|
+
/** Oldest eligible item + up to maxBatch-1 more for the same destination. */
|
|
4763
|
+
takeBatch(now) {
|
|
4764
|
+
const first = this.queue.find((q) => q.eligibleAt <= now);
|
|
4765
|
+
if (!first) return [];
|
|
4766
|
+
const batch = [];
|
|
4767
|
+
const rest = [];
|
|
4768
|
+
for (const q of this.queue) {
|
|
4769
|
+
if (batch.length < this.opts.maxBatch && q.destKey === first.destKey && q.eligibleAt <= now) {
|
|
4770
|
+
q.inFlight = true;
|
|
4771
|
+
batch.push(q);
|
|
4772
|
+
} else {
|
|
4773
|
+
rest.push(q);
|
|
4774
|
+
}
|
|
4775
|
+
}
|
|
4776
|
+
this.queue = rest;
|
|
4777
|
+
return batch;
|
|
4778
|
+
}
|
|
4779
|
+
/** POST one batch. Returns false when the drain should pause (backoff). */
|
|
4780
|
+
async send(batch) {
|
|
4781
|
+
const dest = batch[0].dest;
|
|
4782
|
+
const useKey = dest.apiKey && !this.rejectedKeys.has(dest.apiKey) ? dest.apiKey : void 0;
|
|
4783
|
+
const reportedDropped = this.droppedSinceLast;
|
|
4784
|
+
const meta = {
|
|
4785
|
+
droppedSinceLast: reportedDropped,
|
|
4786
|
+
queueDepth: this.queue.length,
|
|
4787
|
+
adapter: dest.adapter ?? "edge-core",
|
|
4788
|
+
version: SDK_VERSION
|
|
4789
|
+
};
|
|
4790
|
+
const controller = typeof AbortController !== "undefined" ? new AbortController() : void 0;
|
|
4791
|
+
const response = await withBudget(
|
|
4792
|
+
sdkFetch(`${dest.apiBaseUrl}/verification-activity/edge-events`, {
|
|
4793
|
+
method: "POST",
|
|
4794
|
+
headers: {
|
|
4795
|
+
"Content-Type": "application/json",
|
|
4796
|
+
...useKey && { Authorization: `Bearer ${useKey}` }
|
|
4797
|
+
},
|
|
4798
|
+
body: JSON.stringify({ events: batch.map((q) => q.item), meta }),
|
|
4799
|
+
...controller && { signal: controller.signal }
|
|
4800
|
+
}),
|
|
4801
|
+
this.opts.requestTimeoutMs
|
|
4802
|
+
);
|
|
4803
|
+
if (!response) controller?.abort();
|
|
4804
|
+
const status = response?.status;
|
|
4805
|
+
for (const q of batch) q.inFlight = false;
|
|
4806
|
+
if (status !== void 0 && status >= 200 && status < 300) {
|
|
4807
|
+
this.droppedSinceLast = Math.max(0, this.droppedSinceLast - reportedDropped);
|
|
4808
|
+
this.deliveredTotal += batch.length;
|
|
4809
|
+
return true;
|
|
4810
|
+
}
|
|
4811
|
+
if (status === 401 && useKey) {
|
|
4812
|
+
this.rejectedKeys.add(useKey);
|
|
4813
|
+
this.requeue(batch, false);
|
|
4814
|
+
return true;
|
|
4815
|
+
}
|
|
4816
|
+
if (status === 429) {
|
|
4817
|
+
const retryAfter = Number(response?.headers.get("retry-after"));
|
|
4818
|
+
const waitMs = Number.isFinite(retryAfter) && retryAfter > 0 ? Math.min(retryAfter, 120) * 1e3 : 3e4;
|
|
4819
|
+
this.backoffUntil = this.opts.now() + waitMs;
|
|
4820
|
+
this.requeue(batch, false);
|
|
4821
|
+
return false;
|
|
4822
|
+
}
|
|
4823
|
+
if (status !== void 0 && status >= 400 && status < 500) {
|
|
4824
|
+
this.countDropped(batch.length);
|
|
4825
|
+
return true;
|
|
4826
|
+
}
|
|
4827
|
+
this.backoffUntil = this.opts.now() + this.opts.retryBackoffMs;
|
|
4828
|
+
this.requeue(batch, true);
|
|
4829
|
+
return false;
|
|
4830
|
+
}
|
|
4831
|
+
/** Put a failed batch back at the head of the queue (order preserved). */
|
|
4832
|
+
requeue(batch, countAttempt) {
|
|
4833
|
+
const keep = [];
|
|
4834
|
+
for (const q of batch) {
|
|
4835
|
+
if (countAttempt) q.attempts += 1;
|
|
4836
|
+
if (q.attempts >= this.opts.maxAttempts) this.countDropped(1);
|
|
4837
|
+
else keep.push(q);
|
|
4838
|
+
}
|
|
4839
|
+
this.queue = [...keep, ...this.queue];
|
|
4840
|
+
while (this.queue.length > this.opts.maxItems) this.dropOldest();
|
|
4841
|
+
}
|
|
4842
|
+
};
|
|
4843
|
+
var beaconBuffer = new BeaconBuffer();
|
|
4844
|
+
|
|
4566
4845
|
// src/adapters/express.ts
|
|
4567
4846
|
var DEFAULT_ROUTES_REFRESH_MS = 5 * 60 * 1e3;
|
|
4568
4847
|
|
|
@@ -5156,11 +5435,445 @@ var AstraSyncGateway = class {
|
|
|
5156
5435
|
}
|
|
5157
5436
|
};
|
|
5158
5437
|
|
|
5438
|
+
// src/claude-code/guard-config.ts
|
|
5439
|
+
var import_os2 = require("os");
|
|
5440
|
+
|
|
5441
|
+
// src/fleet/enrolment-store.ts
|
|
5442
|
+
var import_fs4 = require("fs");
|
|
5443
|
+
var import_os = require("os");
|
|
5444
|
+
var import_path3 = require("path");
|
|
5445
|
+
function machineScopeDir(platform = process.platform, env = process.env) {
|
|
5446
|
+
if (platform === "darwin") return "/Library/Application Support/AstraSync";
|
|
5447
|
+
if (platform === "win32") return (0, import_path3.join)(env.ProgramData || "C:\\ProgramData", "AstraSync");
|
|
5448
|
+
return "/etc/astrasync";
|
|
5449
|
+
}
|
|
5450
|
+
function fleetPaths(options = {}) {
|
|
5451
|
+
const home = options.home ?? (0, import_os.homedir)();
|
|
5452
|
+
const machineDir = options.machineDir ?? options.env?.ASTRASYNC_FLEET_MACHINE_DIR ?? machineScopeDir(options.platform, options.env);
|
|
5453
|
+
const userDir = (0, import_path3.join)(home, ".astrasync", "fleet");
|
|
5454
|
+
return {
|
|
5455
|
+
machineDevice: (0, import_path3.join)(machineDir, "fleet.json"),
|
|
5456
|
+
machinePolicy: (0, import_path3.join)(machineDir, "policy.jws"),
|
|
5457
|
+
userDir,
|
|
5458
|
+
userDevice: (0, import_path3.join)(userDir, "device.json"),
|
|
5459
|
+
userPolicy: (0, import_path3.join)(userDir, "policy.jws"),
|
|
5460
|
+
userKeyChain: (0, import_path3.join)(userDir, "keychain.json"),
|
|
5461
|
+
state: (0, import_path3.join)(userDir, "state.json")
|
|
5462
|
+
};
|
|
5463
|
+
}
|
|
5464
|
+
function isEnrolment(value) {
|
|
5465
|
+
const v = value;
|
|
5466
|
+
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);
|
|
5467
|
+
}
|
|
5468
|
+
function loadEnrolment(paths) {
|
|
5469
|
+
const candidates = [
|
|
5470
|
+
[paths.machineDevice, "machine"],
|
|
5471
|
+
[paths.userDevice, "user"]
|
|
5472
|
+
];
|
|
5473
|
+
for (const [path, scope] of candidates) {
|
|
5474
|
+
if (!(0, import_fs4.existsSync)(path)) continue;
|
|
5475
|
+
try {
|
|
5476
|
+
const parsed = JSON.parse((0, import_fs4.readFileSync)(path, "utf-8"));
|
|
5477
|
+
if (isEnrolment(parsed)) return { enrolment: parsed, scope };
|
|
5478
|
+
} catch {
|
|
5479
|
+
}
|
|
5480
|
+
return { corrupt: true, path };
|
|
5481
|
+
}
|
|
5482
|
+
return null;
|
|
5483
|
+
}
|
|
5484
|
+
function writeFileMode(path, content, mode) {
|
|
5485
|
+
(0, import_fs4.mkdirSync)((0, import_path3.dirname)(path), { recursive: true, mode: 448 });
|
|
5486
|
+
(0, import_fs4.writeFileSync)(path, content, { encoding: "utf-8", mode });
|
|
5487
|
+
try {
|
|
5488
|
+
(0, import_fs4.chmodSync)(path, mode);
|
|
5489
|
+
} catch {
|
|
5490
|
+
}
|
|
5491
|
+
}
|
|
5492
|
+
function readCachedPolicies(paths) {
|
|
5493
|
+
const out = [];
|
|
5494
|
+
for (const p of [paths.userPolicy, paths.machinePolicy]) {
|
|
5495
|
+
try {
|
|
5496
|
+
if ((0, import_fs4.existsSync)(p)) out.push((0, import_fs4.readFileSync)(p, "utf-8").trim());
|
|
5497
|
+
} catch {
|
|
5498
|
+
}
|
|
5499
|
+
}
|
|
5500
|
+
return out.filter(Boolean);
|
|
5501
|
+
}
|
|
5502
|
+
function writeCachedPolicy(paths, jws) {
|
|
5503
|
+
writeFileMode(paths.userPolicy, jws, 384);
|
|
5504
|
+
}
|
|
5505
|
+
function readKeyChain(paths) {
|
|
5506
|
+
try {
|
|
5507
|
+
const parsed = JSON.parse((0, import_fs4.readFileSync)(paths.userKeyChain, "utf-8"));
|
|
5508
|
+
return Array.isArray(parsed) ? parsed.filter((j) => typeof j === "string") : [];
|
|
5509
|
+
} catch {
|
|
5510
|
+
return [];
|
|
5511
|
+
}
|
|
5512
|
+
}
|
|
5513
|
+
function writeKeyChain(paths, chain) {
|
|
5514
|
+
writeFileMode(paths.userKeyChain, JSON.stringify(chain), 384);
|
|
5515
|
+
}
|
|
5516
|
+
function readState(paths) {
|
|
5517
|
+
try {
|
|
5518
|
+
const parsed = JSON.parse((0, import_fs4.readFileSync)(paths.state, "utf-8"));
|
|
5519
|
+
const c = parsed.counts;
|
|
5520
|
+
return {
|
|
5521
|
+
...parsed,
|
|
5522
|
+
counts: { allow: c?.allow ?? 0, deny: c?.deny ?? 0, ask: c?.ask ?? 0 }
|
|
5523
|
+
};
|
|
5524
|
+
} catch {
|
|
5525
|
+
return { counts: { allow: 0, deny: 0, ask: 0 } };
|
|
5526
|
+
}
|
|
5527
|
+
}
|
|
5528
|
+
function writeState(paths, state) {
|
|
5529
|
+
try {
|
|
5530
|
+
writeFileMode(paths.state, JSON.stringify(state, null, 2), 384);
|
|
5531
|
+
} catch {
|
|
5532
|
+
}
|
|
5533
|
+
}
|
|
5534
|
+
|
|
5535
|
+
// src/claude-code/guard-config.ts
|
|
5536
|
+
var FLEET_ENROL_INSTRUCTION = "This machine must be enrolled: run `astrasync guard enroll <token>` (token from your organisation admin)";
|
|
5537
|
+
function resolveFleet(env, home = (0, import_os2.homedir)(), paths = fleetPaths({ home, env })) {
|
|
5538
|
+
const loaded = loadEnrolment(paths);
|
|
5539
|
+
if (loaded && "corrupt" in loaded) {
|
|
5540
|
+
return {
|
|
5541
|
+
kind: "fleet-missing",
|
|
5542
|
+
reason: `fleet enrolment at ${loaded.path} is unreadable \u2014 ${FLEET_ENROL_INSTRUCTION}`,
|
|
5543
|
+
paths
|
|
5544
|
+
};
|
|
5545
|
+
}
|
|
5546
|
+
if (loaded) return { kind: "fleet", enrolment: loaded.enrolment, scope: loaded.scope, paths };
|
|
5547
|
+
if (env.ASTRASYNC_FLEET_REQUIRED === "1") {
|
|
5548
|
+
return { kind: "fleet-missing", reason: FLEET_ENROL_INSTRUCTION, paths };
|
|
5549
|
+
}
|
|
5550
|
+
return null;
|
|
5551
|
+
}
|
|
5552
|
+
|
|
5553
|
+
// src/fleet/client.ts
|
|
5554
|
+
var FleetApiError = class extends Error {
|
|
5555
|
+
constructor(message, status, code) {
|
|
5556
|
+
super(message);
|
|
5557
|
+
this.status = status;
|
|
5558
|
+
this.code = code;
|
|
5559
|
+
this.name = "FleetApiError";
|
|
5560
|
+
}
|
|
5561
|
+
};
|
|
5562
|
+
function base(url) {
|
|
5563
|
+
return url.replace(/\/+$/, "");
|
|
5564
|
+
}
|
|
5565
|
+
async function call(url, init, timeoutMs) {
|
|
5566
|
+
const controller = new AbortController();
|
|
5567
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
5568
|
+
try {
|
|
5569
|
+
const res = await sdkFetch(url, { ...init, signal: controller.signal });
|
|
5570
|
+
const text = await res.text();
|
|
5571
|
+
let body = text;
|
|
5572
|
+
try {
|
|
5573
|
+
body = text ? JSON.parse(text) : null;
|
|
5574
|
+
} catch {
|
|
5575
|
+
}
|
|
5576
|
+
return { status: res.status, body, headers: res.headers };
|
|
5577
|
+
} finally {
|
|
5578
|
+
clearTimeout(timer);
|
|
5579
|
+
}
|
|
5580
|
+
}
|
|
5581
|
+
function apiError(status, body, what) {
|
|
5582
|
+
const b = body ?? {};
|
|
5583
|
+
const code = b.code ?? (typeof b.error === "string" ? b.error : void 0);
|
|
5584
|
+
return new FleetApiError(`${what} failed (${status}${code ? ` ${code}` : ""})`, status, code);
|
|
5585
|
+
}
|
|
5586
|
+
function deviceHeaders(enrolment) {
|
|
5587
|
+
return { Authorization: `Device ${enrolment.credential}` };
|
|
5588
|
+
}
|
|
5589
|
+
function policyEtag(sha256, kid) {
|
|
5590
|
+
return `${sha256}.${kid ?? ""}`;
|
|
5591
|
+
}
|
|
5592
|
+
async function fetchFleetPolicy(enrolment, ifNoneMatch, timeoutMs = 1500) {
|
|
5593
|
+
const headers = deviceHeaders(enrolment);
|
|
5594
|
+
if (ifNoneMatch) headers["If-None-Match"] = `"${ifNoneMatch}"`;
|
|
5595
|
+
const r = await call(`${base(enrolment.apiBaseUrl)}/fleet/policy`, { headers }, timeoutMs);
|
|
5596
|
+
if (r.status === 304) return { status: "unchanged" };
|
|
5597
|
+
if (r.status !== 200) throw apiError(r.status, r.body, "Policy fetch");
|
|
5598
|
+
const data = r.body;
|
|
5599
|
+
if (!data?.policyJws) throw new FleetApiError("Policy response has no policyJws", r.status);
|
|
5600
|
+
return {
|
|
5601
|
+
status: "ok",
|
|
5602
|
+
policyJws: data.policyJws,
|
|
5603
|
+
sha256: data.sha256 ?? "",
|
|
5604
|
+
version: data.version ?? 0,
|
|
5605
|
+
keyChain: Array.isArray(data.keyChain) ? data.keyChain.filter((j) => typeof j === "string") : []
|
|
5606
|
+
};
|
|
5607
|
+
}
|
|
5608
|
+
async function sendHeartbeat(enrolment, request, timeoutMs = 1500) {
|
|
5609
|
+
const r = await call(
|
|
5610
|
+
`${base(enrolment.apiBaseUrl)}/fleet/heartbeat`,
|
|
5611
|
+
{
|
|
5612
|
+
method: "POST",
|
|
5613
|
+
headers: { ...deviceHeaders(enrolment), "Content-Type": "application/json" },
|
|
5614
|
+
body: JSON.stringify(request)
|
|
5615
|
+
},
|
|
5616
|
+
timeoutMs
|
|
5617
|
+
);
|
|
5618
|
+
if (r.status !== 200) throw apiError(r.status, r.body, "Heartbeat");
|
|
5619
|
+
return r.body;
|
|
5620
|
+
}
|
|
5621
|
+
|
|
5622
|
+
// src/fleet/policy-verify.ts
|
|
5623
|
+
var import_crypto = require("crypto");
|
|
5624
|
+
var import_jose2 = require("jose");
|
|
5625
|
+
|
|
5626
|
+
// src/fleet/types.ts
|
|
5627
|
+
var FLEET_POLICY_TYP = "fleet-policy+jwt";
|
|
5628
|
+
var FLEET_POLICY_AUD = "astrasync:fleet-policy";
|
|
5629
|
+
var FLEET_POLICY_ISS = "astrasync.ai";
|
|
5630
|
+
var FLEET_KEYS_TYP = "fleet-keys+jwt";
|
|
5631
|
+
var FLEET_KEYS_AUD = "astrasync:fleet-keys";
|
|
5632
|
+
var DEFAULT_HEARTBEAT_INTERVAL_SEC = 900;
|
|
5633
|
+
|
|
5634
|
+
// src/fleet/policy-verify.ts
|
|
5635
|
+
var FleetPolicyError = class extends Error {
|
|
5636
|
+
constructor(message) {
|
|
5637
|
+
super(message);
|
|
5638
|
+
this.name = "FleetPolicyError";
|
|
5639
|
+
}
|
|
5640
|
+
};
|
|
5641
|
+
function sha256Hex(text) {
|
|
5642
|
+
return (0, import_crypto.createHash)("sha256").update(text, "utf8").digest("hex");
|
|
5643
|
+
}
|
|
5644
|
+
function parseSettings(raw) {
|
|
5645
|
+
if (!raw || typeof raw !== "object") throw new FleetPolicyError("settings missing");
|
|
5646
|
+
const s = raw;
|
|
5647
|
+
if (s.posture !== "active" && s.posture !== "passive") {
|
|
5648
|
+
throw new FleetPolicyError("settings.posture must be active or passive");
|
|
5649
|
+
}
|
|
5650
|
+
if (s.failOpen !== void 0 && typeof s.failOpen !== "boolean") {
|
|
5651
|
+
throw new FleetPolicyError("settings.failOpen must be a boolean");
|
|
5652
|
+
}
|
|
5653
|
+
if (s.governMcp !== void 0 && typeof s.governMcp !== "boolean") {
|
|
5654
|
+
throw new FleetPolicyError("settings.governMcp must be a boolean");
|
|
5655
|
+
}
|
|
5656
|
+
if (s.skipTools !== void 0 && !(Array.isArray(s.skipTools) && s.skipTools.every((t) => typeof t === "string"))) {
|
|
5657
|
+
throw new FleetPolicyError("settings.skipTools must be a string array");
|
|
5658
|
+
}
|
|
5659
|
+
const num = (v) => typeof v === "number" && Number.isFinite(v) && v > 0 ? v : void 0;
|
|
5660
|
+
return {
|
|
5661
|
+
posture: s.posture,
|
|
5662
|
+
failOpen: s.failOpen === true,
|
|
5663
|
+
...typeof s.governMcp === "boolean" && { governMcp: s.governMcp },
|
|
5664
|
+
...Array.isArray(s.skipTools) && { skipTools: s.skipTools },
|
|
5665
|
+
...num(s.heartbeatIntervalSec) && { heartbeatIntervalSec: num(s.heartbeatIntervalSec) },
|
|
5666
|
+
...num(s.staleAfterSec) && { staleAfterSec: num(s.staleAfterSec) }
|
|
5667
|
+
};
|
|
5668
|
+
}
|
|
5669
|
+
async function verifyFleetPolicy(jws, jwks, options) {
|
|
5670
|
+
let payloadBytes;
|
|
5671
|
+
let header;
|
|
5672
|
+
try {
|
|
5673
|
+
const keySet = (0, import_jose2.createLocalJWKSet)(jwks);
|
|
5674
|
+
const result = await (0, import_jose2.compactVerify)(jws, keySet, { algorithms: ["ES256"] });
|
|
5675
|
+
payloadBytes = result.payload;
|
|
5676
|
+
header = result.protectedHeader;
|
|
5677
|
+
} catch (err) {
|
|
5678
|
+
throw new FleetPolicyError(
|
|
5679
|
+
`signature verification failed (${err instanceof Error ? err.message : String(err)})`
|
|
5680
|
+
);
|
|
5681
|
+
}
|
|
5682
|
+
if (header.typ !== FLEET_POLICY_TYP) throw new FleetPolicyError(`unexpected typ ${header.typ}`);
|
|
5683
|
+
let claims;
|
|
5684
|
+
try {
|
|
5685
|
+
claims = JSON.parse(new TextDecoder().decode(payloadBytes));
|
|
5686
|
+
} catch {
|
|
5687
|
+
throw new FleetPolicyError("payload is not JSON");
|
|
5688
|
+
}
|
|
5689
|
+
if (claims.aud !== FLEET_POLICY_AUD) throw new FleetPolicyError(`unexpected aud ${claims.aud}`);
|
|
5690
|
+
if (claims.iss !== FLEET_POLICY_ISS) throw new FleetPolicyError(`unexpected iss ${claims.iss}`);
|
|
5691
|
+
if (!options.expectedSubs.includes(claims.sub)) {
|
|
5692
|
+
throw new FleetPolicyError("policy was issued for a different organisation or device");
|
|
5693
|
+
}
|
|
5694
|
+
if (!Number.isInteger(claims.ver) || claims.ver < 1) {
|
|
5695
|
+
throw new FleetPolicyError("ver must be a positive integer");
|
|
5696
|
+
}
|
|
5697
|
+
if (options.minVer !== void 0 && claims.ver < options.minVer) {
|
|
5698
|
+
throw new FleetPolicyError(
|
|
5699
|
+
`policy version ${claims.ver} is older than the accepted version ${options.minVer} (rollback)`
|
|
5700
|
+
);
|
|
5701
|
+
}
|
|
5702
|
+
if (typeof claims.yaml !== "string" || sha256Hex(claims.yaml) !== claims.sha256) {
|
|
5703
|
+
throw new FleetPolicyError("policy hash mismatch");
|
|
5704
|
+
}
|
|
5705
|
+
const settings = parseSettings(claims.settings);
|
|
5706
|
+
const parsed = parseYaml(claims.yaml);
|
|
5707
|
+
if (!parsed.policy) {
|
|
5708
|
+
const first = parsed.validation.errors[0];
|
|
5709
|
+
throw new FleetPolicyError(
|
|
5710
|
+
`signed policy is invalid${first ? ` (${first.path}: ${first.message})` : ""}`
|
|
5711
|
+
);
|
|
5712
|
+
}
|
|
5713
|
+
return { kid: header.kid, claims: { ...claims, settings }, policy: parsed.policy, settings, jws };
|
|
5714
|
+
}
|
|
5715
|
+
async function resolveFleetJwks(pinned, chain, ownerId) {
|
|
5716
|
+
let trusted = pinned;
|
|
5717
|
+
let lastSeq = 0;
|
|
5718
|
+
for (const link of chain) {
|
|
5719
|
+
try {
|
|
5720
|
+
const { payload, protectedHeader } = await (0, import_jose2.compactVerify)(
|
|
5721
|
+
link,
|
|
5722
|
+
(0, import_jose2.createLocalJWKSet)(trusted),
|
|
5723
|
+
{ algorithms: ["ES256"] }
|
|
5724
|
+
);
|
|
5725
|
+
if (protectedHeader.typ !== FLEET_KEYS_TYP) continue;
|
|
5726
|
+
const c = JSON.parse(new TextDecoder().decode(payload));
|
|
5727
|
+
if (c.aud !== FLEET_KEYS_AUD || c.iss !== FLEET_POLICY_ISS || c.sub !== ownerId) continue;
|
|
5728
|
+
if (!Number.isInteger(c.seq) || c.seq <= lastSeq) continue;
|
|
5729
|
+
const keys = Array.isArray(c.keys) ? c.keys.filter((k) => k && k.kty === "EC" && k.crv === "P-256" && !("d" in k)) : [];
|
|
5730
|
+
if (keys.length === 0) continue;
|
|
5731
|
+
trusted = { keys };
|
|
5732
|
+
lastSeq = c.seq;
|
|
5733
|
+
} catch {
|
|
5734
|
+
}
|
|
5735
|
+
}
|
|
5736
|
+
return trusted;
|
|
5737
|
+
}
|
|
5738
|
+
async function pickNewestValid(candidates, jwks, options) {
|
|
5739
|
+
let best = null;
|
|
5740
|
+
let lastError = new FleetPolicyError("no cached fleet policy");
|
|
5741
|
+
for (const jws of candidates) {
|
|
5742
|
+
try {
|
|
5743
|
+
const v = await verifyFleetPolicy(jws, jwks, options);
|
|
5744
|
+
if (!best || v.claims.ver > best.claims.ver) best = v;
|
|
5745
|
+
} catch (err) {
|
|
5746
|
+
lastError = err instanceof Error ? err : new FleetPolicyError(String(err));
|
|
5747
|
+
}
|
|
5748
|
+
}
|
|
5749
|
+
if (!best) throw lastError;
|
|
5750
|
+
return best;
|
|
5751
|
+
}
|
|
5752
|
+
|
|
5753
|
+
// src/fleet/runtime.ts
|
|
5754
|
+
var FLEET_SYNC_BUDGET_MS = 1500;
|
|
5755
|
+
function expectedSubs(enrolment) {
|
|
5756
|
+
return [enrolment.org.ownerId, enrolment.deviceId];
|
|
5757
|
+
}
|
|
5758
|
+
function fleetJwks(ctx, chain = readKeyChain(ctx.paths)) {
|
|
5759
|
+
return resolveFleetJwks(ctx.enrolment.jwks, chain, ctx.enrolment.org.ownerId);
|
|
5760
|
+
}
|
|
5761
|
+
async function acceptFetched(ctx, fetched, verifyOpts) {
|
|
5762
|
+
const jwks = await fleetJwks(ctx, fetched.keyChain);
|
|
5763
|
+
const verified = await verifyFleetPolicy(fetched.policyJws, jwks, verifyOpts);
|
|
5764
|
+
if (fetched.keyChain.length > 0) writeKeyChain(ctx.paths, fetched.keyChain);
|
|
5765
|
+
writeCachedPolicy(ctx.paths, fetched.policyJws);
|
|
5766
|
+
return verified;
|
|
5767
|
+
}
|
|
5768
|
+
function remember(paths, state, verified) {
|
|
5769
|
+
state.appliedSha256 = verified.claims.sha256;
|
|
5770
|
+
state.maxVer = Math.max(state.maxVer ?? 0, verified.claims.ver);
|
|
5771
|
+
writeState(paths, state);
|
|
5772
|
+
}
|
|
5773
|
+
async function loadFleetPolicy(ctx, options = {}) {
|
|
5774
|
+
const state = readState(ctx.paths);
|
|
5775
|
+
const verifyOpts = { expectedSubs: expectedSubs(ctx.enrolment), minVer: state.maxVer };
|
|
5776
|
+
const cached = readCachedPolicies(ctx.paths);
|
|
5777
|
+
let cacheError = null;
|
|
5778
|
+
if (cached.length > 0) {
|
|
5779
|
+
try {
|
|
5780
|
+
const verified = await pickNewestValid(cached, await fleetJwks(ctx), verifyOpts);
|
|
5781
|
+
if (verified.claims.sha256 !== state.appliedSha256 || verified.claims.ver !== state.maxVer) {
|
|
5782
|
+
remember(ctx.paths, state, verified);
|
|
5783
|
+
}
|
|
5784
|
+
return verified;
|
|
5785
|
+
} catch (err) {
|
|
5786
|
+
cacheError = err instanceof Error ? err : new FleetPolicyError(String(err));
|
|
5787
|
+
}
|
|
5788
|
+
}
|
|
5789
|
+
try {
|
|
5790
|
+
const fetched = await fetchFleetPolicy(
|
|
5791
|
+
ctx.enrolment,
|
|
5792
|
+
null,
|
|
5793
|
+
options.fetchTimeoutMs ?? FLEET_SYNC_BUDGET_MS
|
|
5794
|
+
);
|
|
5795
|
+
if (fetched.status === "ok") {
|
|
5796
|
+
const verified = await acceptFetched(ctx, fetched, verifyOpts);
|
|
5797
|
+
remember(ctx.paths, state, verified);
|
|
5798
|
+
return verified;
|
|
5799
|
+
}
|
|
5800
|
+
} catch (err) {
|
|
5801
|
+
if (err instanceof FleetApiError && err.code === "DEVICE_REVOKED") {
|
|
5802
|
+
state.revoked = true;
|
|
5803
|
+
writeState(ctx.paths, state);
|
|
5804
|
+
}
|
|
5805
|
+
const detail = err instanceof Error ? err.message : String(err);
|
|
5806
|
+
throw new FleetPolicyError(
|
|
5807
|
+
cacheError ? `${cacheError.message}; refresh failed (${detail})` : `no fleet policy (${detail})`
|
|
5808
|
+
);
|
|
5809
|
+
}
|
|
5810
|
+
throw cacheError ?? new FleetPolicyError("no fleet policy available");
|
|
5811
|
+
}
|
|
5812
|
+
async function recordDecisionAndHeartbeat(ctx, applied, decision, options) {
|
|
5813
|
+
const now = options.now ?? Date.now;
|
|
5814
|
+
const state = readState(ctx.paths);
|
|
5815
|
+
if (decision) state.counts[decision] += 1;
|
|
5816
|
+
const intervalSec = applied.settings.heartbeatIntervalSec ?? ctx.enrolment.heartbeatIntervalSec ?? DEFAULT_HEARTBEAT_INTERVAL_SEC;
|
|
5817
|
+
const last = state.lastHeartbeatAt ? Date.parse(state.lastHeartbeatAt) : 0;
|
|
5818
|
+
if (now() - last < intervalSec * 1e3) {
|
|
5819
|
+
writeState(ctx.paths, state);
|
|
5820
|
+
return "throttled";
|
|
5821
|
+
}
|
|
5822
|
+
const deadline = now() + (options.budgetMs ?? FLEET_SYNC_BUDGET_MS);
|
|
5823
|
+
const remaining = () => Math.max(1, deadline - now());
|
|
5824
|
+
state.lastHeartbeatAt = new Date(now()).toISOString();
|
|
5825
|
+
let outcome = "sent";
|
|
5826
|
+
try {
|
|
5827
|
+
const hb = await sendHeartbeat(
|
|
5828
|
+
ctx.enrolment,
|
|
5829
|
+
{
|
|
5830
|
+
appliedSha256: applied.claims.sha256,
|
|
5831
|
+
guardVersion: options.guardVersion,
|
|
5832
|
+
adapters: [{ kind: options.adapterKind, managed: true }],
|
|
5833
|
+
posture: applied.settings.posture,
|
|
5834
|
+
decisionCounts: { ...state.counts }
|
|
5835
|
+
},
|
|
5836
|
+
remaining()
|
|
5837
|
+
);
|
|
5838
|
+
state.counts = { allow: 0, deny: 0, ask: 0 };
|
|
5839
|
+
state.revoked = false;
|
|
5840
|
+
delete state.lastError;
|
|
5841
|
+
const policyChanged = !!hb.activeSha256 && hb.activeSha256 !== applied.claims.sha256;
|
|
5842
|
+
const keyChanged = !!hb.activeKid && !!applied.kid && hb.activeKid !== applied.kid;
|
|
5843
|
+
if (policyChanged || keyChanged) {
|
|
5844
|
+
const fetched = await fetchFleetPolicy(
|
|
5845
|
+
ctx.enrolment,
|
|
5846
|
+
policyEtag(applied.claims.sha256, applied.kid),
|
|
5847
|
+
remaining()
|
|
5848
|
+
);
|
|
5849
|
+
if (fetched.status === "ok") {
|
|
5850
|
+
const verified = await acceptFetched(ctx, fetched, {
|
|
5851
|
+
expectedSubs: expectedSubs(ctx.enrolment),
|
|
5852
|
+
minVer: Math.max(state.maxVer ?? 0, applied.claims.ver)
|
|
5853
|
+
});
|
|
5854
|
+
state.appliedSha256 = verified.claims.sha256;
|
|
5855
|
+
state.maxVer = Math.max(state.maxVer ?? 0, verified.claims.ver);
|
|
5856
|
+
outcome = "refreshed";
|
|
5857
|
+
}
|
|
5858
|
+
}
|
|
5859
|
+
} catch (err) {
|
|
5860
|
+
if (err instanceof FleetApiError && err.code === "DEVICE_REVOKED") {
|
|
5861
|
+
state.revoked = true;
|
|
5862
|
+
outcome = "revoked";
|
|
5863
|
+
} else {
|
|
5864
|
+
state.lastError = err instanceof Error ? err.message : String(err);
|
|
5865
|
+
outcome = "failed";
|
|
5866
|
+
}
|
|
5867
|
+
}
|
|
5868
|
+
writeState(ctx.paths, state);
|
|
5869
|
+
return outcome;
|
|
5870
|
+
}
|
|
5871
|
+
|
|
5159
5872
|
// src/cursor/extension.ts
|
|
5160
5873
|
var adapter = null;
|
|
5161
5874
|
var gateway = null;
|
|
5162
|
-
async function activate(vscodeApi) {
|
|
5163
|
-
const config = loadConfiguration();
|
|
5875
|
+
async function activate(vscodeApi, options = {}) {
|
|
5876
|
+
const config = await loadConfiguration(options);
|
|
5164
5877
|
gateway = new AstraSyncGateway(config.gatewayConfig);
|
|
5165
5878
|
adapter = new CursorAdapter({
|
|
5166
5879
|
vscode: vscodeApi,
|
|
@@ -5183,10 +5896,18 @@ Action: ${context.action} \u2192 ${context.target}`,
|
|
|
5183
5896
|
const channel = vscodeApi.window.createOutputChannel("AstraSync Policy");
|
|
5184
5897
|
channel.appendLine(`Mode: ${gateway.mode}`);
|
|
5185
5898
|
channel.appendLine(`Posture: ${gateway.posture}`);
|
|
5186
|
-
channel.appendLine(
|
|
5899
|
+
channel.appendLine(
|
|
5900
|
+
config.fleetManaged ? `Policy: organisation fleet policy (${config.fleetManaged})` : `Policy file: ${config.gatewayConfig.policyFile || "(inline/none)"}`
|
|
5901
|
+
);
|
|
5187
5902
|
channel.show();
|
|
5188
5903
|
});
|
|
5189
5904
|
vscodeApi.commands.registerCommand("astrasync.switchMode", async () => {
|
|
5905
|
+
if (config.fleetManaged) {
|
|
5906
|
+
vscodeApi.window.showInformationMessage(
|
|
5907
|
+
"AstraSync: this machine is governed by your organisation\u2019s fleet policy \u2014 mode is managed centrally."
|
|
5908
|
+
);
|
|
5909
|
+
return;
|
|
5910
|
+
}
|
|
5190
5911
|
if (!config.gatewayConfig.apiKey) {
|
|
5191
5912
|
vscodeApi.window.showInformationMessage(
|
|
5192
5913
|
"AstraSync: Set astrasync.apiKey in settings to enable online/hybrid mode."
|
|
@@ -5216,7 +5937,62 @@ async function deactivate() {
|
|
|
5216
5937
|
gateway = null;
|
|
5217
5938
|
}
|
|
5218
5939
|
}
|
|
5219
|
-
function
|
|
5940
|
+
function fleetLockdownPolicy() {
|
|
5941
|
+
return {
|
|
5942
|
+
version: "1.0",
|
|
5943
|
+
name: "fleet-enrolment-required",
|
|
5944
|
+
purposes: [
|
|
5945
|
+
"file.read",
|
|
5946
|
+
"file.write",
|
|
5947
|
+
"file.delete",
|
|
5948
|
+
"shell.exec",
|
|
5949
|
+
"network.fetch",
|
|
5950
|
+
"payment.execute",
|
|
5951
|
+
"email.send"
|
|
5952
|
+
].map((id) => ({ id, allowed: false }))
|
|
5953
|
+
};
|
|
5954
|
+
}
|
|
5955
|
+
async function loadConfiguration(options) {
|
|
5956
|
+
const env = options.env ?? process.env;
|
|
5957
|
+
const fleet = resolveFleet(env, options.home ?? (0, import_os3.homedir)(), options.fleetPaths);
|
|
5958
|
+
if (fleet?.kind === "fleet") {
|
|
5959
|
+
const ctx = { enrolment: fleet.enrolment, paths: fleet.paths };
|
|
5960
|
+
try {
|
|
5961
|
+
const verified = await loadFleetPolicy(ctx);
|
|
5962
|
+
void recordDecisionAndHeartbeat(ctx, verified, null, {
|
|
5963
|
+
guardVersion: SDK_VERSION,
|
|
5964
|
+
adapterKind: "cursor"
|
|
5965
|
+
}).catch(() => void 0);
|
|
5966
|
+
return {
|
|
5967
|
+
fleetManaged: `${fleet.enrolment.org.name ?? fleet.enrolment.org.ownerId} v${verified.claims.ver}`,
|
|
5968
|
+
gatewayConfig: {
|
|
5969
|
+
mode: "local",
|
|
5970
|
+
policy: verified.policy,
|
|
5971
|
+
posture: verified.settings.posture,
|
|
5972
|
+
traceEnabled: true,
|
|
5973
|
+
tracePath: (0, import_path4.join)(fleet.paths.userDir, "traces"),
|
|
5974
|
+
debug: false
|
|
5975
|
+
}
|
|
5976
|
+
};
|
|
5977
|
+
} catch {
|
|
5978
|
+
}
|
|
5979
|
+
}
|
|
5980
|
+
if (fleet) {
|
|
5981
|
+
return {
|
|
5982
|
+
fleetManaged: "unavailable \u2014 enrol this machine with `astrasync guard enroll <token>`",
|
|
5983
|
+
gatewayConfig: {
|
|
5984
|
+
mode: "local",
|
|
5985
|
+
policy: fleetLockdownPolicy(),
|
|
5986
|
+
posture: "active",
|
|
5987
|
+
traceEnabled: true,
|
|
5988
|
+
tracePath: (0, import_path4.join)(fleet.paths.userDir, "traces"),
|
|
5989
|
+
debug: false
|
|
5990
|
+
}
|
|
5991
|
+
};
|
|
5992
|
+
}
|
|
5993
|
+
return loadLocalConfiguration();
|
|
5994
|
+
}
|
|
5995
|
+
function loadLocalConfiguration() {
|
|
5220
5996
|
return {
|
|
5221
5997
|
gatewayConfig: {
|
|
5222
5998
|
mode: "local",
|