@canopy-io/node 0.15.0 → 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1199,7 +1199,7 @@ var MAX_WALK_DEPTH = 256;
1199
1199
  var PAGE_SIZE = 100;
1200
1200
  var MAX_PAGES = 1e4;
1201
1201
  var MAX_TREE_WALKS = 3;
1202
- var KNOWN_MODEL_VERSIONS = /* @__PURE__ */ new Set([1]);
1202
+ var KNOWN_MODEL_VERSIONS = /* @__PURE__ */ new Set([1, 2]);
1203
1203
  function understood(page, path) {
1204
1204
  if (!KNOWN_MODEL_VERSIONS.has(page.model_version)) {
1205
1205
  throw new CanopyAuthorizerError(
@@ -1218,6 +1218,17 @@ function inEffect(window, now) {
1218
1218
  function reasonAmong(windows, now) {
1219
1219
  return windows.some((window) => window.to !== null && window.to <= now) ? "expired" : "not_yet_effective";
1220
1220
  }
1221
+ function refusedOn(denied, lineage) {
1222
+ if (!denied) {
1223
+ return false;
1224
+ }
1225
+ for (const node of lineage) {
1226
+ if (denied.has(node)) {
1227
+ return true;
1228
+ }
1229
+ }
1230
+ return false;
1231
+ }
1221
1232
  var LocalAuthorizer = class {
1222
1233
  client;
1223
1234
  ttlMs;
@@ -1315,55 +1326,63 @@ var LocalAuthorizer = class {
1315
1326
  * that keeps a closure table or a materialised path: it filters by "beneath
1316
1327
  * one of these" without an expanded set.
1317
1328
  *
1329
+ * A grant a deny refuses (a deny root at its node or above it) is left out,
1330
+ * and a deny root beneath a root is named in `excludedRoots`, so the filter
1331
+ * becomes "beneath one of the roots and not beneath an excluded one".
1332
+ * `envWide` holds only while nothing is excluded.
1333
+ *
1318
1334
  * Reads the tree, since minimality and `envWide` are questions about it; a
1319
1335
  * root the readable hierarchy does not contain raises
1320
1336
  * `authorizer.hierarchy_incomplete`, as a check at such a node does.
1321
1337
  */
1322
1338
  async grantRoots(identityId, permission, subject = {}) {
1323
1339
  const now = this.now();
1324
- const held = (await this.grantRootsFor(
1340
+ const holdings = await this.grantRootsFor(
1325
1341
  identityId,
1326
1342
  subject.principalType ?? "identity"
1327
- )).get(permission) ?? [];
1343
+ );
1328
1344
  const granted = new Set(
1329
- held.filter((window) => inEffect(window, now)).map((w) => w.node)
1345
+ (holdings.roots.get(permission) ?? []).filter((window) => inEffect(window, now)).map((w) => w.node)
1330
1346
  );
1331
1347
  if (granted.size === 0) {
1332
- return { roots: [], envWide: false };
1348
+ return { roots: [], excludedRoots: [], envWide: false };
1333
1349
  }
1350
+ const denied = holdings.denies.get(permission);
1334
1351
  const { parents } = await this.treeEntry();
1335
1352
  const roots = [];
1336
- let envWide = false;
1353
+ let rootGranted = false;
1337
1354
  for (const node of granted) {
1338
1355
  if (!parents.has(node)) {
1339
1356
  throw this.hierarchyIncomplete(node);
1340
1357
  }
1341
- if (parents.get(node) === null) {
1342
- envWide = true;
1358
+ const path = this.lineage(parents, node);
1359
+ if (refusedOn(denied, path)) {
1360
+ continue;
1343
1361
  }
1344
- let covered = false;
1345
- let depth = 0;
1346
- for (let current = parents.get(node); current && depth < MAX_WALK_DEPTH; current = parents.get(current), depth += 1) {
1347
- if (granted.has(current)) {
1348
- covered = true;
1349
- break;
1350
- }
1362
+ if (parents.get(node) === null) {
1363
+ rootGranted = true;
1351
1364
  }
1352
- if (!covered) {
1365
+ if (!path.slice(1).some((n) => granted.has(n))) {
1353
1366
  roots.push(node);
1354
1367
  }
1355
1368
  }
1356
- return { roots, envWide };
1369
+ const excludedRoots = this.excludedBeneath(parents, denied, roots);
1370
+ return {
1371
+ roots,
1372
+ excludedRoots,
1373
+ envWide: rootGranted && excludedRoots.length === 0
1374
+ };
1357
1375
  }
1358
1376
  /**
1359
1377
  * Where the identity may act with the permission, as the set a list query
1360
- * filters by: {@link grantRoots} expanded through their descendants, up to
1361
- * `maxNodes` (the authorizer's default unless given here).
1378
+ * filters by: {@link grantRoots} expanded through their descendants, the
1379
+ * subtrees beneath `excludedRoots` left out, up to `maxNodes` (the
1380
+ * authorizer's default unless given here).
1362
1381
  *
1363
1382
  * Past the cap `nodes` is null and `complete` false: the set is too large
1364
- * to be worth an `IN (...)` list, and the caller filters by `roots`
1365
- * instead. An `envWide` answer carries no set either, since every node
1366
- * qualifies and a filter would only slow the query down.
1383
+ * to be worth an `IN (...)` list, and the caller filters by `roots` and
1384
+ * `excludedRoots` instead. An `envWide` answer carries no set either, since
1385
+ * every node qualifies and a filter would only slow the query down.
1367
1386
  *
1368
1387
  * Computed from the same cached grant roots and tree a check uses, so a
1369
1388
  * list filtered by it and the check on a write cannot disagree, and it goes
@@ -1371,32 +1390,55 @@ var LocalAuthorizer = class {
1371
1390
  */
1372
1391
  async accessibleNodes(identityId, permission, options = {}) {
1373
1392
  const cap = options.maxNodes ?? this.maxNodes;
1374
- const { roots, envWide } = await this.grantRoots(identityId, permission, {
1375
- ...options.principalType ? { principalType: options.principalType } : {}
1376
- });
1393
+ const { roots, excludedRoots, envWide } = await this.grantRoots(
1394
+ identityId,
1395
+ permission,
1396
+ {
1397
+ ...options.principalType ? { principalType: options.principalType } : {}
1398
+ }
1399
+ );
1377
1400
  if (envWide) {
1378
- return { roots, envWide: true, nodes: null, complete: true };
1401
+ return {
1402
+ roots,
1403
+ excludedRoots,
1404
+ envWide: true,
1405
+ nodes: null,
1406
+ complete: true
1407
+ };
1379
1408
  }
1380
1409
  if (roots.length === 0) {
1381
- return { roots, envWide: false, nodes: /* @__PURE__ */ new Set(), complete: true };
1410
+ return {
1411
+ roots,
1412
+ excludedRoots,
1413
+ envWide: false,
1414
+ nodes: /* @__PURE__ */ new Set(),
1415
+ complete: true
1416
+ };
1382
1417
  }
1383
1418
  const { children } = await this.treeEntry();
1419
+ const excluded = new Set(excludedRoots);
1384
1420
  const nodes = /* @__PURE__ */ new Set();
1385
1421
  const pending = [...roots];
1386
1422
  while (pending.length > 0) {
1387
1423
  const node = pending.pop();
1388
- if (nodes.has(node)) {
1424
+ if (nodes.has(node) || excluded.has(node)) {
1389
1425
  continue;
1390
1426
  }
1391
1427
  nodes.add(node);
1392
1428
  if (nodes.size > cap) {
1393
- return { roots, envWide: false, nodes: null, complete: false };
1429
+ return {
1430
+ roots,
1431
+ excludedRoots,
1432
+ envWide: false,
1433
+ nodes: null,
1434
+ complete: false
1435
+ };
1394
1436
  }
1395
1437
  for (const child of children.get(node) ?? []) {
1396
1438
  pending.push(child);
1397
1439
  }
1398
1440
  }
1399
- return { roots, envWide: false, nodes, complete: true };
1441
+ return { roots, excludedRoots, envWide: false, nodes, complete: true };
1400
1442
  }
1401
1443
  /**
1402
1444
  * Whether the identity holds the permission anywhere at or beneath a node,
@@ -1407,7 +1449,9 @@ var LocalAuthorizer = class {
1407
1449
  * `roots` is `[subtreeRootId]` when a grant at the node or above it reaches
1408
1450
  * the whole subtree; otherwise the topmost granted nodes inside it, the
1409
1451
  * ones no other granted node inside it already covers. A list query filters
1410
- * by "beneath one of these".
1452
+ * by "beneath one of these and not beneath one of `excludedRoots`", the
1453
+ * deny roots inside them. A deny at the node or above it refuses the whole
1454
+ * subtree, and a grant a deny refuses counts for nothing.
1411
1455
  *
1412
1456
  * **A read for menus and lists, never a guard.** Holding the permission
1413
1457
  * somewhere in the subtree says nothing about the record a write touches:
@@ -1420,34 +1464,48 @@ var LocalAuthorizer = class {
1420
1464
  */
1421
1465
  async holdsAnywhereUnder(identityId, permission, subtreeRootId, subject = {}) {
1422
1466
  const now = this.now();
1423
- const held = (await this.grantRootsFor(
1467
+ const holdings = await this.grantRootsFor(
1424
1468
  identityId,
1425
1469
  subject.principalType ?? "identity"
1426
- )).get(permission) ?? [];
1470
+ );
1427
1471
  const granted = new Set(
1428
- held.filter((window) => inEffect(window, now)).map((w) => w.node)
1472
+ (holdings.roots.get(permission) ?? []).filter((window) => inEffect(window, now)).map((w) => w.node)
1429
1473
  );
1474
+ const denied = holdings.denies.get(permission);
1430
1475
  const { parents } = await this.treeEntry();
1431
1476
  if (!parents.has(subtreeRootId)) {
1432
1477
  throw this.hierarchyIncomplete(subtreeRootId);
1433
1478
  }
1434
- if (granted.size === 0) {
1435
- return { held: false, roots: [] };
1479
+ const above = this.lineage(parents, subtreeRootId);
1480
+ if (granted.size === 0 || refusedOn(denied, above)) {
1481
+ return { held: false, roots: [], excludedRoots: [] };
1436
1482
  }
1437
- if (this.lineage(parents, subtreeRootId).some((n) => granted.has(n))) {
1438
- return { held: true, roots: [subtreeRootId] };
1483
+ if (above.some((n) => granted.has(n))) {
1484
+ return {
1485
+ held: true,
1486
+ roots: [subtreeRootId],
1487
+ excludedRoots: this.excludedBeneath(parents, denied, [subtreeRootId])
1488
+ };
1439
1489
  }
1440
1490
  const roots = [];
1441
1491
  for (const node of granted) {
1442
1492
  if (!parents.has(node)) {
1443
1493
  throw this.hierarchyIncomplete(node);
1444
1494
  }
1445
- const above = this.lineage(parents, node).slice(1);
1446
- if (above.includes(subtreeRootId) && !above.some((n) => granted.has(n))) {
1495
+ const path = this.lineage(parents, node);
1496
+ if (refusedOn(denied, path)) {
1497
+ continue;
1498
+ }
1499
+ const ancestors = path.slice(1);
1500
+ if (ancestors.includes(subtreeRootId) && !ancestors.some((n) => granted.has(n))) {
1447
1501
  roots.push(node);
1448
1502
  }
1449
1503
  }
1450
- return { held: roots.length > 0, roots };
1504
+ return {
1505
+ held: roots.length > 0,
1506
+ roots,
1507
+ excludedRoots: this.excludedBeneath(parents, denied, roots)
1508
+ };
1451
1509
  }
1452
1510
  /** The node and every ancestor up to the root, nearest first. */
1453
1511
  lineage(parents, nodeId) {
@@ -1458,10 +1516,32 @@ var LocalAuthorizer = class {
1458
1516
  }
1459
1517
  return path;
1460
1518
  }
1519
+ /**
1520
+ * The deny roots strictly beneath one of `roots`: the subtrees a filter by
1521
+ * those roots has to cut out. A deny root at a root or above it never
1522
+ * reaches here, because that root was refused and left out.
1523
+ */
1524
+ excludedBeneath(parents, denied, roots) {
1525
+ if (!denied || roots.length === 0) {
1526
+ return [];
1527
+ }
1528
+ const rootSet = new Set(roots);
1529
+ const excluded = [];
1530
+ for (const node of denied) {
1531
+ if (!parents.has(node)) {
1532
+ throw this.hierarchyIncomplete(node);
1533
+ }
1534
+ if (this.lineage(parents, node).slice(1).some((n) => rootSet.has(n))) {
1535
+ excluded.push(node);
1536
+ }
1537
+ }
1538
+ return excluded;
1539
+ }
1461
1540
  /**
1462
1541
  * Every permission the identity holds at the node, by grant or by
1463
- * inheritance: the list a screen renders its controls from, answered from
1464
- * the same cached grant roots and tree a check uses.
1542
+ * inheritance, less those a deny refuses there: the list a screen renders
1543
+ * its controls from, answered from the same cached grant roots and tree a
1544
+ * check uses.
1465
1545
  */
1466
1546
  async permissionsAt(identityId, nodeId, subject = {}) {
1467
1547
  const answers = await this.permissionsAtMany(identityId, [nodeId], subject);
@@ -1474,7 +1554,7 @@ var LocalAuthorizer = class {
1474
1554
  */
1475
1555
  async permissionsAtMany(identityId, nodeIds, subject = {}) {
1476
1556
  const now = this.now();
1477
- const roots = await this.grantRootsFor(
1557
+ const holdings = await this.grantRootsFor(
1478
1558
  identityId,
1479
1559
  subject.principalType ?? "identity"
1480
1560
  );
@@ -1482,7 +1562,10 @@ var LocalAuthorizer = class {
1482
1562
  for (const nodeId of new Set(nodeIds)) {
1483
1563
  const path = await this.lineageOf(nodeId);
1484
1564
  const held = [];
1485
- for (const [permission, windows] of roots) {
1565
+ for (const [permission, windows] of holdings.roots) {
1566
+ if (refusedOn(holdings.denies.get(permission), path)) {
1567
+ continue;
1568
+ }
1486
1569
  if (windows.some(
1487
1570
  (window) => path.has(window.node) && inEffect(window, now)
1488
1571
  )) {
@@ -1495,19 +1578,34 @@ var LocalAuthorizer = class {
1495
1578
  }
1496
1579
  async decide(query) {
1497
1580
  const now = this.now();
1498
- const held = (await this.grantRootsFor(
1581
+ const holdings = await this.grantRootsFor(
1499
1582
  query.identity_id,
1500
1583
  query.principal_type ?? "identity"
1501
- )).get(query.permission);
1502
- if (!held || held.length === 0) {
1503
- return { allowed: false, reason: "permission_not_held" };
1504
- }
1584
+ );
1585
+ const held = holdings.roots.get(query.permission) ?? [];
1586
+ const denied = holdings.denies.get(query.permission);
1505
1587
  if ((query.scope ?? "node") === "app_wide") {
1506
- return held.some((window) => inEffect(window, now)) ? { allowed: true, reason: null } : { allowed: false, reason: reasonAmong(held, now) };
1588
+ if (held.length === 0) {
1589
+ return { allowed: false, reason: "permission_not_held" };
1590
+ }
1591
+ const standing = held.filter((window) => inEffect(window, now));
1592
+ if (standing.length === 0) {
1593
+ return { allowed: false, reason: reasonAmong(held, now) };
1594
+ }
1595
+ return await this.anyClearOf(denied, standing) ? { allowed: true, reason: null } : { allowed: false, reason: "denied" };
1596
+ }
1597
+ if (held.length === 0 && !denied) {
1598
+ return { allowed: false, reason: "permission_not_held" };
1507
1599
  }
1508
1600
  if (!query.node_id) {
1509
1601
  return { allowed: false, reason: "node_missing" };
1510
1602
  }
1603
+ if (denied && refusedOn(denied, await this.lineageOf(query.node_id))) {
1604
+ return { allowed: false, reason: "denied" };
1605
+ }
1606
+ if (held.length === 0) {
1607
+ return { allowed: false, reason: "permission_not_held" };
1608
+ }
1511
1609
  if (held.some(
1512
1610
  (window) => window.node === query.node_id && inEffect(window, now)
1513
1611
  )) {
@@ -1523,6 +1621,22 @@ var LocalAuthorizer = class {
1523
1621
  }
1524
1622
  return { allowed: false, reason: reasonAmong(reachable, now) };
1525
1623
  }
1624
+ /**
1625
+ * Whether one of the grants stands clear of every deny root: none at its
1626
+ * node or above it. A grant with a deny strictly beneath it still reaches
1627
+ * the rest of its subtree. Reads the tree only when there is a deny.
1628
+ */
1629
+ async anyClearOf(denied, windows) {
1630
+ if (!denied) {
1631
+ return true;
1632
+ }
1633
+ for (const window of windows) {
1634
+ if (!refusedOn(denied, await this.lineageOf(window.node))) {
1635
+ return true;
1636
+ }
1637
+ }
1638
+ return false;
1639
+ }
1526
1640
  /** Counters for observability — how much traffic the cache is actually saving. */
1527
1641
  snapshot() {
1528
1642
  return { ...this.stats };
@@ -1540,7 +1654,9 @@ var LocalAuthorizer = class {
1540
1654
  * With no argument, everything goes: grants and the hierarchy tree. Not
1541
1655
  * needed in normal operation, where entries expire on their own; useful in
1542
1656
  * tests and after a change whose reach you cannot name (a role's
1543
- * permissions edited, a role deactivated or reactivated, a node moved). A
1657
+ * permissions edited, a role deactivated or reactivated, a node moved, a
1658
+ * deny created or deleted, since a deny on a role binds everyone holding
1659
+ * it). A
1544
1660
  * deactivated role grants nothing to anyone holding it, so until the cache
1545
1661
  * is cleared or expires, its holders keep being allowed locally.
1546
1662
  *
@@ -1602,7 +1718,7 @@ var LocalAuthorizer = class {
1602
1718
  const key = subjectKey(type, identityId);
1603
1719
  const cached = this.grants.get(key);
1604
1720
  if (cached && cached.expiresAt > this.now()) {
1605
- return cached.roots;
1721
+ return cached.holdings;
1606
1722
  }
1607
1723
  const pending = this.pendingGrants.get(key);
1608
1724
  if (pending) {
@@ -1628,17 +1744,35 @@ var LocalAuthorizer = class {
1628
1744
  * `expired` or `not_yet_effective`, and the entry expires at the nearest
1629
1745
  * window boundary when that comes before the TTL, so a grant takes effect
1630
1746
  * or ends on time rather than up to `ttlMs` late.
1747
+ *
1748
+ * Every page carries the subject's deny roots whole, beside its rows; they
1749
+ * are gathered from each page, so a read is one snapshot of both.
1631
1750
  */
1632
1751
  async fetchGrantRoots(identityId, type) {
1633
1752
  this.stats.grantFetches += 1;
1634
1753
  const path = GRANTS_PATH(type, identityId);
1754
+ const denies = /* @__PURE__ */ new Map();
1635
1755
  const rows = paginate(
1636
1756
  (query) => {
1637
1757
  this.stats.grantPages += 1;
1638
1758
  return this.client.request("GET", path, {
1639
1759
  ...this.readOptions,
1640
1760
  query: { ...query, include: "windows" }
1641
- }).then((page) => understood(page, path));
1761
+ }).then((page) => {
1762
+ const read = understood(
1763
+ page,
1764
+ path
1765
+ ).denies;
1766
+ for (const deny of read ?? []) {
1767
+ const nodes = denies.get(deny.permission);
1768
+ if (nodes) {
1769
+ nodes.add(deny.node_id);
1770
+ } else {
1771
+ denies.set(deny.permission, /* @__PURE__ */ new Set([deny.node_id]));
1772
+ }
1773
+ }
1774
+ return page;
1775
+ });
1642
1776
  },
1643
1777
  { page: 1, take: PAGE_SIZE },
1644
1778
  { maxPages: MAX_PAGES }
@@ -1664,11 +1798,12 @@ var LocalAuthorizer = class {
1664
1798
  roots.set(row.permission, [window]);
1665
1799
  }
1666
1800
  }
1801
+ const holdings = { roots, denies };
1667
1802
  this.grants.set(subjectKey(type, identityId), {
1668
- roots,
1803
+ holdings,
1669
1804
  expiresAt: Math.min(now + this.ttlMs, boundary)
1670
1805
  });
1671
- return roots;
1806
+ return holdings;
1672
1807
  }
1673
1808
  async treeEntry() {
1674
1809
  if (this.tree && this.tree.expiresAt > this.now()) {