@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.
@@ -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
- // 不带有 file 的不需要输出日志,因为一般来说会使用 promiseSpawn 的返回值进行日志输出
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={}] - 可选的 spawn 配置项
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();
@@ -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
- // 受信任的一方 blocklet DIDs,这些组件可以获得额外的权限(如 --allow-worker, --allow-fs-read=*)
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
- // 缓存 Node.js 版本对应的权限选项
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: 前端加载了整个文件,所以 execAsync 必须在这里创建
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
- // 当我们处于调试模式的时候,那么自动跳过权限模型,否则无法正常工作。see: https://github.com/microsoft/vscode-js-debug/issues/2245#issuecomment-3045560337
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
- // 受信任的 blocklet 及其子组件跳过文件权限管理
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: vscode 开启调试模式的时候,需要携带某些权限
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: 目前我们非常多的应用都依赖使用 child_process 安装 sqlite,所以暂时放行。等 @blocklet/db ready 了,我们把限制再加上,@jianchao
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: 如果不这么做,会找不到 @arcblock/pm2.io,也无法使用 static server
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: 兼容 Mac OS 下的 Cursor 的安装路径
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
- // 这部分配置作为默认值,消除 helmet 默认配置
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
- // 以下配置可通过 service 管理界面配置
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, // 意思为是否移除 powered-by 头,所以默认值应该是 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
- // service 在开发阶段会依赖这个地址做热更新
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
- // 添加 domain 的别名,需要放行 .well-known/ping 接口
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
- // 以下三个域名都是 iconify 的服务
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
- // stripe 服务
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
- // stripe 服务
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 = [];
@@ -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
- // private-ip does not cover multicast ranges (CVE: GHSA-jm66-6qc7-hx83)
10
- // IPv4 multicast: 224.0.0.0/4 (224-239.x.x.x)
11
- // IPv6 multicast: ff00::/8
12
- function isMulticastIP(ip) {
13
- if (!ip) return false;
14
- const ipv4Match = ip.match(/^(\d{1,3})\./);
15
- if (ipv4Match) {
16
- const first = parseInt(ipv4Match[1], 10);
17
- return first >= 224 && first <= 239;
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
- return /^[Ff][Ff]/i.test(ip);
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
- return isPrivateIP(ip) || isMulticastIP(ip);
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
- // 允许的协议, 只允许 https
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
- // 允许的 host, 只能是当前站点
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
- // 检查referer是否来自当前站点
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: false }, (error, address) => {
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
- * 解析域名并验证是否为私有IP
67
- * @param {string} hostname - 要解析的域名
68
- * @returns {Promise<boolean>} - 如果解析到的IP不是私有IP则返回true,否则返回false
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
- // 使用 lookup 方法解析域名
77
- const address = await resolveDomain(hostname);
92
+ const addresses = await resolveDomain(hostname);
78
93
 
79
- // 检查是否为私有IP或组播IP,返回相反的结果
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
- * 解析URL为IP地址并验证是否为私有IP
89
- * @param {string} url - 要验证的URL
90
- * @returns {Promise<boolean>} - 如果IP不是私有IP则返回true,否则返回false
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
- // 如果是IP地址,直接验证
105
- if (isIP(hostname)) {
118
+ // if this is an IP address, validate directly
119
+ if (parseIP(hostname)) {
106
120
  return !isBlockedIP(hostname);
107
121
  }
108
122
 
109
- // 如果域名在跳过列表中,则直接返回true
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
- // 测试和开发环境不进行 DNS 解析验证
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
- // 如果是域名,进行DNS解析
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
  };