@nocobase/flow-engine 2.1.11 → 2.2.0-alpha.2
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/lib/JSRunner.d.ts +1 -0
- package/lib/JSRunner.js +110 -20
- package/lib/components/FlowContextSelector.js +24 -3
- package/lib/components/variables/VariableHybridInput.d.ts +8 -0
- package/lib/components/variables/VariableHybridInput.js +128 -12
- package/lib/components/variables/types.d.ts +8 -0
- package/lib/flowContext.d.ts +1 -1
- package/lib/flowContext.js +33 -7
- package/lib/flowI18n.js +3 -3
- package/lib/locale/en-US.json +1 -0
- package/lib/locale/index.d.ts +2 -0
- package/lib/locale/zh-CN.json +1 -0
- package/lib/resources/apiResource.js +2 -1
- package/lib/resources/baseRecordResource.js +6 -17
- package/lib/resources/multiRecordResource.js +13 -3
- package/lib/resources/singleRecordResource.js +7 -2
- package/lib/runjs-context/helpers.js +12 -5
- package/lib/utils/dataSourceDirty.d.ts +20 -0
- package/lib/utils/dataSourceDirty.js +139 -0
- package/lib/utils/dirtyAwareApiClient.d.ts +11 -0
- package/lib/utils/dirtyAwareApiClient.js +378 -0
- package/lib/utils/index.d.ts +1 -1
- package/lib/utils/index.js +11 -11
- package/lib/utils/openViewRouteState.d.ts +28 -0
- package/lib/utils/openViewRouteState.js +125 -0
- package/lib/utils/parsePathnameToViewParams.d.ts +3 -0
- package/lib/utils/parsePathnameToViewParams.js +18 -1
- package/lib/utils/resolveRunJSObjectValues.js +3 -2
- package/lib/utils/runjsModuleLoader.js +0 -30
- package/lib/views/ViewNavigation.js +5 -0
- package/package.json +4 -4
- package/src/JSRunner.ts +112 -25
- package/src/__tests__/JSRunner.test.ts +4 -5
- package/src/__tests__/flowContext.test.ts +88 -0
- package/src/__tests__/flowEngine.dataSourceDirty.test.ts +51 -0
- package/src/__tests__/flowI18n.test.ts +11 -0
- package/src/__tests__/runjsRuntimeFeatures.test.ts +15 -2
- package/src/components/FlowContextSelector.tsx +33 -2
- package/src/components/variables/VariableHybridInput.tsx +166 -9
- package/src/components/variables/__tests__/VariableHybridInput.test.tsx +178 -0
- package/src/components/variables/types.ts +8 -0
- package/src/flowContext.ts +43 -8
- package/src/flowI18n.ts +8 -3
- package/src/locale/en-US.json +1 -0
- package/src/locale/zh-CN.json +1 -0
- package/src/resources/apiResource.ts +2 -1
- package/src/resources/baseRecordResource.ts +6 -23
- package/src/resources/multiRecordResource.ts +13 -3
- package/src/resources/singleRecordResource.ts +6 -1
- package/src/runjs-context/helpers.ts +12 -6
- package/src/utils/__tests__/dirtyAwareApiClient.test.ts +392 -0
- package/src/utils/__tests__/openViewRouteState.test.ts +40 -0
- package/src/utils/__tests__/parsePathnameToViewParams.test.ts +36 -0
- package/src/utils/dataSourceDirty.ts +126 -0
- package/src/utils/dirtyAwareApiClient.ts +430 -0
- package/src/utils/index.ts +10 -9
- package/src/utils/openViewRouteState.ts +107 -0
- package/src/utils/parsePathnameToViewParams.ts +23 -1
- package/src/utils/resolveRunJSObjectValues.ts +5 -2
- package/src/utils/runjsModuleLoader.ts +0 -32
- package/src/views/ViewNavigation.ts +6 -1
- package/src/views/__tests__/ViewNavigation.test.ts +15 -0
- package/lib/utils/safeGlobals.d.ts +0 -28
- package/lib/utils/safeGlobals.js +0 -367
- package/src/utils/__tests__/runjsRequireAsyncAutoWhitelist.test.ts +0 -38
- package/src/utils/__tests__/safeGlobals.test.ts +0 -106
- package/src/utils/safeGlobals.ts +0 -406
|
@@ -7,6 +7,8 @@
|
|
|
7
7
|
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
+
import { decodeOpenViewRouteState, type OpenViewRouteState } from './openViewRouteState';
|
|
11
|
+
|
|
10
12
|
export interface ViewParam {
|
|
11
13
|
/** 视图唯一标识符,一般为某个 Model 实例的 uid */
|
|
12
14
|
viewUid: string;
|
|
@@ -16,6 +18,8 @@ export interface ViewParam {
|
|
|
16
18
|
filterByTk?: string | Record<string, string | number>;
|
|
17
19
|
/** source Id */
|
|
18
20
|
sourceId?: string;
|
|
21
|
+
/** RunJS ctx.openView runtime display overrides decoded from URL. */
|
|
22
|
+
openViewRouteState?: OpenViewRouteState;
|
|
19
23
|
}
|
|
20
24
|
|
|
21
25
|
export interface ParsePathnameToViewParamsOptions {
|
|
@@ -109,7 +113,25 @@ export const parsePathnameToViewParams = (
|
|
|
109
113
|
}
|
|
110
114
|
}
|
|
111
115
|
// 处理参数
|
|
112
|
-
else if (currentView
|
|
116
|
+
else if (currentView) {
|
|
117
|
+
if (segment === 'opts') {
|
|
118
|
+
if (i + 1 < segments.length) {
|
|
119
|
+
const routeState = decodeOpenViewRouteState(currentView.viewUid, segments[i + 1]);
|
|
120
|
+
if (routeState) {
|
|
121
|
+
currentView.openViewRouteState = routeState;
|
|
122
|
+
}
|
|
123
|
+
i += 2;
|
|
124
|
+
} else {
|
|
125
|
+
i++;
|
|
126
|
+
}
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (i + 1 >= segments.length) {
|
|
131
|
+
i++;
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
|
|
113
135
|
const rawValue = segments[i + 1];
|
|
114
136
|
// 尝试对路径段进行解码
|
|
115
137
|
let decoded: string = rawValue;
|
|
@@ -8,7 +8,6 @@
|
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
import { isRunJSValue, normalizeRunJSValue } from './runjsValue';
|
|
11
|
-
import { runjsWithSafeGlobals } from './safeGlobals';
|
|
12
11
|
|
|
13
12
|
/**
|
|
14
13
|
* Resolve an object's values, executing any RunJSValue entries via ctx.runjs.
|
|
@@ -29,7 +28,11 @@ export async function resolveRunJSObjectValues(ctx: unknown, raw: unknown): Prom
|
|
|
29
28
|
if (isRunJSValue(value)) {
|
|
30
29
|
const { code, version } = normalizeRunJSValue(value);
|
|
31
30
|
if (!code.trim()) continue;
|
|
32
|
-
const
|
|
31
|
+
const runjsCtx = ctx as
|
|
32
|
+
| { runjs?: (code: string, variables?: Record<string, any>, options?: Record<string, any>) => Promise<any> }
|
|
33
|
+
| undefined
|
|
34
|
+
| null;
|
|
35
|
+
const ret = await runjsCtx?.runjs?.(code, undefined, { version });
|
|
33
36
|
if (!ret?.success) {
|
|
34
37
|
throw new Error(`RunJS execution failed for "${key}"`);
|
|
35
38
|
}
|
|
@@ -9,7 +9,6 @@
|
|
|
9
9
|
|
|
10
10
|
import { setRunJSLibOverride } from '../runjsLibs';
|
|
11
11
|
import { resolveModuleUrl } from './resolveModuleUrl';
|
|
12
|
-
import { registerRunJSSafeDocumentGlobals, registerRunJSSafeWindowGlobals } from './safeGlobals';
|
|
13
12
|
|
|
14
13
|
/**
|
|
15
14
|
* RunJS 外部模块加载辅助(浏览器侧)。
|
|
@@ -38,26 +37,6 @@ type ParsedPackageSpecifier = {
|
|
|
38
37
|
subpath?: string;
|
|
39
38
|
};
|
|
40
39
|
|
|
41
|
-
function snapshotOwnKeys(obj: any): string[] {
|
|
42
|
-
try {
|
|
43
|
-
if (!obj || (typeof obj !== 'object' && typeof obj !== 'function')) return [];
|
|
44
|
-
return Object.getOwnPropertyNames(obj);
|
|
45
|
-
} catch (_) {
|
|
46
|
-
return [];
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
function diffAddedKeys(afterKeys: string[], beforeKeys: string[]): string[] {
|
|
51
|
-
if (!afterKeys.length) return [];
|
|
52
|
-
if (!beforeKeys.length) return [...afterKeys];
|
|
53
|
-
const beforeSet = new Set(beforeKeys);
|
|
54
|
-
const added: string[] = [];
|
|
55
|
-
for (const k of afterKeys) {
|
|
56
|
-
if (!beforeSet.has(k)) added.push(k);
|
|
57
|
-
}
|
|
58
|
-
return added;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
40
|
/**
|
|
62
41
|
* 使用全局 Promise 链实现“互斥锁”:
|
|
63
42
|
* - 锁存放在 `globalThis.__nocobaseRunjsModuleLoadLock`;
|
|
@@ -298,9 +277,6 @@ async function prefetchEsmModule(url: string, options?: { timeoutMs?: number }):
|
|
|
298
277
|
*/
|
|
299
278
|
export async function runjsRequireAsync(requirejs: RequireJsLike, url: string): Promise<any> {
|
|
300
279
|
return await withRunjsModuleLoadLock(async () => {
|
|
301
|
-
const beforeWinKeys = typeof window !== 'undefined' ? snapshotOwnKeys(window) : [];
|
|
302
|
-
const beforeDocKeys = typeof document !== 'undefined' ? snapshotOwnKeys(document) : [];
|
|
303
|
-
|
|
304
280
|
let result: any;
|
|
305
281
|
let error: any;
|
|
306
282
|
try {
|
|
@@ -319,14 +295,6 @@ export async function runjsRequireAsync(requirejs: RequireJsLike, url: string):
|
|
|
319
295
|
});
|
|
320
296
|
} catch (e) {
|
|
321
297
|
error = e;
|
|
322
|
-
} finally {
|
|
323
|
-
const afterWinKeys = typeof window !== 'undefined' ? snapshotOwnKeys(window) : [];
|
|
324
|
-
const afterDocKeys = typeof document !== 'undefined' ? snapshotOwnKeys(document) : [];
|
|
325
|
-
const addedWinKeys = diffAddedKeys(afterWinKeys, beforeWinKeys);
|
|
326
|
-
const addedDocKeys = diffAddedKeys(afterDocKeys, beforeDocKeys);
|
|
327
|
-
// Best-effort: allow RunJS safe window/document to access globals introduced by this module load.
|
|
328
|
-
registerRunJSSafeWindowGlobals(addedWinKeys);
|
|
329
|
-
registerRunJSSafeDocumentGlobals(addedDocKeys);
|
|
330
298
|
}
|
|
331
299
|
|
|
332
300
|
if (error) throw error;
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
|
|
10
10
|
import { FlowEngineContext } from '../flowContext';
|
|
11
11
|
import { define, observable } from '../reactive';
|
|
12
|
-
import { ViewParam as SharedViewParam } from '../utils';
|
|
12
|
+
import { encodeOpenViewRouteState, ViewParam as SharedViewParam } from '../utils';
|
|
13
13
|
|
|
14
14
|
type ViewParams = Omit<SharedViewParam, 'viewUid'> & { viewUid?: string };
|
|
15
15
|
|
|
@@ -79,6 +79,11 @@ export function generatePathnameFromViewParams(
|
|
|
79
79
|
// 添加视图 UID
|
|
80
80
|
segments.push(viewParam.viewUid);
|
|
81
81
|
|
|
82
|
+
const openViewRouteStateToken = encodeOpenViewRouteState(viewParam.viewUid, viewParam.openViewRouteState);
|
|
83
|
+
if (openViewRouteStateToken) {
|
|
84
|
+
segments.push('opts', openViewRouteStateToken);
|
|
85
|
+
}
|
|
86
|
+
|
|
82
87
|
// 添加参数
|
|
83
88
|
if (viewParam.tabUid) {
|
|
84
89
|
segments.push('tab', viewParam.tabUid);
|
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
+
import { encodeOpenViewRouteState } from '../../utils/openViewRouteState';
|
|
10
11
|
import { ViewNavigation, generatePathnameFromViewParams } from '../ViewNavigation';
|
|
11
12
|
|
|
12
13
|
describe('ViewNavigation', () => {
|
|
@@ -220,6 +221,20 @@ describe('generatePathnameFromViewParams', () => {
|
|
|
220
221
|
expect(generatePathnameFromViewParams([{ viewUid: 'xxx' }, { viewUid: 'yyy' }])).toBe('/admin/xxx/view/yyy');
|
|
221
222
|
});
|
|
222
223
|
|
|
224
|
+
it('should generate RunJS openView route state params after the matching view uid', () => {
|
|
225
|
+
const token = encodeOpenViewRouteState('yyy', { mode: 'dialog', size: 'large' });
|
|
226
|
+
if (!token) {
|
|
227
|
+
throw new Error('Expected openView route state token.');
|
|
228
|
+
}
|
|
229
|
+
const pathname = generatePathnameFromViewParams([
|
|
230
|
+
{ viewUid: 'xxx' },
|
|
231
|
+
{ viewUid: 'yyy', openViewRouteState: { mode: 'dialog', size: 'large' }, filterByTk: '1' },
|
|
232
|
+
]);
|
|
233
|
+
|
|
234
|
+
expect(token).toMatch(/^[A-Za-z]{8}$/);
|
|
235
|
+
expect(pathname).toBe(`/admin/xxx/view/yyy/opts/${token}/filterbytk/1`);
|
|
236
|
+
});
|
|
237
|
+
|
|
223
238
|
it('should generate complex path with all parameters', () => {
|
|
224
239
|
expect(
|
|
225
240
|
generatePathnameFromViewParams([
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This file is part of the NocoBase (R) project.
|
|
3
|
-
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
-
* Authors: NocoBase Team.
|
|
5
|
-
*
|
|
6
|
-
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
-
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
-
*/
|
|
9
|
-
export declare function registerRunJSSafeWindowGlobals(keys: Iterable<string> | null | undefined): void;
|
|
10
|
-
export declare function registerRunJSSafeDocumentGlobals(keys: Iterable<string> | null | undefined): void;
|
|
11
|
-
export declare function __resetRunJSSafeGlobalsRegistryForTests(): void;
|
|
12
|
-
export declare function createSafeWindow(extra?: Record<string, any>): Record<string, any>;
|
|
13
|
-
export declare function createSafeDocument(extra?: Record<string, any>): Record<string, any>;
|
|
14
|
-
export declare function createSafeNavigator(extra?: Record<string, any>): {};
|
|
15
|
-
/**
|
|
16
|
-
* Create a safe globals object for RunJS execution.
|
|
17
|
-
*
|
|
18
|
-
* - Always tries to provide `navigator`
|
|
19
|
-
* - Best-effort provides `window` and `document` in browser environments
|
|
20
|
-
* - Never throws (so callers can decide how to handle missing globals)
|
|
21
|
-
*/
|
|
22
|
-
export declare function createSafeRunJSGlobals(extraGlobals?: Record<string, any>): Record<string, any>;
|
|
23
|
-
/**
|
|
24
|
-
* Execute RunJS with safe globals (window/document/navigator).
|
|
25
|
-
*
|
|
26
|
-
* Keeps `this` binding by calling `ctx.runjs(...)` instead of passing bare function references.
|
|
27
|
-
*/
|
|
28
|
-
export declare function runjsWithSafeGlobals(ctx: unknown, code: string, options?: any, extraGlobals?: Record<string, any>): Promise<any>;
|
package/lib/utils/safeGlobals.js
DELETED
|
@@ -1,367 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This file is part of the NocoBase (R) project.
|
|
3
|
-
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
-
* Authors: NocoBase Team.
|
|
5
|
-
*
|
|
6
|
-
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
-
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
var __defProp = Object.defineProperty;
|
|
11
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
12
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
13
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
14
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
15
|
-
var __export = (target, all) => {
|
|
16
|
-
for (var name in all)
|
|
17
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
18
|
-
};
|
|
19
|
-
var __copyProps = (to, from, except, desc) => {
|
|
20
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
21
|
-
for (let key of __getOwnPropNames(from))
|
|
22
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
23
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
24
|
-
}
|
|
25
|
-
return to;
|
|
26
|
-
};
|
|
27
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
-
var safeGlobals_exports = {};
|
|
29
|
-
__export(safeGlobals_exports, {
|
|
30
|
-
__resetRunJSSafeGlobalsRegistryForTests: () => __resetRunJSSafeGlobalsRegistryForTests,
|
|
31
|
-
createSafeDocument: () => createSafeDocument,
|
|
32
|
-
createSafeNavigator: () => createSafeNavigator,
|
|
33
|
-
createSafeRunJSGlobals: () => createSafeRunJSGlobals,
|
|
34
|
-
createSafeWindow: () => createSafeWindow,
|
|
35
|
-
registerRunJSSafeDocumentGlobals: () => registerRunJSSafeDocumentGlobals,
|
|
36
|
-
registerRunJSSafeWindowGlobals: () => registerRunJSSafeWindowGlobals,
|
|
37
|
-
runjsWithSafeGlobals: () => runjsWithSafeGlobals
|
|
38
|
-
});
|
|
39
|
-
module.exports = __toCommonJS(safeGlobals_exports);
|
|
40
|
-
function getRunJSSafeGlobalsRegistry() {
|
|
41
|
-
var _a, _b;
|
|
42
|
-
const g = globalThis;
|
|
43
|
-
if (((_a = g.__nocobaseRunJSSafeGlobalsRegistry) == null ? void 0 : _a.windowAllow) && ((_b = g.__nocobaseRunJSSafeGlobalsRegistry) == null ? void 0 : _b.documentAllow)) {
|
|
44
|
-
return g.__nocobaseRunJSSafeGlobalsRegistry;
|
|
45
|
-
}
|
|
46
|
-
const reg = {
|
|
47
|
-
windowAllow: /* @__PURE__ */ new Set(),
|
|
48
|
-
documentAllow: /* @__PURE__ */ new Set()
|
|
49
|
-
};
|
|
50
|
-
g.__nocobaseRunJSSafeGlobalsRegistry = reg;
|
|
51
|
-
return reg;
|
|
52
|
-
}
|
|
53
|
-
__name(getRunJSSafeGlobalsRegistry, "getRunJSSafeGlobalsRegistry");
|
|
54
|
-
function registerRunJSSafeWindowGlobals(keys) {
|
|
55
|
-
if (!keys) return;
|
|
56
|
-
const reg = getRunJSSafeGlobalsRegistry();
|
|
57
|
-
for (const k of keys) {
|
|
58
|
-
if (typeof k !== "string") continue;
|
|
59
|
-
const key = k.trim();
|
|
60
|
-
if (!key) continue;
|
|
61
|
-
reg.windowAllow.add(key);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
__name(registerRunJSSafeWindowGlobals, "registerRunJSSafeWindowGlobals");
|
|
65
|
-
function registerRunJSSafeDocumentGlobals(keys) {
|
|
66
|
-
if (!keys) return;
|
|
67
|
-
const reg = getRunJSSafeGlobalsRegistry();
|
|
68
|
-
for (const k of keys) {
|
|
69
|
-
if (typeof k !== "string") continue;
|
|
70
|
-
const key = k.trim();
|
|
71
|
-
if (!key) continue;
|
|
72
|
-
reg.documentAllow.add(key);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
__name(registerRunJSSafeDocumentGlobals, "registerRunJSSafeDocumentGlobals");
|
|
76
|
-
function __resetRunJSSafeGlobalsRegistryForTests() {
|
|
77
|
-
var _a, _b, _c, _d;
|
|
78
|
-
const g = globalThis;
|
|
79
|
-
if (g.__nocobaseRunJSSafeGlobalsRegistry) {
|
|
80
|
-
try {
|
|
81
|
-
(_b = (_a = g.__nocobaseRunJSSafeGlobalsRegistry.windowAllow) == null ? void 0 : _a.clear) == null ? void 0 : _b.call(_a);
|
|
82
|
-
(_d = (_c = g.__nocobaseRunJSSafeGlobalsRegistry.documentAllow) == null ? void 0 : _c.clear) == null ? void 0 : _d.call(_c);
|
|
83
|
-
} catch {
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
__name(__resetRunJSSafeGlobalsRegistryForTests, "__resetRunJSSafeGlobalsRegistryForTests");
|
|
88
|
-
function isAllowedDynamicWindowKey(key) {
|
|
89
|
-
return getRunJSSafeGlobalsRegistry().windowAllow.has(key);
|
|
90
|
-
}
|
|
91
|
-
__name(isAllowedDynamicWindowKey, "isAllowedDynamicWindowKey");
|
|
92
|
-
function isAllowedDynamicDocumentKey(key) {
|
|
93
|
-
return getRunJSSafeGlobalsRegistry().documentAllow.has(key);
|
|
94
|
-
}
|
|
95
|
-
__name(isAllowedDynamicDocumentKey, "isAllowedDynamicDocumentKey");
|
|
96
|
-
function createSafeWindow(extra) {
|
|
97
|
-
const getSafeBaseHref = /* @__PURE__ */ __name(() => `${window.location.origin}${window.location.pathname}`, "getSafeBaseHref");
|
|
98
|
-
const safeOpen = /* @__PURE__ */ __name((url, target2, features) => {
|
|
99
|
-
const isSafeUrl = /* @__PURE__ */ __name((u) => {
|
|
100
|
-
try {
|
|
101
|
-
const parsed = new URL(u, getSafeBaseHref());
|
|
102
|
-
const protocol = parsed.protocol.toLowerCase();
|
|
103
|
-
if (protocol === "about:") return parsed.href === "about:blank";
|
|
104
|
-
return protocol === "http:" || protocol === "https:";
|
|
105
|
-
} catch {
|
|
106
|
-
return false;
|
|
107
|
-
}
|
|
108
|
-
}, "isSafeUrl");
|
|
109
|
-
if (!isSafeUrl(url)) {
|
|
110
|
-
throw new Error("Unsafe URL: window.open only allows http/https/about:blank.");
|
|
111
|
-
}
|
|
112
|
-
const sanitizedTarget = "_blank";
|
|
113
|
-
const enforceFeatures = /* @__PURE__ */ __name((f) => {
|
|
114
|
-
const set = /* @__PURE__ */ new Set();
|
|
115
|
-
if (f) {
|
|
116
|
-
f.split(",").map((s) => s.trim()).filter(Boolean).forEach((part) => {
|
|
117
|
-
const key = part.split("=")[0].trim().toLowerCase();
|
|
118
|
-
if (key !== "noopener" && key !== "noreferrer") set.add(part);
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
set.add("noopener");
|
|
122
|
-
set.add("noreferrer");
|
|
123
|
-
return Array.from(set).join(",");
|
|
124
|
-
}, "enforceFeatures");
|
|
125
|
-
const sanitizedFeatures = enforceFeatures(features);
|
|
126
|
-
const newWin = window.open.call(window, url, sanitizedTarget, sanitizedFeatures);
|
|
127
|
-
if (newWin && "opener" in newWin) {
|
|
128
|
-
try {
|
|
129
|
-
newWin.opener = null;
|
|
130
|
-
} catch {
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
return newWin;
|
|
134
|
-
}, "safeOpen");
|
|
135
|
-
const guardedNavigate = /* @__PURE__ */ __name((rawUrl, opts) => {
|
|
136
|
-
const parsed = new URL(rawUrl, getSafeBaseHref());
|
|
137
|
-
const protocol = parsed.protocol.toLowerCase();
|
|
138
|
-
const isAboutBlank = protocol === "about:" && parsed.href === "about:blank";
|
|
139
|
-
const isHttp = protocol === "http:" || protocol === "https:";
|
|
140
|
-
if (!isHttp && !isAboutBlank) {
|
|
141
|
-
throw new Error("Unsafe URL: only http/https/about:blank are allowed.");
|
|
142
|
-
}
|
|
143
|
-
if (isAboutBlank) {
|
|
144
|
-
return (opts == null ? void 0 : opts.replace) ? window.location.replace("about:blank") : window.location.assign("about:blank");
|
|
145
|
-
}
|
|
146
|
-
const sameOrigin = parsed.protocol === window.location.protocol && parsed.hostname === window.location.hostname && parsed.port === window.location.port;
|
|
147
|
-
if (sameOrigin) {
|
|
148
|
-
return (opts == null ? void 0 : opts.replace) ? window.location.replace(parsed.href) : window.location.assign(parsed.href);
|
|
149
|
-
}
|
|
150
|
-
const win = safeOpen(parsed.href);
|
|
151
|
-
if (!win) throw new Error("Popup blocked: cross-origin navigation is opened in a new tab.");
|
|
152
|
-
}, "guardedNavigate");
|
|
153
|
-
const safeLocation = new Proxy(
|
|
154
|
-
{},
|
|
155
|
-
{
|
|
156
|
-
get(_t, prop) {
|
|
157
|
-
switch (prop) {
|
|
158
|
-
case "origin":
|
|
159
|
-
return window.location.origin;
|
|
160
|
-
case "protocol":
|
|
161
|
-
return window.location.protocol;
|
|
162
|
-
case "host":
|
|
163
|
-
return window.location.host;
|
|
164
|
-
case "hostname":
|
|
165
|
-
return window.location.hostname;
|
|
166
|
-
case "port":
|
|
167
|
-
return window.location.port;
|
|
168
|
-
case "pathname":
|
|
169
|
-
return window.location.pathname;
|
|
170
|
-
case "assign":
|
|
171
|
-
return (u) => guardedNavigate(u, { replace: false });
|
|
172
|
-
case "replace":
|
|
173
|
-
return (u) => guardedNavigate(u, { replace: true });
|
|
174
|
-
case "reload":
|
|
175
|
-
return window.location.reload.bind(window.location);
|
|
176
|
-
case "href":
|
|
177
|
-
throw new Error("Reading location.href is not allowed.");
|
|
178
|
-
default:
|
|
179
|
-
throw new Error(`Access to location property "${prop}" is not allowed.`);
|
|
180
|
-
}
|
|
181
|
-
},
|
|
182
|
-
set(_t, prop, value) {
|
|
183
|
-
if (prop === "href") {
|
|
184
|
-
guardedNavigate(String(value), { replace: false });
|
|
185
|
-
return true;
|
|
186
|
-
}
|
|
187
|
-
throw new Error("Mutation on location is not allowed.");
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
);
|
|
191
|
-
const allowedGlobals = {
|
|
192
|
-
// 需绑定到原始 window,避免严格模式下触发 Illegal invocation
|
|
193
|
-
setTimeout: window.setTimeout.bind(window),
|
|
194
|
-
clearTimeout: window.clearTimeout.bind(window),
|
|
195
|
-
setInterval: window.setInterval.bind(window),
|
|
196
|
-
clearInterval: window.clearInterval.bind(window),
|
|
197
|
-
console,
|
|
198
|
-
Math,
|
|
199
|
-
Date,
|
|
200
|
-
FormData,
|
|
201
|
-
...typeof Blob !== "undefined" ? { Blob } : {},
|
|
202
|
-
...typeof URL !== "undefined" ? { URL } : {},
|
|
203
|
-
// 事件侦听仅绑定到真实 window,便于少量需要的全局监听
|
|
204
|
-
addEventListener: addEventListener.bind(window),
|
|
205
|
-
// 安全的 window.open 代理
|
|
206
|
-
open: safeOpen,
|
|
207
|
-
// 安全的 location 代理
|
|
208
|
-
location: safeLocation,
|
|
209
|
-
...extra || {}
|
|
210
|
-
};
|
|
211
|
-
const target = /* @__PURE__ */ Object.create(null);
|
|
212
|
-
return new Proxy(target, {
|
|
213
|
-
get(t, prop) {
|
|
214
|
-
if (typeof prop !== "string") {
|
|
215
|
-
return Reflect.get(t, prop);
|
|
216
|
-
}
|
|
217
|
-
if (prop in allowedGlobals) return allowedGlobals[prop];
|
|
218
|
-
if (Object.prototype.hasOwnProperty.call(t, prop)) return t[prop];
|
|
219
|
-
if (isAllowedDynamicWindowKey(prop)) {
|
|
220
|
-
const v = window[prop];
|
|
221
|
-
if (typeof v === "function") return v.bind(window);
|
|
222
|
-
return v;
|
|
223
|
-
}
|
|
224
|
-
throw new Error(`Access to global property "${prop}" is not allowed.`);
|
|
225
|
-
},
|
|
226
|
-
set(t, prop, value) {
|
|
227
|
-
if (typeof prop !== "string") {
|
|
228
|
-
Reflect.set(t, prop, value);
|
|
229
|
-
return true;
|
|
230
|
-
}
|
|
231
|
-
if (prop in allowedGlobals) {
|
|
232
|
-
throw new Error(`Mutation of global property "${prop}" is not allowed.`);
|
|
233
|
-
}
|
|
234
|
-
t[prop] = value;
|
|
235
|
-
return true;
|
|
236
|
-
},
|
|
237
|
-
has(t, prop) {
|
|
238
|
-
if (typeof prop !== "string") return Reflect.has(t, prop);
|
|
239
|
-
if (prop in allowedGlobals) return true;
|
|
240
|
-
if (Object.prototype.hasOwnProperty.call(t, prop)) return true;
|
|
241
|
-
if (isAllowedDynamicWindowKey(prop)) return true;
|
|
242
|
-
return false;
|
|
243
|
-
}
|
|
244
|
-
});
|
|
245
|
-
}
|
|
246
|
-
__name(createSafeWindow, "createSafeWindow");
|
|
247
|
-
function createSafeDocument(extra) {
|
|
248
|
-
const allowed = {
|
|
249
|
-
createElement: document.createElement.bind(document),
|
|
250
|
-
querySelector: document.querySelector.bind(document),
|
|
251
|
-
querySelectorAll: document.querySelectorAll.bind(document),
|
|
252
|
-
...extra || {}
|
|
253
|
-
};
|
|
254
|
-
const target = /* @__PURE__ */ Object.create(null);
|
|
255
|
-
return new Proxy(target, {
|
|
256
|
-
get(t, prop) {
|
|
257
|
-
if (typeof prop !== "string") {
|
|
258
|
-
return Reflect.get(t, prop);
|
|
259
|
-
}
|
|
260
|
-
if (prop in allowed) return allowed[prop];
|
|
261
|
-
if (Object.prototype.hasOwnProperty.call(t, prop)) return t[prop];
|
|
262
|
-
if (isAllowedDynamicDocumentKey(prop)) {
|
|
263
|
-
const v = document[prop];
|
|
264
|
-
if (typeof v === "function") return v.bind(document);
|
|
265
|
-
return v;
|
|
266
|
-
}
|
|
267
|
-
throw new Error(`Access to document property "${prop}" is not allowed.`);
|
|
268
|
-
},
|
|
269
|
-
set(t, prop, value) {
|
|
270
|
-
if (typeof prop !== "string") {
|
|
271
|
-
Reflect.set(t, prop, value);
|
|
272
|
-
return true;
|
|
273
|
-
}
|
|
274
|
-
if (prop in allowed) {
|
|
275
|
-
throw new Error(`Mutation of document property "${prop}" is not allowed.`);
|
|
276
|
-
}
|
|
277
|
-
t[prop] = value;
|
|
278
|
-
return true;
|
|
279
|
-
},
|
|
280
|
-
has(t, prop) {
|
|
281
|
-
if (typeof prop !== "string") return Reflect.has(t, prop);
|
|
282
|
-
if (prop in allowed) return true;
|
|
283
|
-
if (Object.prototype.hasOwnProperty.call(t, prop)) return true;
|
|
284
|
-
if (isAllowedDynamicDocumentKey(prop)) return true;
|
|
285
|
-
return false;
|
|
286
|
-
}
|
|
287
|
-
});
|
|
288
|
-
}
|
|
289
|
-
__name(createSafeDocument, "createSafeDocument");
|
|
290
|
-
function createSafeNavigator(extra) {
|
|
291
|
-
var _a;
|
|
292
|
-
const nav = typeof window !== "undefined" && window.navigator || void 0;
|
|
293
|
-
const clipboard = {};
|
|
294
|
-
const writeText = (_a = nav == null ? void 0 : nav.clipboard) == null ? void 0 : _a.writeText;
|
|
295
|
-
if (typeof writeText === "function") {
|
|
296
|
-
clipboard.writeText = writeText.bind(nav.clipboard);
|
|
297
|
-
}
|
|
298
|
-
const allowed = {
|
|
299
|
-
clipboard
|
|
300
|
-
};
|
|
301
|
-
Object.defineProperty(allowed, "onLine", {
|
|
302
|
-
get: /* @__PURE__ */ __name(() => !!(nav == null ? void 0 : nav.onLine), "get"),
|
|
303
|
-
enumerable: true,
|
|
304
|
-
configurable: false
|
|
305
|
-
});
|
|
306
|
-
Object.defineProperty(allowed, "language", {
|
|
307
|
-
get: /* @__PURE__ */ __name(() => nav == null ? void 0 : nav.language, "get"),
|
|
308
|
-
enumerable: true,
|
|
309
|
-
configurable: false
|
|
310
|
-
});
|
|
311
|
-
Object.defineProperty(allowed, "languages", {
|
|
312
|
-
get: /* @__PURE__ */ __name(() => (nav == null ? void 0 : nav.languages) ? [...nav.languages] : void 0, "get"),
|
|
313
|
-
enumerable: true,
|
|
314
|
-
configurable: false
|
|
315
|
-
});
|
|
316
|
-
Object.assign(allowed, extra || {});
|
|
317
|
-
return new Proxy(
|
|
318
|
-
{},
|
|
319
|
-
{
|
|
320
|
-
get(_t, prop) {
|
|
321
|
-
if (prop in allowed) return allowed[prop];
|
|
322
|
-
throw new Error(`Access to navigator property "${String(prop)}" is not allowed.`);
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
);
|
|
326
|
-
}
|
|
327
|
-
__name(createSafeNavigator, "createSafeNavigator");
|
|
328
|
-
function createSafeRunJSGlobals(extraGlobals) {
|
|
329
|
-
const globals = {};
|
|
330
|
-
try {
|
|
331
|
-
const navigator = createSafeNavigator();
|
|
332
|
-
globals.navigator = navigator;
|
|
333
|
-
try {
|
|
334
|
-
globals.window = createSafeWindow({ navigator });
|
|
335
|
-
} catch {
|
|
336
|
-
}
|
|
337
|
-
} catch {
|
|
338
|
-
}
|
|
339
|
-
try {
|
|
340
|
-
globals.document = createSafeDocument();
|
|
341
|
-
} catch {
|
|
342
|
-
}
|
|
343
|
-
return extraGlobals ? { ...globals, ...extraGlobals } : globals;
|
|
344
|
-
}
|
|
345
|
-
__name(createSafeRunJSGlobals, "createSafeRunJSGlobals");
|
|
346
|
-
async function runjsWithSafeGlobals(ctx, code, options, extraGlobals) {
|
|
347
|
-
if (!ctx || typeof ctx !== "object" && typeof ctx !== "function") return void 0;
|
|
348
|
-
const runjs = ctx.runjs;
|
|
349
|
-
if (typeof runjs !== "function") return void 0;
|
|
350
|
-
return ctx.runjs(
|
|
351
|
-
code,
|
|
352
|
-
createSafeRunJSGlobals(extraGlobals),
|
|
353
|
-
options
|
|
354
|
-
);
|
|
355
|
-
}
|
|
356
|
-
__name(runjsWithSafeGlobals, "runjsWithSafeGlobals");
|
|
357
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
358
|
-
0 && (module.exports = {
|
|
359
|
-
__resetRunJSSafeGlobalsRegistryForTests,
|
|
360
|
-
createSafeDocument,
|
|
361
|
-
createSafeNavigator,
|
|
362
|
-
createSafeRunJSGlobals,
|
|
363
|
-
createSafeWindow,
|
|
364
|
-
registerRunJSSafeDocumentGlobals,
|
|
365
|
-
registerRunJSSafeWindowGlobals,
|
|
366
|
-
runjsWithSafeGlobals
|
|
367
|
-
});
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This file is part of the NocoBase (R) project.
|
|
3
|
-
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
-
* Authors: NocoBase Team.
|
|
5
|
-
*
|
|
6
|
-
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
-
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import { beforeEach, describe, expect, it } from 'vitest';
|
|
11
|
-
import { runjsRequireAsync } from '../runjsModuleLoader';
|
|
12
|
-
import { __resetRunJSSafeGlobalsRegistryForTests, createSafeWindow } from '../safeGlobals';
|
|
13
|
-
|
|
14
|
-
beforeEach(() => {
|
|
15
|
-
__resetRunJSSafeGlobalsRegistryForTests();
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
describe('runjsRequireAsync auto whitelist', () => {
|
|
19
|
-
it('should allow safeWindow to access globals introduced during requireAsync', async () => {
|
|
20
|
-
const key = '__nb_require_async_added_global__';
|
|
21
|
-
delete (window as any)[key];
|
|
22
|
-
|
|
23
|
-
const safeWin: any = createSafeWindow();
|
|
24
|
-
expect(() => safeWin[key]).toThrow(/not allowed/);
|
|
25
|
-
|
|
26
|
-
const requirejs: any = (deps: string[], onLoad: (...args: any[]) => void) => {
|
|
27
|
-
// Simulate a remote library attaching itself to the real window.
|
|
28
|
-
(window as any)[key] = { ok: true };
|
|
29
|
-
onLoad(undefined);
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
await runjsRequireAsync(requirejs, 'https://example.com/fake-lib.js');
|
|
33
|
-
|
|
34
|
-
expect(safeWin[key]).toEqual({ ok: true });
|
|
35
|
-
|
|
36
|
-
delete (window as any)[key];
|
|
37
|
-
});
|
|
38
|
-
});
|