@lowdefy/api 7.2.0 → 7.4.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.
@@ -31,7 +31,9 @@ const CALLER_CHECK_FAILED_CODE = 1011;
31
31
  // out, lost its membership, changed roles or switched organization, the socket
32
32
  // closes with code 4401; the client reconnects, which authenticates afresh.
33
33
  // Each check is a live session read - nothing is cached, so a revoked session
34
- // closes the socket within a minute.
34
+ // closes the socket within a minute. The once-a-minute check also runs the
35
+ // websocket check of every open subscription that has one, so revoked access to
36
+ // a record ends that subscription within a minute.
35
37
  function createWebSocketConnection(context, { closeSocket, headers, registry, send, strategies }) {
36
38
  const { logger } = context;
37
39
  const callerFingerprint = getCallerFingerprint({
@@ -141,6 +143,7 @@ function createWebSocketConnection(context, { closeSocket, headers, registry, se
141
143
  await registry.subscribe(context, {
142
144
  websocketId,
143
145
  payload,
146
+ requestId,
144
147
  subscriber
145
148
  });
146
149
  send(JSON.stringify({
@@ -178,6 +181,26 @@ function createWebSocketConnection(context, { closeSocket, headers, registry, se
178
181
  });
179
182
  }
180
183
  }
184
+ function reportError({ error, frameType, requestId, websocketId }) {
185
+ logger.debug({
186
+ err: error
187
+ }, error.message);
188
+ // Expected outcomes - an auth refusal, a UserError - are one warn line, as over HTTP:
189
+ // no error log or Sentry report, but the client is still told.
190
+ if (isExpectedError(error)) {
191
+ logger.warn({
192
+ event: 'ws_refused',
193
+ frameType
194
+ }, error.message);
195
+ } else {
196
+ context.handleError(error);
197
+ }
198
+ sendErrorPayload({
199
+ error,
200
+ requestId,
201
+ websocketId
202
+ });
203
+ }
181
204
  async function processFrame(frame) {
182
205
  if (closed) {
183
206
  return;
@@ -185,26 +208,33 @@ function createWebSocketConnection(context, { closeSocket, headers, registry, se
185
208
  try {
186
209
  await handleFrame(frame);
187
210
  } catch (error) {
188
- logger.debug({
189
- err: error
190
- }, error.message);
191
- // Expected outcomes - an auth refusal, a UserError - are one warn line, as over HTTP:
192
- // no error log or Sentry report, but the client is still told.
193
- if (isExpectedError(error)) {
194
- logger.warn({
195
- event: 'ws_refused',
196
- frameType: frame.type
197
- }, error.message);
198
- } else {
199
- context.handleError(error);
200
- }
201
- sendErrorPayload({
211
+ reportError({
202
212
  error,
213
+ frameType: frame.type,
203
214
  requestId: frame.requestId,
204
215
  websocketId: frame.websocketId
205
216
  });
206
217
  }
207
218
  }
219
+ // A refused subscription is closed by the registry. The error frame carries no
220
+ // requestId, so the client reads it as the feed failing, as it does a source error.
221
+ async function recheckSubscription(websocketId) {
222
+ if (closed) {
223
+ return;
224
+ }
225
+ try {
226
+ await registry.recheckAccess(context, {
227
+ websocketId,
228
+ subscriber
229
+ });
230
+ } catch (error) {
231
+ reportError({
232
+ error,
233
+ frameType: 'recheck',
234
+ websocketId
235
+ });
236
+ }
237
+ }
208
238
  function enqueue(websocketId, task) {
209
239
  const handled = (feedQueues.get(websocketId) ?? Promise.resolve()).then(task);
210
240
  const tail = handled.catch(()=>{});
@@ -246,9 +276,21 @@ function createWebSocketConnection(context, { closeSocket, headers, registry, se
246
276
  // routines it runs.
247
277
  await enqueue(frame.websocketId, ()=>runInCredentialScope(()=>processFrame(frame)));
248
278
  }
279
+ // A websocket check answers for one record, which the caller can lose access
280
+ // to without the caller changing, so the check runs again for every open
281
+ // subscription that has one. Each runs in its feed's queue, after any frame
282
+ // for that feed already waiting.
283
+ async function checkOpenConnection() {
284
+ if (!await checkCaller()) {
285
+ return;
286
+ }
287
+ await Promise.all(registry.getCheckedWebsocketIds({
288
+ subscriber
289
+ }).map((websocketId)=>enqueue(websocketId, ()=>runInCredentialScope(()=>recheckSubscription(websocketId)))));
290
+ }
249
291
  // One timer per connection, cleared by close. It does not keep the process
250
292
  // alive on its own.
251
- const callerCheckTimer = setInterval(checkCaller, CALLER_CHECK_INTERVAL_MS);
293
+ const callerCheckTimer = setInterval(checkOpenConnection, CALLER_CHECK_INTERVAL_MS);
252
294
  callerCheckTimer.unref();
253
295
  logger.debug({
254
296
  event: 'ws_connect'
@@ -14,6 +14,7 @@
14
14
  limitations under the License.
15
15
  */ import { type } from '@lowdefy/helpers';
16
16
  import authorizeWebsocket from './authorizeWebsocket.js';
17
+ import checkWebsocketAccess from './checkWebsocketAccess.js';
17
18
  import checkConnectionRead from '../request/checkConnectionRead.js';
18
19
  import getConnection from '../connections/getConnection.js';
19
20
  import getConnectionConfig from '../connections/getConnectionConfig.js';
@@ -27,6 +28,12 @@ import createEvaluateOperators from '../../context/createEvaluateOperators.js';
27
28
  // websocket definition can produce user- or filter-specific channels. The
28
29
  // evaluated result is the channel identity — subscribers whose evaluation is
29
30
  // identical share one running source.
31
+ //
32
+ // After the role check, the websocket's check endpoint runs, before anything is
33
+ // evaluated for a caller it refuses. The check is per caller and not part of the channel
34
+ // identity, so subscribers that each pass still share a source. A publish is
35
+ // checked too, with the same empty payload its properties are evaluated with,
36
+ // so a caller the check refuses can not publish into a channel either.
30
37
  async function prepareChannel(context, { websocketId, payload }) {
31
38
  context.evaluateOperators = createEvaluateOperators(context);
32
39
  const websocketConfig = await getWebsocketConfig(context, {
@@ -35,6 +42,10 @@ async function prepareChannel(context, { websocketId, payload }) {
35
42
  authorizeWebsocket(context, {
36
43
  websocketConfig
37
44
  });
45
+ await checkWebsocketAccess(context, {
46
+ payload,
47
+ websocketConfig
48
+ });
38
49
  const websocketResolver = getWebsocketResolver(context, {
39
50
  websocketConfig
40
51
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lowdefy/api",
3
- "version": "7.2.0",
3
+ "version": "7.4.0",
4
4
  "license": "Apache-2.0",
5
5
  "description": "",
6
6
  "homepage": "https://lowdefy.com",
@@ -38,16 +38,16 @@
38
38
  "@better-auth/cimd": "1.7.7",
39
39
  "@better-auth/oauth-provider": "1.7.7",
40
40
  "@better-auth/passkey": "1.7.7",
41
- "@lowdefy/ajv": "7.2.0",
42
- "@lowdefy/build": "7.2.0",
43
- "@lowdefy/connection-smtp": "7.2.0",
44
- "@lowdefy/email-templates": "7.2.0",
45
- "@lowdefy/errors": "7.2.0",
46
- "@lowdefy/helpers": "7.2.0",
47
- "@lowdefy/node-utils": "7.2.0",
48
- "@lowdefy/nunjucks": "7.2.0",
49
- "@lowdefy/operators": "7.2.0",
50
- "@lowdefy/operators-js": "7.2.0",
41
+ "@lowdefy/ajv": "7.4.0",
42
+ "@lowdefy/build": "7.4.0",
43
+ "@lowdefy/connection-smtp": "7.4.0",
44
+ "@lowdefy/email-templates": "7.4.0",
45
+ "@lowdefy/errors": "7.4.0",
46
+ "@lowdefy/helpers": "7.4.0",
47
+ "@lowdefy/node-utils": "7.4.0",
48
+ "@lowdefy/nunjucks": "7.4.0",
49
+ "@lowdefy/operators": "7.4.0",
50
+ "@lowdefy/operators-js": "7.4.0",
51
51
  "@modelcontextprotocol/sdk": "1.32.2",
52
52
  "better-auth": "1.7.7",
53
53
  "jose": "^6.2.3",
@@ -55,8 +55,8 @@
55
55
  "nodemailer": "10.1.7"
56
56
  },
57
57
  "devDependencies": {
58
- "@lowdefy/connection-mongodb": "7.2.0",
59
- "@lowdefy/plugin-better-auth": "7.2.0",
58
+ "@lowdefy/connection-mongodb": "7.4.0",
59
+ "@lowdefy/plugin-better-auth": "7.4.0",
60
60
  "@opentelemetry/api": "1.9.1",
61
61
  "@swc/cli": "0.8.1",
62
62
  "@swc/core": "1.15.32",