@north-light/cli 0.3.749 → 0.3.751
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/builtin-memory/crouter-sdk/release-notes.md +10 -0
- package/dist/core/canvas/schema-pg/037-grant-principals.sql +50 -0
- package/dist/core/canvas/schema-version.d.ts +1 -1
- package/dist/core/canvas/schema-version.js +1 -1
- package/dist/core/grants/local.js +1 -1
- package/dist/core/grants/mirror.d.ts +11 -3
- package/dist/core/grants/mirror.js +12 -12
- package/dist/core/grants/owner.d.ts +7 -0
- package/dist/core/grants/owner.js +1 -0
- package/dist/core/grants/shadow.js +1 -1
- package/dist/core/grants/sync.js +1 -1
- package/dist/core/graph/reader.js +3 -3
- package/dist/core/import/load/bind.d.ts +1 -1
- package/dist/core/import/load/bind.js +11 -8
- package/dist/core/ledger/payer.d.ts +4 -4
- package/dist/core/ledger/payer.js +1 -1
- package/dist/core/llm/app-rungs.js +1 -1
- package/dist/core/llm/run.d.ts +2 -2
- package/dist/core/llm/run.js +2 -2
- package/dist/core/runtime/start-check.js +1 -1
- package/dist/core/spaces/permissions.js +1 -1
- package/dist/core/spaces/process.js +1 -1
- package/dist/daemon/api/app-listener.js +12 -12
- package/dist/daemon/api/handlers/git-credential.js +1 -1
- package/dist/daemon/api/handlers/runs.js +1 -1
- package/dist/daemon/api/principal.d.ts +11 -5
- package/dist/daemon/api/principal.js +1 -1
- package/docs/ci-runner.md +1 -1
- package/docs/sdk/release-notes.md +10 -0
- package/package.json +3 -3
- package/packages/crouter-api/dist/core/canvas/schema-version.js +1 -1
- package/packages/crouter-api/dist/core/grants/local.js +1 -1
- package/packages/crouter-api/dist/core/grants/mirror.js +35 -11
- package/packages/crouter-api/dist/core/grants/owner.js +15 -0
- package/packages/crouter-api/dist/core/grants/shadow.js +4 -2
- package/packages/crouter-api/dist/core/grants/sync.js +5 -4
- package/packages/crouter-api/dist/core/graph/reader.js +8 -5
- package/packages/crouter-api/dist/core/ledger/payer.js +8 -3
- package/packages/crouter-api/dist/core/llm/app-rungs.js +2 -2
- package/packages/crouter-api/dist/core/llm/run.js +3 -2
- package/packages/crouter-api/dist/core/runtime/start-check.js +2 -2
- package/packages/crouter-api/dist/core/spaces/permissions.js +10 -4
- package/packages/crouter-api/dist/core/spaces/process.js +5 -3
- package/packages/crouter-api/dist/daemon/api/handlers/git-credential.js +12 -2
- package/packages/crouter-api/dist/daemon/api/principal.js +26 -7
- package/packages/crouter-api/package.json +1 -1
- package/packages/crouter-identity/dist/index.d.ts +4 -2
- package/packages/crouter-identity/dist/index.js +31 -12
- package/packages/crouter-identity/package.json +1 -1
- package/packages/crouter-plugin/dist/serve.js +4 -1
- package/packages/crouter-plugin/dist/verify.js +36 -8
- package/packages/crouter-plugin/package.json +3 -3
- package/runtime.lock.json +14 -14
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createHash } from 'node:crypto';
|
|
2
|
-
import { parseScope } from '@north-light/identity';
|
|
2
|
+
import { parsePrincipal, parseScope } from '@north-light/identity';
|
|
3
3
|
import { openDb, withCanvasWrite } from '../canvas/db.js';
|
|
4
4
|
import { isUnsandboxedMachine } from '../runtime/unsandboxed.js';
|
|
5
5
|
import { replaceWrittenBy, userSubject } from '../permissions/facts.js';
|
|
@@ -7,6 +7,21 @@ import { GROUP_TYPE, OBJECT_TYPE } from '../permissions/builtin.js';
|
|
|
7
7
|
import { shadowCheck } from './shadow.js';
|
|
8
8
|
const SETUP_STATES = ['not_started', 'pending', 'ready', 'failed', 'unavailable'];
|
|
9
9
|
const STRENGTHS = ['ultra', 'strong', 'medium', 'light'];
|
|
10
|
+
/** The payer a mirrored grant carries, as a principal. The directory's older `'user'` (the grant's `sub`) and `'app'` (its `grantee`) read as those principals; anything else must parse as a principal. */
|
|
11
|
+
export function principalPayer(payer, sub, grantee) {
|
|
12
|
+
if (payer === undefined || payer === null || payer === 'user')
|
|
13
|
+
return sub;
|
|
14
|
+
if (payer === 'app')
|
|
15
|
+
return grantee;
|
|
16
|
+
if (typeof payer !== 'string' || parsePrincipal(payer) === null)
|
|
17
|
+
throw new TypeError('invalid grant payer');
|
|
18
|
+
return payer;
|
|
19
|
+
}
|
|
20
|
+
/** Whether the app's inference endpoint pays for this grant's model calls: its payer is an `app:` namespace root. */
|
|
21
|
+
export function endpointPays(grant) {
|
|
22
|
+
const payer = grant.payer === null ? null : parsePrincipal(grant.payer);
|
|
23
|
+
return payer !== null && payer.kind === 'app' && payer.path.length === 0;
|
|
24
|
+
}
|
|
10
25
|
/** The fleet's runtime principal (`CRTR_RUNTIME`): a cloud role reads only its own runtime's grants, so a person's VM or laptop grants are never used. A Mac sets none. */
|
|
11
26
|
export function runtimeScope() {
|
|
12
27
|
const runtime = process.env['CRTR_RUNTIME'];
|
|
@@ -93,7 +108,7 @@ function record(row) {
|
|
|
93
108
|
// A row whose computed columns lag its revision (a consent written but not yet computed) reads as no grant, never as the previous scopes.
|
|
94
109
|
if (!computedFor(row.rev, row.computed_rev, row.scopes_json !== null, row.resolved_scopes_json !== null))
|
|
95
110
|
throw new TypeError('grant columns are not computed for this revision');
|
|
96
|
-
return { id: row.id, runtime: row.runtime, sub: row.sub, grantee: row.grantee,
|
|
111
|
+
return { id: row.id, runtime: row.runtime, sub: row.sub, grantee: row.grantee, grantee_prefix: row.grantee_prefix ?? null,
|
|
97
112
|
rev: row.rev, scopes: entries(JSON.parse(row.scopes_json)),
|
|
98
113
|
resolved_scopes: entries(JSON.parse(row.resolved_scopes_json)),
|
|
99
114
|
spend_cap: row.spend_cap, expires_at: row.expires_at, status: row.status,
|
|
@@ -342,15 +357,22 @@ export async function grantStanding(grantId) {
|
|
|
342
357
|
export async function grantIdsWhere(where) {
|
|
343
358
|
return (await grantRows('id', where)).map((row) => row.id);
|
|
344
359
|
}
|
|
345
|
-
/** Who a grant's model calls are charged to: `app` only
|
|
360
|
+
/** Who a grant's model calls are charged to: `app` only when the app's endpoint pays (`endpointPays`), else the person. */
|
|
346
361
|
export async function payerOfGrant(grantId) {
|
|
347
|
-
|
|
362
|
+
const grant = grantId ? await grantRecord(grantId) : null;
|
|
363
|
+
return grant !== null && endpointPays(grant) ? 'app' : 'user';
|
|
348
364
|
}
|
|
349
365
|
export async function activeGrant(grantId) {
|
|
350
366
|
return active(await grantRow('*', 'id = ?', [grantId]));
|
|
351
367
|
}
|
|
368
|
+
/** `sub`'s active grant to `grantee`: the exact row, else the wildcard row whose `grantee_prefix` is the longest prefix of `grantee`. The `'*'` sentinel never matches as an exact grantee. */
|
|
352
369
|
export async function grantFor(grantee, sub) {
|
|
353
|
-
|
|
370
|
+
const exact = await grantRow('*', 'grantee = ? AND grantee_prefix IS NULL AND sub = ?', [grantee, sub]);
|
|
371
|
+
if (exact)
|
|
372
|
+
return active(exact);
|
|
373
|
+
const rows = await grantRows('*', 'sub = ? AND grantee_prefix IS NOT NULL AND starts_with(?, grantee_prefix)', [sub, grantee]);
|
|
374
|
+
rows.sort((a, b) => b.grantee_prefix.length - a.grantee_prefix.length);
|
|
375
|
+
return active(rows[0]);
|
|
354
376
|
}
|
|
355
377
|
/** The app's own active grant on this machine. Null for a mirror-only grant: an
|
|
356
378
|
* unsandboxed machine runs no app, so a fanned-out hosted grant is never "the
|
|
@@ -430,6 +452,8 @@ export async function replaceGrants(grants, syncedAt, source, teams = []) {
|
|
|
430
452
|
!Number.isInteger(grant.rev) || !['active', 'revoked', 'removed'].includes(grant.status)) {
|
|
431
453
|
throw new TypeError('invalid grant sync response');
|
|
432
454
|
}
|
|
455
|
+
if ((grant.grantee === '*') !== (grant.grantee_prefix != null))
|
|
456
|
+
throw new TypeError('invalid grant sync response');
|
|
433
457
|
ids.add(grant.id);
|
|
434
458
|
entries(grant.scopes);
|
|
435
459
|
entries(grant.resolved_scopes);
|
|
@@ -443,22 +467,22 @@ export async function replaceGrants(grants, syncedAt, source, teams = []) {
|
|
|
443
467
|
? db.prepareUncached(`DELETE FROM grants WHERE id NOT IN (${kept.map(() => '?').join(',')})`).run(...kept)
|
|
444
468
|
: db.prepareUncached('DELETE FROM grants').run())).changes);
|
|
445
469
|
const upsert = db.prepare(`INSERT INTO grants
|
|
446
|
-
(id, runtime, sub, grantee, rev, scopes_json, resolved_scopes_json, spend_cap, expires_at,
|
|
470
|
+
(id, runtime, sub, grantee, grantee_prefix, rev, scopes_json, resolved_scopes_json, spend_cap, expires_at,
|
|
447
471
|
status, revoked_at, approved_at, payer, dependencies_json, run_notice_url, inference_endpoint_json, synced_at, computed_rev)
|
|
448
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
449
|
-
ON CONFLICT (id) DO UPDATE SET runtime = excluded.runtime, sub = excluded.sub, grantee = excluded.grantee, rev = excluded.rev,
|
|
472
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
473
|
+
ON CONFLICT (id) DO UPDATE SET runtime = excluded.runtime, sub = excluded.sub, grantee = excluded.grantee, grantee_prefix = excluded.grantee_prefix, rev = excluded.rev,
|
|
450
474
|
scopes_json = excluded.scopes_json, resolved_scopes_json = excluded.resolved_scopes_json, spend_cap = excluded.spend_cap,
|
|
451
475
|
expires_at = excluded.expires_at, status = excluded.status, revoked_at = excluded.revoked_at, approved_at = excluded.approved_at,
|
|
452
476
|
payer = excluded.payer, dependencies_json = excluded.dependencies_json, run_notice_url = excluded.run_notice_url,
|
|
453
477
|
inference_endpoint_json = excluded.inference_endpoint_json, synced_at = excluded.synced_at, computed_rev = excluded.computed_rev
|
|
454
|
-
WHERE (grants.runtime, grants.sub, grants.grantee, grants.rev, grants.scopes_json, grants.resolved_scopes_json, grants.spend_cap,
|
|
478
|
+
WHERE (grants.runtime, grants.sub, grants.grantee, grants.grantee_prefix, grants.rev, grants.scopes_json, grants.resolved_scopes_json, grants.spend_cap,
|
|
455
479
|
grants.expires_at, grants.status, grants.revoked_at, grants.approved_at, grants.payer, grants.dependencies_json,
|
|
456
480
|
grants.run_notice_url, grants.inference_endpoint_json, grants.computed_rev)
|
|
457
|
-
IS DISTINCT FROM (excluded.runtime, excluded.sub, excluded.grantee, excluded.rev, excluded.scopes_json, excluded.resolved_scopes_json,
|
|
481
|
+
IS DISTINCT FROM (excluded.runtime, excluded.sub, excluded.grantee, excluded.grantee_prefix, excluded.rev, excluded.scopes_json, excluded.resolved_scopes_json,
|
|
458
482
|
excluded.spend_cap, excluded.expires_at, excluded.status, excluded.revoked_at, excluded.approved_at, excluded.payer,
|
|
459
483
|
excluded.dependencies_json, excluded.run_notice_url, excluded.inference_endpoint_json, excluded.computed_rev)`);
|
|
460
484
|
for (const grant of grants) {
|
|
461
|
-
changed += Number((await upsert.run(grant.id, grant.runtime, grant.sub, grant.grantee, grant.rev, JSON.stringify(grant.scopes), JSON.stringify(grant.resolved_scopes), grant.spend_cap, grant.expires_at, grant.status, grant.revoked_at, grant.approved_at, grant.payer, JSON.stringify(dependenciesOf(grant.dependencies)), grant.run_notice_url ?? null, grant.inference_endpoint ? JSON.stringify(grant.inference_endpoint) : null, grant.synced_at, grant.rev)).changes);
|
|
485
|
+
changed += Number((await upsert.run(grant.id, grant.runtime, grant.sub, grant.grantee, grant.grantee_prefix ?? null, grant.rev, JSON.stringify(grant.scopes), JSON.stringify(grant.resolved_scopes), grant.spend_cap, grant.expires_at, grant.status, grant.revoked_at, grant.approved_at, principalPayer(grant.payer, grant.sub, grant.grantee), JSON.stringify(dependenciesOf(grant.dependencies)), grant.run_notice_url ?? null, grant.inference_endpoint ? JSON.stringify(grant.inference_endpoint) : null, grant.synced_at, grant.rev)).changes);
|
|
462
486
|
}
|
|
463
487
|
await replaceWrittenBy(db, 'directory', GROUP_TYPE, 'member', teams.flatMap(({ team, members }) => members.map((member) => ({ resourceId: team, subject: userSubject(member) }))));
|
|
464
488
|
await reconcileAppAdminFacts(db);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { parsePrincipal, parseScope } from '@north-light/identity';
|
|
2
|
+
import { pluginAppToken } from '../secrets.js';
|
|
3
|
+
/** Whether `sub` lies in `grantee`'s own namespace (`app:x` for `app:x` and `app:x/…`). A namespace owns itself and needs no grant row. */
|
|
4
|
+
export function isNamespaceOwner(grantee, sub) {
|
|
5
|
+
const principal = parsePrincipal(sub);
|
|
6
|
+
return principal !== null && principal.kind === 'app' && principal.namespace === grantee;
|
|
7
|
+
}
|
|
8
|
+
/** Whether a run with no grant row may proceed as its namespace owner: `sub` is in the grantee's namespace and no grant id is named (a named grant that is gone or revoked still refuses). */
|
|
9
|
+
export function ownsWithoutRow(grantee, sub, grantId) {
|
|
10
|
+
return !grantId && isNamespaceOwner(grantee, sub);
|
|
11
|
+
}
|
|
12
|
+
/** The scopes an owner principal holds with no grant row: the app's declared scopes (its `crtr_app_` token's), none when it declares none. */
|
|
13
|
+
export function ownerScopes(grantee) {
|
|
14
|
+
return (pluginAppToken(grantee.replace(/^app:/, ''))?.scopes ?? []).map((scope) => parseScope(scope));
|
|
15
|
+
}
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
import { directoryIssuer } from '../directory/issuer.js';
|
|
4
4
|
import { outboundFetch } from '../events/outbound.js';
|
|
5
5
|
import { warn } from '../output.js';
|
|
6
|
-
import { canonicalJson } from './mirror.js';
|
|
6
|
+
import { canonicalJson, principalPayer } from './mirror.js';
|
|
7
7
|
let ttlMs = 5_000;
|
|
8
8
|
/** Test hook: how long a cached answer is used before its epochs are re-checked. */
|
|
9
9
|
export function setShadowTtl(ms) { ttlMs = ms; }
|
|
@@ -67,7 +67,9 @@ async function compare(stored) {
|
|
|
67
67
|
shadowCounts.lagging++;
|
|
68
68
|
return;
|
|
69
69
|
}
|
|
70
|
-
|
|
70
|
+
// The directory may still send the older `'user'`/`'app'` payer; the mirror stores it as a principal.
|
|
71
|
+
const liveSide = { ...live, payer: principalPayer(live['payer'], stored.sub, stored.grantee) };
|
|
72
|
+
const fields = COMPARED.filter((key) => canonicalJson(liveSide[key] ?? null) !== canonicalJson(stored[key] ?? null));
|
|
71
73
|
if (fields.length === 0) {
|
|
72
74
|
shadowCounts.agree++;
|
|
73
75
|
return;
|
|
@@ -7,7 +7,7 @@ import { reconcilePackages } from '../graph/package-reconcile.js';
|
|
|
7
7
|
import { isUnsandboxedMachine } from '../runtime/unsandboxed.js';
|
|
8
8
|
import { emitEvent } from '../events/emit.js';
|
|
9
9
|
import { describeOutboundFailure, outboundFetch, outboundJson, outboundRefusal } from '../events/outbound.js';
|
|
10
|
-
import { dependenciesOf, inferenceEndpointOf, replaceGrants, teamsOf } from './mirror.js';
|
|
10
|
+
import { dependenciesOf, endpointPays, inferenceEndpointOf, principalPayer, replaceGrants, teamsOf } from './mirror.js';
|
|
11
11
|
import { onGrantSync } from './on-sync.js';
|
|
12
12
|
import { hostedEndpointOf, paymentSetupUrlOf, storeHostedEndpoint } from './hosted-endpoint.js';
|
|
13
13
|
import { adoptInstallPerson } from '../scoped-state/migrate.js';
|
|
@@ -108,11 +108,12 @@ function responseGrant(value, client) {
|
|
|
108
108
|
const approvedAt = revisions?.find((revision) => revision.rev === grant.rev)?.approved_at;
|
|
109
109
|
if (typeof approvedAt !== 'string')
|
|
110
110
|
throw new TypeError('grant revision has no approval time');
|
|
111
|
-
//
|
|
112
|
-
const payer = grant.payer
|
|
111
|
+
// The payer is a principal; an older directory's `'user'`/`'app'` (or none) maps to the grant's `sub`/`grantee`. The endpoint rides only a grant the app's endpoint pays for.
|
|
112
|
+
const payer = principalPayer(grant.payer, grant.sub, grant.grantee);
|
|
113
113
|
return { ...grant, spend_cap: grant.spend_cap ?? null, expires_at: grant.expires_at ?? null,
|
|
114
|
+
grantee_prefix: typeof grant.grantee_prefix === 'string' ? grant.grantee_prefix : null,
|
|
114
115
|
revoked_at: grant.revoked_at ?? null, approved_at: approvedAt, payer, dependencies: dependenciesOf(grant.dependencies),
|
|
115
|
-
inference_endpoint: payer
|
|
116
|
+
inference_endpoint: endpointPays({ payer }) ? inferenceEndpointOf(grant.inference_endpoint) : null,
|
|
116
117
|
run_notice_url: typeof grant.run_notice_url === 'string' && grant.run_notice_url !== '' ? grant.run_notice_url : null };
|
|
117
118
|
}
|
|
118
119
|
/** The person this pull serves: the bound one, else (a hosted first boot has none until its first snapshot commits) the one
|
|
@@ -4,6 +4,7 @@ import { existsSync } from 'node:fs';
|
|
|
4
4
|
import { CrtrError } from '../errors.js';
|
|
5
5
|
import { getCachedProfile } from '../scoped-state/person-store.js';
|
|
6
6
|
import { parseScope } from '@north-light/identity';
|
|
7
|
+
import { isNamespaceOwner, ownerScopes } from '../grants/owner.js';
|
|
7
8
|
import { activeGrant, grantFor, runtimeScope } from '../grants/mirror.js';
|
|
8
9
|
import { withCanvasWrite } from '../canvas/db.js';
|
|
9
10
|
import { ApiError } from '../../api/errors.js';
|
|
@@ -125,11 +126,12 @@ export async function readerFor(db, principal, reposFn) {
|
|
|
125
126
|
if (principal.actor.kind === 'person')
|
|
126
127
|
return { class: 'person', person: await personObject(db) };
|
|
127
128
|
const grant = await grantFor(principal.actor.app, principal.sub);
|
|
128
|
-
|
|
129
|
+
const resolved = grant ? grant.resolved : isNamespaceOwner(principal.actor.app, principal.sub) ? ownerScopes(principal.actor.app) : null;
|
|
130
|
+
if (resolved === null)
|
|
129
131
|
throw new ApiError(403, 'app_not_granted', `${principal.actor.app} holds no active grant for ${principal.sub} on this machine`);
|
|
130
132
|
const stored = principal.actor.node_scopes;
|
|
131
133
|
return { class: 'app', app: await appObject(db, principal.actor.app), sub: principal.sub, place: principal.actor.place, launched: stored,
|
|
132
|
-
scopes: stored === null ?
|
|
134
|
+
scopes: stored === null ? resolved : stored.filter((scope) => coveredByAny(resolved, scope)) };
|
|
133
135
|
}
|
|
134
136
|
if (principal.class === 'cron') {
|
|
135
137
|
// A cron has no node: it reads as its app, bounded by its stored scopes, each kept while the grant's current set covers it.
|
|
@@ -242,8 +244,9 @@ async function readerForStub(db, stub, identity, lookups) {
|
|
|
242
244
|
return { class: 'object', object: stub, node: stub, owner: person, app: person, scopes: null, repos: [] };
|
|
243
245
|
const app = identity?.app == null ? null : await appObject(db, identity.app);
|
|
244
246
|
const grant = identity?.app == null ? null : await lookups.grantFor(identity.app, identity.sub);
|
|
245
|
-
const
|
|
246
|
-
|
|
247
|
+
const resolved = grant !== null ? grant.resolved : identity?.app != null && isNamespaceOwner(identity.app, identity.sub) ? ownerScopes(identity.app) : null;
|
|
248
|
+
const scopes = identity === null || resolved === null ? null
|
|
249
|
+
: identity.node_scopes === null ? resolved : identity.node_scopes.filter((scope) => coveredByAny(resolved, scope));
|
|
247
250
|
const owner = app ?? (stub.owner === null ? person : await lookups.object(stub.owner) ?? person);
|
|
248
251
|
return { class: 'object', object: stub, node: stub, owner, app: owner, scopes, launched: identity?.node_scopes ?? null, repos: [],
|
|
249
252
|
...(identity?.place == null ? {} : { place: identity.place }) };
|
|
@@ -277,7 +280,7 @@ export function publishLookups(db) {
|
|
|
277
280
|
/** What the run may reach beyond its own app: the active grant, narrowed by the launched scope list. */
|
|
278
281
|
async function nodeScopes(app, sub, grantId, launched, lookups, allowRevoked = false) {
|
|
279
282
|
const grant = grantId ? await lookups.grantById(grantId) : await lookups.grantFor(app, sub);
|
|
280
|
-
const resolved = grant && grant.grantee === app ? grant.resolved : null;
|
|
283
|
+
const resolved = grant && grant.grantee === app ? grant.resolved : grant === null && grantId === null && isNamespaceOwner(app, sub) ? ownerScopes(app) : null;
|
|
281
284
|
if (resolved === null) {
|
|
282
285
|
if (allowRevoked)
|
|
283
286
|
return null;
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// Payer resolution (design-ledger, "Payer resolution"): one function. It reads the grant's payer and the credential route,
|
|
2
2
|
// never a request header, so a caller cannot name its payer.
|
|
3
|
+
import { endpointPays } from '../grants/mirror.js';
|
|
4
|
+
import { isNamespaceOwner } from '../grants/owner.js';
|
|
3
5
|
/** Who pays, and with what funding. A device call is not priced and never asks. */
|
|
4
6
|
export function payerOf(call) {
|
|
5
7
|
const paid = fundedBy(call);
|
|
@@ -10,9 +12,12 @@ function fundedBy(call) {
|
|
|
10
12
|
case 'model':
|
|
11
13
|
return call.route === 'endpoint' ? { payer: call.grantee, funding: 'endpoint' } : { payer: call.sub, funding: call.route };
|
|
12
14
|
case 'tool':
|
|
13
|
-
return
|
|
14
|
-
case 'start':
|
|
15
|
+
return { payer: call.tokenPayer || call.sub, funding: 'bill' };
|
|
16
|
+
case 'start': {
|
|
15
17
|
// Funding is chosen per candidate later; a start check ignores it.
|
|
16
|
-
|
|
18
|
+
if (call.grantPayer)
|
|
19
|
+
return { payer: call.grantPayer, funding: endpointPays({ payer: call.grantPayer }) ? 'endpoint' : 'bill' };
|
|
20
|
+
return { payer: isNamespaceOwner(call.grantee, call.sub) ? call.grantee : call.sub, funding: 'bill' };
|
|
21
|
+
}
|
|
17
22
|
}
|
|
18
23
|
}
|
|
@@ -5,7 +5,7 @@ import { defaultModelLaddersConfig } from '../../types.js';
|
|
|
5
5
|
import { MODEL_STRENGTHS, stripThinkingSuffix } from '../runtime/model-selection.js';
|
|
6
6
|
import { modelTokenStrength } from '../runtime/launch.js';
|
|
7
7
|
import { RunError } from '../runs/operations.js';
|
|
8
|
-
import { grantRecord } from '../grants/mirror.js';
|
|
8
|
+
import { endpointPays, grantRecord } from '../grants/mirror.js';
|
|
9
9
|
const STRENGTHS = MODEL_STRENGTHS;
|
|
10
10
|
/** The endpoint's declared rungs, nearest to `strength` first (the stronger on a tie). */
|
|
11
11
|
function rungsByDistance(endpoint, strength) {
|
|
@@ -55,7 +55,7 @@ export function appCandidates(endpoint, requested, exact = false) {
|
|
|
55
55
|
* each rung's own level. */
|
|
56
56
|
export async function appRungForGrant(grantId, requested, exact = false) {
|
|
57
57
|
const grant = grantId ? await grantRecord(grantId) : null;
|
|
58
|
-
if (grant
|
|
58
|
+
if (!grant || !endpointPays(grant))
|
|
59
59
|
return null;
|
|
60
60
|
if (!grant.inference_endpoint)
|
|
61
61
|
throw new RunError(404, 'model_unavailable', `${grant.grantee} is app-paid but its grant carries no inference endpoint`);
|
|
@@ -15,6 +15,7 @@ import { eligibleHostedEndpoint, readHostedEndpoint } from '../grants/hosted-end
|
|
|
15
15
|
import { parseProviderPayment } from '../provider-failure.js';
|
|
16
16
|
import { personIdAsync } from '../canvas/person.js';
|
|
17
17
|
import { getNode } from '../canvas/canvas.js';
|
|
18
|
+
import { endpointPays } from '../grants/mirror.js';
|
|
18
19
|
import { outboundToken } from '../providers/mint.js';
|
|
19
20
|
import { discardFile } from '../providers/token-cache.js';
|
|
20
21
|
import { ProviderError } from '../providers/errors.js';
|
|
@@ -154,7 +155,7 @@ function allowedFallback(model, responseModel) {
|
|
|
154
155
|
return fallbacks?.find((fallback) => fallback.provider === model.provider && fallback.model === responseModel);
|
|
155
156
|
}
|
|
156
157
|
export function llmCallerOf(grant) {
|
|
157
|
-
return grant ? { payer: grant.payer
|
|
158
|
+
return grant ? { payer: grant.payer, sub: grant.sub, grantee: grant.grantee, inference_endpoint: grant.inference_endpoint ?? null } : undefined;
|
|
158
159
|
}
|
|
159
160
|
/** A directory-token failure as the `llm` refusal it is (RT-RTE-26): the directory unreachable or failing is
|
|
160
161
|
* transient, a refused mint is a configuration fault. */
|
|
@@ -184,7 +185,7 @@ function vendorRequestId(headers) {
|
|
|
184
185
|
* and a completed call writes one `charges` row. Omitted, the call is neither checked nor recorded.
|
|
185
186
|
* `causedBy`: the request's `Crtr-Caused-By` value (`message:<id>`, `request:<id>`, `cron_run:<id>`), when it carried one; stored on the row. */
|
|
186
187
|
export async function runLlm(input, signal, onChunk, target, spend, caller, causedBy) {
|
|
187
|
-
const app = caller
|
|
188
|
+
const app = caller && endpointPays(caller) ? caller : undefined;
|
|
188
189
|
if (input.payer === 'app' && !app)
|
|
189
190
|
throw new RunError(403, 'payer_not_app', 'this grant is not app-paid');
|
|
190
191
|
if (input.payer === 'user' && app)
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// starts or a dormant node revives. The candidate's payer is whoever pays the node's model calls.
|
|
3
3
|
import { check, environmentPayerOfProfile, findRefusal, payerOf } from '../ledger/index.js';
|
|
4
4
|
import { RunError } from '../runs/operations.js';
|
|
5
|
-
import {
|
|
5
|
+
import { grantRecord, grantStanding } from '../grants/mirror.js';
|
|
6
6
|
import { callerTerminalGrant } from '../grants/terminal.js';
|
|
7
7
|
/** The start candidate for an identity, or null when no person is known yet (a terminal node before its first grant snapshot). */
|
|
8
8
|
export async function startCandidateOf(who) {
|
|
@@ -10,7 +10,7 @@ export async function startCandidateOf(who) {
|
|
|
10
10
|
const sub = who.sub ?? (grantee === 'app:terminal' ? (await callerTerminalGrant())?.sub : undefined) ?? null;
|
|
11
11
|
if (sub === null)
|
|
12
12
|
return null;
|
|
13
|
-
const paid = await
|
|
13
|
+
const paid = who.grant_id ? (await grantRecord(who.grant_id))?.payer : null;
|
|
14
14
|
const payer = payerOf({ call: 'start', grantPayer: paid, sub, grantee, environmentPayer: await environmentPayerOfProfile(who.profile_id) });
|
|
15
15
|
return { scope: 'start', payer: payer.payer, sub, grantee };
|
|
16
16
|
}
|
|
@@ -1,31 +1,37 @@
|
|
|
1
1
|
import { parseScope } from '@north-light/identity';
|
|
2
2
|
import { coveredByAny } from '../../shared/env.js';
|
|
3
3
|
import { activeGrant, grantFor } from '../grants/mirror.js';
|
|
4
|
+
import { isNamespaceOwner, ownerScopes } from '../grants/owner.js';
|
|
4
5
|
import { appSpace, userFilesRoot } from '../layout.js';
|
|
5
6
|
import { contains } from './file-root.js';
|
|
6
7
|
import { join, resolve } from 'node:path';
|
|
7
8
|
import { lstatSync, realpathSync } from 'node:fs';
|
|
8
9
|
/** Only the active mirror and the launched scope list determine a run's mounts. */
|
|
9
10
|
export async function permissionsForRun(app, sub, grantId, launched) {
|
|
10
|
-
|
|
11
|
+
const grant = grantId ? await activeGrant(grantId) : await grantFor(app, sub);
|
|
12
|
+
// A revoked grant named by id never falls back to namespace ownership.
|
|
13
|
+
if (grantId && !grant)
|
|
14
|
+
throw new Error(`no active grant for ${app}`);
|
|
15
|
+
return permissionsForGrant(app, sub, grant, launched);
|
|
11
16
|
}
|
|
12
17
|
/** The same computation over a given grant record, e.g. the mirror before a sync replaced it. */
|
|
13
18
|
export function permissionsForGrant(app, sub, grant, launched) {
|
|
14
|
-
if (
|
|
19
|
+
if (grant ? grant.grantee !== app || grant.sub !== sub : !isNamespaceOwner(app, sub))
|
|
15
20
|
throw new Error(`no active grant for ${app}`);
|
|
16
21
|
if (launched !== null && !Array.isArray(launched))
|
|
17
22
|
throw new Error('invalid launched scopes');
|
|
18
23
|
const otherFiles = [];
|
|
19
24
|
const otherNodes = [];
|
|
20
25
|
let ownNodes = launched === null;
|
|
21
|
-
const
|
|
26
|
+
const resolved = grant ? grant.resolved : ownerScopes(app);
|
|
27
|
+
const scopes = launched ?? resolved;
|
|
22
28
|
for (const scope of scopes) {
|
|
23
29
|
if (typeof scope !== 'string')
|
|
24
30
|
throw new Error('invalid launched scope');
|
|
25
31
|
parseScope(scope);
|
|
26
32
|
const own = scope === `crtr:memory:read:${app}` || scope === `crtr:memory:rw:${app}` ||
|
|
27
33
|
scope.startsWith(`crtr:memory:read:${app}:`) || scope.startsWith(`crtr:memory:rw:${app}:`);
|
|
28
|
-
if (launched !== null && !own && !coveredByAny(
|
|
34
|
+
if (launched !== null && !own && !coveredByAny(resolved, scope))
|
|
29
35
|
throw new Error(`launched scope not in grant: ${scope}`);
|
|
30
36
|
const files = /^crtr:files:(read|rw):(user|app:[A-Za-z0-9_-]{1,64}):(.+)$/.exec(scope);
|
|
31
37
|
if (files) {
|
|
@@ -6,6 +6,7 @@ import { getNode } from '../canvas/canvas.js';
|
|
|
6
6
|
import { nodeAgentDir, nodeHome, nodeRunDir } from '../canvas/paths.js';
|
|
7
7
|
import { appSpace } from '../layout.js';
|
|
8
8
|
import { activeGrant, activeGrantForApp, grantFor, grantsGeneration } from '../grants/mirror.js';
|
|
9
|
+
import { ownsWithoutRow } from '../grants/owner.js';
|
|
9
10
|
import { emitEvent } from '../events/emit.js';
|
|
10
11
|
import { ensureAppSpace } from './space.js';
|
|
11
12
|
import { compileLaunch, launchStillGranted } from './mounts.js';
|
|
@@ -44,7 +45,7 @@ export async function cwdIsMounted(app, cwd, nodeId, isolated = false, folder, p
|
|
|
44
45
|
// A stored principal decides the grant and scopes even for an anchored run (see launchAppProcess).
|
|
45
46
|
const grant = principal !== undefined ? (principal.grantId ? await activeGrant(principal.grantId) : await grantFor(app, principal.sub))
|
|
46
47
|
: row ? null : await activeGrantForApp(app);
|
|
47
|
-
if (principal !== undefined ? !grant : !row && !grant)
|
|
48
|
+
if (principal !== undefined ? !grant && !ownsWithoutRow(app, principal.sub, principal.grantId) : !row && !grant)
|
|
48
49
|
throw new Error(`no active grant for ${app}`);
|
|
49
50
|
const spec = await compileLaunch({ nodeId: nodeId ?? `proc-cwd-check`, app,
|
|
50
51
|
...(principal !== undefined
|
|
@@ -88,11 +89,12 @@ export async function launchAppProcess(options) {
|
|
|
88
89
|
let grant = null;
|
|
89
90
|
if (stored !== undefined) {
|
|
90
91
|
grant = stored.grantId ? await activeGrant(stored.grantId) : await grantFor(app, stored.sub);
|
|
91
|
-
if (
|
|
92
|
+
if (grant ? grant.grantee !== app || grant.sub !== stored.sub : !ownsWithoutRow(app, stored.sub, stored.grantId))
|
|
92
93
|
throw new Error(`no active grant for ${app}`);
|
|
93
94
|
}
|
|
94
95
|
else if (row) {
|
|
95
|
-
|
|
96
|
+
const rowSub = launchSub(row.sub, row.node_id);
|
|
97
|
+
if (app !== 'app:terminal' && !ownsWithoutRow(app, rowSub, row.grant_id) && !(row.grant_id ? await activeGrant(row.grant_id) : await grantFor(app, rowSub))) {
|
|
96
98
|
throw new Error(`no active grant for ${app}`);
|
|
97
99
|
}
|
|
98
100
|
}
|
|
@@ -48,8 +48,18 @@ export async function mintInstallationToken(sub, repositories, allowPullRequestW
|
|
|
48
48
|
if (!response.ok) {
|
|
49
49
|
const code = typeof parsed.error === 'object' ? parsed.error?.code : parsed.code;
|
|
50
50
|
const message = (typeof parsed.error === 'object' ? parsed.error?.message : parsed.message) ?? `the GitHub service answered ${response.status}`;
|
|
51
|
-
|
|
52
|
-
|
|
51
|
+
// The GitHub app names the page where the person links GitHub and installs the App; the CLI shows it as the next step.
|
|
52
|
+
const next = typeof parsed.error === 'object' ? parsed.error?.next : parsed.next;
|
|
53
|
+
if (response.status === 409 && (code === 'repositories_span_installations' || code === 'github_not_connected')) {
|
|
54
|
+
const connect = typeof next === 'string' && next !== '' ? next : undefined;
|
|
55
|
+
throw new ApiError(409, code, message, { repositories, ...(connect === undefined ? {} : {
|
|
56
|
+
error: code, message,
|
|
57
|
+
next: code === 'github_not_connected'
|
|
58
|
+
? `Ask the person to open ${connect}, link GitHub and install the Northlight GitHub App on the account that owns these repositories (choose "All repositories"), then retry.`
|
|
59
|
+
: `List repositories from one GitHub account only; manage installations at ${connect}.`,
|
|
60
|
+
url: connect,
|
|
61
|
+
}) });
|
|
62
|
+
}
|
|
53
63
|
throw new ApiError(502, 'github_service_failed', message, { status: response.status, ...(code === undefined ? {} : { code }) });
|
|
54
64
|
}
|
|
55
65
|
if (typeof parsed.token !== 'string' || typeof parsed.expires_at !== 'string')
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { covers, JwksCache, MissingRevClaimError, verifyCallToken } from '@north-light/identity';
|
|
1
|
+
import { covers, JwksCache, parsePrincipal, MissingRevClaimError, verifyCallToken } from '@north-light/identity';
|
|
2
2
|
import { createLocalJWKSet, decodeProtectedHeader, jwtVerify } from 'jose';
|
|
3
3
|
import { callerGrantOf, coverGrant } from '../../core/grants/cover.js';
|
|
4
|
+
import { ownerScopes } from '../../core/grants/owner.js';
|
|
4
5
|
import { RunError } from '../../core/runs/operations.js';
|
|
5
6
|
import { NoticeLedger } from './sync-notice.js';
|
|
6
7
|
/** The 401 for a handoff that does not verify; one message per purpose, shared by the resolver and the vendor and key pages. */
|
|
@@ -119,21 +120,33 @@ export class AppPrincipalResolver {
|
|
|
119
120
|
async resolve(authorization, scope) {
|
|
120
121
|
return this.resolveVerified(await this.verifyCall(authorization), scope);
|
|
121
122
|
}
|
|
122
|
-
/** The caller an already-verified
|
|
123
|
-
* (
|
|
124
|
-
*
|
|
123
|
+
/** The caller an already-verified token names, from `chain[0]` (which must be the whole chain): (a) an app link holding a
|
|
124
|
+
* grant, (b) an owner link (a grantless app link whose app owns `sub`'s namespace: no grant row, the app's declared
|
|
125
|
+
* scopes), or (c) a person's grant-bearing link on an exchanged token. A grant is covered by the mirror and read now
|
|
126
|
+
* (`coverGrant`). Any other presenter is refused 401. */
|
|
125
127
|
async resolveVerified(token, scope) {
|
|
126
128
|
const link = token.chain[0];
|
|
127
129
|
const claims = token.claims;
|
|
128
|
-
|
|
130
|
+
const grantLink = link !== undefined && link.grant !== undefined && link.rev !== undefined;
|
|
131
|
+
const person = link !== undefined && parsePrincipal(link.sub)?.kind === 'user';
|
|
132
|
+
if (token.chain.length !== 1 || !link || !/^[A-Za-z0-9_-]{1,64}$/.test(claims.client_id) ||
|
|
133
|
+
!(link.owner === true || (grantLink && (link.sub.startsWith('app:') || person))) ||
|
|
129
134
|
(claims.grantee !== undefined && claims.grantee !== link.sub) || (claims['rev'] !== undefined && claims['rev'] !== link.rev)) {
|
|
130
135
|
throw new RunError(401, 'unauthorized', 'invalid directory access token');
|
|
131
136
|
}
|
|
137
|
+
if (link.owner === true) {
|
|
138
|
+
const resolved = ownerScopes(link.sub);
|
|
139
|
+
if (scope && !resolved.some((held) => covers(held, scope))) {
|
|
140
|
+
throw new RunError(403, 'scope_missing', `missing scope ${scope}`, undefined, undefined, scope);
|
|
141
|
+
}
|
|
142
|
+
return { sub: token.sub, grantee: link.sub, actor: link.sub, client_id: claims.client_id, grant: null, rev: null, resolved,
|
|
143
|
+
resolved_scopes: resolved.map((held) => ({ scope: held, mode: 'always' })), spend_cap: null, dependencies: {}, exp: claims.exp };
|
|
144
|
+
}
|
|
132
145
|
const grant = await coverGrant(link.grant, link.rev, token.sub, link.sub, this.syncGrants);
|
|
133
146
|
if (scope && !grant.resolved.some((held) => covers(held, scope))) {
|
|
134
147
|
throw new RunError(403, 'scope_missing', `missing scope ${scope}`, undefined, undefined, scope);
|
|
135
148
|
}
|
|
136
|
-
return { sub: token.sub, grantee: link.sub, client_id: claims.client_id, grant: link.grant,
|
|
149
|
+
return { sub: token.sub, grantee: link.sub, actor: link.sub, client_id: claims.client_id, grant: link.grant,
|
|
137
150
|
rev: grant.rev, resolved: grant.resolved, resolved_scopes: grant.resolved_scopes,
|
|
138
151
|
spend_cap: grant.spend_cap, dependencies: grant.dependencies ?? {}, exp: claims.exp };
|
|
139
152
|
}
|
|
@@ -143,5 +156,11 @@ export class AppPrincipalResolver {
|
|
|
143
156
|
* Otherwise 401 `grant_revoked`. `resolve()` and every later re-read during a
|
|
144
157
|
* request (a `grant.get` long-poll waking) apply this one check. */
|
|
145
158
|
export async function callerGrant(caller) {
|
|
146
|
-
return await callerGrantOf(caller
|
|
159
|
+
return await callerGrantOf(grantIdOf(caller), caller.sub, caller.grantee);
|
|
160
|
+
}
|
|
161
|
+
/** The grant id of a caller that must hold a grant row; an owner principal (CR-9 (b)) holds none, so the operation is 403. */
|
|
162
|
+
export function grantIdOf(caller) {
|
|
163
|
+
if (caller.grant === null)
|
|
164
|
+
throw new RunError(403, 'permission_error', 'this operation needs a grant; an owner token holds none');
|
|
165
|
+
return caller.grant;
|
|
147
166
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@north-light/api",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.751",
|
|
4
4
|
"description": "Typed crtrd /v1 API contract — DTOs, route builders, the error contract, the CrtrClient, and the command-plugin manifest format. Zero runtime dependencies.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/api/index.js",
|
|
@@ -59,10 +59,12 @@ export declare class JwksCache {
|
|
|
59
59
|
forKid(kid: string | undefined): Promise<JSONWebKeySet>;
|
|
60
60
|
}
|
|
61
61
|
/** One link of a call token's actor chain. `grant` and `rev` appear only on an `app:` link. */
|
|
62
|
+
/** `owner` marks an app link with no grant whose app owns the namespace of the token's `sub`. */
|
|
62
63
|
export interface CallLink {
|
|
63
64
|
sub: string;
|
|
64
65
|
grant?: string;
|
|
65
66
|
rev?: number;
|
|
67
|
+
owner?: true;
|
|
66
68
|
}
|
|
67
69
|
/** The verified payload of a call token. Fields beyond `sub`/`aud`/`client_id` are present on some uses only. */
|
|
68
70
|
export interface CallTokenClaims extends JWTPayload {
|
|
@@ -85,7 +87,7 @@ export interface CallTokenClaims extends JWTPayload {
|
|
|
85
87
|
}
|
|
86
88
|
/** A verified call token. The verifier authorizes nothing: the caller decides authority from `chain`. */
|
|
87
89
|
export interface CallToken {
|
|
88
|
-
/** `
|
|
90
|
+
/** Any principal `parsePrincipal` accepts. */
|
|
89
91
|
sub: string;
|
|
90
92
|
aud: string;
|
|
91
93
|
/** The actor chain, index 0 = the presenter. */
|
|
@@ -97,7 +99,7 @@ export interface CallToken {
|
|
|
97
99
|
/**
|
|
98
100
|
* Verifies a call token of any use (peer, app access, outbound provider, run notice) and returns its actor chain,
|
|
99
101
|
* presenter first. Checks in order: header `typ` `at+jwt`, `alg` ES256 or RS256, non-empty `kid`; signature; `iss`;
|
|
100
|
-
* `aud` a string equal to `audience`; `exp` and `iat` numbers; `sub`
|
|
102
|
+
* `aud` a string equal to `audience`; `exp` and `iat` numbers; `sub` a principal `parsePrincipal` accepts; `client_id` non-empty; `scope`
|
|
101
103
|
* a string when present; then the chain. A token with no `act` is refused: the legacy machine and app shapes are not
|
|
102
104
|
* accepted. Any `machine_id` must equal the first link, any top-level `grant` an app link's, and, on a token without
|
|
103
105
|
* `scope`, any `grantee` and `rev` the app link's (or, with no app link, `grantee` = `sub` and no `rev`). It authorizes
|
|
@@ -245,9 +245,7 @@ function isDeveloperCredentialEnvelope(p) {
|
|
|
245
245
|
function chainOf(payload) {
|
|
246
246
|
const p = payload;
|
|
247
247
|
const bad = () => { throw new Error('Invalid call token claims'); };
|
|
248
|
-
const
|
|
249
|
-
if (grant === undefined)
|
|
250
|
-
return { sub };
|
|
248
|
+
const grantLink = (sub, grant, rev) => {
|
|
251
249
|
if (typeof grant !== 'string' || grant === '')
|
|
252
250
|
return bad();
|
|
253
251
|
if (rev === undefined)
|
|
@@ -256,6 +254,7 @@ function chainOf(payload) {
|
|
|
256
254
|
return bad();
|
|
257
255
|
return { sub, grant, rev };
|
|
258
256
|
};
|
|
257
|
+
const tokenSub = typeof p.sub === 'string' ? parsePrincipal(p.sub) : null;
|
|
259
258
|
if (p.act === undefined)
|
|
260
259
|
return bad();
|
|
261
260
|
const chain = [];
|
|
@@ -273,19 +272,39 @@ function chainOf(payload) {
|
|
|
273
272
|
chain.push({ sub: link.sub });
|
|
274
273
|
break;
|
|
275
274
|
}
|
|
276
|
-
if (
|
|
275
|
+
if (typeof link.sub !== 'string')
|
|
277
276
|
return bad();
|
|
278
|
-
if (link.sub.startsWith('
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
return bad();
|
|
282
|
-
else
|
|
277
|
+
if (link.sub.startsWith('runtime:')) {
|
|
278
|
+
if (!validatePrincipal(link.sub, 'runtime') || link.grant !== undefined || link.rev !== undefined)
|
|
279
|
+
return bad();
|
|
283
280
|
chain.push({ sub: link.sub });
|
|
281
|
+
}
|
|
282
|
+
else {
|
|
283
|
+
const parsed = parsePrincipal(link.sub);
|
|
284
|
+
// A link names a namespace root, never a path principal.
|
|
285
|
+
if (parsed === null || parsed.path.length > 0)
|
|
286
|
+
return bad();
|
|
287
|
+
if (parsed.kind === 'app' && link.grant === undefined) {
|
|
288
|
+
if (link.rev !== undefined)
|
|
289
|
+
return bad();
|
|
290
|
+
// Marked `owner` when the app owns the namespace of the token's `sub`; otherwise the existing outbound shape (an app link without a grant).
|
|
291
|
+
chain.push(tokenSub !== null && tokenSub.namespace === link.sub ? { sub: link.sub, owner: true } : { sub: link.sub });
|
|
292
|
+
}
|
|
293
|
+
else if (parsed.kind === 'app')
|
|
294
|
+
chain.push(grantLink(link.sub, link.grant, link.rev));
|
|
295
|
+
else if (link.grant === undefined && link.rev === undefined)
|
|
296
|
+
chain.push({ sub: link.sub });
|
|
297
|
+
// A person's grant-bearing link is accepted only on an exchanged token: the person acts for a different subject.
|
|
298
|
+
else if (p.sub !== link.sub)
|
|
299
|
+
chain.push(grantLink(link.sub, link.grant, link.rev));
|
|
300
|
+
else
|
|
301
|
+
return bad();
|
|
302
|
+
}
|
|
284
303
|
node = link.act;
|
|
285
304
|
}
|
|
286
305
|
if (p.machine_id !== undefined && p.machine_id !== chain[0].sub)
|
|
287
306
|
return bad();
|
|
288
|
-
if (p.grant !== undefined && !chain.some((l) => l.
|
|
307
|
+
if (p.grant !== undefined && !chain.some((l) => l.grant === p.grant))
|
|
289
308
|
return bad();
|
|
290
309
|
// A token's top-level claims (kept for providers pinned to an older `@north-light/plugin`, which read them instead of
|
|
291
310
|
// `act`) must say what its chain says, or the old and new verifiers authorize different identities from one signed token.
|
|
@@ -311,7 +330,7 @@ function chainOf(payload) {
|
|
|
311
330
|
/**
|
|
312
331
|
* Verifies a call token of any use (peer, app access, outbound provider, run notice) and returns its actor chain,
|
|
313
332
|
* presenter first. Checks in order: header `typ` `at+jwt`, `alg` ES256 or RS256, non-empty `kid`; signature; `iss`;
|
|
314
|
-
* `aud` a string equal to `audience`; `exp` and `iat` numbers; `sub`
|
|
333
|
+
* `aud` a string equal to `audience`; `exp` and `iat` numbers; `sub` a principal `parsePrincipal` accepts; `client_id` non-empty; `scope`
|
|
315
334
|
* a string when present; then the chain. A token with no `act` is refused: the legacy machine and app shapes are not
|
|
316
335
|
* accepted. Any `machine_id` must equal the first link, any top-level `grant` an app link's, and, on a token without
|
|
317
336
|
* `scope`, any `grantee` and `rev` the app link's (or, with no app link, `grantee` = `sub` and no `rev`). It authorizes
|
|
@@ -329,7 +348,7 @@ export async function verifyCallToken(options) {
|
|
|
329
348
|
issuer: options.issuer, audience: options.audience, algorithms: ['ES256', 'RS256'], typ: 'at+jwt', requiredClaims: ['exp'],
|
|
330
349
|
});
|
|
331
350
|
if (payload.aud !== options.audience || typeof payload.exp !== 'number' || typeof payload.iat !== 'number' ||
|
|
332
|
-
|
|
351
|
+
typeof payload.sub !== 'string' || parsePrincipal(payload.sub) === null || !nonEmptyString(payload.client_id) ||
|
|
333
352
|
(payload.scope !== undefined && typeof payload.scope !== 'string')) {
|
|
334
353
|
throw new Error('Invalid call token claims');
|
|
335
354
|
}
|
|
@@ -153,6 +153,9 @@ export function createFetchHandler(plugins, options = {}) {
|
|
|
153
153
|
: undefined;
|
|
154
154
|
throw scopeMissing(command.scope, other);
|
|
155
155
|
}
|
|
156
|
+
if (verified?.tools !== undefined && !verified.tools.includes(command.path.slice(1).join(' '))) {
|
|
157
|
+
throw scopeMissing(`${command.scope} (tool ${command.path.slice(1).join(' ')})`);
|
|
158
|
+
}
|
|
156
159
|
if (verified !== undefined) {
|
|
157
160
|
if (requestId === null || requestId === '') {
|
|
158
161
|
return answer(errorResponse(400, { code: 'invalid_request', message: 'The request carries no Crtr-Request-Id header.' }));
|
|
@@ -211,7 +214,7 @@ export function createFetchHandler(plugins, options = {}) {
|
|
|
211
214
|
const price = usage.usdEquivalent ?? (row?.usd_per_unit != null ? usage.units * row.usd_per_unit : 0);
|
|
212
215
|
const receipt = {
|
|
213
216
|
receipt_id: receiptId, provider: options.receipts.provider, tool,
|
|
214
|
-
sub: caller.sub, grantee: caller.grantee, payer: caller.payer,
|
|
217
|
+
sub: caller.sub, grantee: caller.grantee, payer: caller.payer, actor: caller.actor,
|
|
215
218
|
units: usage.units, unit: usage.unit, usd_equivalent: price,
|
|
216
219
|
request_id: caller.requestId,
|
|
217
220
|
...(caller.runId !== undefined ? { run_id: caller.runId } : {}),
|