@immediately-run/preauth-core 0.1.9 → 0.1.10

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.
@@ -77,6 +77,19 @@ export declare const granteeId: (uid: string) => string;
77
77
  * each had their own copy of this; sharing it keeps the "omit absent optionals"
78
78
  * rule identical on both sides. */
79
79
  export declare const defined: <T extends Record<string, unknown>>(obj: T) => T;
80
+ /** Thrown when an `appKey` is not a single Firestore path segment. Carries a
81
+ * machine `code` so a caller can map it to its own error vocabulary. */
82
+ export declare class InvalidAppKeyError extends Error {
83
+ readonly code = "invalid-app-key";
84
+ constructor(appKey: string, why: string);
85
+ }
86
+ /** Is `appKey` usable as exactly one Firestore path segment? Empty, `/`-bearing,
87
+ * and the two relative-path doc-ids Firestore reserves are all refused. */
88
+ export declare const isAppKeySegment: (appKey: string) => boolean;
89
+ /** Refuse an `appKey` that is not one path segment — the shared chokepoint every
90
+ * grant-store path builder runs first (R3-285). Returns the key so it can wrap a
91
+ * segment in place. */
92
+ export declare const assertAppKeySegment: (appKey: string) => string;
80
93
  export declare const spacePath: (spaceId: string) => DocPath;
81
94
  export declare const memberPath: (spaceId: string, grantee: string) => DocPath;
82
95
  export declare const userSpacePath: (uid: string, spaceId: string) => DocPath;
package/dist/docLayout.js CHANGED
@@ -15,8 +15,18 @@
15
15
  // `FieldValue.serverTimestamp()`/`FieldValue.increment()`). The raw
16
16
  // `.set()`/`.update()` is the only thing each adapter does itself. Drift is then
17
17
  // impossible without editing a helper both consume.
18
+ //
19
+ // HONESTY NOTE (R3-285): that guarantee holds TODAY for the FIELD builders only.
20
+ // The browser `FirestoreSpaceStore` builds its refs with the Web SDK's variadic
21
+ // `doc(db, 'user-app-spaces', uid, 'apps', appKey, …)` and does NOT call the
22
+ // `*Path` builders below — they are backend-only. The two constructions are
23
+ // equivalent (`doc()` joins with `/` and re-parses exactly as the backend's
24
+ // `segments.join('/')` does), but "one source for the paths" is an aspiration
25
+ // here, not a fact, and a bug fixed in a `*Path` builder does not reach the
26
+ // browser. What both sides DO share is `assertAppKeySegment` — the one property
27
+ // a wrong path would violate. Unifying the ref construction is tracked debt.
18
28
  Object.defineProperty(exports, "__esModule", { value: true });
19
- exports.appCapabilitiesGrantFields = exports.mergeCapabilities = exports.netFetchGrantFields = exports.mergeNetFetchHosts = exports.appSpaceGrantFields = exports.appKeyTouchFields = exports.appCountFields = exports.userCountFields = exports.ownerUserSpaceFields = exports.ownerMemberFields = exports.spaceDocFields = exports.appCountPath = exports.userCountPath = exports.appSpacePath = exports.appKeyPath = exports.userSpacePath = exports.memberPath = exports.spacePath = exports.defined = exports.granteeId = exports.GRANT_EXPIRY_MS = exports.parseGrantDocId = exports.grantDocId = exports.GRANT_DOCID_DELIM = exports.parseGrantKey = exports.grantKeyWithPrincipal = exports.grantKey = void 0;
29
+ exports.appCapabilitiesGrantFields = exports.mergeCapabilities = exports.netFetchGrantFields = exports.mergeNetFetchHosts = exports.appSpaceGrantFields = exports.appKeyTouchFields = exports.appCountFields = exports.userCountFields = exports.ownerUserSpaceFields = exports.ownerMemberFields = exports.spaceDocFields = exports.appCountPath = exports.userCountPath = exports.appSpacePath = exports.appKeyPath = exports.userSpacePath = exports.memberPath = exports.spacePath = exports.assertAppKeySegment = exports.isAppKeySegment = exports.InvalidAppKeyError = exports.defined = exports.granteeId = exports.GRANT_EXPIRY_MS = exports.parseGrantDocId = exports.grantDocId = exports.GRANT_DOCID_DELIM = exports.parseGrantKey = exports.grantKeyWithPrincipal = exports.grantKey = void 0;
20
30
  /** Stable per-user identifier for a grant `(appKey, spaceId)`, used as the value
21
31
  * of a delegated grant's `parentGrantId`. `::` is delimiter-safe: `appKey` uses
22
32
  * `__` separators and a Firestore `spaceId` is alphanumeric. */
@@ -96,6 +106,64 @@ exports.granteeId = granteeId;
96
106
  * rule identical on both sides. */
97
107
  const defined = (obj) => Object.fromEntries(Object.entries(obj).filter(([, v]) => v !== undefined));
98
108
  exports.defined = defined;
