@proteinjs/user 1.14.0 → 1.15.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/CHANGELOG.md +11 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/src/SharedScopeKeyOwners.d.ts +91 -0
- package/dist/src/SharedScopeKeyOwners.d.ts.map +1 -0
- package/dist/src/SharedScopeKeyOwners.js +282 -0
- package/dist/src/SharedScopeKeyOwners.js.map +1 -0
- package/dist/src/tables/AccessGrantTable.d.ts.map +1 -1
- package/dist/src/tables/AccessGrantTable.js +7 -0
- package/dist/src/tables/AccessGrantTable.js.map +1 -1
- package/index.ts +1 -0
- package/package.json +2 -2
- package/src/SharedScopeKeyOwners.ts +237 -0
- package/src/tables/AccessGrantTable.ts +7 -0
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,17 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [1.15.0](https://github.com/proteinjs/user/compare/@proteinjs/user@1.14.0...@proteinjs/user@1.15.0) (2026-08-31)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* shared-scope encryption key owners — the encryption-sharing MVP (TRUST_AND_COMPLIANCE §4/§4.4): SharedRecord rows key by the SCOPE-ROOT OWNER, and a share grant extends decrypt + blind-index search to the recipient ([6f5d0db](https://github.com/proteinjs/user/commit/6f5d0dbd1e56d2b0bf0236b138af50164b244b3c))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
6
17
|
# [1.14.0](https://github.com/proteinjs/user/compare/@proteinjs/user@1.13.0...@proteinjs/user@1.14.0) (2026-08-31)
|
|
7
18
|
|
|
8
19
|
|
package/dist/index.d.ts
CHANGED
|
@@ -23,5 +23,6 @@ export * from './src/RolesCatalog';
|
|
|
23
23
|
export * from './src/UserRepo';
|
|
24
24
|
export * from './src/ScopedRecord';
|
|
25
25
|
export * from './src/SharedRecord';
|
|
26
|
+
export * from './src/SharedScopeKeyOwners';
|
|
26
27
|
export * from '@proteinjs/user-auth';
|
|
27
28
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,cAAc,oCAAoC,CAAC;AACnD,cAAc,uCAAuC,CAAC;AACtD,cAAc,0CAA0C,CAAC;AACzD,cAAc,6BAA6B,CAAC;AAC5C,cAAc,qCAAqC,CAAC;AACpD,cAAc,8BAA8B,CAAC;AAC7C,cAAc,sCAAsC,CAAC;AAErD,cAAc,+BAA+B,CAAC;AAC9C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,mCAAmC,CAAC;AAClD,cAAc,0BAA0B,CAAC;AACzC,cAAc,kCAAkC,CAAC;AACjD,cAAc,gCAAgC,CAAC;AAC/C,cAAc,mCAAmC,CAAC;AAClD,cAAc,wBAAwB,CAAC;AACvC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,qBAAqB,CAAC;AACpC,cAAc,cAAc,CAAC;AAC7B,cAAc,iBAAiB,CAAC;AAChC,cAAc,mBAAmB,CAAC;AAClC,cAAc,sBAAsB,CAAC;AACrC,cAAc,oBAAoB,CAAC;AACnC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,cAAc,oCAAoC,CAAC;AACnD,cAAc,uCAAuC,CAAC;AACtD,cAAc,0CAA0C,CAAC;AACzD,cAAc,6BAA6B,CAAC;AAC5C,cAAc,qCAAqC,CAAC;AACpD,cAAc,8BAA8B,CAAC;AAC7C,cAAc,sCAAsC,CAAC;AAErD,cAAc,+BAA+B,CAAC;AAC9C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,mCAAmC,CAAC;AAClD,cAAc,0BAA0B,CAAC;AACzC,cAAc,kCAAkC,CAAC;AACjD,cAAc,gCAAgC,CAAC;AAC/C,cAAc,mCAAmC,CAAC;AAClD,cAAc,wBAAwB,CAAC;AACvC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,qBAAqB,CAAC;AACpC,cAAc,cAAc,CAAC;AAC7B,cAAc,iBAAiB,CAAC;AAChC,cAAc,mBAAmB,CAAC;AAClC,cAAc,sBAAsB,CAAC;AACrC,cAAc,oBAAoB,CAAC;AACnC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,4BAA4B,CAAC;AAE3C,cAAc,sBAAsB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -39,5 +39,6 @@ __exportStar(require("./src/RolesCatalog"), exports);
|
|
|
39
39
|
__exportStar(require("./src/UserRepo"), exports);
|
|
40
40
|
__exportStar(require("./src/ScopedRecord"), exports);
|
|
41
41
|
__exportStar(require("./src/SharedRecord"), exports);
|
|
42
|
+
__exportStar(require("./src/SharedScopeKeyOwners"), exports);
|
|
42
43
|
__exportStar(require("@proteinjs/user-auth"), exports);
|
|
43
44
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,qEAAmD;AACnD,wEAAsD;AACtD,2EAAyD;AACzD,8DAA4C;AAC5C,sEAAoD;AACpD,+DAA6C;AAC7C,uEAAqD;AAErD,gEAA8C;AAC9C,iEAA+C;AAC/C,oEAAkD;AAClD,2DAAyC;AACzC,mEAAiD;AACjD,iEAA+C;AAC/C,oEAAkD;AAClD,yDAAuC;AACvC,4DAA0C;AAC1C,sDAAoC;AACpC,+CAA6B;AAC7B,kDAAgC;AAChC,oDAAkC;AAClC,uDAAqC;AACrC,qDAAmC;AACnC,iDAA+B;AAC/B,qDAAmC;AACnC,qDAAmC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,qEAAmD;AACnD,wEAAsD;AACtD,2EAAyD;AACzD,8DAA4C;AAC5C,sEAAoD;AACpD,+DAA6C;AAC7C,uEAAqD;AAErD,gEAA8C;AAC9C,iEAA+C;AAC/C,oEAAkD;AAClD,2DAAyC;AACzC,mEAAiD;AACjD,iEAA+C;AAC/C,oEAAkD;AAClD,yDAAuC;AACvC,4DAA0C;AAC1C,sDAAoC;AACpC,+CAA6B;AAC7B,kDAAgC;AAChC,oDAAkC;AAClC,uDAAqC;AACrC,qDAAmC;AACnC,iDAA+B;AAC/B,qDAAmC;AACnC,qDAAmC;AACnC,6DAA2C;AAE3C,uDAAqC"}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { Table } from '@proteinjs/db';
|
|
2
|
+
/**
|
|
3
|
+
* The sharing-aware half of the column-encryption key model (TRUST_AND_COMPLIANCE §4/§4.4;
|
|
4
|
+
* SHARING_EXPANSION §3): rows in a shared permission scope are keyed by the SCOPE-ROOT
|
|
5
|
+
* OWNER's data key, and a reader's encrypted-search fingerprints fan out over the owners
|
|
6
|
+
* sharing content into their view. Compose an instance into the app's `DbEncryptionConfig`:
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* const keyOwners = new SharedScopeKeyOwners();
|
|
10
|
+
* const config: DbEncryptionConfig = {
|
|
11
|
+
* masterKeyProvider,
|
|
12
|
+
* resolveKeyOwner: (args) => keyOwners.resolveKeyOwner(args),
|
|
13
|
+
* getAccessibleKeyOwners: (args) => keyOwners.getAccessibleKeyOwners(args),
|
|
14
|
+
* };
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* Design facts this class is the single owner of:
|
|
18
|
+
*
|
|
19
|
+
* - **Owner-keyed shared scopes.** A `SharedRecord` row's key owner is the principal of the
|
|
20
|
+
* EARLIEST `owner` grant on its permission source (creation order; the creator). A
|
|
21
|
+
* contributor's write into someone else's document is keyed by the DOCUMENT owner, never
|
|
22
|
+
* the writer — so crypto-shred agrees exactly with the account-deletion purge walker,
|
|
23
|
+
* which drains shared trees grant-derived (by owned permission sources, never by the
|
|
24
|
+
* writer's scope): the rows a deletion removes and the rows a shred unreads are the same
|
|
25
|
+
* set. Keys are NOT access control — whether a reader may see a row is the permission
|
|
26
|
+
* layer's decision; revoking a share revokes access without re-encrypting anything.
|
|
27
|
+
* - **Root birth.** A scope root's owner grant is minted only after its insert lands
|
|
28
|
+
* (`SharedRecord.onAfterInsert`), so a root insert that touches encrypted columns resolves
|
|
29
|
+
* the creator (the session user) directly — the same identity the grant is about to name.
|
|
30
|
+
* - **Accessible owners = self + every OTHER owner-grant principal on resources the caller
|
|
31
|
+
* holds any grant on.** Resource-agnostic (thought, chat, space — whatever rides
|
|
32
|
+
* AccessGrant) and bounded by how much is shared with the caller. This includes conferred
|
|
33
|
+
* co-owners in both directions. Computed fresh per query — a revoked share drops out of
|
|
34
|
+
* the fan-out immediately (the permission layer already refused the rows themselves).
|
|
35
|
+
* - **System queries** (`runAsSystem`) search under EVERY key owner (the
|
|
36
|
+
* `data_encryption_key` owner set): an unscoped query silently missing rows of
|
|
37
|
+
* inaccessible owners would be a wrong answer, not a degraded one. Rare by design;
|
|
38
|
+
* cost scales with the owner count, correctness does not.
|
|
39
|
+
* - **Non-shared tables fall through** (`resolveKeyOwner` returns undefined): the framework
|
|
40
|
+
* default — the row's `scope` column — already IS the scope owner for ScopedRecord tables.
|
|
41
|
+
*
|
|
42
|
+
* Named limitation (owner transfer): after a full ownership transfer (a conferred owner
|
|
43
|
+
* grant plus deletion of the original owner's), NEW writes key under the new owner while
|
|
44
|
+
* historical rows keep their old envelopes. Readers whose grant set no longer reaches the
|
|
45
|
+
* old owner can still read those rows (envelopes are self-describing) but search fan-out no
|
|
46
|
+
* longer covers them; a lifecycle re-encrypt walk over the tree converges the envelopes to
|
|
47
|
+
* the current owner.
|
|
48
|
+
*/
|
|
49
|
+
export declare class SharedScopeKeyOwners {
|
|
50
|
+
/** Scope-root owner cache TTL — bounds cross-process staleness after an ownership transfer. */
|
|
51
|
+
private static readonly OWNER_TTL_MS;
|
|
52
|
+
private static readonly OWNER_CACHE_MAX_ENTRIES;
|
|
53
|
+
private static readonly OWNER_CACHE_GLOBAL_KEY;
|
|
54
|
+
/**
|
|
55
|
+
* The data-key owner for a `SharedRecord` row being written: the earliest `owner`-grant
|
|
56
|
+
* principal on the row's permission source, or the session user for a scope-root birth
|
|
57
|
+
* (whose grant is minted post-DML). Returns undefined for non-shared tables and for rows
|
|
58
|
+
* whose owner cannot be resolved — the framework then falls through to the row's `scope`
|
|
59
|
+
* value or refuses the write loudly (`EncryptionRecordHooks.resolveKeyOwnerForWrite`).
|
|
60
|
+
*/
|
|
61
|
+
resolveKeyOwner(args: {
|
|
62
|
+
table: Table<any>;
|
|
63
|
+
record: any;
|
|
64
|
+
}): Promise<string | undefined>;
|
|
65
|
+
/**
|
|
66
|
+
* The key owners whose rows the current caller's encrypted searches must cover (see the
|
|
67
|
+
* class doc for the definition). Loud refusal when no caller identity exists on a
|
|
68
|
+
* non-system query: returning an empty set would make the search silently match nothing.
|
|
69
|
+
*/
|
|
70
|
+
getAccessibleKeyOwners(args: {
|
|
71
|
+
runAsSystem: boolean;
|
|
72
|
+
}): Promise<string[]>;
|
|
73
|
+
/**
|
|
74
|
+
* The earliest `owner`-grant principal on a permission source — the scope-root owner.
|
|
75
|
+
* Earliest by creation (id tiebreak) so a document with conferred co-owners still keys
|
|
76
|
+
* deterministically by its original owner.
|
|
77
|
+
*/
|
|
78
|
+
private scopeRootOwner;
|
|
79
|
+
/** Every key owner that exists — the distinct `data_encryption_key` owner set. */
|
|
80
|
+
private allKeyOwners;
|
|
81
|
+
/** The id inside a `Reference`-or-string field, or undefined. */
|
|
82
|
+
private referenceId;
|
|
83
|
+
/**
|
|
84
|
+
* Cache on the global object (the duplicate-module pattern — see `setSkipAccessGrants`):
|
|
85
|
+
* per-package installs put multiple live copies of this class in one process, and the
|
|
86
|
+
* scope-root owner must be one answer across all of them.
|
|
87
|
+
*/
|
|
88
|
+
private ownerCache;
|
|
89
|
+
private cacheOwner;
|
|
90
|
+
}
|
|
91
|
+
//# sourceMappingURL=SharedScopeKeyOwners.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SharedScopeKeyOwners.d.ts","sourceRoot":"","sources":["../../src/SharedScopeKeyOwners.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmD,KAAK,EAAiB,MAAM,eAAe,CAAC;AActG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8CG;AACH,qBAAa,oBAAoB;IAC/B,+FAA+F;IAC/F,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,YAAY,CAAa;IACjD,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,uBAAuB,CAAU;IACzD,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,sBAAsB,CAA4C;IAE1F;;;;;;OAMG;IACG,eAAe,CAAC,IAAI,EAAE;QAAE,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC;QAAC,MAAM,EAAE,GAAG,CAAA;KAAE,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC;IAuC5F;;;;OAIG;IACG,sBAAsB,CAAC,IAAI,EAAE;QAAE,WAAW,EAAE,OAAO,CAAA;KAAE,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAuC/E;;;;OAIG;YACW,cAAc;IAqB5B,kFAAkF;YACpE,YAAY;IAM1B,iEAAiE;IACjE,OAAO,CAAC,WAAW;IAUnB;;;;OAIG;IACH,OAAO,CAAC,UAAU;IAQlB,OAAO,CAAC,UAAU;CAOnB"}
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.SharedScopeKeyOwners = void 0;
|
|
40
|
+
var db_1 = require("@proteinjs/db");
|
|
41
|
+
var SharedRecord_1 = require("./SharedRecord");
|
|
42
|
+
var UserRepo_1 = require("./UserRepo");
|
|
43
|
+
var tables_1 = require("./tables/tables");
|
|
44
|
+
/**
|
|
45
|
+
* The sharing-aware half of the column-encryption key model (TRUST_AND_COMPLIANCE §4/§4.4;
|
|
46
|
+
* SHARING_EXPANSION §3): rows in a shared permission scope are keyed by the SCOPE-ROOT
|
|
47
|
+
* OWNER's data key, and a reader's encrypted-search fingerprints fan out over the owners
|
|
48
|
+
* sharing content into their view. Compose an instance into the app's `DbEncryptionConfig`:
|
|
49
|
+
*
|
|
50
|
+
* ```ts
|
|
51
|
+
* const keyOwners = new SharedScopeKeyOwners();
|
|
52
|
+
* const config: DbEncryptionConfig = {
|
|
53
|
+
* masterKeyProvider,
|
|
54
|
+
* resolveKeyOwner: (args) => keyOwners.resolveKeyOwner(args),
|
|
55
|
+
* getAccessibleKeyOwners: (args) => keyOwners.getAccessibleKeyOwners(args),
|
|
56
|
+
* };
|
|
57
|
+
* ```
|
|
58
|
+
*
|
|
59
|
+
* Design facts this class is the single owner of:
|
|
60
|
+
*
|
|
61
|
+
* - **Owner-keyed shared scopes.** A `SharedRecord` row's key owner is the principal of the
|
|
62
|
+
* EARLIEST `owner` grant on its permission source (creation order; the creator). A
|
|
63
|
+
* contributor's write into someone else's document is keyed by the DOCUMENT owner, never
|
|
64
|
+
* the writer — so crypto-shred agrees exactly with the account-deletion purge walker,
|
|
65
|
+
* which drains shared trees grant-derived (by owned permission sources, never by the
|
|
66
|
+
* writer's scope): the rows a deletion removes and the rows a shred unreads are the same
|
|
67
|
+
* set. Keys are NOT access control — whether a reader may see a row is the permission
|
|
68
|
+
* layer's decision; revoking a share revokes access without re-encrypting anything.
|
|
69
|
+
* - **Root birth.** A scope root's owner grant is minted only after its insert lands
|
|
70
|
+
* (`SharedRecord.onAfterInsert`), so a root insert that touches encrypted columns resolves
|
|
71
|
+
* the creator (the session user) directly — the same identity the grant is about to name.
|
|
72
|
+
* - **Accessible owners = self + every OTHER owner-grant principal on resources the caller
|
|
73
|
+
* holds any grant on.** Resource-agnostic (thought, chat, space — whatever rides
|
|
74
|
+
* AccessGrant) and bounded by how much is shared with the caller. This includes conferred
|
|
75
|
+
* co-owners in both directions. Computed fresh per query — a revoked share drops out of
|
|
76
|
+
* the fan-out immediately (the permission layer already refused the rows themselves).
|
|
77
|
+
* - **System queries** (`runAsSystem`) search under EVERY key owner (the
|
|
78
|
+
* `data_encryption_key` owner set): an unscoped query silently missing rows of
|
|
79
|
+
* inaccessible owners would be a wrong answer, not a degraded one. Rare by design;
|
|
80
|
+
* cost scales with the owner count, correctness does not.
|
|
81
|
+
* - **Non-shared tables fall through** (`resolveKeyOwner` returns undefined): the framework
|
|
82
|
+
* default — the row's `scope` column — already IS the scope owner for ScopedRecord tables.
|
|
83
|
+
*
|
|
84
|
+
* Named limitation (owner transfer): after a full ownership transfer (a conferred owner
|
|
85
|
+
* grant plus deletion of the original owner's), NEW writes key under the new owner while
|
|
86
|
+
* historical rows keep their old envelopes. Readers whose grant set no longer reaches the
|
|
87
|
+
* old owner can still read those rows (envelopes are self-describing) but search fan-out no
|
|
88
|
+
* longer covers them; a lifecycle re-encrypt walk over the tree converges the envelopes to
|
|
89
|
+
* the current owner.
|
|
90
|
+
*/
|
|
91
|
+
var SharedScopeKeyOwners = /** @class */ (function () {
|
|
92
|
+
function SharedScopeKeyOwners() {
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* The data-key owner for a `SharedRecord` row being written: the earliest `owner`-grant
|
|
96
|
+
* principal on the row's permission source, or the session user for a scope-root birth
|
|
97
|
+
* (whose grant is minted post-DML). Returns undefined for non-shared tables and for rows
|
|
98
|
+
* whose owner cannot be resolved — the framework then falls through to the row's `scope`
|
|
99
|
+
* value or refuses the write loudly (`EncryptionRecordHooks.resolveKeyOwnerForWrite`).
|
|
100
|
+
*/
|
|
101
|
+
SharedScopeKeyOwners.prototype.resolveKeyOwner = function (args) {
|
|
102
|
+
var _a;
|
|
103
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
104
|
+
var table, record, sourceId, sourceTable, cacheKey, cached, owner;
|
|
105
|
+
return __generator(this, function (_b) {
|
|
106
|
+
switch (_b.label) {
|
|
107
|
+
case 0:
|
|
108
|
+
table = args.table, record = args.record;
|
|
109
|
+
if (!(0, SharedRecord_1.isSharedTable)(table)) {
|
|
110
|
+
return [2 /*return*/, undefined];
|
|
111
|
+
}
|
|
112
|
+
sourceId = this.referenceId(record === null || record === void 0 ? void 0 : record.permissionSource);
|
|
113
|
+
sourceTable = (_a = record === null || record === void 0 ? void 0 : record.permissionSourceTable) !== null && _a !== void 0 ? _a : table.name;
|
|
114
|
+
if (!sourceId) {
|
|
115
|
+
return [2 /*return*/, undefined];
|
|
116
|
+
}
|
|
117
|
+
if ((0, SharedRecord_1.skipAccessGrantsEnabled)()) {
|
|
118
|
+
// Grant machinery is off (test environments): the only resolvable owner is the caller.
|
|
119
|
+
return [2 /*return*/, new UserRepo_1.UserRepo().getUser().id || undefined];
|
|
120
|
+
}
|
|
121
|
+
cacheKey = "".concat(sourceTable, ":").concat(sourceId);
|
|
122
|
+
cached = this.ownerCache().get(cacheKey);
|
|
123
|
+
if (cached && Date.now() - cached.cachedAt < SharedScopeKeyOwners.OWNER_TTL_MS) {
|
|
124
|
+
return [2 /*return*/, cached.owner];
|
|
125
|
+
}
|
|
126
|
+
return [4 /*yield*/, this.scopeRootOwner(sourceId, sourceTable)];
|
|
127
|
+
case 1:
|
|
128
|
+
owner = _b.sent();
|
|
129
|
+
if (owner) {
|
|
130
|
+
this.cacheOwner(cacheKey, owner);
|
|
131
|
+
return [2 /*return*/, owner];
|
|
132
|
+
}
|
|
133
|
+
// Scope-root birth: the row IS its own permission source and its owner grant does not
|
|
134
|
+
// exist yet — the creator (session user) is the owner that grant is about to name. Not
|
|
135
|
+
// cached: the authoritative grant lands post-DML.
|
|
136
|
+
if (sourceId === (record === null || record === void 0 ? void 0 : record.id) && sourceTable === table.name) {
|
|
137
|
+
return [2 /*return*/, new UserRepo_1.UserRepo().getUser().id || undefined];
|
|
138
|
+
}
|
|
139
|
+
return [2 /*return*/, undefined];
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
};
|
|
144
|
+
/**
|
|
145
|
+
* The key owners whose rows the current caller's encrypted searches must cover (see the
|
|
146
|
+
* class doc for the definition). Loud refusal when no caller identity exists on a
|
|
147
|
+
* non-system query: returning an empty set would make the search silently match nothing.
|
|
148
|
+
*/
|
|
149
|
+
SharedScopeKeyOwners.prototype.getAccessibleKeyOwners = function (args) {
|
|
150
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
151
|
+
var callerId, callerResources, qb, otherOwnerGrants, owners, _i, otherOwnerGrants_1, grant, principalId;
|
|
152
|
+
return __generator(this, function (_a) {
|
|
153
|
+
switch (_a.label) {
|
|
154
|
+
case 0:
|
|
155
|
+
if (!args.runAsSystem) return [3 /*break*/, 2];
|
|
156
|
+
return [4 /*yield*/, this.allKeyOwners()];
|
|
157
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
158
|
+
case 2:
|
|
159
|
+
callerId = new UserRepo_1.UserRepo().getUser().id;
|
|
160
|
+
if (!callerId) {
|
|
161
|
+
throw new Error("Cannot search encrypted columns without a caller identity: no session user is present. " +
|
|
162
|
+
"System-context searches cover all key owners; caller-context searches require a signed-in user.");
|
|
163
|
+
}
|
|
164
|
+
if ((0, SharedRecord_1.skipAccessGrantsEnabled)()) {
|
|
165
|
+
return [2 /*return*/, [callerId]];
|
|
166
|
+
}
|
|
167
|
+
callerResources = new db_1.QueryBuilder(tables_1.tables.AccessGrant.name);
|
|
168
|
+
callerResources.select({ fields: ['resource'] });
|
|
169
|
+
callerResources.condition({ field: 'principal', operator: '=', value: callerId });
|
|
170
|
+
qb = new db_1.QueryBuilder(tables_1.tables.AccessGrant.name);
|
|
171
|
+
qb.condition({ field: 'accessLevel', operator: '=', value: 'owner' });
|
|
172
|
+
qb.condition({ field: 'principal', operator: '!=', value: callerId });
|
|
173
|
+
qb.condition({ field: 'resource', operator: 'IN', value: callerResources });
|
|
174
|
+
return [4 /*yield*/, (0, db_1.getDbAsSystem)().query(tables_1.tables.AccessGrant, qb)];
|
|
175
|
+
case 3:
|
|
176
|
+
otherOwnerGrants = _a.sent();
|
|
177
|
+
owners = new Set([callerId]);
|
|
178
|
+
for (_i = 0, otherOwnerGrants_1 = otherOwnerGrants; _i < otherOwnerGrants_1.length; _i++) {
|
|
179
|
+
grant = otherOwnerGrants_1[_i];
|
|
180
|
+
principalId = this.referenceId(grant.principal);
|
|
181
|
+
if (principalId) {
|
|
182
|
+
owners.add(principalId);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return [2 /*return*/, Array.from(owners)];
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
};
|
|
190
|
+
/**
|
|
191
|
+
* The earliest `owner`-grant principal on a permission source — the scope-root owner.
|
|
192
|
+
* Earliest by creation (id tiebreak) so a document with conferred co-owners still keys
|
|
193
|
+
* deterministically by its original owner.
|
|
194
|
+
*/
|
|
195
|
+
SharedScopeKeyOwners.prototype.scopeRootOwner = function (sourceId, sourceTable) {
|
|
196
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
197
|
+
var qb, grants, earliest;
|
|
198
|
+
return __generator(this, function (_a) {
|
|
199
|
+
switch (_a.label) {
|
|
200
|
+
case 0:
|
|
201
|
+
qb = new db_1.QueryBuilder(tables_1.tables.AccessGrant.name);
|
|
202
|
+
qb.condition({ field: 'resource', operator: '=', value: sourceId });
|
|
203
|
+
qb.condition({ field: 'resourceTable', operator: '=', value: sourceTable });
|
|
204
|
+
qb.condition({ field: 'accessLevel', operator: '=', value: 'owner' });
|
|
205
|
+
return [4 /*yield*/, (0, db_1.getDbAsSystem)().query(tables_1.tables.AccessGrant, qb)];
|
|
206
|
+
case 1:
|
|
207
|
+
grants = _a.sent();
|
|
208
|
+
if (grants.length === 0) {
|
|
209
|
+
return [2 /*return*/, undefined];
|
|
210
|
+
}
|
|
211
|
+
earliest = grants.reduce(function (best, candidate) {
|
|
212
|
+
var _a, _b, _c, _d;
|
|
213
|
+
var bestCreated = (_b = (_a = best.created) === null || _a === void 0 ? void 0 : _a.valueOf()) !== null && _b !== void 0 ? _b : 0;
|
|
214
|
+
var candidateCreated = (_d = (_c = candidate.created) === null || _c === void 0 ? void 0 : _c.valueOf()) !== null && _d !== void 0 ? _d : 0;
|
|
215
|
+
if (candidateCreated !== bestCreated) {
|
|
216
|
+
return candidateCreated < bestCreated ? candidate : best;
|
|
217
|
+
}
|
|
218
|
+
return candidate.id < best.id ? candidate : best;
|
|
219
|
+
});
|
|
220
|
+
return [2 /*return*/, this.referenceId(earliest.principal)];
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
});
|
|
224
|
+
};
|
|
225
|
+
/** Every key owner that exists — the distinct `data_encryption_key` owner set. */
|
|
226
|
+
SharedScopeKeyOwners.prototype.allKeyOwners = function () {
|
|
227
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
228
|
+
var keyTable, rows;
|
|
229
|
+
return __generator(this, function (_a) {
|
|
230
|
+
switch (_a.label) {
|
|
231
|
+
case 0:
|
|
232
|
+
keyTable = new db_1.DataEncryptionKeyTable();
|
|
233
|
+
return [4 /*yield*/, (0, db_1.getDbAsSystem)().query(keyTable, {})];
|
|
234
|
+
case 1:
|
|
235
|
+
rows = _a.sent();
|
|
236
|
+
return [2 /*return*/, Array.from(new Set(rows.map(function (row) { return row.owner; })))];
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
});
|
|
240
|
+
};
|
|
241
|
+
/** The id inside a `Reference`-or-string field, or undefined. */
|
|
242
|
+
SharedScopeKeyOwners.prototype.referenceId = function (value) {
|
|
243
|
+
if (!value) {
|
|
244
|
+
return undefined;
|
|
245
|
+
}
|
|
246
|
+
if (typeof value === 'string') {
|
|
247
|
+
return value;
|
|
248
|
+
}
|
|
249
|
+
return value._id || undefined;
|
|
250
|
+
};
|
|
251
|
+
/**
|
|
252
|
+
* Cache on the global object (the duplicate-module pattern — see `setSkipAccessGrants`):
|
|
253
|
+
* per-package installs put multiple live copies of this class in one process, and the
|
|
254
|
+
* scope-root owner must be one answer across all of them.
|
|
255
|
+
*/
|
|
256
|
+
SharedScopeKeyOwners.prototype.ownerCache = function () {
|
|
257
|
+
var globalObject = globalThis;
|
|
258
|
+
if (!globalObject[SharedScopeKeyOwners.OWNER_CACHE_GLOBAL_KEY]) {
|
|
259
|
+
globalObject[SharedScopeKeyOwners.OWNER_CACHE_GLOBAL_KEY] = new Map();
|
|
260
|
+
}
|
|
261
|
+
return globalObject[SharedScopeKeyOwners.OWNER_CACHE_GLOBAL_KEY];
|
|
262
|
+
};
|
|
263
|
+
SharedScopeKeyOwners.prototype.cacheOwner = function (cacheKey, owner) {
|
|
264
|
+
var cache = this.ownerCache();
|
|
265
|
+
if (cache.size >= SharedScopeKeyOwners.OWNER_CACHE_MAX_ENTRIES) {
|
|
266
|
+
cache.clear();
|
|
267
|
+
}
|
|
268
|
+
cache.set(cacheKey, { cachedAt: Date.now(), owner: owner });
|
|
269
|
+
};
|
|
270
|
+
/** Scope-root owner cache TTL — bounds cross-process staleness after an ownership transfer. */
|
|
271
|
+
SharedScopeKeyOwners.OWNER_TTL_MS = 60 * 1000;
|
|
272
|
+
SharedScopeKeyOwners.OWNER_CACHE_MAX_ENTRIES = 10000;
|
|
273
|
+
SharedScopeKeyOwners.OWNER_CACHE_GLOBAL_KEY = '__proteinjs_user_sharedScopeOwnerCache';
|
|
274
|
+
return SharedScopeKeyOwners;
|
|
275
|
+
}());
|
|
276
|
+
exports.SharedScopeKeyOwners = SharedScopeKeyOwners;
|
|
277
|
+
var _seamCheck = {
|
|
278
|
+
resolveKeyOwner: function (args) { return new SharedScopeKeyOwners().resolveKeyOwner(args); },
|
|
279
|
+
getAccessibleKeyOwners: function (args) { return new SharedScopeKeyOwners().getAccessibleKeyOwners(args); },
|
|
280
|
+
};
|
|
281
|
+
void _seamCheck;
|
|
282
|
+
//# sourceMappingURL=SharedScopeKeyOwners.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SharedScopeKeyOwners.js","sourceRoot":"","sources":["../../src/SharedScopeKeyOwners.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,oCAAsG;AAItG,+CAAwE;AACxE,uCAAsC;AACtC,0CAAyC;AAQzC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8CG;AACH;IAAA;IAgKA,CAAC;IA1JC;;;;;;OAMG;IACG,8CAAe,GAArB,UAAsB,IAAwC;;;;;;;wBACpD,KAAK,GAAa,IAAI,MAAjB,EAAE,MAAM,GAAK,IAAI,OAAT,CAAU;wBAC/B,IAAI,CAAC,IAAA,4BAAa,EAAC,KAAK,CAAC,EAAE;4BACzB,sBAAO,SAAS,EAA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AC,CAAC,MAAM,CAAC,CAAC;IACnC,WAAW,EAAE,MAAM,GAAG,OAAO,GAAG,OAAO,GAAG,OAAO,CAAC;CACnD,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,iBAAiB,EAAE;KAAG,CAAC,IAAI,WAAW,CAAC,aAAa,CAAC,GAAG,MAAM;CAK1E,CAAC;AAEF;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,WAAW,CAAC,aAAa,CAAC,EAAE,GAAG,WAAW,CAAC,aAAa,CAAC,GAAG,SAAS,CAQ3G;AAED;;;;;;GAMG;AACH,qBAAa,yBAA0B,SAAQ,KAAK;gBACtC,OAAO,EAAE,MAAM;CAI5B;AAED,eAAO,MAAM,2BAA2B,UAAW,OAAO,uCACiD,CAAC;AAE5G,qBAAa,gBAAiB,SAAQ,KAAK,CAAC,WAAW,CAAC;IACtD,IAAI,SAAkB;IACtB,IAAI,EAAE,KAAK,CAAC,WAAW,CAAC,CAAC,MAAM,CAAC,CAO9B;IACF,OAAO;;;QAYL;IACF,OAAO,EAAE,KAAK,CAAC,WAAW,CAAC,CAAC,SAAS,CAAC,CAkHnC;IAEH,2GAA2G;YAC7F,gBAAgB;CAU/B"}
|
|
@@ -122,6 +122,13 @@ var AccessGrantTable = /** @class */ (function (_super) {
|
|
|
122
122
|
name: 'idx_ag_principal_table_level_resource',
|
|
123
123
|
columns: ['principal', 'resourceTable', 'accessLevel', 'resource'],
|
|
124
124
|
},
|
|
125
|
+
// The resource-side axis: scope-root owner resolution (SharedScopeKeyOwners — per
|
|
126
|
+
// encrypted write into a shared scope) and outbound-grant enumeration (deletion
|
|
127
|
+
// manifests, rosters) look grants up BY RESOURCE; without this they scan.
|
|
128
|
+
{
|
|
129
|
+
name: 'idx_ag_resource_level_principal',
|
|
130
|
+
columns: ['resource', 'accessLevel', 'principal'],
|
|
131
|
+
},
|
|
125
132
|
];
|
|
126
133
|
_this.columns = (0, db_1.withRecordColumns)({
|
|
127
134
|
accessLevel: new db_1.StringColumn('access_level'),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AccessGrantTable.js","sourceRoot":"","sources":["../../../src/tables/AccessGrantTable.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,oCAWuB;AAEvB,oCAAsC;AAEtC,wCAAuC;AASvC;;;;;GAKG;AACU,QAAA,iBAAiB,GAAkD;IAC9E,IAAI,EAAE,CAAC;IACP,KAAK,EAAE,CAAC;IACR,KAAK,EAAE,CAAC;IACR,KAAK,EAAE,CAAC;CACT,CAAC;AAEF;;;;GAIG;AACH,SAAgB,cAAc,CAAC,MAAoC;IACjE,IAAI,IAA4C,CAAC;IACjD,KAAoB,UAAM,EAAN,iBAAM,EAAN,oBAAM,EAAN,IAAM,EAAE;QAAvB,IAAM,KAAK,eAAA;QACd,IAAI,CAAC,IAAI,IAAI,yBAAiB,CAAC,KAAK,CAAC,GAAG,yBAAiB,CAAC,IAAI,CAAC,EAAE;YAC/D,IAAI,GAAG,KAAK,CAAC;SACd;KACF;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AARD,wCAQC;AAED;;;;;;GAMG;AACH;IAA+C,6CAAK;IAClD,mCAAY,OAAe;QAA3B,YACE,kBAAM,OAAO,CAAC,SAEf;QADC,KAAI,CAAC,IAAI,GAAG,2BAA2B,CAAC;;IAC1C,CAAC;IACH,gCAAC;AAAD,CAAC,AALD,CAA+C,KAAK,GAKnD;AALY,8DAAyB;AAO/B,IAAM,2BAA2B,GAAG,UAAC,KAAc;IACxD,OAAA,CAAC,CAAC,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAK,KAA2B,CAAC,IAAI,KAAK,2BAA2B;AAAzG,CAAyG,CAAC;AAD/F,QAAA,2BAA2B,+BACoE;AAE5G;IAAsC,oCAAkB;IAAxD;QAAA,
|
|
1
|
+
{"version":3,"file":"AccessGrantTable.js","sourceRoot":"","sources":["../../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,IAAI;wCACd,KAAK,EAAE,qBAAqB;qCAC7B;iCACF,CAAC,CAAC;6BACJ;;;;iBACF;aACF,CAAC;SACH,CAAC,CAAC;;IAaL,CAAC;IAXC,2GAA2G;IAC7F,2CAAgB,GAA9B,UAA+B,SAAsB;;;;;gBAC7C,OAAO,GAAI,CAAC,WAAW,EAAE,UAAU,CAAW,CAAC,MAAM,CAAC,UAAC,KAAK,YAAK,OAAA,CAAC,CAAA,MAAA,SAAS,CAAC,KAAK,CAAC,0CAAE,GAAG,CAAA,CAAA,EAAA,CAAC,CAAC;gBAC/F,IAAI,OAAO,CAAC,MAAM,GAAG,CAAC,EAAE;oBACtB,MAAM,IAAI,yBAAyB,CACjC,uCAAgC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,0BAAuB;wBAC1E,yBAAkB,MAAA,MAAA,SAAS,CAAC,aAAa,mCAAI,MAAA,SAAS,CAAC,QAAQ,0CAAE,MAAM,mCAAI,SAAS,OAAI;wBACxF,sBAAe,SAAS,CAAC,WAAW,MAAG,CAC1C,CAAC;iBACH;;;;KACF;IACH,uBAAC;AAAD,CAAC,AAtJD,CAAsC,UAAK,GAsJ1C;AAtJY,4CAAgB"}
|
package/index.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@proteinjs/user",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.15.0",
|
|
4
4
|
"description": "User management",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -46,5 +46,5 @@
|
|
|
46
46
|
},
|
|
47
47
|
"main": "./dist/generated/index.js",
|
|
48
48
|
"types": "./dist/generated/index.d.ts",
|
|
49
|
-
"gitHead": "
|
|
49
|
+
"gitHead": "2f490c4aa828ed0a241421fb01d9fd372d88e4a2"
|
|
50
50
|
}
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
import { DataEncryptionKeyTable, QueryBuilder, Reference, Table, getDbAsSystem } from '@proteinjs/db';
|
|
2
|
+
import type { DataEncryptionKey, DbEncryptionConfig } from '@proteinjs/db';
|
|
3
|
+
|
|
4
|
+
import { AccessGrant } from './tables/AccessGrantTable';
|
|
5
|
+
import { isSharedTable, skipAccessGrantsEnabled } from './SharedRecord';
|
|
6
|
+
import { UserRepo } from './UserRepo';
|
|
7
|
+
import { tables } from './tables/tables';
|
|
8
|
+
|
|
9
|
+
/** One resolved scope-root owner, cached briefly (see `SharedScopeKeyOwners`). */
|
|
10
|
+
interface OwnerCacheEntry {
|
|
11
|
+
cachedAt: number;
|
|
12
|
+
owner: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The sharing-aware half of the column-encryption key model (TRUST_AND_COMPLIANCE §4/§4.4;
|
|
17
|
+
* SHARING_EXPANSION §3): rows in a shared permission scope are keyed by the SCOPE-ROOT
|
|
18
|
+
* OWNER's data key, and a reader's encrypted-search fingerprints fan out over the owners
|
|
19
|
+
* sharing content into their view. Compose an instance into the app's `DbEncryptionConfig`:
|
|
20
|
+
*
|
|
21
|
+
* ```ts
|
|
22
|
+
* const keyOwners = new SharedScopeKeyOwners();
|
|
23
|
+
* const config: DbEncryptionConfig = {
|
|
24
|
+
* masterKeyProvider,
|
|
25
|
+
* resolveKeyOwner: (args) => keyOwners.resolveKeyOwner(args),
|
|
26
|
+
* getAccessibleKeyOwners: (args) => keyOwners.getAccessibleKeyOwners(args),
|
|
27
|
+
* };
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* Design facts this class is the single owner of:
|
|
31
|
+
*
|
|
32
|
+
* - **Owner-keyed shared scopes.** A `SharedRecord` row's key owner is the principal of the
|
|
33
|
+
* EARLIEST `owner` grant on its permission source (creation order; the creator). A
|
|
34
|
+
* contributor's write into someone else's document is keyed by the DOCUMENT owner, never
|
|
35
|
+
* the writer — so crypto-shred agrees exactly with the account-deletion purge walker,
|
|
36
|
+
* which drains shared trees grant-derived (by owned permission sources, never by the
|
|
37
|
+
* writer's scope): the rows a deletion removes and the rows a shred unreads are the same
|
|
38
|
+
* set. Keys are NOT access control — whether a reader may see a row is the permission
|
|
39
|
+
* layer's decision; revoking a share revokes access without re-encrypting anything.
|
|
40
|
+
* - **Root birth.** A scope root's owner grant is minted only after its insert lands
|
|
41
|
+
* (`SharedRecord.onAfterInsert`), so a root insert that touches encrypted columns resolves
|
|
42
|
+
* the creator (the session user) directly — the same identity the grant is about to name.
|
|
43
|
+
* - **Accessible owners = self + every OTHER owner-grant principal on resources the caller
|
|
44
|
+
* holds any grant on.** Resource-agnostic (thought, chat, space — whatever rides
|
|
45
|
+
* AccessGrant) and bounded by how much is shared with the caller. This includes conferred
|
|
46
|
+
* co-owners in both directions. Computed fresh per query — a revoked share drops out of
|
|
47
|
+
* the fan-out immediately (the permission layer already refused the rows themselves).
|
|
48
|
+
* - **System queries** (`runAsSystem`) search under EVERY key owner (the
|
|
49
|
+
* `data_encryption_key` owner set): an unscoped query silently missing rows of
|
|
50
|
+
* inaccessible owners would be a wrong answer, not a degraded one. Rare by design;
|
|
51
|
+
* cost scales with the owner count, correctness does not.
|
|
52
|
+
* - **Non-shared tables fall through** (`resolveKeyOwner` returns undefined): the framework
|
|
53
|
+
* default — the row's `scope` column — already IS the scope owner for ScopedRecord tables.
|
|
54
|
+
*
|
|
55
|
+
* Named limitation (owner transfer): after a full ownership transfer (a conferred owner
|
|
56
|
+
* grant plus deletion of the original owner's), NEW writes key under the new owner while
|
|
57
|
+
* historical rows keep their old envelopes. Readers whose grant set no longer reaches the
|
|
58
|
+
* old owner can still read those rows (envelopes are self-describing) but search fan-out no
|
|
59
|
+
* longer covers them; a lifecycle re-encrypt walk over the tree converges the envelopes to
|
|
60
|
+
* the current owner.
|
|
61
|
+
*/
|
|
62
|
+
export class SharedScopeKeyOwners {
|
|
63
|
+
/** Scope-root owner cache TTL — bounds cross-process staleness after an ownership transfer. */
|
|
64
|
+
private static readonly OWNER_TTL_MS = 60 * 1000;
|
|
65
|
+
private static readonly OWNER_CACHE_MAX_ENTRIES = 10_000;
|
|
66
|
+
private static readonly OWNER_CACHE_GLOBAL_KEY = '__proteinjs_user_sharedScopeOwnerCache';
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* The data-key owner for a `SharedRecord` row being written: the earliest `owner`-grant
|
|
70
|
+
* principal on the row's permission source, or the session user for a scope-root birth
|
|
71
|
+
* (whose grant is minted post-DML). Returns undefined for non-shared tables and for rows
|
|
72
|
+
* whose owner cannot be resolved — the framework then falls through to the row's `scope`
|
|
73
|
+
* value or refuses the write loudly (`EncryptionRecordHooks.resolveKeyOwnerForWrite`).
|
|
74
|
+
*/
|
|
75
|
+
async resolveKeyOwner(args: { table: Table<any>; record: any }): Promise<string | undefined> {
|
|
76
|
+
const { table, record } = args;
|
|
77
|
+
if (!isSharedTable(table)) {
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const sourceId = this.referenceId(record?.permissionSource);
|
|
82
|
+
const sourceTable: string = record?.permissionSourceTable ?? table.name;
|
|
83
|
+
if (!sourceId) {
|
|
84
|
+
return undefined;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (skipAccessGrantsEnabled()) {
|
|
88
|
+
// Grant machinery is off (test environments): the only resolvable owner is the caller.
|
|
89
|
+
return new UserRepo().getUser().id || undefined;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const cacheKey = `${sourceTable}:${sourceId}`;
|
|
93
|
+
const cached = this.ownerCache().get(cacheKey);
|
|
94
|
+
if (cached && Date.now() - cached.cachedAt < SharedScopeKeyOwners.OWNER_TTL_MS) {
|
|
95
|
+
return cached.owner;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const owner = await this.scopeRootOwner(sourceId, sourceTable);
|
|
99
|
+
if (owner) {
|
|
100
|
+
this.cacheOwner(cacheKey, owner);
|
|
101
|
+
return owner;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Scope-root birth: the row IS its own permission source and its owner grant does not
|
|
105
|
+
// exist yet — the creator (session user) is the owner that grant is about to name. Not
|
|
106
|
+
// cached: the authoritative grant lands post-DML.
|
|
107
|
+
if (sourceId === record?.id && sourceTable === table.name) {
|
|
108
|
+
return new UserRepo().getUser().id || undefined;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return undefined;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* The key owners whose rows the current caller's encrypted searches must cover (see the
|
|
116
|
+
* class doc for the definition). Loud refusal when no caller identity exists on a
|
|
117
|
+
* non-system query: returning an empty set would make the search silently match nothing.
|
|
118
|
+
*/
|
|
119
|
+
async getAccessibleKeyOwners(args: { runAsSystem: boolean }): Promise<string[]> {
|
|
120
|
+
if (args.runAsSystem) {
|
|
121
|
+
return await this.allKeyOwners();
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const callerId = new UserRepo().getUser().id;
|
|
125
|
+
if (!callerId) {
|
|
126
|
+
throw new Error(
|
|
127
|
+
`Cannot search encrypted columns without a caller identity: no session user is present. ` +
|
|
128
|
+
`System-context searches cover all key owners; caller-context searches require a signed-in user.`
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (skipAccessGrantsEnabled()) {
|
|
133
|
+
return [callerId];
|
|
134
|
+
}
|
|
135
|
+
// Every OTHER owner-grant principal on resources the caller holds any grant on — the
|
|
136
|
+
// same subquery shape SharedRecord's access filter uses, inverted onto the owner side.
|
|
137
|
+
const callerResources = new QueryBuilder(tables.AccessGrant.name);
|
|
138
|
+
callerResources.select({ fields: ['resource'] });
|
|
139
|
+
callerResources.condition({ field: 'principal', operator: '=', value: callerId });
|
|
140
|
+
|
|
141
|
+
const qb = new QueryBuilder(tables.AccessGrant.name);
|
|
142
|
+
qb.condition({ field: 'accessLevel', operator: '=', value: 'owner' });
|
|
143
|
+
qb.condition({ field: 'principal', operator: '!=', value: callerId });
|
|
144
|
+
qb.condition({ field: 'resource', operator: 'IN', value: callerResources });
|
|
145
|
+
const otherOwnerGrants = await getDbAsSystem<AccessGrant>().query(tables.AccessGrant, qb);
|
|
146
|
+
|
|
147
|
+
const owners = new Set<string>([callerId]);
|
|
148
|
+
for (const grant of otherOwnerGrants) {
|
|
149
|
+
const principalId = this.referenceId(grant.principal);
|
|
150
|
+
if (principalId) {
|
|
151
|
+
owners.add(principalId);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return Array.from(owners);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* The earliest `owner`-grant principal on a permission source — the scope-root owner.
|
|
160
|
+
* Earliest by creation (id tiebreak) so a document with conferred co-owners still keys
|
|
161
|
+
* deterministically by its original owner.
|
|
162
|
+
*/
|
|
163
|
+
private async scopeRootOwner(sourceId: string, sourceTable: string): Promise<string | undefined> {
|
|
164
|
+
const qb = new QueryBuilder(tables.AccessGrant.name);
|
|
165
|
+
qb.condition({ field: 'resource', operator: '=', value: sourceId });
|
|
166
|
+
qb.condition({ field: 'resourceTable', operator: '=', value: sourceTable });
|
|
167
|
+
qb.condition({ field: 'accessLevel', operator: '=', value: 'owner' });
|
|
168
|
+
const grants = await getDbAsSystem<AccessGrant>().query(tables.AccessGrant, qb);
|
|
169
|
+
if (grants.length === 0) {
|
|
170
|
+
return undefined;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const earliest = grants.reduce((best, candidate) => {
|
|
174
|
+
const bestCreated = best.created?.valueOf() ?? 0;
|
|
175
|
+
const candidateCreated = candidate.created?.valueOf() ?? 0;
|
|
176
|
+
if (candidateCreated !== bestCreated) {
|
|
177
|
+
return candidateCreated < bestCreated ? candidate : best;
|
|
178
|
+
}
|
|
179
|
+
return candidate.id < best.id ? candidate : best;
|
|
180
|
+
});
|
|
181
|
+
return this.referenceId(earliest.principal);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** Every key owner that exists — the distinct `data_encryption_key` owner set. */
|
|
185
|
+
private async allKeyOwners(): Promise<string[]> {
|
|
186
|
+
const keyTable = new DataEncryptionKeyTable() as Table<DataEncryptionKey>;
|
|
187
|
+
const rows = await getDbAsSystem<DataEncryptionKey>().query(keyTable, {} as Partial<DataEncryptionKey>);
|
|
188
|
+
return Array.from(new Set(rows.map((row) => row.owner)));
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** The id inside a `Reference`-or-string field, or undefined. */
|
|
192
|
+
private referenceId(value: Reference<any> | string | undefined | null): string | undefined {
|
|
193
|
+
if (!value) {
|
|
194
|
+
return undefined;
|
|
195
|
+
}
|
|
196
|
+
if (typeof value === 'string') {
|
|
197
|
+
return value;
|
|
198
|
+
}
|
|
199
|
+
return value._id || undefined;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Cache on the global object (the duplicate-module pattern — see `setSkipAccessGrants`):
|
|
204
|
+
* per-package installs put multiple live copies of this class in one process, and the
|
|
205
|
+
* scope-root owner must be one answer across all of them.
|
|
206
|
+
*/
|
|
207
|
+
private ownerCache(): Map<string, OwnerCacheEntry> {
|
|
208
|
+
const globalObject = globalThis as any;
|
|
209
|
+
if (!globalObject[SharedScopeKeyOwners.OWNER_CACHE_GLOBAL_KEY]) {
|
|
210
|
+
globalObject[SharedScopeKeyOwners.OWNER_CACHE_GLOBAL_KEY] = new Map<string, OwnerCacheEntry>();
|
|
211
|
+
}
|
|
212
|
+
return globalObject[SharedScopeKeyOwners.OWNER_CACHE_GLOBAL_KEY];
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
private cacheOwner(cacheKey: string, owner: string) {
|
|
216
|
+
const cache = this.ownerCache();
|
|
217
|
+
if (cache.size >= SharedScopeKeyOwners.OWNER_CACHE_MAX_ENTRIES) {
|
|
218
|
+
cache.clear();
|
|
219
|
+
}
|
|
220
|
+
cache.set(cacheKey, { cachedAt: Date.now(), owner });
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Type-level check that the two methods satisfy the framework seams they compose into —
|
|
226
|
+
* a drifted signature fails compilation here, not at an app's config site.
|
|
227
|
+
*/
|
|
228
|
+
type _ResolveKeyOwnerSeam = NonNullable<DbEncryptionConfig['resolveKeyOwner']>;
|
|
229
|
+
type _GetAccessibleKeyOwnersSeam = NonNullable<DbEncryptionConfig['getAccessibleKeyOwners']>;
|
|
230
|
+
const _seamCheck: {
|
|
231
|
+
resolveKeyOwner: _ResolveKeyOwnerSeam;
|
|
232
|
+
getAccessibleKeyOwners: _GetAccessibleKeyOwnersSeam;
|
|
233
|
+
} = {
|
|
234
|
+
resolveKeyOwner: (args) => new SharedScopeKeyOwners().resolveKeyOwner(args),
|
|
235
|
+
getAccessibleKeyOwners: (args) => new SharedScopeKeyOwners().getAccessibleKeyOwners(args),
|
|
236
|
+
};
|
|
237
|
+
void _seamCheck;
|
|
@@ -82,6 +82,13 @@ export class AccessGrantTable extends Table<AccessGrant> {
|
|
|
82
82
|
name: 'idx_ag_principal_table_level_resource',
|
|
83
83
|
columns: ['principal', 'resourceTable', 'accessLevel', 'resource'] satisfies (keyof AccessGrant)[],
|
|
84
84
|
},
|
|
85
|
+
// The resource-side axis: scope-root owner resolution (SharedScopeKeyOwners — per
|
|
86
|
+
// encrypted write into a shared scope) and outbound-grant enumeration (deletion
|
|
87
|
+
// manifests, rosters) look grants up BY RESOURCE; without this they scan.
|
|
88
|
+
{
|
|
89
|
+
name: 'idx_ag_resource_level_principal',
|
|
90
|
+
columns: ['resource', 'accessLevel', 'principal'] satisfies (keyof AccessGrant)[],
|
|
91
|
+
},
|
|
85
92
|
];
|
|
86
93
|
columns: Table<AccessGrant>['columns'] = withRecordColumns<AccessGrant>({
|
|
87
94
|
accessLevel: new StringColumn('access_level'),
|