@kne/fastify-task 3.0.0-alpha.5 → 3.0.0-alpha.6
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/index.js +4 -35
- package/libs/controllers/main.js +1 -0
- package/libs/services/main.js +329 -226
- package/libs/services/statistics.js +27 -18
- package/libs/utils/cache-manager.js +162 -0
- package/libs/utils/config.js +326 -0
- package/libs/utils/errors.js +191 -0
- package/libs/utils/modules/task-executor.js +196 -0
- package/libs/utils/modules/task-processor.js +185 -0
- package/libs/utils/security-validator.js +203 -0
- package/package.json +2 -2
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
const crypto = require('node:crypto');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 安全验证工具类
|
|
6
|
+
* 用于路径验证、输入验证、签名验证等安全检查
|
|
7
|
+
*/
|
|
8
|
+
class SecurityValidator {
|
|
9
|
+
/**
|
|
10
|
+
* 验证脚本路径是否安全,防止路径遍历攻击
|
|
11
|
+
* @param {string} baseDir - 基础目录
|
|
12
|
+
* @param {string} requestedPath - 请求的路径
|
|
13
|
+
* @returns {string} 解析后的安全路径
|
|
14
|
+
* @throws {Error} 如果路径不安全
|
|
15
|
+
*/
|
|
16
|
+
static validateScriptPath(baseDir, requestedPath) {
|
|
17
|
+
if (!baseDir || !requestedPath) {
|
|
18
|
+
throw new Error('路径参数不能为空');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// 解析为绝对路径
|
|
22
|
+
const resolvedBaseDir = path.resolve(baseDir);
|
|
23
|
+
const resolvedPath = path.resolve(baseDir, requestedPath);
|
|
24
|
+
|
|
25
|
+
// 检查是否在基础目录内
|
|
26
|
+
if (!resolvedPath.startsWith(resolvedBaseDir)) {
|
|
27
|
+
throw new Error(`非法路径访问: ${requestedPath}`);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// 检查文件扩展名
|
|
31
|
+
const ext = path.extname(resolvedPath);
|
|
32
|
+
if (ext !== '.js') {
|
|
33
|
+
throw new Error(`只允许 .js 文件: ${requestedPath}`);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// 检查路径中是否包含可疑字符
|
|
37
|
+
const suspiciousChars = ['..', '~', '$'];
|
|
38
|
+
for (const char of suspiciousChars) {
|
|
39
|
+
if (requestedPath.includes(char)) {
|
|
40
|
+
throw new Error(`路径包含非法字符: ${char}`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return resolvedPath;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* 验证任务类型是否安全
|
|
49
|
+
* @param {string} taskType - 任务类型
|
|
50
|
+
* @param {Array} allowedTypes - 允许的任务类型列表
|
|
51
|
+
* @throws {Error} 如果任务类型不安全
|
|
52
|
+
*/
|
|
53
|
+
static validateTaskType(taskType, allowedTypes = []) {
|
|
54
|
+
if (!taskType || typeof taskType !== 'string') {
|
|
55
|
+
throw new Error('任务类型必须为非空字符串');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// 检查任务类型格式
|
|
59
|
+
if (!/^[a-zA-Z0-9_-]+$/.test(taskType)) {
|
|
60
|
+
throw new Error(`任务类型格式无效: ${taskType}`);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// 如果有允许列表,检查是否在列表中
|
|
64
|
+
if (allowedTypes.length > 0 && !allowedTypes.includes(taskType)) {
|
|
65
|
+
throw new Error(`不支持的任务类型: ${taskType}`);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return taskType;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* 验证数值参数
|
|
73
|
+
* @param {number} value - 要验证的值
|
|
74
|
+
* @param {Object} options - 验证选项
|
|
75
|
+
* @returns {number} 验证后的值
|
|
76
|
+
* @throws {Error} 如果验证失败
|
|
77
|
+
*/
|
|
78
|
+
static validateNumber(value, options = {}) {
|
|
79
|
+
const {
|
|
80
|
+
min = 0,
|
|
81
|
+
max = Number.MAX_SAFE_INTEGER,
|
|
82
|
+
integer = true,
|
|
83
|
+
required = true,
|
|
84
|
+
defaultValue = 0
|
|
85
|
+
} = options;
|
|
86
|
+
|
|
87
|
+
if (value === undefined || value === null) {
|
|
88
|
+
if (required) {
|
|
89
|
+
throw new Error('数值参数不能为空');
|
|
90
|
+
}
|
|
91
|
+
return defaultValue;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const numValue = Number(value);
|
|
95
|
+
|
|
96
|
+
if (isNaN(numValue)) {
|
|
97
|
+
throw new Error('参数必须为有效数字');
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (integer && !Number.isInteger(numValue)) {
|
|
101
|
+
throw new Error('参数必须为整数');
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (numValue < min || numValue > max) {
|
|
105
|
+
throw new Error(`参数必须在 ${min} 到 ${max} 之间`);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return numValue;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* 生成HMAC-SHA256签名
|
|
113
|
+
* @param {Object} params - 签名参数
|
|
114
|
+
* @returns {string} 签名结果
|
|
115
|
+
*/
|
|
116
|
+
static generateSignature({ secret, id, data }) {
|
|
117
|
+
if (!secret) {
|
|
118
|
+
throw new Error('签名密钥不能为空');
|
|
119
|
+
}
|
|
120
|
+
if (!id) {
|
|
121
|
+
throw new Error('签名ID不能为空');
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const dataToSign = `${id}|${typeof data === 'string' ? data : JSON.stringify(data)}`;
|
|
125
|
+
const hmac = crypto.createHmac('sha256', secret);
|
|
126
|
+
hmac.update(dataToSign);
|
|
127
|
+
return hmac.digest('hex');
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* 验证HMAC-SHA256签名
|
|
132
|
+
* @param {Object} params - 验证参数
|
|
133
|
+
* @returns {boolean} 验证结果
|
|
134
|
+
*/
|
|
135
|
+
static verifySignature({ secret, id, data, signature }) {
|
|
136
|
+
try {
|
|
137
|
+
if (!signature) {
|
|
138
|
+
return false;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (!secret) {
|
|
142
|
+
// 如果没有配置密钥,则跳过签名验证(向后兼容)
|
|
143
|
+
return true;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const expectedSignature = this.generateSignature({ secret, id, data });
|
|
147
|
+
return signature === expectedSignature;
|
|
148
|
+
} catch (error) {
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* 净化错误堆栈信息,防止敏感信息泄露
|
|
155
|
+
* @param {Error} error - 错误对象
|
|
156
|
+
* @param {string} cwd - 当前工作目录
|
|
157
|
+
* @returns {string} 净化后的堆栈信息
|
|
158
|
+
*/
|
|
159
|
+
static sanitizeStackTrace(error, cwd = process.cwd()) {
|
|
160
|
+
if (!error || !error.stack) {
|
|
161
|
+
return '';
|
|
162
|
+
}
|
|
163
|
+
return error.stack.replaceAll(cwd, '/server');
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* 验证JSON数据
|
|
168
|
+
* @param {string} jsonString - JSON字符串
|
|
169
|
+
* @param {*} defaultValue - 默认值
|
|
170
|
+
* @returns {*} 解析后的JSON对象
|
|
171
|
+
*/
|
|
172
|
+
static safeJSONParse(jsonString, defaultValue = null) {
|
|
173
|
+
try {
|
|
174
|
+
if (typeof jsonString === 'object') {
|
|
175
|
+
return jsonString;
|
|
176
|
+
}
|
|
177
|
+
return JSON.parse(jsonString);
|
|
178
|
+
} catch (error) {
|
|
179
|
+
return defaultValue;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* 验证并限制日志大小
|
|
185
|
+
* @param {Array} logs - 日志数组
|
|
186
|
+
* @param {number} maxSize - 最大日志数量
|
|
187
|
+
* @returns {Array} 限制后的日志数组
|
|
188
|
+
*/
|
|
189
|
+
static limitLogsSize(logs, maxSize = 100) {
|
|
190
|
+
if (!Array.isArray(logs)) {
|
|
191
|
+
return [];
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (logs.length <= maxSize) {
|
|
195
|
+
return logs;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// 保留最新的 maxSize 条日志
|
|
199
|
+
return logs.slice(-maxSize);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
module.exports = SecurityValidator;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kne/fastify-task",
|
|
3
|
-
"version": "3.0.0-alpha.
|
|
3
|
+
"version": "3.0.0-alpha.6",
|
|
4
4
|
"description": "一个 Fastify 插件,用于任务编排、追踪任务执行状态和结果。支持系统自动执行和手动执行两种模式,提供完整的任务生命周期管理",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -56,7 +56,7 @@
|
|
|
56
56
|
"sqlite3": "^5.1.7"
|
|
57
57
|
},
|
|
58
58
|
"dependencies": {
|
|
59
|
-
"@kne/fastify-statistics": "^0.1.0
|
|
59
|
+
"@kne/fastify-statistics": "^0.1.0",
|
|
60
60
|
"dayjs": "^1.11.20",
|
|
61
61
|
"fastify-cron": "^1.4.0",
|
|
62
62
|
"fs-extra": "^11.3.0"
|