109
+ // --- the appKey grammar guard (R3-285) --------------------------------------
110
+ //
111
+ // An `appKey` is ONE Firestore path segment. The canonical grammar is
112
+ // site-main's `spaceId.appKey()` — `enc(provider)__enc(namespace)__enc(repository)`
113
+ // — which is punctuation-free by construction. The DANGER is the neighbouring
114
+ // grammar: a *binding id* (`provider:namespace/repository`) is a different
115
+ // identifier that also names a repo, and feeding one to the grant store is a
116
+ // silent catastrophe rather than a loud one:
117
+ //
118
+ // • one slash (`github:acme/notes`) → the joined path has an ODD segment
119
+ // count, so `doc()` throws `invalid-argument` — noisy, fails closed;
120
+ // • two slashes (`gitlab:g/sub/notes`, a nested namespace) → the path is EVEN
121
+ // and perfectly valid, so the grant is WRITTEN — to a document no reader
122
+ // looks at, that the §8.11 audit view does not enumerate, and that the
123
+ // §8.15 revoke cascade cannot reach. A durable, invisible, unrevokable grant.
124
+ //
125
+ // So the fix is NOT to encode: encoding would make the second case succeed
126
+ // quietly at the wrong key. It is to refuse a key that is not one segment, at
127
+ // the one place both adapters can share, and let the caller be corrected.
128
+ //
129
+ // This asserts the SEGMENT property (what Firestore requires), not the `__`
130
+ // grammar (which lives in site-main and may still gain components) — the widest
131
+ // check that still catches every wrong-grammar key we have seen.
132
+ /** Thrown when an `appKey` is not a single Firestore path segment. Carries a
133
+ * machine `code` so a caller can map it to its own error vocabulary. */
134
+ class InvalidAppKeyError extends Error {
135
+ code = 'invalid-app-key';
136
+ constructor(appKey, why) {
137
+ super(`appKey ${JSON.stringify(appKey)} is not one Firestore path segment (${why}). ` +
138
+ 'Expected the grant-store key grammar (`provider__namespace__repository`), ' +
139
+ 'not a binding id (`provider:namespace/repository`).');
140
+ this.name = 'InvalidAppKeyError';
141
+ }
142
+ }
143
+ exports.InvalidAppKeyError = InvalidAppKeyError;
144
+ /** Is `appKey` usable as exactly one Firestore path segment? Empty, `/`-bearing,
145
+ * and the two relative-path doc-ids Firestore reserves are all refused. */
146
+ const isAppKeySegment = (appKey) => typeof appKey === 'string' &&
147
+ appKey.length > 0 &&
148
+ !appKey.includes('/') &&
149
+ appKey !== '.' &&
150
+ appKey !== '..';
151
+ exports.isAppKeySegment = isAppKeySegment;
152
+ /** Refuse an `appKey` that is not one path segment — the shared chokepoint every
153
+ * grant-store path builder runs first (R3-285). Returns the key so it can wrap a
154
+ * segment in place. */
155
+ const assertAppKeySegment = (appKey) => {
156
+ if (typeof appKey !== 'string' || appKey.length === 0) {
157
+ throw new InvalidAppKeyError(String(appKey), 'empty');
158
+ }
159
+ if (appKey.includes('/'))
160
+ throw new InvalidAppKeyError(appKey, 'contains "/"');
161
+ if (appKey === '.' || appKey === '..') {
162
+ throw new InvalidAppKeyError(appKey, 'is a reserved relative path');
163
+ }
164
+ return appKey;
165
+ };
166
+ exports.assertAppKeySegment = assertAppKeySegment;
99
167
  // --- document paths (pure, sentinel-free) -----------------------------------
100
168
  const spacePath = (spaceId) => ['spaces', spaceId];
101
169
  exports.spacePath = spacePath;
@@ -117,7 +185,7 @@ const appKeyPath = (uid, appKey) => [
117
185
  'user-app-spaces',
118
186
  uid,
119
187
  'apps',
120
- appKey,
188
+ (0, exports.assertAppKeySegment)(appKey),
121
189
  ];
122
190
  exports.appKeyPath = appKeyPath;
123
191
  /** `user-app-spaces/{uid}/apps/{appKey}/spaces/{docId}` — the durable §8.7 grant
@@ -129,7 +197,7 @@ const appSpacePath = (uid, appKey, spaceId, qualifyingPrincipal) => [
129
197
  'user-app-spaces',
130
198
  uid,
131
199
  'apps',
132
- appKey,
200
+ (0, exports.assertAppKeySegment)(appKey),
133
201
  'spaces',
134
202
  (0, exports.grantDocId)(spaceId, qualifyingPrincipal),
135
203
  ];
@@ -140,7 +208,7 @@ const appCountPath = (uid, appKey) => [
140
208
  'space-counts',
141
209
  uid,
142
210
  'apps',
143
- appKey,
211
+ (0, exports.assertAppKeySegment)(appKey),
144
212
  ];
145
213
  exports.appCountPath = appCountPath;
146
214
  // --- field objects (inject the timestamp/increment sentinels) ---------------
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@immediately-run/preauth-core",
3
- "version": "0.1.9",
3
+ "version": "0.1.10",
4
4
  "description": "The shared §8.9 pre-auth target check + the single grant-mint path (mintConsentedGrants) + the capability vocabulary + the byte-faithful grant/space/net-fetch document layout. Consumed by site-main (browser Firestore) and the backend (admin Firestore) so there is ONE gate, ONE mint path, ONE wire layout.",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {