@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.
- package/dist/context/resolveAuthentication.js +4 -10
- package/dist/routes/auth/getBetterAuthConfig.js +9 -6
- package/dist/routes/auth/isUserBanned.js +28 -0
- package/dist/routes/auth/organizations/buildOrganizationPlugin.js +14 -5
- package/dist/routes/auth/organizations/checkInvitationContactLink.js +60 -0
- package/dist/routes/auth/organizations/createAcceptExistingMemberHook.js +42 -12
- package/dist/routes/auth/organizations/createAfterAcceptInvitationHook.js +40 -16
- package/dist/routes/auth/organizations/createBeforeAcceptInvitationHook.js +35 -0
- package/dist/routes/auth/organizations/createEmailSendGate.js +35 -7
- package/dist/routes/auth/organizations/isEmailAdmitted.js +13 -9
- package/dist/routes/auth/requestHooks/buildRequestHooks.js +6 -2
- package/dist/routes/page/dynamic/policy/checkHtmlTag.js +30 -0
- package/dist/routes/page/dynamic/policy/checkKey.js +41 -0
- package/dist/routes/page/dynamic/policy/checkPolicy.js +15 -0
- package/dist/routes/page/dynamic/policy/checkTextUrls.js +92 -0
- package/dist/routes/page/dynamic/policy/checkValue.js +21 -80
- package/dist/routes/websocket/checkWebsocketAccess.js +81 -0
- package/dist/routes/websocket/createChannelRegistry.js +115 -20
- package/dist/routes/websocket/createWebSocketConnection.js +58 -16
- package/dist/routes/websocket/prepareChannel.js +11 -0
- package/package.json +13 -13
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
import { getPossibleOperators, isNestedDeeperThan, MAX_DATA_DEPTH } from '@lowdefy/operators';
|
|
17
17
|
import getPropertiesSchemaErrors from '../getPropertiesSchemaErrors.js';
|
|
18
18
|
import checkEvents from './checkEvents.js';
|
|
19
|
+
import checkKey from './checkKey.js';
|
|
19
20
|
import checkValue from './checkValue.js';
|
|
20
21
|
import collectSchemaKinds from './collectSchemaKinds.js';
|
|
21
22
|
import findDataRows from './findDataRows.js';
|
|
@@ -239,6 +240,20 @@ function checkBlock({ block, path, depth, walk }) {
|
|
|
239
240
|
walk
|
|
240
241
|
});
|
|
241
242
|
}
|
|
243
|
+
// Tabs and Collapse draw a slot name as its title when no tabs or panels are given.
|
|
244
|
+
[
|
|
245
|
+
'areas',
|
|
246
|
+
'slots'
|
|
247
|
+
].forEach((containerKey)=>{
|
|
248
|
+
Object.keys(block[containerKey] ?? {}).forEach((name)=>{
|
|
249
|
+
if (name.startsWith('~')) return;
|
|
250
|
+
checkKey({
|
|
251
|
+
key: name,
|
|
252
|
+
path: `${path}.${containerKey}.${name}`,
|
|
253
|
+
walk
|
|
254
|
+
});
|
|
255
|
+
});
|
|
256
|
+
});
|
|
242
257
|
childBlockLists(block).forEach((list)=>{
|
|
243
258
|
// eslint-disable-next-line no-use-before-define
|
|
244
259
|
checkBlockList({
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import findEmbeddedUrls from './findEmbeddedUrls.js';
|
|
16
|
+
import findHtmlUrls from './findHtmlUrls.js';
|
|
17
|
+
import isAllowedUrl from './isAllowedUrl.js';
|
|
18
|
+
// Which URLs a string can carry depends on how its block draws it (textKind,
|
|
19
|
+
// from the block schema). Text and HTML a block draws are no link and render no
|
|
20
|
+
// markdown, so a whole value, a markdown target or a protocol-relative word in
|
|
21
|
+
// them is words. HTML can carry a style, a link or a source attribute only when
|
|
22
|
+
// the policy allows tags (findHtmlUrls). A string no schema marks may be drawn
|
|
23
|
+
// as markdown, a style or a link the schema does not mark, so every scan applies.
|
|
24
|
+
function getUrlScans({ textKind, policy }) {
|
|
25
|
+
if (textKind === 'text') {
|
|
26
|
+
return {
|
|
27
|
+
wholeValue: false,
|
|
28
|
+
markdown: false,
|
|
29
|
+
css: false
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
if (textKind === 'html') {
|
|
33
|
+
return {
|
|
34
|
+
wholeValue: false,
|
|
35
|
+
markdown: false,
|
|
36
|
+
css: policy.html === true
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
wholeValue: true,
|
|
41
|
+
markdown: true,
|
|
42
|
+
css: true
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
// Searches a string that is not itself a URL value for the URLs it can make
|
|
46
|
+
// the client load or navigate to, and reports each one the policy does not allow.
|
|
47
|
+
function checkTextUrls({ value, path, textKind, walk }) {
|
|
48
|
+
const { errors, policy, routes } = walk;
|
|
49
|
+
findEmbeddedUrls({
|
|
50
|
+
value,
|
|
51
|
+
...getUrlScans({
|
|
52
|
+
textKind,
|
|
53
|
+
policy
|
|
54
|
+
})
|
|
55
|
+
}).forEach((url)=>{
|
|
56
|
+
if (!isAllowedUrl({
|
|
57
|
+
value: url,
|
|
58
|
+
policy,
|
|
59
|
+
navigation: false,
|
|
60
|
+
schemeless: false,
|
|
61
|
+
routes
|
|
62
|
+
})) {
|
|
63
|
+
errors.push({
|
|
64
|
+
path,
|
|
65
|
+
rule: 'policy.urls',
|
|
66
|
+
message: `URL "${url}" is not an origin dynamic blocks policy "${policy.id}" allows.`
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
// With tags allowed, any string a block may draw as HTML can carry a link or
|
|
71
|
+
// a source in an attribute.
|
|
72
|
+
if (policy.html === true && textKind !== 'text') {
|
|
73
|
+
findHtmlUrls({
|
|
74
|
+
value
|
|
75
|
+
}).forEach(({ url, navigation })=>{
|
|
76
|
+
if (!isAllowedUrl({
|
|
77
|
+
value: url,
|
|
78
|
+
policy,
|
|
79
|
+
navigation,
|
|
80
|
+
schemeless: false,
|
|
81
|
+
routes
|
|
82
|
+
})) {
|
|
83
|
+
errors.push({
|
|
84
|
+
path,
|
|
85
|
+
rule: 'policy.urls',
|
|
86
|
+
message: `HTML attribute URL "${url}" is not a page or origin dynamic blocks policy "${policy.id}" allows.`
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
export default checkTextUrls;
|
|
@@ -14,8 +14,9 @@
|
|
|
14
14
|
limitations under the License.
|
|
15
15
|
*/ import { type } from '@lowdefy/helpers';
|
|
16
16
|
import { getPossibleOperators } from '@lowdefy/operators';
|
|
17
|
-
import
|
|
18
|
-
import
|
|
17
|
+
import checkHtmlTag from './checkHtmlTag.js';
|
|
18
|
+
import checkKey from './checkKey.js';
|
|
19
|
+
import checkTextUrls from './checkTextUrls.js';
|
|
19
20
|
import isAllowedUrl from './isAllowedUrl.js';
|
|
20
21
|
import isUnderState from './isUnderState.js';
|
|
21
22
|
// How the client uses a URL value, for values no schema marks. A block schema
|
|
@@ -35,7 +36,6 @@ const URL_KEY_KINDS = {
|
|
|
35
36
|
srcSet: 'srcSet',
|
|
36
37
|
url: 'url'
|
|
37
38
|
};
|
|
38
|
-
const HTML_TAG = /<[a-zA-Z/!]/;
|
|
39
39
|
function joinPath(path, key) {
|
|
40
40
|
return path ? `${path}.${key}` : `${key}`;
|
|
41
41
|
}
|
|
@@ -52,34 +52,6 @@ function listUrls({ value, urlKind }) {
|
|
|
52
52
|
value
|
|
53
53
|
];
|
|
54
54
|
}
|
|
55
|
-
// Which URLs a string can carry depends on how its block draws it (textKind,
|
|
56
|
-
// from the block schema). A value is a URL where it is used as a link (a URL
|
|
57
|
-
// key or urlKind mark, judged whole above); text and HTML a block draws are no
|
|
58
|
-
// link and render no markdown, so a whole value, a markdown target or a
|
|
59
|
-
// protocol-relative word in them is words. HTML can carry a style, a link or a
|
|
60
|
-
// source attribute only when the policy allows tags (findHtmlUrls). A value no schema marks may be drawn as
|
|
61
|
-
// markdown, a style or a link the schema does not mark, so every scan applies.
|
|
62
|
-
function getUrlScans({ textKind, policy }) {
|
|
63
|
-
if (textKind === 'text') {
|
|
64
|
-
return {
|
|
65
|
-
wholeValue: false,
|
|
66
|
-
markdown: false,
|
|
67
|
-
css: false
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
if (textKind === 'html') {
|
|
71
|
-
return {
|
|
72
|
-
wholeValue: false,
|
|
73
|
-
markdown: false,
|
|
74
|
-
css: policy.html === true
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
return {
|
|
78
|
-
wholeValue: true,
|
|
79
|
-
markdown: true,
|
|
80
|
-
css: true
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
55
|
function checkString({ value, key, path, walk }) {
|
|
84
56
|
const { errors, policy, routes } = walk;
|
|
85
57
|
// Data is stored or sent, never drawn or followed by the content, so every
|
|
@@ -88,14 +60,13 @@ function checkString({ value, key, path, walk }) {
|
|
|
88
60
|
return;
|
|
89
61
|
}
|
|
90
62
|
const textKind = walk.textKinds?.get(path) ?? null;
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
}
|
|
63
|
+
checkHtmlTag({
|
|
64
|
+
value,
|
|
65
|
+
path,
|
|
66
|
+
textKind,
|
|
67
|
+
subject: 'String',
|
|
68
|
+
walk
|
|
69
|
+
});
|
|
99
70
|
if (key === 'pageId' && !policy.links.pages.includes(value)) {
|
|
100
71
|
errors.push({
|
|
101
72
|
path,
|
|
@@ -133,48 +104,12 @@ function checkString({ value, key, path, walk }) {
|
|
|
133
104
|
return;
|
|
134
105
|
}
|
|
135
106
|
// A URL value was judged whole above; other strings are searched.
|
|
136
|
-
|
|
107
|
+
checkTextUrls({
|
|
137
108
|
value,
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
})
|
|
142
|
-
}).forEach((url)=>{
|
|
143
|
-
if (!isAllowedUrl({
|
|
144
|
-
value: url,
|
|
145
|
-
policy,
|
|
146
|
-
navigation: false,
|
|
147
|
-
schemeless: false,
|
|
148
|
-
routes
|
|
149
|
-
})) {
|
|
150
|
-
errors.push({
|
|
151
|
-
path,
|
|
152
|
-
rule: 'policy.urls',
|
|
153
|
-
message: `URL "${url}" is not an origin dynamic blocks policy "${policy.id}" allows.`
|
|
154
|
-
});
|
|
155
|
-
}
|
|
109
|
+
path,
|
|
110
|
+
textKind,
|
|
111
|
+
walk
|
|
156
112
|
});
|
|
157
|
-
// With tags allowed, any string a block may draw as HTML can carry a link or
|
|
158
|
-
// a source in an attribute.
|
|
159
|
-
if (policy.html === true && textKind !== 'text') {
|
|
160
|
-
findHtmlUrls({
|
|
161
|
-
value
|
|
162
|
-
}).forEach(({ url, navigation })=>{
|
|
163
|
-
if (!isAllowedUrl({
|
|
164
|
-
value: url,
|
|
165
|
-
policy,
|
|
166
|
-
navigation,
|
|
167
|
-
schemeless: false,
|
|
168
|
-
routes
|
|
169
|
-
})) {
|
|
170
|
-
errors.push({
|
|
171
|
-
path,
|
|
172
|
-
rule: 'policy.urls',
|
|
173
|
-
message: `HTML attribute URL "${url}" is not a page or origin dynamic blocks policy "${policy.id}" allows.`
|
|
174
|
-
});
|
|
175
|
-
}
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
113
|
}
|
|
179
114
|
// A _state read may only name a literal key under the policy's state, so
|
|
180
115
|
// content cannot read other page state and send it on (a CallAPI payload).
|
|
@@ -294,10 +229,16 @@ function checkValue({ value, key = null, path, walk: parentWalk }) {
|
|
|
294
229
|
});
|
|
295
230
|
Object.keys(value).forEach((childKey)=>{
|
|
296
231
|
if (childKey.startsWith('~')) return;
|
|
232
|
+
const childPath = joinPath(path, childKey);
|
|
233
|
+
checkKey({
|
|
234
|
+
key: childKey,
|
|
235
|
+
path: childPath,
|
|
236
|
+
walk
|
|
237
|
+
});
|
|
297
238
|
checkValue({
|
|
298
239
|
value: value[childKey],
|
|
299
240
|
key: childKey,
|
|
300
|
-
path:
|
|
241
|
+
path: childPath,
|
|
301
242
|
walk
|
|
302
243
|
});
|
|
303
244
|
});
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import { AuthorizationError, isExpectedError } from '@lowdefy/errors';
|
|
16
|
+
import { type } from '@lowdefy/helpers';
|
|
17
|
+
import invokeEndpoint from '../endpoints/invokeEndpoint.js';
|
|
18
|
+
// Role rules decide who may hear a channel at all. A channel whose payload
|
|
19
|
+
// names a record also needs to know whether this subscriber may see that
|
|
20
|
+
// record, which only the app's own data can answer - so the websocket's check
|
|
21
|
+
// endpoint runs as the subscriber and must answer exactly true. It runs on
|
|
22
|
+
// every subscribe and publish, and again on the connection's periodic caller
|
|
23
|
+
// check while a subscription stays open, so revoked access ends it.
|
|
24
|
+
//
|
|
25
|
+
// Every refusal reads as a missing websocket, as a role refusal does
|
|
26
|
+
// (authorizeWebsocket), so a subscriber can not tell a record it may not see
|
|
27
|
+
// from a channel that does not exist.
|
|
28
|
+
async function checkWebsocketAccess(context, { payload, websocketConfig }) {
|
|
29
|
+
const { check, websocketId } = websocketConfig;
|
|
30
|
+
if (type.isNone(check)) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
const { logger } = context;
|
|
34
|
+
let allowed = false;
|
|
35
|
+
try {
|
|
36
|
+
const checkPayload = type.isNone(check.payload) ? payload : context.evaluateOperators({
|
|
37
|
+
input: check.payload,
|
|
38
|
+
location: websocketId,
|
|
39
|
+
payload,
|
|
40
|
+
state: {},
|
|
41
|
+
steps: {}
|
|
42
|
+
});
|
|
43
|
+
// The subscriber's client built the payload, so a payload the endpoint's
|
|
44
|
+
// schema refuses is that caller's mistake, as for a CallAPI with no page
|
|
45
|
+
// (validatePayload), not a fault to log at error level.
|
|
46
|
+
const result = await invokeEndpoint(context, {
|
|
47
|
+
endpointId: check.endpointId,
|
|
48
|
+
payload: checkPayload,
|
|
49
|
+
endpointDepth: 0,
|
|
50
|
+
outsideCaller: true
|
|
51
|
+
});
|
|
52
|
+
// A routine that errored was logged by runRoutine.
|
|
53
|
+
allowed = result.status === 'return' && result.response === true;
|
|
54
|
+
logger.debug({
|
|
55
|
+
event: 'debug_websocket_check',
|
|
56
|
+
websocketId,
|
|
57
|
+
allowed,
|
|
58
|
+
status: result.status
|
|
59
|
+
});
|
|
60
|
+
} catch (error) {
|
|
61
|
+
// The endpoint refusing the caller is a refusal like any other. Anything
|
|
62
|
+
// else is a fault in the check's config or the endpoint, which the
|
|
63
|
+
// developer needs to see; the subscriber is refused either way.
|
|
64
|
+
if (isExpectedError(error)) {
|
|
65
|
+
logger.debug({
|
|
66
|
+
event: 'debug_websocket_check_refused',
|
|
67
|
+
websocketId,
|
|
68
|
+
err: error
|
|
69
|
+
});
|
|
70
|
+
} else {
|
|
71
|
+
if (error instanceof Object && type.isNone(error.configKey)) {
|
|
72
|
+
error.configKey = check['~k'] ?? websocketConfig['~k'];
|
|
73
|
+
}
|
|
74
|
+
await context.handleError(error);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
if (!allowed) {
|
|
78
|
+
throw new AuthorizationError(`Websocket "${websocketId}" does not exist.`);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
export default checkWebsocketAccess;
|
|
@@ -13,13 +13,17 @@
|
|
|
13
13
|
See the License for the specific language governing permissions and
|
|
14
14
|
limitations under the License.
|
|
15
15
|
*/ import crypto from 'crypto';
|
|
16
|
-
import { serializer } from '@lowdefy/helpers';
|
|
16
|
+
import { serializer, type } from '@lowdefy/helpers';
|
|
17
17
|
import { ConfigError } from '@lowdefy/errors';
|
|
18
|
+
import checkWebsocketAccess from './checkWebsocketAccess.js';
|
|
19
|
+
import createEvaluateOperators from '../../context/createEvaluateOperators.js';
|
|
18
20
|
import createWireProjection from '../../response/createWireProjection.js';
|
|
19
21
|
import prepareChannel from './prepareChannel.js';
|
|
20
22
|
import wrapResolverError from '../request/wrapResolverError.js';
|
|
21
|
-
|
|
23
|
+
// A failed source is restarted for as long as its channel has subscribers, 1 s
|
|
24
|
+
// after the first failure, doubling up to 60 s apart.
|
|
22
25
|
const RETRY_BASE_MS = 1000;
|
|
26
|
+
const RETRY_CAP_MS = 60 * 1000;
|
|
23
27
|
const HEALTHY_RESET_MS = 60 * 1000;
|
|
24
28
|
// One registry per server process. The unit of sharing is the evaluated
|
|
25
29
|
// subscription: subscribers whose evaluated connection and websocket
|
|
@@ -28,6 +32,10 @@ const HEALTHY_RESET_MS = 60 * 1000;
|
|
|
28
32
|
// last one leaves.
|
|
29
33
|
function createChannelRegistry() {
|
|
30
34
|
const channels = new Map();
|
|
35
|
+
// subscriber → (websocketId → { payload, websocketConfig }) for the open
|
|
36
|
+
// subscriptions whose websocket has a check, so the check can run again while
|
|
37
|
+
// they stay open (recheckAccess).
|
|
38
|
+
const checkedSubscriptions = new WeakMap();
|
|
31
39
|
function getChannelKey({ connectionProperties, properties, tenant, websocketId }) {
|
|
32
40
|
// The tenant verdict is part of the channel identity: two callers whose
|
|
33
41
|
// evaluated properties are identical but whose organizations differ must
|
|
@@ -90,8 +98,8 @@ function createChannelRegistry() {
|
|
|
90
98
|
channel.abortController?.abort();
|
|
91
99
|
channels.delete(channel.key);
|
|
92
100
|
}
|
|
93
|
-
function handleResolverError({ channel, error,
|
|
94
|
-
if (
|
|
101
|
+
function handleResolverError({ channel, error, run }) {
|
|
102
|
+
if (run.abortController.signal.aborted) {
|
|
95
103
|
// Errors thrown while winding down are expected (closed cursors, etc).
|
|
96
104
|
channel.logger.debug({
|
|
97
105
|
event: 'ws_source_stopped_error_ignored',
|
|
@@ -99,6 +107,8 @@ function createChannelRegistry() {
|
|
|
99
107
|
}, error.message);
|
|
100
108
|
return;
|
|
101
109
|
}
|
|
110
|
+
// Anything the failed run left behind can no longer publish or report it is ready.
|
|
111
|
+
run.abortController.abort();
|
|
102
112
|
const wrapped = wrapSourceError({
|
|
103
113
|
channel,
|
|
104
114
|
error
|
|
@@ -106,21 +116,27 @@ function createChannelRegistry() {
|
|
|
106
116
|
channel.logger.debug({
|
|
107
117
|
err: wrapped
|
|
108
118
|
}, wrapped.message);
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
//
|
|
112
|
-
if (Date.now() -
|
|
119
|
+
// A run that stayed ready this long was healthy, and resets the backoff. Time
|
|
120
|
+
// spent starting does not count: a source waiting on an unreachable database
|
|
121
|
+
// can take a minute to fail.
|
|
122
|
+
if (run.readyAt !== null && Date.now() - run.readyAt > HEALTHY_RESET_MS) {
|
|
113
123
|
channel.retryCount = 0;
|
|
114
124
|
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
125
|
+
const delay = Math.min(RETRY_BASE_MS * 2 ** channel.retryCount, RETRY_CAP_MS);
|
|
126
|
+
// An outage lasts from the source's first failure until it is ready again, and
|
|
127
|
+
// is reported once: the restarts that fail during it are warnings.
|
|
128
|
+
if (channel.failing) {
|
|
129
|
+
channel.logger.warn({
|
|
130
|
+
event: 'ws_source_restart_failed',
|
|
131
|
+
websocketId: channel.websocketId,
|
|
132
|
+
retryCount: channel.retryCount,
|
|
133
|
+
retryInMs: delay
|
|
134
|
+
}, wrapped.message);
|
|
135
|
+
} else {
|
|
136
|
+
channel.failing = true;
|
|
137
|
+
channel.context.handleError(wrapped);
|
|
122
138
|
}
|
|
123
|
-
|
|
139
|
+
broadcastError(channel, wrapped);
|
|
124
140
|
channel.retryCount += 1;
|
|
125
141
|
channel.restartTimer = setTimeout(()=>{
|
|
126
142
|
channel.restartTimer = null;
|
|
@@ -140,11 +156,41 @@ function createChannelRegistry() {
|
|
|
140
156
|
function startResolver(channel) {
|
|
141
157
|
const abortController = new AbortController();
|
|
142
158
|
channel.abortController = abortController;
|
|
143
|
-
const
|
|
159
|
+
const run = {
|
|
160
|
+
abortController,
|
|
161
|
+
readyAt: null
|
|
162
|
+
};
|
|
163
|
+
// A source calls ready once it is listening, and a publish shows it is. The
|
|
164
|
+
// source heard nothing while it was down, so after an outage each subscriber
|
|
165
|
+
// gets a subscribed frame for its subscribe, which fires the page's
|
|
166
|
+
// onSubscribe for it to fetch what it missed.
|
|
167
|
+
function ready() {
|
|
168
|
+
var _run;
|
|
169
|
+
if (abortController.signal.aborted) {
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
(_run = run).readyAt ?? (_run.readyAt = Date.now());
|
|
173
|
+
if (!channel.failing) {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
channel.failing = false;
|
|
177
|
+
channel.logger.info({
|
|
178
|
+
event: 'ws_source_recovered',
|
|
179
|
+
websocketId: channel.websocketId
|
|
180
|
+
});
|
|
181
|
+
channel.subscribers.forEach((subscriber)=>{
|
|
182
|
+
subscriber.send(JSON.stringify({
|
|
183
|
+
type: 'subscribed',
|
|
184
|
+
websocketId: channel.websocketId,
|
|
185
|
+
requestId: channel.subscribeRequestIds.get(subscriber)
|
|
186
|
+
}));
|
|
187
|
+
});
|
|
188
|
+
}
|
|
144
189
|
function publish({ data }) {
|
|
145
190
|
if (abortController.signal.aborted) {
|
|
146
191
|
return;
|
|
147
192
|
}
|
|
193
|
+
ready();
|
|
148
194
|
broadcast(channel, {
|
|
149
195
|
type: 'message',
|
|
150
196
|
websocketId: channel.websocketId,
|
|
@@ -158,16 +204,17 @@ function createChannelRegistry() {
|
|
|
158
204
|
connection: channel.connectionProperties,
|
|
159
205
|
properties: channel.properties,
|
|
160
206
|
publish,
|
|
207
|
+
ready,
|
|
161
208
|
signal: abortController.signal,
|
|
162
209
|
logger: channel.logger,
|
|
163
210
|
tenant: channel.tenant ?? null
|
|
164
211
|
})).catch((error)=>handleResolverError({
|
|
165
212
|
channel,
|
|
166
213
|
error,
|
|
167
|
-
|
|
214
|
+
run
|
|
168
215
|
}));
|
|
169
216
|
}
|
|
170
|
-
async function subscribe(context, { websocketId, payload, subscriber }) {
|
|
217
|
+
async function subscribe(context, { websocketId, payload, requestId, subscriber }) {
|
|
171
218
|
// One subscription per websocketId per connection — a re-subscribe (e.g.
|
|
172
219
|
// with a new payload) replaces the previous one. The client stops reading
|
|
173
220
|
// the previous one when it sends the re-subscribe, so it is removed before
|
|
@@ -178,9 +225,12 @@ function createChannelRegistry() {
|
|
|
178
225
|
subscriber
|
|
179
226
|
});
|
|
180
227
|
}
|
|
228
|
+
// The client serializes the payload, as it does for a publish and a CallAPI,
|
|
229
|
+
// so properties and the check endpoint read Dates as Dates.
|
|
230
|
+
const deserializedPayload = serializer.deserialize(payload ?? {});
|
|
181
231
|
const { connectionProperties, properties, tenant, websocketConfig, websocketResolver } = await prepareChannel(context, {
|
|
182
232
|
websocketId,
|
|
183
|
-
payload
|
|
233
|
+
payload: deserializedPayload
|
|
184
234
|
});
|
|
185
235
|
const key = getChannelKey({
|
|
186
236
|
connectionProperties,
|
|
@@ -199,9 +249,14 @@ function createChannelRegistry() {
|
|
|
199
249
|
properties,
|
|
200
250
|
tenant,
|
|
201
251
|
subscribers: new Set(),
|
|
252
|
+
// subscriber → the requestId of the subscribe that joined it. The client
|
|
253
|
+
// reads a subscribed frame with any other requestId as a reply to a
|
|
254
|
+
// subscribe it has since replaced, and ignores it.
|
|
255
|
+
subscribeRequestIds: new Map(),
|
|
202
256
|
abortController: null,
|
|
203
257
|
restartTimer: null,
|
|
204
258
|
retryCount: 0,
|
|
259
|
+
failing: false,
|
|
205
260
|
publish: null,
|
|
206
261
|
context,
|
|
207
262
|
logger: context.logger
|
|
@@ -210,7 +265,17 @@ function createChannelRegistry() {
|
|
|
210
265
|
startResolver(channel);
|
|
211
266
|
}
|
|
212
267
|
channel.subscribers.add(subscriber);
|
|
268
|
+
channel.subscribeRequestIds.set(subscriber, requestId);
|
|
213
269
|
subscriber.subscriptions.set(websocketId, key);
|
|
270
|
+
if (!type.isNone(websocketConfig.check)) {
|
|
271
|
+
if (!checkedSubscriptions.has(subscriber)) {
|
|
272
|
+
checkedSubscriptions.set(subscriber, new Map());
|
|
273
|
+
}
|
|
274
|
+
checkedSubscriptions.get(subscriber).set(websocketId, {
|
|
275
|
+
payload: deserializedPayload,
|
|
276
|
+
websocketConfig
|
|
277
|
+
});
|
|
278
|
+
}
|
|
214
279
|
context.logger.debug({
|
|
215
280
|
event: 'ws_subscribe',
|
|
216
281
|
websocketId,
|
|
@@ -220,6 +285,7 @@ function createChannelRegistry() {
|
|
|
220
285
|
function unsubscribe({ websocketId, subscriber }) {
|
|
221
286
|
const key = subscriber.subscriptions.get(websocketId);
|
|
222
287
|
subscriber.subscriptions.delete(websocketId);
|
|
288
|
+
checkedSubscriptions.get(subscriber)?.delete(websocketId);
|
|
223
289
|
if (!key) {
|
|
224
290
|
return;
|
|
225
291
|
}
|
|
@@ -228,6 +294,7 @@ function createChannelRegistry() {
|
|
|
228
294
|
return;
|
|
229
295
|
}
|
|
230
296
|
channel.subscribers.delete(subscriber);
|
|
297
|
+
channel.subscribeRequestIds.delete(subscriber);
|
|
231
298
|
if (channel.subscribers.size === 0) {
|
|
232
299
|
removeChannel(channel);
|
|
233
300
|
}
|
|
@@ -242,6 +309,32 @@ function createChannelRegistry() {
|
|
|
242
309
|
});
|
|
243
310
|
});
|
|
244
311
|
}
|
|
312
|
+
function getCheckedWebsocketIds({ subscriber }) {
|
|
313
|
+
return [
|
|
314
|
+
...checkedSubscriptions.get(subscriber)?.keys() ?? []
|
|
315
|
+
];
|
|
316
|
+
}
|
|
317
|
+
// Access to a record can be revoked while a subscription to it stays open, so
|
|
318
|
+
// the connection runs the check again on its periodic caller check, with the
|
|
319
|
+
// payload and config the subscription was opened with. A refused
|
|
320
|
+
// subscription is closed and the refusal thrown for the caller to be told.
|
|
321
|
+
// The subscription may have been closed while this waited in its feed queue.
|
|
322
|
+
async function recheckAccess(context, { websocketId, subscriber }) {
|
|
323
|
+
const subscription = checkedSubscriptions.get(subscriber)?.get(websocketId);
|
|
324
|
+
if (type.isUndefined(subscription)) {
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
context.evaluateOperators = createEvaluateOperators(context);
|
|
328
|
+
try {
|
|
329
|
+
await checkWebsocketAccess(context, subscription);
|
|
330
|
+
} catch (error) {
|
|
331
|
+
unsubscribe({
|
|
332
|
+
websocketId,
|
|
333
|
+
subscriber
|
|
334
|
+
});
|
|
335
|
+
throw error;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
245
338
|
async function publish(context, { websocketId, payload }) {
|
|
246
339
|
// Publish identity is evaluated without a subscription payload — channels
|
|
247
340
|
// that fragment on _payload/_user in properties are not publish targets.
|
|
@@ -281,7 +374,9 @@ function createChannelRegistry() {
|
|
|
281
374
|
});
|
|
282
375
|
}
|
|
283
376
|
return {
|
|
377
|
+
getCheckedWebsocketIds,
|
|
284
378
|
publish,
|
|
379
|
+
recheckAccess,
|
|
285
380
|
subscribe,
|
|
286
381
|
unsubscribe,
|
|
287
382
|
unsubscribeAll,
|