@north-light/api 0.3.749 → 0.3.750

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.
@@ -0,0 +1,7 @@
1
+ import { type Scope } from '@north-light/identity';
2
+ /** 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. */
3
+ export declare function isNamespaceOwner(grantee: string, sub: string): boolean;
4
+ /** 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). */
5
+ export declare function ownsWithoutRow(grantee: string, sub: string, grantId: string | null | undefined): boolean;
6
+ /** 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. */
7
+ export declare function ownerScopes(grantee: string): Scope[];
@@ -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
+ }
@@ -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
- if (!grant)
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 ? grant.resolved : stored.filter((scope) => coveredByAny(grant.resolved, scope)) };
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 scopes = identity === null || grant === null ? null
246
- : identity.node_scopes === null ? grant.resolved : identity.node_scopes.filter((scope) => coveredByAny(grant.resolved, scope));
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,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
- return permissionsForGrant(app, sub, grantId ? await activeGrant(grantId) : await grantFor(app, sub), launched);
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 (!grant || grant.grantee !== app || grant.sub !== sub)
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 scopes = launched ?? grant.resolved;
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(grant.resolved, scope))
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 (!grant || grant.grantee !== app || grant.sub !== stored.sub)
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
- if (app !== 'app:terminal' && !(row.grant_id ? await activeGrant(row.grant_id) : await grantFor(app, launchSub(row.sub, row.node_id)))) {
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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@north-light/api",
3
- "version": "0.3.749",
3
+ "version": "0.3.750",
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",