@abtnode/util 1.17.12 → 1.17.13-beta-20260512-004004-69bacba8
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/blocklet-cache.js +13 -13
- package/lib/deep-clone.js +1 -2
- package/lib/did-document.js +1 -1
- package/lib/download-file.js +1 -1
- package/lib/ensure-docker-endpoint-healthy.js +2 -2
- package/lib/ensure-endpoint-healthy.js +5 -5
- package/lib/format-context.js +1 -1
- package/lib/fs.js +1 -1
- package/lib/gcp.js +2 -2
- package/lib/get-pm2-process-info.js +1 -1
- package/lib/get-token-from-req.js +1 -2
- package/lib/logo-middleware.js +3 -3
- package/lib/middlewares/ensure-locale.js +1 -1
- package/lib/notification-preview/highlight.js +6 -6
- package/lib/notification-preview/util.js +8 -9
- package/lib/pm2/pm2-start-or-reload.js +4 -4
- package/lib/pm2/setup-graceful-shutdown.js +42 -42
- package/lib/port.js +1 -1
- package/lib/promise-spawn.js +5 -7
- package/lib/safe-tar.js +83 -0
- package/lib/sanitize.js +2 -2
- package/lib/security.js +23 -23
- package/lib/ssrf-protector.js +52 -36
- package/lib/upload-component.js +81 -82
- package/lib/url-evaluation/check-accessible-browser.js +3 -3
- package/lib/url-evaluation/check-accessible-node.js +1 -1
- package/lib/url-evaluation/index.js +15 -15
- package/lib/user.js +4 -4
- package/lib/zip.js +2 -2
- package/package.json +22 -9
package/lib/promise-spawn.js
CHANGED
|
@@ -24,7 +24,6 @@ function retryFn(fn, retries) {
|
|
|
24
24
|
});
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
// 格式化时间戳函数
|
|
28
27
|
function getFormattedDate() {
|
|
29
28
|
const now = new Date();
|
|
30
29
|
return now.toISOString().replace('T', ' ').replace(/\..+/, '');
|
|
@@ -32,7 +31,7 @@ function getFormattedDate() {
|
|
|
32
31
|
|
|
33
32
|
function createSafeStream(file) {
|
|
34
33
|
if (!file) {
|
|
35
|
-
//
|
|
34
|
+
// without a file, no log output is needed; callers typically log using the promiseSpawn return value
|
|
36
35
|
return {
|
|
37
36
|
end: () => {},
|
|
38
37
|
write: () => {},
|
|
@@ -50,10 +49,10 @@ function createSafeStream(file) {
|
|
|
50
49
|
}
|
|
51
50
|
|
|
52
51
|
/**
|
|
53
|
-
*
|
|
54
|
-
* @param {string} commandString -
|
|
55
|
-
* @param {object} [options={}] -
|
|
56
|
-
* @returns {Promise<string>} -
|
|
52
|
+
* Generic command execution function that accepts a command string
|
|
53
|
+
* @param {string} commandString - the full command string to execute
|
|
54
|
+
* @param {object} [options={}] - optional spawn configuration options
|
|
55
|
+
* @returns {Promise<string>} - execution result
|
|
57
56
|
*/
|
|
58
57
|
function basePromiseSpawn(commandString, options = {}, timeout = 0, mute = false) {
|
|
59
58
|
return new Promise((resolve, reject) => {
|
|
@@ -127,7 +126,6 @@ function basePromiseSpawn(commandString, options = {}, timeout = 0, mute = false
|
|
|
127
126
|
});
|
|
128
127
|
|
|
129
128
|
if (timeout > 0) {
|
|
130
|
-
// 启用超时功能
|
|
131
129
|
timer = setTimeout(() => {
|
|
132
130
|
childProcess.stdout.destroy();
|
|
133
131
|
childProcess.stderr.destroy();
|
package/lib/safe-tar.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
const fs = require('fs-extra');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const tar = require('tar');
|
|
4
|
+
|
|
5
|
+
const DISALLOWED_ENTRY_TYPES = new Set(['SymbolicLink', 'Link']);
|
|
6
|
+
|
|
7
|
+
const isWindowsAbsolutePath = (entryPath) => /^[a-zA-Z]:[\\/]/.test(entryPath) || entryPath.startsWith('\\\\');
|
|
8
|
+
|
|
9
|
+
const isPathInside = (target, root) => {
|
|
10
|
+
const relative = path.relative(root, target);
|
|
11
|
+
return relative === '' || (!relative.startsWith('..') && !path.isAbsolute(relative));
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const assertNoExistingSymlinkParent = (target, root) => {
|
|
15
|
+
let current = path.dirname(target);
|
|
16
|
+
const resolvedRoot = path.resolve(root);
|
|
17
|
+
|
|
18
|
+
while (isPathInside(current, resolvedRoot) && current !== resolvedRoot) {
|
|
19
|
+
if (fs.existsSync(current) && fs.lstatSync(current).isSymbolicLink()) {
|
|
20
|
+
throw new Error(`Unsafe tar entry parent: ${path.relative(resolvedRoot, current)}`);
|
|
21
|
+
}
|
|
22
|
+
current = path.dirname(current);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const normalizeEntryPath = (entryPath) => {
|
|
27
|
+
if (!entryPath || entryPath.includes('\0')) {
|
|
28
|
+
throw new Error('Unsafe tar entry path');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (path.isAbsolute(entryPath) || isWindowsAbsolutePath(entryPath)) {
|
|
32
|
+
throw new Error(`Unsafe tar entry path: ${entryPath}`);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const normalized = entryPath.replace(/\\/g, '/');
|
|
36
|
+
if (normalized.split('/').includes('..')) {
|
|
37
|
+
throw new Error(`Unsafe tar entry path: ${entryPath}`);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return normalized;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const validateTarEntry = ({ entryPath, entry, cwd }) => {
|
|
44
|
+
if (DISALLOWED_ENTRY_TYPES.has(entry?.type)) {
|
|
45
|
+
throw new Error(`Unsafe tar entry type: ${entry.type}`);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const normalized = normalizeEntryPath(entryPath);
|
|
49
|
+
const resolvedCwd = path.resolve(cwd);
|
|
50
|
+
const target = path.resolve(resolvedCwd, normalized);
|
|
51
|
+
if (!isPathInside(target, resolvedCwd)) {
|
|
52
|
+
throw new Error(`Unsafe tar entry path: ${entryPath}`);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
assertNoExistingSymlinkParent(target, resolvedCwd);
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const createSafeTarExtractOptions = ({ cwd, strip = 0, ...options }) => {
|
|
59
|
+
const resolvedCwd = path.resolve(cwd);
|
|
60
|
+
fs.ensureDirSync(resolvedCwd);
|
|
61
|
+
return {
|
|
62
|
+
...options,
|
|
63
|
+
C: resolvedCwd,
|
|
64
|
+
strip,
|
|
65
|
+
preservePaths: false,
|
|
66
|
+
unlink: true,
|
|
67
|
+
filter: (entryPath, entry) => {
|
|
68
|
+
validateTarEntry({ entryPath, entry, cwd: resolvedCwd });
|
|
69
|
+
return typeof options.filter === 'function' ? options.filter(entryPath, entry) : true;
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
const createSafeTarExtractStream = (options) => tar.x(createSafeTarExtractOptions(options));
|
|
75
|
+
|
|
76
|
+
const safeTarExtract = ({ file, ...options }) => tar.x({ file, ...createSafeTarExtractOptions(options) });
|
|
77
|
+
|
|
78
|
+
module.exports = {
|
|
79
|
+
createSafeTarExtractOptions,
|
|
80
|
+
createSafeTarExtractStream,
|
|
81
|
+
safeTarExtract,
|
|
82
|
+
validateTarEntry,
|
|
83
|
+
};
|
package/lib/sanitize.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const { initSanitize } = require('@blocklet/xss');
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* Sanitize user input by removing any HTML tags from the content
|
|
5
5
|
* @param {*} content
|
|
6
6
|
* @returns
|
|
7
7
|
*/
|
|
@@ -23,7 +23,7 @@ const sanitizeTag = (content) => {
|
|
|
23
23
|
};
|
|
24
24
|
|
|
25
25
|
/**
|
|
26
|
-
*
|
|
26
|
+
* Escape HTML tags in the given content
|
|
27
27
|
* @param {*} content
|
|
28
28
|
* @returns
|
|
29
29
|
*/
|
package/lib/security.js
CHANGED
|
@@ -13,7 +13,7 @@ const { promisify } = require('util');
|
|
|
13
13
|
|
|
14
14
|
const cloneDeep = require('./deep-clone');
|
|
15
15
|
|
|
16
|
-
//
|
|
16
|
+
// trusted first-party blocklet DIDs; these components can receive extra permissions (such as --allow-worker, --allow-fs-read=*)
|
|
17
17
|
const TRUSTED_BLOCKLET_DIDS = [
|
|
18
18
|
'z8iZiDFg3vkkrPwsiba1TLXy3H9XHzFERsP8o', // pages-kit
|
|
19
19
|
'z8iZjMn7Hcyh93rKf8PqcSM94XnS8nRqSrPoP', // arcblock-metrics
|
|
@@ -85,7 +85,7 @@ function findExecutable(executable) {
|
|
|
85
85
|
}
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
-
//
|
|
88
|
+
// cache permission options by Node.js version
|
|
89
89
|
const nodeStableOptionCache = new Map();
|
|
90
90
|
|
|
91
91
|
/**
|
|
@@ -95,12 +95,12 @@ const nodeStableOptionCache = new Map();
|
|
|
95
95
|
* @return {Promise<string>}
|
|
96
96
|
*/
|
|
97
97
|
async function getNodeStableOption({ optionsKey, optionsValues }) {
|
|
98
|
-
// @note:
|
|
98
|
+
// @note: the frontend loads this whole file, so execAsync must be created here
|
|
99
99
|
const execAsync = promisify(exec);
|
|
100
100
|
const nodeVersion = process.version;
|
|
101
101
|
const key = `${nodeVersion}.${optionsKey}`;
|
|
102
102
|
|
|
103
|
-
//
|
|
103
|
+
// check cache
|
|
104
104
|
if (nodeStableOptionCache.has(key)) {
|
|
105
105
|
return nodeStableOptionCache.get(key);
|
|
106
106
|
}
|
|
@@ -113,7 +113,7 @@ async function getNodeStableOption({ optionsKey, optionsValues }) {
|
|
|
113
113
|
throw new Error(`Can not get permission option: ${optionsKey} for this Node.js version: ${nodeVersion}`);
|
|
114
114
|
}
|
|
115
115
|
|
|
116
|
-
//
|
|
116
|
+
// cache result
|
|
117
117
|
nodeStableOptionCache.set(key, permissionOption);
|
|
118
118
|
return permissionOption;
|
|
119
119
|
} catch (error) {
|
|
@@ -163,7 +163,7 @@ const getSecurityNodeOptions = async (blocklet, enableFileSystemIsolation = true
|
|
|
163
163
|
throw new Error('blocklet is not defined');
|
|
164
164
|
}
|
|
165
165
|
|
|
166
|
-
//
|
|
166
|
+
// In debug mode, automatically skip the permission model; otherwise debugging cannot work properly. See: https://github.com/microsoft/vscode-js-debug/issues/2245#issuecomment-3045560337
|
|
167
167
|
if (
|
|
168
168
|
nodeOptions.includes('ms-vscode.js-debug/src/bootloader.js') &&
|
|
169
169
|
(blocklet.mode === BLOCKLET_MODES.DEVELOPMENT ||
|
|
@@ -172,13 +172,13 @@ const getSecurityNodeOptions = async (blocklet, enableFileSystemIsolation = true
|
|
|
172
172
|
return nodeOptions;
|
|
173
173
|
}
|
|
174
174
|
|
|
175
|
-
//
|
|
175
|
+
// trusted blocklets and their child components skip file permission management
|
|
176
176
|
// Trusted blocklets and their children skip file permission management entirely
|
|
177
177
|
if (isTrustedBlocklet(blocklet)) {
|
|
178
178
|
return nodeOptions || '';
|
|
179
179
|
}
|
|
180
180
|
|
|
181
|
-
// @note:
|
|
181
|
+
// @note: VS Code debug mode requires certain permissions
|
|
182
182
|
const options = [nodeOptions].filter((x) => Boolean(x) && x !== 'undefined');
|
|
183
183
|
|
|
184
184
|
if (!canUseFileSystemIsolateApi() || !enableFileSystemIsolation) {
|
|
@@ -216,13 +216,13 @@ const getSecurityNodeOptions = async (blocklet, enableFileSystemIsolation = true
|
|
|
216
216
|
]
|
|
217
217
|
.filter(Boolean)
|
|
218
218
|
.map((dir) => `--allow-fs-write=${join(dir, '/*')}`),
|
|
219
|
-
// @note: sqlite3
|
|
219
|
+
// @note: sqlite3 needs this
|
|
220
220
|
'--allow-addons',
|
|
221
|
-
// FIXME:
|
|
221
|
+
// FIXME: many apps currently rely on child_process to install sqlite, so allow this temporarily; tighten the restriction once @blocklet/db is ready
|
|
222
222
|
'--allow-child-process'
|
|
223
223
|
);
|
|
224
224
|
|
|
225
|
-
// @note:
|
|
225
|
+
// @note: users should not be able to change runtime mode in production
|
|
226
226
|
if (
|
|
227
227
|
blocklet.mode === BLOCKLET_MODES.DEVELOPMENT ||
|
|
228
228
|
blocklet.environmentObj.BLOCKLET_MODE === BLOCKLET_MODES.DEVELOPMENT
|
|
@@ -236,7 +236,7 @@ const getSecurityNodeOptions = async (blocklet, enableFileSystemIsolation = true
|
|
|
236
236
|
blocklet.environmentObj.BLOCKLET_CACHE_DIR,
|
|
237
237
|
blocklet.environmentObj.BLOCKLET_APP_DATA_DIR, // allow all components to read blocklet data
|
|
238
238
|
dirname(dirname(blocklet.environmentObj.BLOCKLET_APP_DIR)), // allow all components to read source code
|
|
239
|
-
// @note:
|
|
239
|
+
// @note: without this, @arcblock/pm2.io cannot be found and static server cannot be used
|
|
240
240
|
pm2Path && dirname(dirname(dirname(dirname(pm2Path)))),
|
|
241
241
|
blockletCliPath,
|
|
242
242
|
meiliSearchPath,
|
|
@@ -245,7 +245,7 @@ const getSecurityNodeOptions = async (blocklet, enableFileSystemIsolation = true
|
|
|
245
245
|
process.cwd(),
|
|
246
246
|
tmpdir(),
|
|
247
247
|
join(homedir(), '.cursor-server'),
|
|
248
|
-
// @note:
|
|
248
|
+
// @note: compatible with Cursor installation paths on macOS
|
|
249
249
|
'/Applications/Cursor.app',
|
|
250
250
|
...pnpmGlobalPaths,
|
|
251
251
|
]
|
|
@@ -259,7 +259,7 @@ const getSecurityNodeOptions = async (blocklet, enableFileSystemIsolation = true
|
|
|
259
259
|
};
|
|
260
260
|
|
|
261
261
|
const DEFAULT_HELMET_CONFIG = {
|
|
262
|
-
//
|
|
262
|
+
// these defaults override helmet default configuration
|
|
263
263
|
crossOriginEmbedderPolicy: false,
|
|
264
264
|
crossOriginOpenerPolicy: false,
|
|
265
265
|
crossOriginResourcePolicy: false,
|
|
@@ -269,11 +269,11 @@ const DEFAULT_HELMET_CONFIG = {
|
|
|
269
269
|
xDnsPrefetchControl: false,
|
|
270
270
|
xDownloadOptions: false,
|
|
271
271
|
xPermittedCrossDomainPolicies: false,
|
|
272
|
-
//
|
|
272
|
+
// the following settings can be configured in the service management UI
|
|
273
273
|
contentSecurityPolicy: false,
|
|
274
274
|
referrerPolicy: false,
|
|
275
275
|
xFrameOptions: false,
|
|
276
|
-
xPoweredBy: true, //
|
|
276
|
+
xPoweredBy: true, // means whether to remove the powered-by header, so the default should be true
|
|
277
277
|
xXssProtection: false,
|
|
278
278
|
};
|
|
279
279
|
|
|
@@ -306,14 +306,14 @@ async function patchResponseHeader(rawConfig, { node, blocklet, trustedDomains =
|
|
|
306
306
|
'connect-src',
|
|
307
307
|
[
|
|
308
308
|
"'self'",
|
|
309
|
-
//
|
|
309
|
+
// services rely on this URL for hot reload during development
|
|
310
310
|
'ws://localhost:3040',
|
|
311
311
|
]
|
|
312
312
|
);
|
|
313
313
|
}
|
|
314
314
|
|
|
315
315
|
const nodeInfo = info || (await node.getNodeInfo({ useCache: true }));
|
|
316
|
-
//
|
|
316
|
+
// adding domain aliases requires allowing the .well-known/ping endpoint
|
|
317
317
|
let domainAliases = await node.getBlockletDomainAliases({ blocklet, nodeInfo });
|
|
318
318
|
domainAliases = (domainAliases || []).map((x) => x.value);
|
|
319
319
|
|
|
@@ -324,7 +324,7 @@ async function patchResponseHeader(rawConfig, { node, blocklet, trustedDomains =
|
|
|
324
324
|
"'self'",
|
|
325
325
|
...domainAliases.filter((x) => x).map((x) => `https://${x}/.well-known/ping`),
|
|
326
326
|
|
|
327
|
-
//
|
|
327
|
+
// the following three domains are Iconify services
|
|
328
328
|
'https://api.simplesvg.com',
|
|
329
329
|
'https://api.iconify.design',
|
|
330
330
|
'https://api.unisvg.com',
|
|
@@ -336,7 +336,7 @@ async function patchResponseHeader(rawConfig, { node, blocklet, trustedDomains =
|
|
|
336
336
|
'script-src',
|
|
337
337
|
[
|
|
338
338
|
"'self'",
|
|
339
|
-
//
|
|
339
|
+
// Stripe service
|
|
340
340
|
'https://js.stripe.com',
|
|
341
341
|
]
|
|
342
342
|
);
|
|
@@ -345,7 +345,7 @@ async function patchResponseHeader(rawConfig, { node, blocklet, trustedDomains =
|
|
|
345
345
|
'frame-src',
|
|
346
346
|
[
|
|
347
347
|
"'self'",
|
|
348
|
-
//
|
|
348
|
+
// Stripe service
|
|
349
349
|
'https://js.stripe.com',
|
|
350
350
|
...trustedDomains.filter(Boolean).map((x) => `https://${x}`),
|
|
351
351
|
]
|
|
@@ -367,14 +367,14 @@ async function patchResponseHeader(rawConfig, { node, blocklet, trustedDomains =
|
|
|
367
367
|
async function patchCors(rawConfig, { node, blocklet, info }) {
|
|
368
368
|
const config = cloneDeep(rawConfig);
|
|
369
369
|
if (config?.origin?.smart) {
|
|
370
|
-
//
|
|
370
|
+
// get all domain aliases for the current app itself
|
|
371
371
|
const result = [];
|
|
372
372
|
result.push(...(config.origin?.value || []));
|
|
373
373
|
const nodeInfo = info || (await node.getNodeInfo({ useCache: true }));
|
|
374
374
|
let domainAliases = await node.getBlockletDomainAliases({ blocklet, nodeInfo });
|
|
375
375
|
domainAliases = (domainAliases || []).map((x) => x.value);
|
|
376
376
|
result.push(...domainAliases.map((x) => withHttps(x)));
|
|
377
|
-
//
|
|
377
|
+
// get all domain aliases for all federated login sites
|
|
378
378
|
const federated = blocklet.settings.federated || {};
|
|
379
379
|
const sites = (federated?.sites || []).filter((x) => x?.isMaster !== false || x.status === 'approved');
|
|
380
380
|
const siteAlias = [];
|
package/lib/ssrf-protector.js
CHANGED
|
@@ -1,29 +1,45 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* SSRF Protector
|
|
3
3
|
*/
|
|
4
|
-
const isPrivateIP = require('private-ip');
|
|
5
|
-
const isIP = require('is-ip');
|
|
6
4
|
const dns = require('dns');
|
|
5
|
+
const ipaddr = require('ipaddr.js');
|
|
7
6
|
const { isDidDomainUrl } = require('./url-evaluation');
|
|
8
7
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
8
|
+
const BLOCKED_IP_RANGES = new Set([
|
|
9
|
+
'broadcast',
|
|
10
|
+
'carrierGradeNat',
|
|
11
|
+
'linkLocal',
|
|
12
|
+
'loopback',
|
|
13
|
+
'multicast',
|
|
14
|
+
'private',
|
|
15
|
+
'reserved',
|
|
16
|
+
'uniqueLocal',
|
|
17
|
+
'unspecified',
|
|
18
|
+
]);
|
|
19
|
+
|
|
20
|
+
function parseIP(ip) {
|
|
21
|
+
if (!ip || typeof ip !== 'string') {
|
|
22
|
+
return null;
|
|
18
23
|
}
|
|
19
|
-
|
|
24
|
+
|
|
25
|
+
const normalized = ip.startsWith('[') && ip.endsWith(']') ? ip.slice(1, -1) : ip;
|
|
26
|
+
if (!ipaddr.isValid(normalized)) {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const address = ipaddr.parse(normalized);
|
|
31
|
+
return address.kind() === 'ipv6' && address.isIPv4MappedAddress() ? address.toIPv4Address() : address;
|
|
20
32
|
}
|
|
21
33
|
|
|
22
34
|
function isBlockedIP(ip) {
|
|
23
|
-
|
|
35
|
+
const address = parseIP(ip);
|
|
36
|
+
if (!address) {
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
return BLOCKED_IP_RANGES.has(address.range());
|
|
24
40
|
}
|
|
25
41
|
|
|
26
|
-
//
|
|
42
|
+
// allowed protocols; only https is allowed
|
|
27
43
|
function isAllowedProtocol(protocol) {
|
|
28
44
|
if (!protocol) {
|
|
29
45
|
return false;
|
|
@@ -34,7 +50,7 @@ function isAllowedProtocol(protocol) {
|
|
|
34
50
|
return ['https'].includes(_protocol);
|
|
35
51
|
}
|
|
36
52
|
|
|
37
|
-
//
|
|
53
|
+
// allowed host; must be the current site
|
|
38
54
|
function isAllowedReferer(referer, host) {
|
|
39
55
|
if (!referer || !host) {
|
|
40
56
|
return false;
|
|
@@ -43,8 +59,8 @@ function isAllowedReferer(referer, host) {
|
|
|
43
59
|
try {
|
|
44
60
|
const refererUrl = new URL(referer);
|
|
45
61
|
|
|
46
|
-
//
|
|
47
|
-
return refererUrl.hostname === host || refererUrl.hostname === host.split(':')[0]; //
|
|
62
|
+
// check whether referer comes from the current site
|
|
63
|
+
return refererUrl.hostname === host || refererUrl.hostname === host.split(':')[0]; // handle host with port
|
|
48
64
|
} catch {
|
|
49
65
|
return false;
|
|
50
66
|
}
|
|
@@ -52,9 +68,9 @@ function isAllowedReferer(referer, host) {
|
|
|
52
68
|
|
|
53
69
|
const resolveDomain = (domain) => {
|
|
54
70
|
return new Promise((resolve, reject) => {
|
|
55
|
-
dns.lookup(domain, { all:
|
|
71
|
+
dns.lookup(domain, { all: true, verbatim: true }, (error, addresses) => {
|
|
56
72
|
if (!error) {
|
|
57
|
-
resolve(address);
|
|
73
|
+
resolve(addresses.map((x) => x.address));
|
|
58
74
|
} else {
|
|
59
75
|
reject(error);
|
|
60
76
|
}
|
|
@@ -63,9 +79,9 @@ const resolveDomain = (domain) => {
|
|
|
63
79
|
};
|
|
64
80
|
|
|
65
81
|
/**
|
|
66
|
-
*
|
|
67
|
-
* @param {string} hostname -
|
|
68
|
-
* @returns {Promise<boolean>} -
|
|
82
|
+
* Resolve hostname and verify it is not a private IP
|
|
83
|
+
* @param {string} hostname - hostname to resolve
|
|
84
|
+
* @returns {Promise<boolean>} - true if resolved IP is not private, false otherwise
|
|
69
85
|
*/
|
|
70
86
|
async function resolveAndValidateHostname(hostname) {
|
|
71
87
|
if (!hostname) {
|
|
@@ -73,21 +89,19 @@ async function resolveAndValidateHostname(hostname) {
|
|
|
73
89
|
}
|
|
74
90
|
|
|
75
91
|
try {
|
|
76
|
-
|
|
77
|
-
const address = await resolveDomain(hostname);
|
|
92
|
+
const addresses = await resolveDomain(hostname);
|
|
78
93
|
|
|
79
|
-
|
|
80
|
-
return !isBlockedIP(address);
|
|
94
|
+
return addresses.length > 0 && addresses.every((address) => !isBlockedIP(address));
|
|
81
95
|
} catch (error) {
|
|
82
|
-
// DNS
|
|
96
|
+
// reject access when DNS resolution fails
|
|
83
97
|
return false;
|
|
84
98
|
}
|
|
85
99
|
}
|
|
86
100
|
|
|
87
101
|
/**
|
|
88
|
-
*
|
|
89
|
-
* @param {string} url -
|
|
90
|
-
* @returns {Promise<boolean>} -
|
|
102
|
+
* Resolve URL to IP address and verify it is not private
|
|
103
|
+
* @param {string} url - URL to validate
|
|
104
|
+
* @returns {Promise<boolean>} - true if IP is not private, false otherwise
|
|
91
105
|
*/
|
|
92
106
|
async function isAllowedURL(url) {
|
|
93
107
|
if (!url) {
|
|
@@ -101,27 +115,27 @@ async function isAllowedURL(url) {
|
|
|
101
115
|
return false;
|
|
102
116
|
}
|
|
103
117
|
|
|
104
|
-
//
|
|
105
|
-
if (
|
|
118
|
+
// if this is an IP address, validate directly
|
|
119
|
+
if (parseIP(hostname)) {
|
|
106
120
|
return !isBlockedIP(hostname);
|
|
107
121
|
}
|
|
108
122
|
|
|
109
|
-
//
|
|
123
|
+
// if hostname is in the skip list, return true directly
|
|
110
124
|
if (isDidDomainUrl(hostname)) {
|
|
111
125
|
return true;
|
|
112
126
|
}
|
|
113
127
|
|
|
114
|
-
//
|
|
128
|
+
// skip DNS validation in test and development environments
|
|
115
129
|
if (process.env.NODE_ENV === 'test' || process.env.NODE_ENV === 'development') {
|
|
116
130
|
return true;
|
|
117
131
|
}
|
|
118
132
|
|
|
119
|
-
//
|
|
133
|
+
// if this is a hostname, perform DNS resolution
|
|
120
134
|
const result = await resolveAndValidateHostname(hostname);
|
|
121
135
|
|
|
122
136
|
return result;
|
|
123
137
|
} catch (error) {
|
|
124
|
-
// URL
|
|
138
|
+
// URL parsing failed
|
|
125
139
|
return false;
|
|
126
140
|
}
|
|
127
141
|
}
|
|
@@ -130,5 +144,7 @@ module.exports = {
|
|
|
130
144
|
isAllowedProtocol,
|
|
131
145
|
isAllowedReferer,
|
|
132
146
|
isAllowedURL,
|
|
147
|
+
isBlockedIP,
|
|
148
|
+
parseIP,
|
|
133
149
|
resolveAndValidateHostname,
|
|
134
150
|
};
|