@nr-connect/node-red 0.0.0-stage → 1.0.1
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/LICENSE +21 -0
- package/README.md +169 -2
- package/examples/starter-demo.json +776 -0
- package/index.html +1939 -0
- package/index.js +76 -0
- package/lib/binaryManager.js +557 -0
- package/lib/connectionManager.js +1060 -0
- package/lib/connectorManifest.json +61 -0
- package/lib/dashboardRbac.js +670 -0
- package/lib/expressInterceptor.js +917 -0
- package/lib/pageSyncAgent.js +149 -0
- package/lib/pushAgent.js +253 -0
- package/lib/qrCode.js +625 -0
- package/lib/statusEndpoint.js +231 -0
- package/lib/tokenCodec.js +98 -0
- package/lib/tunnelDaemon.js +236 -0
- package/nodes/nr-connect-notify.html +258 -0
- package/nodes/nr-connect-notify.js +303 -0
- package/nodes/ui-nr-connect-profile.html +211 -0
- package/nodes/ui-nr-connect-profile.js +66 -0
- package/package.json +85 -6
- package/resources/ui-nr-connect-profile.umd.js +213 -0
- package/ui/UINrConnectProfile.js +1173 -0
- package/ui/profile.js +1 -0
|
@@ -0,0 +1,670 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
function isPlainObject(value) {
|
|
4
|
+
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function getRequestHeader(req, name) {
|
|
8
|
+
if (!req || !req.headers) return undefined;
|
|
9
|
+
const val = req.headers[name] ?? req.headers[name.toLowerCase()];
|
|
10
|
+
return Array.isArray(val) ? val[0] : val;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Detect whether a request carries any X-NR-Connect-* worker identity/signature headers.
|
|
15
|
+
* Used to fail closed if signature verification fails instead of falling back to loopback bypass.
|
|
16
|
+
*
|
|
17
|
+
* @param {{ headers?: Record<string, string | string[] | undefined> }} req
|
|
18
|
+
* @returns {boolean}
|
|
19
|
+
*/
|
|
20
|
+
function hasWorkerHeaders(req) {
|
|
21
|
+
return Boolean(
|
|
22
|
+
getRequestHeader(req, "x-nr-connect-signature") ||
|
|
23
|
+
getRequestHeader(req, "x-nr-connect-user") ||
|
|
24
|
+
getRequestHeader(req, "x-nr-connect-allowed-pages") ||
|
|
25
|
+
getRequestHeader(req, "x-nr-connect-timestamp")
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function isWidgetAllowedInConfig(
|
|
30
|
+
widget,
|
|
31
|
+
permittedPageIds,
|
|
32
|
+
permittedGroupIds,
|
|
33
|
+
permittedDashboardIds
|
|
34
|
+
) {
|
|
35
|
+
if (!widget || typeof widget !== "object") return false;
|
|
36
|
+
const groupId = widget.props?.group || widget.group || "";
|
|
37
|
+
const pageId = widget.props?.page || widget.page || "";
|
|
38
|
+
const uiId = widget.props?.ui || widget.ui || "";
|
|
39
|
+
|
|
40
|
+
if (groupId) {
|
|
41
|
+
return permittedGroupIds.has(groupId);
|
|
42
|
+
}
|
|
43
|
+
if (pageId) {
|
|
44
|
+
return permittedPageIds.has(pageId);
|
|
45
|
+
}
|
|
46
|
+
if (uiId) {
|
|
47
|
+
return permittedDashboardIds.size === 0 || permittedDashboardIds.has(uiId);
|
|
48
|
+
}
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function sanitizeMetaWysiwyg(meta, permittedPageIds) {
|
|
53
|
+
if (!meta || typeof meta !== "object") return meta;
|
|
54
|
+
const wysiwyg = meta.wysiwyg;
|
|
55
|
+
if (!wysiwyg || typeof wysiwyg !== "object") return meta;
|
|
56
|
+
if (!wysiwyg.page || permittedPageIds.has(wysiwyg.page)) return meta;
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
...meta,
|
|
60
|
+
wysiwyg: {
|
|
61
|
+
...wysiwyg,
|
|
62
|
+
enabled: false,
|
|
63
|
+
page: null,
|
|
64
|
+
editKey: null,
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Strip unpermitted pages, groups, and widgets from either a FlowFuse Dashboard 2.0
|
|
71
|
+
* Socket.IO `ui-config` payload (object maps) or an array-shaped `/_setup` payload.
|
|
72
|
+
* Never mutates the input payload.
|
|
73
|
+
*
|
|
74
|
+
* @param {object} payload
|
|
75
|
+
* @param {string[]} allowedPages
|
|
76
|
+
* @returns {object}
|
|
77
|
+
*/
|
|
78
|
+
function filterDashboardSetupPayload(payload, allowedPages) {
|
|
79
|
+
if (!payload || typeof payload !== "object") return payload;
|
|
80
|
+
if (!Array.isArray(allowedPages) || allowedPages.includes("*")) {
|
|
81
|
+
return payload;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const allowedPageSet = new Set(allowedPages);
|
|
85
|
+
const permittedDashboardIds = new Set();
|
|
86
|
+
if (isPlainObject(payload.dashboards)) {
|
|
87
|
+
for (const key of Object.keys(payload.dashboards)) {
|
|
88
|
+
permittedDashboardIds.add(key);
|
|
89
|
+
}
|
|
90
|
+
} else if (Array.isArray(payload.dashboards)) {
|
|
91
|
+
for (const dash of payload.dashboards) {
|
|
92
|
+
if (dash?.id) permittedDashboardIds.add(dash.id);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (Array.isArray(payload.pages)) {
|
|
97
|
+
const filteredPages = payload.pages.filter(
|
|
98
|
+
(page) => page && allowedPageSet.has(page.id)
|
|
99
|
+
);
|
|
100
|
+
const permittedPageIds = new Set(filteredPages.map((page) => page.id));
|
|
101
|
+
|
|
102
|
+
let filteredGroups = payload.groups;
|
|
103
|
+
const permittedGroupIds = new Set();
|
|
104
|
+
if (Array.isArray(payload.groups)) {
|
|
105
|
+
filteredGroups = payload.groups.filter(
|
|
106
|
+
(group) => group && permittedPageIds.has(group.page)
|
|
107
|
+
);
|
|
108
|
+
for (const group of filteredGroups) {
|
|
109
|
+
if (group?.id) permittedGroupIds.add(group.id);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
let filteredWidgets = payload.widgets;
|
|
114
|
+
if (Array.isArray(payload.widgets)) {
|
|
115
|
+
filteredWidgets = payload.widgets.filter((widget) =>
|
|
116
|
+
isWidgetAllowedInConfig(
|
|
117
|
+
widget,
|
|
118
|
+
permittedPageIds,
|
|
119
|
+
permittedGroupIds,
|
|
120
|
+
permittedDashboardIds
|
|
121
|
+
)
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return {
|
|
126
|
+
...payload,
|
|
127
|
+
...(payload.meta
|
|
128
|
+
? { meta: sanitizeMetaWysiwyg(payload.meta, permittedPageIds) }
|
|
129
|
+
: {}),
|
|
130
|
+
pages: filteredPages,
|
|
131
|
+
...(Array.isArray(payload.groups) ? { groups: filteredGroups } : {}),
|
|
132
|
+
...(Array.isArray(payload.widgets) ? { widgets: filteredWidgets } : {}),
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (isPlainObject(payload.pages)) {
|
|
137
|
+
const filteredPages = {};
|
|
138
|
+
const permittedPageIds = new Set();
|
|
139
|
+
for (const [key, page] of Object.entries(payload.pages)) {
|
|
140
|
+
const pageId = page?.id || key;
|
|
141
|
+
if (allowedPageSet.has(pageId) || allowedPageSet.has(key)) {
|
|
142
|
+
filteredPages[key] = page;
|
|
143
|
+
permittedPageIds.add(key);
|
|
144
|
+
if (page?.id) permittedPageIds.add(page.id);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
let filteredGroups = payload.groups;
|
|
149
|
+
const permittedGroupIds = new Set();
|
|
150
|
+
if (isPlainObject(payload.groups)) {
|
|
151
|
+
filteredGroups = {};
|
|
152
|
+
for (const [key, group] of Object.entries(payload.groups)) {
|
|
153
|
+
if (group && permittedPageIds.has(group.page)) {
|
|
154
|
+
filteredGroups[key] = group;
|
|
155
|
+
permittedGroupIds.add(key);
|
|
156
|
+
if (group.id) permittedGroupIds.add(group.id);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
let filteredWidgets = payload.widgets;
|
|
162
|
+
if (isPlainObject(payload.widgets)) {
|
|
163
|
+
filteredWidgets = {};
|
|
164
|
+
for (const [key, widget] of Object.entries(payload.widgets)) {
|
|
165
|
+
if (
|
|
166
|
+
isWidgetAllowedInConfig(
|
|
167
|
+
widget,
|
|
168
|
+
permittedPageIds,
|
|
169
|
+
permittedGroupIds,
|
|
170
|
+
permittedDashboardIds
|
|
171
|
+
)
|
|
172
|
+
) {
|
|
173
|
+
filteredWidgets[key] = widget;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return {
|
|
179
|
+
...payload,
|
|
180
|
+
...(payload.meta
|
|
181
|
+
? { meta: sanitizeMetaWysiwyg(payload.meta, permittedPageIds) }
|
|
182
|
+
: {}),
|
|
183
|
+
pages: filteredPages,
|
|
184
|
+
...(isPlainObject(payload.groups) ? { groups: filteredGroups } : {}),
|
|
185
|
+
...(isPlainObject(payload.widgets) ? { widgets: filteredWidgets } : {}),
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
return payload;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function getUiBaseNodes(RED, explicitBaseId) {
|
|
193
|
+
const seen = new Set();
|
|
194
|
+
const bases = [];
|
|
195
|
+
|
|
196
|
+
const addBaseById = (id) => {
|
|
197
|
+
if (!id || seen.has(id) || typeof RED?.nodes?.getNode !== "function") return;
|
|
198
|
+
const live = RED.nodes.getNode(id);
|
|
199
|
+
if (live && !seen.has(live)) {
|
|
200
|
+
seen.add(id);
|
|
201
|
+
seen.add(live);
|
|
202
|
+
bases.push(live);
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
if (explicitBaseId) {
|
|
207
|
+
addBaseById(explicitBaseId);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if (typeof RED?.nodes?.eachNode === "function") {
|
|
211
|
+
try {
|
|
212
|
+
RED.nodes.eachNode((n) => {
|
|
213
|
+
if (n && n.type === "ui-base") {
|
|
214
|
+
addBaseById(n.id);
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
} catch {
|
|
218
|
+
// RED.nodes.eachNode throws during early plugin load before activeFlowConfig is initialized.
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
return bases;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function findPageIdForGroup(RED, bases, groupId) {
|
|
226
|
+
for (const base of bases) {
|
|
227
|
+
const group = base?.ui?.groups?.get?.(groupId);
|
|
228
|
+
if (group?.page) return group.page;
|
|
229
|
+
}
|
|
230
|
+
const groupNode = RED?.nodes?.getNode?.(groupId);
|
|
231
|
+
if (groupNode?.page) return groupNode.page;
|
|
232
|
+
|
|
233
|
+
let resolved = null;
|
|
234
|
+
if (typeof RED?.nodes?.eachNode === "function") {
|
|
235
|
+
try {
|
|
236
|
+
RED.nodes.eachNode((n) => {
|
|
237
|
+
if (!resolved && n && n.id === groupId && n.type === "ui-group") {
|
|
238
|
+
resolved = n.page || null;
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
} catch {
|
|
242
|
+
// Ignore uninitialized flow config
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
return resolved;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Determine whether a Dashboard 2.0 widget, group, or page ID is permitted for `allowedPages`.
|
|
250
|
+
* Fails closed when `itemId` cannot be verified against a permitted page or UI-wide node.
|
|
251
|
+
*
|
|
252
|
+
* @param {object} RED
|
|
253
|
+
* @param {string} itemId
|
|
254
|
+
* @param {string[]} allowedPages
|
|
255
|
+
* @returns {boolean}
|
|
256
|
+
*/
|
|
257
|
+
function isDashboardItemAllowed(RED, itemId, allowedPages) {
|
|
258
|
+
if (!Array.isArray(allowedPages) || allowedPages.includes("*")) {
|
|
259
|
+
return true;
|
|
260
|
+
}
|
|
261
|
+
if (typeof itemId !== "string" || !itemId) {
|
|
262
|
+
return false;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const allowedSet = new Set(allowedPages);
|
|
266
|
+
const bases = getUiBaseNodes(RED);
|
|
267
|
+
|
|
268
|
+
for (const base of bases) {
|
|
269
|
+
if (base?.ui?.pages?.has?.(itemId)) {
|
|
270
|
+
return allowedSet.has(itemId);
|
|
271
|
+
}
|
|
272
|
+
if (base?.ui?.groups?.has?.(itemId)) {
|
|
273
|
+
const group = base.ui.groups.get(itemId);
|
|
274
|
+
return Boolean(group && allowedSet.has(group.page));
|
|
275
|
+
}
|
|
276
|
+
if (base?.ui?.widgets?.has?.(itemId)) {
|
|
277
|
+
const widget = base.ui.widgets.get(itemId);
|
|
278
|
+
const groupId = widget?.props?.group || widget?.group || "";
|
|
279
|
+
const pageId = widget?.props?.page || widget?.page || "";
|
|
280
|
+
const uiId = widget?.props?.ui || widget?.ui || "";
|
|
281
|
+
if (groupId) {
|
|
282
|
+
const page = findPageIdForGroup(RED, bases, groupId);
|
|
283
|
+
return Boolean(page && allowedSet.has(page));
|
|
284
|
+
}
|
|
285
|
+
if (pageId) {
|
|
286
|
+
return allowedSet.has(pageId);
|
|
287
|
+
}
|
|
288
|
+
if (uiId) {
|
|
289
|
+
return true;
|
|
290
|
+
}
|
|
291
|
+
return false;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
const node = RED?.nodes?.getNode?.(itemId);
|
|
296
|
+
if (node) {
|
|
297
|
+
if (node.type === "ui-page") {
|
|
298
|
+
return allowedSet.has(node.id);
|
|
299
|
+
}
|
|
300
|
+
if (node.type === "ui-group") {
|
|
301
|
+
return Boolean(node.page && allowedSet.has(node.page));
|
|
302
|
+
}
|
|
303
|
+
const groupId = node.group || "";
|
|
304
|
+
const pageId = node.page || "";
|
|
305
|
+
const uiId = node.ui || "";
|
|
306
|
+
if (groupId) {
|
|
307
|
+
const page = findPageIdForGroup(RED, bases, groupId);
|
|
308
|
+
return Boolean(page && allowedSet.has(page));
|
|
309
|
+
}
|
|
310
|
+
if (pageId) {
|
|
311
|
+
return allowedSet.has(pageId);
|
|
312
|
+
}
|
|
313
|
+
if (uiId) {
|
|
314
|
+
return true;
|
|
315
|
+
}
|
|
316
|
+
return false;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// Dashboard 2.0 BaselineLayout creates a built-in client notification widget with id "ui-notification".
|
|
320
|
+
if (itemId === "ui-notification") {
|
|
321
|
+
return true;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
return false;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
function isSocketRestricted(socket) {
|
|
328
|
+
return (
|
|
329
|
+
Array.isArray(socket?.nrConnectAllowedPages) &&
|
|
330
|
+
!socket.nrConnectAllowedPages.includes("*")
|
|
331
|
+
);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
function sanitizeUiControlSection(RED, section, allowedPages) {
|
|
335
|
+
if (!isPlainObject(section)) return section;
|
|
336
|
+
const out = {};
|
|
337
|
+
for (const [verb, ids] of Object.entries(section)) {
|
|
338
|
+
if (Array.isArray(ids)) {
|
|
339
|
+
out[verb] = ids.filter((id) =>
|
|
340
|
+
isDashboardItemAllowed(RED, String(id), allowedPages)
|
|
341
|
+
);
|
|
342
|
+
} else {
|
|
343
|
+
out[verb] = ids;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
return out;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
function isPageNameForbidden(RED, pageName, allowedPages) {
|
|
350
|
+
if (typeof pageName !== "string" || !pageName) return false;
|
|
351
|
+
if (pageName === "+1" || pageName === "-1") return false;
|
|
352
|
+
|
|
353
|
+
const allowedSet = new Set(allowedPages);
|
|
354
|
+
const bases = getUiBaseNodes(RED);
|
|
355
|
+
for (const base of bases) {
|
|
356
|
+
if (!base?.ui?.pages) continue;
|
|
357
|
+
for (const [id, page] of base.ui.pages.entries()) {
|
|
358
|
+
if (page?.name === pageName) {
|
|
359
|
+
return !allowedSet.has(page.id || id);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
return false;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
function sanitizeUiControlMessage(RED, msg, allowedPages) {
|
|
367
|
+
if (!isPlainObject(msg) || !isPlainObject(msg.payload)) return msg;
|
|
368
|
+
const payload = msg.payload;
|
|
369
|
+
const nextPayload = { ...payload };
|
|
370
|
+
let modified = false;
|
|
371
|
+
|
|
372
|
+
if (
|
|
373
|
+
typeof payload.page === "string" &&
|
|
374
|
+
isPageNameForbidden(RED, payload.page, allowedPages)
|
|
375
|
+
) {
|
|
376
|
+
delete nextPayload.page;
|
|
377
|
+
delete nextPayload.tab;
|
|
378
|
+
modified = true;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
if (isPlainObject(payload.pages)) {
|
|
382
|
+
nextPayload.pages = sanitizeUiControlSection(RED, payload.pages, allowedPages);
|
|
383
|
+
modified = true;
|
|
384
|
+
}
|
|
385
|
+
if (isPlainObject(payload.groups)) {
|
|
386
|
+
nextPayload.groups = sanitizeUiControlSection(RED, payload.groups, allowedPages);
|
|
387
|
+
modified = true;
|
|
388
|
+
}
|
|
389
|
+
if (isPlainObject(payload.widgets)) {
|
|
390
|
+
nextPayload.widgets = sanitizeUiControlSection(
|
|
391
|
+
RED,
|
|
392
|
+
payload.widgets,
|
|
393
|
+
allowedPages
|
|
394
|
+
);
|
|
395
|
+
modified = true;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
return modified ? { ...msg, payload: nextPayload } : msg;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* Wrap `socket.emit` idempotently so Dashboard 2.0 `ui-config` and widget/control broadcasts
|
|
403
|
+
* respect the socket's `nrConnectAllowedPages`.
|
|
404
|
+
*
|
|
405
|
+
* @param {object} socket
|
|
406
|
+
* @param {object} RED
|
|
407
|
+
*/
|
|
408
|
+
function wrapDashboardSocketEmit(socket, RED) {
|
|
409
|
+
if (!socket || typeof socket.emit !== "function" || socket.__nrConnectEmitWrapped) {
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
socket.__nrConnectEmitWrapped = true;
|
|
414
|
+
const originalEmit = socket.emit.bind(socket);
|
|
415
|
+
|
|
416
|
+
socket.emit = function nrConnectFilteredEmit(event, ...args) {
|
|
417
|
+
if (!isSocketRestricted(socket)) {
|
|
418
|
+
return originalEmit(event, ...args);
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
if (event === "ui-config") {
|
|
422
|
+
const nextArgs = [...args];
|
|
423
|
+
if (nextArgs.length >= 2) {
|
|
424
|
+
nextArgs[1] = filterDashboardSetupPayload(
|
|
425
|
+
nextArgs[1],
|
|
426
|
+
socket.nrConnectAllowedPages
|
|
427
|
+
);
|
|
428
|
+
}
|
|
429
|
+
return originalEmit(event, ...nextArgs);
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
if (typeof event === "string") {
|
|
433
|
+
const sep = event.indexOf(":");
|
|
434
|
+
if (sep !== -1) {
|
|
435
|
+
const prefix = event.slice(0, sep + 1);
|
|
436
|
+
const itemId = event.slice(sep + 1);
|
|
437
|
+
|
|
438
|
+
if (
|
|
439
|
+
prefix === "msg-input:" ||
|
|
440
|
+
prefix === "widget-load:" ||
|
|
441
|
+
prefix === "widget-sync:"
|
|
442
|
+
) {
|
|
443
|
+
if (
|
|
444
|
+
!isDashboardItemAllowed(RED, itemId, socket.nrConnectAllowedPages)
|
|
445
|
+
) {
|
|
446
|
+
return true;
|
|
447
|
+
}
|
|
448
|
+
} else if (prefix === "ui-control:") {
|
|
449
|
+
const nextArgs = [...args];
|
|
450
|
+
if (nextArgs.length >= 1) {
|
|
451
|
+
nextArgs[0] = sanitizeUiControlMessage(
|
|
452
|
+
RED,
|
|
453
|
+
nextArgs[0],
|
|
454
|
+
socket.nrConnectAllowedPages
|
|
455
|
+
);
|
|
456
|
+
}
|
|
457
|
+
return originalEmit(event, ...nextArgs);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
return originalEmit(event, ...args);
|
|
463
|
+
};
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/**
|
|
467
|
+
* Authenticate a Dashboard 2.0 Socket.IO socket from its handshake request headers,
|
|
468
|
+
* populate `socket.nrConnectUser` / `socket.nrConnectAllowedPages` / `socketUserCache`,
|
|
469
|
+
* enforce Require secure access, and wrap `socket.emit`.
|
|
470
|
+
*/
|
|
471
|
+
function authenticateDashboardSocket(socket, getPluginConfig, deps) {
|
|
472
|
+
const {
|
|
473
|
+
RED,
|
|
474
|
+
socketUserCache,
|
|
475
|
+
verifyWorkerHeaders,
|
|
476
|
+
isLocalEscapeHatchActive,
|
|
477
|
+
logger,
|
|
478
|
+
} = deps;
|
|
479
|
+
|
|
480
|
+
if (!socket) return { ok: true };
|
|
481
|
+
if (socket.__nrConnectSocketAuthed) return { ok: true };
|
|
482
|
+
|
|
483
|
+
const config = typeof getPluginConfig === "function" ? getPluginConfig() : null;
|
|
484
|
+
if (!config || !config.sharedSecret) {
|
|
485
|
+
return { ok: true };
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
const req = socket.request;
|
|
489
|
+
const verified = verifyWorkerHeaders(req, config.sharedSecret);
|
|
490
|
+
|
|
491
|
+
if (verified) {
|
|
492
|
+
socket.__nrConnectSocketAuthed = true;
|
|
493
|
+
socket.nrConnectUser = verified.user;
|
|
494
|
+
socket.nrConnectAllowedPages = verified.allowedPages;
|
|
495
|
+
socket.handshake = socket.handshake || {};
|
|
496
|
+
socket.handshake.auth = socket.handshake.auth || {};
|
|
497
|
+
socket.handshake.auth.user = verified.user;
|
|
498
|
+
|
|
499
|
+
if (socketUserCache) {
|
|
500
|
+
socketUserCache.set(socket.id, verified.user);
|
|
501
|
+
socket.on?.("disconnect", () => {
|
|
502
|
+
socketUserCache.delete(socket.id);
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
wrapDashboardSocketEmit(socket, RED);
|
|
507
|
+
return { ok: true };
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
if (hasWorkerHeaders(req)) {
|
|
511
|
+
logger?.warn?.(
|
|
512
|
+
"[NR Connect] Rejected Socket.IO connection with invalid or expired signature"
|
|
513
|
+
);
|
|
514
|
+
return {
|
|
515
|
+
ok: false,
|
|
516
|
+
error: new Error("Forbidden: Invalid or expired NR Connect signature."),
|
|
517
|
+
};
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
if (
|
|
521
|
+
config.requireSecureAccess &&
|
|
522
|
+
!isLocalEscapeHatchActive(req, config.userDir)
|
|
523
|
+
) {
|
|
524
|
+
return {
|
|
525
|
+
ok: false,
|
|
526
|
+
error: new Error(
|
|
527
|
+
"Forbidden: Direct access disabled. Connect through NR Connect."
|
|
528
|
+
),
|
|
529
|
+
};
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
socket.__nrConnectSocketAuthed = true;
|
|
533
|
+
return { ok: true };
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
/**
|
|
537
|
+
* Attach Socket.IO auth & `ui-config` filtering middleware onto every Dashboard 2.0 `uiShared.ioServer`.
|
|
538
|
+
*/
|
|
539
|
+
function attachDashboardIoProtection(RED, getPluginConfig, deps, explicitBaseId) {
|
|
540
|
+
const bases = getUiBaseNodes(RED, explicitBaseId);
|
|
541
|
+
for (const baseNode of bases) {
|
|
542
|
+
const uiShared = baseNode?.uiShared;
|
|
543
|
+
const ioServer = uiShared?.ioServer;
|
|
544
|
+
if (!ioServer) continue;
|
|
545
|
+
|
|
546
|
+
if (!ioServer.__nrConnectProtected) {
|
|
547
|
+
ioServer.__nrConnectProtected = true;
|
|
548
|
+
if (typeof ioServer.use === "function") {
|
|
549
|
+
ioServer.use((socket, next) => {
|
|
550
|
+
const result = authenticateDashboardSocket(
|
|
551
|
+
socket,
|
|
552
|
+
getPluginConfig,
|
|
553
|
+
{ ...deps, RED }
|
|
554
|
+
);
|
|
555
|
+
if (!result.ok) {
|
|
556
|
+
return next(result.error);
|
|
557
|
+
}
|
|
558
|
+
return next();
|
|
559
|
+
});
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
const activeSockets = [];
|
|
564
|
+
if (uiShared?.connections && typeof uiShared.connections === "object") {
|
|
565
|
+
activeSockets.push(...Object.values(uiShared.connections));
|
|
566
|
+
}
|
|
567
|
+
if (ioServer.sockets?.sockets && typeof ioServer.sockets.sockets.values === "function") {
|
|
568
|
+
activeSockets.push(...ioServer.sockets.sockets.values());
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
for (const socket of activeSockets) {
|
|
572
|
+
if (socket && !socket.__nrConnectSocketAuthed) {
|
|
573
|
+
const result = authenticateDashboardSocket(socket, getPluginConfig, {
|
|
574
|
+
...deps,
|
|
575
|
+
RED,
|
|
576
|
+
});
|
|
577
|
+
if (!result.ok && typeof socket.disconnect === "function") {
|
|
578
|
+
socket.disconnect(true);
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
/**
|
|
586
|
+
* Build the `node-red-dashboard-2` plugin hooks object using Dashboard 2.0's actual hook contract.
|
|
587
|
+
*/
|
|
588
|
+
function createDashboardPluginHooks(deps) {
|
|
589
|
+
const { RED, getPluginConfig, socketUserCache } = deps;
|
|
590
|
+
|
|
591
|
+
const isConnPermittedForWidget = (conn, widgetId) => {
|
|
592
|
+
if (!isSocketRestricted(conn)) return true;
|
|
593
|
+
return isDashboardItemAllowed(RED, widgetId, conn.nrConnectAllowedPages);
|
|
594
|
+
};
|
|
595
|
+
|
|
596
|
+
return {
|
|
597
|
+
onSetup: (config) => {
|
|
598
|
+
attachDashboardIoProtection(RED, getPluginConfig, deps, config?.id);
|
|
599
|
+
return {};
|
|
600
|
+
},
|
|
601
|
+
|
|
602
|
+
onAddConnectionCredentials: (conn, item) => {
|
|
603
|
+
if (!item || typeof item !== "object") return item;
|
|
604
|
+
const user =
|
|
605
|
+
conn?.nrConnectUser ||
|
|
606
|
+
(conn?.id ? socketUserCache?.get?.(conn.id) : null);
|
|
607
|
+
if (user) {
|
|
608
|
+
item._client = {
|
|
609
|
+
...(item._client && typeof item._client === "object"
|
|
610
|
+
? item._client
|
|
611
|
+
: {}),
|
|
612
|
+
user,
|
|
613
|
+
};
|
|
614
|
+
}
|
|
615
|
+
return item;
|
|
616
|
+
},
|
|
617
|
+
|
|
618
|
+
onInput: (msg, send, done) => {
|
|
619
|
+
if (
|
|
620
|
+
msg?._client?.socketId &&
|
|
621
|
+
socketUserCache?.has?.(msg._client.socketId)
|
|
622
|
+
) {
|
|
623
|
+
msg._client.user = socketUserCache.get(msg._client.socketId);
|
|
624
|
+
}
|
|
625
|
+
if (typeof send === "function") {
|
|
626
|
+
send(msg);
|
|
627
|
+
if (typeof done === "function") done();
|
|
628
|
+
}
|
|
629
|
+
return msg;
|
|
630
|
+
},
|
|
631
|
+
|
|
632
|
+
onAction: (conn, id, msg) => {
|
|
633
|
+
if (!isConnPermittedForWidget(conn, id)) {
|
|
634
|
+
return null;
|
|
635
|
+
}
|
|
636
|
+
return msg;
|
|
637
|
+
},
|
|
638
|
+
|
|
639
|
+
onChange: (conn, id, msg) => {
|
|
640
|
+
if (!isConnPermittedForWidget(conn, id)) {
|
|
641
|
+
return null;
|
|
642
|
+
}
|
|
643
|
+
return msg;
|
|
644
|
+
},
|
|
645
|
+
|
|
646
|
+
onSend: (conn, id, msg) => {
|
|
647
|
+
if (!isConnPermittedForWidget(conn, id)) {
|
|
648
|
+
return null;
|
|
649
|
+
}
|
|
650
|
+
return msg;
|
|
651
|
+
},
|
|
652
|
+
|
|
653
|
+
onLoad: (conn, id, msg) => {
|
|
654
|
+
if (!isConnPermittedForWidget(conn, id)) {
|
|
655
|
+
return null;
|
|
656
|
+
}
|
|
657
|
+
return msg;
|
|
658
|
+
},
|
|
659
|
+
};
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
module.exports = {
|
|
663
|
+
hasWorkerHeaders,
|
|
664
|
+
filterDashboardSetupPayload,
|
|
665
|
+
isDashboardItemAllowed,
|
|
666
|
+
wrapDashboardSocketEmit,
|
|
667
|
+
authenticateDashboardSocket,
|
|
668
|
+
attachDashboardIoProtection,
|
|
669
|
+
createDashboardPluginHooks,
|
|
670
|
+
};
|