@whyour/qinglong 2.22.0 → 2.23.0

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.
Files changed (150) hide show
  1. package/back/protos/cron.proto +4 -0
  2. package/docs/development/node-cron-migration.md +49 -0
  3. package/docs/development/task-startup.md +78 -0
  4. package/docs/releases/2.22.0.md +8 -0
  5. package/docs/releases/2.23.0.md +33 -0
  6. package/docs/script-history.md +53 -0
  7. package/package.json +9 -8
  8. package/scripts/benchmark-script-history.cjs +39 -0
  9. package/scripts/publish-static.sh +34 -0
  10. package/scripts/sync-static.sh +20 -0
  11. package/scripts/wait-for-mirror.sh +214 -0
  12. package/shell/api.sh +56 -70
  13. package/shell/node_path_cache.sh +10 -2
  14. package/shell/otask.sh +7 -9
  15. package/shell/preload/sitecustomize.js +6 -2
  16. package/shell/preload/sitecustomize.py +4 -2
  17. package/shell/share.sh +1 -1
  18. package/shell/task-timeout.sh +63 -0
  19. package/shell/task.sh +8 -6
  20. package/shell/update.sh +40 -95
  21. package/shell/upgrade.cjs +8 -0
  22. package/static/build/api/config.js +2 -6
  23. package/static/build/api/env.js +15 -14
  24. package/static/build/api/open.js +2 -1
  25. package/static/build/api/script.js +155 -9
  26. package/static/build/api/subscription.js +3 -6
  27. package/static/build/api/system.js +35 -3
  28. package/static/build/config/grpcCerts.js +89 -40
  29. package/static/build/config/subscription.js +19 -11
  30. package/static/build/config/util.js +6 -4
  31. package/static/build/loaders/express.js +2 -0
  32. package/static/build/loaders/initData.js +20 -30
  33. package/static/build/loaders/initTask.js +11 -1
  34. package/static/build/middlewares/uniqueConstraintError.js +15 -0
  35. package/static/build/protos/cron.js +63 -2
  36. package/static/build/schedule/addCron.js +45 -63
  37. package/static/build/schedule/client.js +19 -1
  38. package/static/build/schedule/delCron.js +1 -1
  39. package/static/build/schedule/setConcurrency.js +27 -0
  40. package/static/build/services/config.js +2 -3
  41. package/static/build/services/cron.js +36 -10
  42. package/static/build/services/env.js +1 -1
  43. package/static/build/services/grpc.js +2 -1
  44. package/static/build/services/log.js +1 -1
  45. package/static/build/services/open.js +106 -31
  46. package/static/build/services/schedule.js +20 -10
  47. package/static/build/services/script.js +1 -1
  48. package/static/build/services/scriptHistory.js +30 -0
  49. package/static/build/services/sshKey.js +39 -14
  50. package/static/build/services/subscription.js +104 -53
  51. package/static/build/services/system.js +67 -32
  52. package/static/build/services/user.js +15 -10
  53. package/static/build/shared/cronCommand.js +17 -0
  54. package/static/build/shared/cronSchedule.js +101 -0
  55. package/static/build/shared/cronScheduler.js +73 -0
  56. package/static/build/shared/i18n.js +3 -2
  57. package/static/build/shared/logPath.js +15 -0
  58. package/static/build/shared/loginThrottle.js +45 -0
  59. package/static/build/shared/pLimit.js +19 -6
  60. package/static/build/shared/runCron.js +13 -4
  61. package/static/build/shared/scriptHistory.js +445 -0
  62. package/static/build/shared/shellQuote.js +11 -0
  63. package/static/build/shared/subscriptionMutationLock.js +34 -0
  64. package/static/build/shared/subscriptionPath.js +41 -0
  65. package/static/build/shared/subscriptionStorage.js +98 -0
  66. package/static/build/shared/utils.js +2 -1
  67. package/static/build/token.js +3 -14
  68. package/static/build/validation/schedule.js +3 -18
  69. package/static/dist/292.264757f0.async.js +1 -0
  70. package/static/dist/3512.95e41255.async.js +1 -0
  71. package/static/dist/index.html +2 -2
  72. package/static/dist/preload_helper.01fcd534.js +1 -0
  73. package/static/dist/src__pages__dependence__modal.0fa8b421.async.js +1 -0
  74. package/static/dist/src__pages__diff__index.b94e5dc5.async.js +1 -0
  75. package/static/dist/src__pages__script__editModal.afabf073.async.js +1 -0
  76. package/static/dist/src__pages__script__editNameModal.789ad87b.async.js +1 -0
  77. package/static/dist/src__pages__script__historyDrawer.8c58d958.async.js +1 -0
  78. package/static/dist/src__pages__script__historyDrawer.a449193a.chunk.css +1 -0
  79. package/static/dist/src__pages__script__index.ab360776.chunk.css +1 -0
  80. package/static/dist/src__pages__script__index.fc575945.async.js +1 -0
  81. package/static/dist/src__pages__script__saveModal.8fca1a22.async.js +1 -0
  82. package/static/dist/src__pages__script__saveWithHistory.99ece830.async.js +1 -0
  83. package/static/dist/src__pages__subscription__modal.30868ba8.async.js +1 -0
  84. package/static/dist/{umi.fc019050.js → umi.3a24a9fa.js} +1 -1
  85. package/test/back/advisory-input-security.test.cjs +416 -0
  86. package/test/back/code-scanning-security.test.cjs +189 -0
  87. package/test/back/cron-macros.test.cjs +51 -0
  88. package/test/back/cron-schedule-compatibility.test.cjs +69 -0
  89. package/test/back/cron-scheduler-routing.test.cjs +59 -0
  90. package/test/back/draft-path-security.test.cjs +235 -0
  91. package/test/back/env-import-security.test.cjs +91 -0
  92. package/test/back/execution-lifecycle.test.cjs +1 -1
  93. package/test/back/file-access-scope.test.cjs +180 -0
  94. package/test/back/grpc-mtls-security.test.cjs +136 -0
  95. package/test/back/http-security.test.cjs +105 -5
  96. package/test/back/legacy-cron-recovery.test.cjs +76 -0
  97. package/test/back/linux-mirror-result.test.cjs +176 -0
  98. package/test/back/listen-security.test.cjs +1 -0
  99. package/test/back/mirror-status.test.cjs +257 -0
  100. package/test/back/node-cron-migration.test.cjs +65 -0
  101. package/test/back/node-path-cache.test.cjs +29 -0
  102. package/test/back/node-path-lock.test.cjs +6 -2
  103. package/test/back/preload-lazy-notify.test.cjs +168 -0
  104. package/test/back/queued-cron-invalidation.test.cjs +98 -0
  105. package/test/back/restart-instance-recovery.test.cjs +116 -0
  106. package/test/back/scheduler-bulk-recovery.test.cjs +40 -0
  107. package/test/back/scheduler-concurrency.test.cjs +411 -0
  108. package/test/back/scheduler-reconciliation.test.cjs +58 -4
  109. package/test/back/script-file-access.test.cjs +210 -0
  110. package/test/back/script-history-api.test.cjs +551 -0
  111. package/test/back/script-history.test.cjs +383 -0
  112. package/test/back/shell-api-parsing.test.cjs +4 -4
  113. package/test/back/shell-translation-output.test.cjs +16 -0
  114. package/test/back/shell-upgrade.test.cjs +312 -0
  115. package/test/back/shellApiToken.test.cjs +151 -0
  116. package/test/back/ssh-config-home.test.cjs +113 -0
  117. package/test/back/static-mirror-trigger.test.cjs +441 -0
  118. package/test/back/static-mirror.test.cjs +179 -0
  119. package/test/back/stop-race.test.cjs +5 -1
  120. package/test/back/subscription-mutation-lock.test.cjs +83 -0
  121. package/test/back/subscription-review-regression.test.cjs +607 -0
  122. package/test/back/subscription-storage-regression.test.cjs +553 -0
  123. package/test/back/system-dependence-cache.test.cjs +1 -0
  124. package/test/back/system-export-history.test.cjs +119 -0
  125. package/test/back/systemTokenRenewal.test.cjs +497 -0
  126. package/test/back/task-shell-lifecycle.test.cjs +194 -0
  127. package/test/back/unique-constraint-error.test.cjs +72 -0
  128. package/test/back/upgrade-guards.test.cjs +285 -0
  129. package/test/back/upgrade-lifecycle.test.cjs +288 -0
  130. package/test/back/upgrade-transaction.test.cjs +133 -0
  131. package/test/back/user-security.test.cjs +102 -1
  132. package/test/back/wpush-grpc.test.cjs +1 -0
  133. package/test/fixtures/legacy-cron.json +1371 -0
  134. package/test/front/diff-file-routing.test.cjs +126 -0
  135. package/test/front/script-history-save.test.cjs +121 -0
  136. package/test/helpers/upgrade-fixture.cjs +30 -0
  137. package/version.yaml +14 -19
  138. package/static/dist/2096.6d98fd12.async.js +0 -1
  139. package/static/dist/4859.97d6653c.async.js +0 -1
  140. package/static/dist/6707.801b886b.async.js +0 -1
  141. package/static/dist/preload_helper.cf7de3fa.js +0 -1
  142. package/static/dist/src__pages__dependence__modal.a29c1a0f.async.js +0 -1
  143. package/static/dist/src__pages__diff__index.1714b941.async.js +0 -1
  144. package/static/dist/src__pages__script__editModal.22acdf7c.async.js +0 -1
  145. package/static/dist/src__pages__script__editNameModal.1a521cc0.async.js +0 -1
  146. package/static/dist/src__pages__script__index.60b6f827.chunk.css +0 -1
  147. package/static/dist/src__pages__script__index.a18b2692.async.js +0 -1
  148. package/static/dist/src__pages__script__saveModal.8965b74c.async.js +0 -1
  149. package/static/dist/src__pages__subscription__modal.38991e1e.async.js +0 -1
  150. /package/static/dist/{4859.1b962b29.chunk.css → 292.1b962b29.chunk.css} +0 -0
@@ -31,7 +31,6 @@ const typedi_1 = require("typedi");
31
31
  const config_1 = __importDefault(require("../config"));
32
32
  const fs = __importStar(require("fs/promises"));
33
33
  const celebrate_1 = require("celebrate");
34
- const path_1 = require("path");
35
34
  const const_1 = require("../config/const");
36
35
  const i18n_1 = require("../shared/i18n");
37
36
  const config_2 = __importDefault(require("../services/config"));
@@ -86,13 +85,10 @@ exports.default = (app) => {
86
85
  const logger = typedi_1.Container.get('logger');
87
86
  try {
88
87
  const { name, content } = req.body;
89
- // Resolve path first to prevent traversal attacks
90
- let basePath = config_1.default.configPath;
91
88
  if (name.startsWith('data/scripts/')) {
92
- basePath = (0, path_1.join)(config_1.default.rootPath, 'data/scripts');
89
+ return res.send({ code: 403, message: (0, i18n_1.t)('文件无法访问') });
93
90
  }
94
- const cleanName = name.replace(/^data\/scripts\//, '');
95
- const normalized = (0, fileAccess_1.resolveFileAccess)(basePath, [cleanName], config_1.default.blackFileList);
91
+ const normalized = (0, fileAccess_1.resolveFileAccess)(config_1.default.configPath, [name], config_1.default.blackFileList);
96
92
  if (!normalized) {
97
93
  return res.send({ code: 403, message: (0, i18n_1.t)('文件无法访问') });
98
94
  }
@@ -5,23 +5,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const celebrate_1 = require("celebrate");
7
7
  const express_1 = require("express");
8
- const fs_1 = __importDefault(require("fs"));
9
8
  const multer_1 = __importDefault(require("multer"));
10
9
  const typedi_1 = require("typedi");
11
- const config_1 = __importDefault(require("../config"));
12
- const util_1 = require("../config/util");
13
10
  const i18n_1 = require("../shared/i18n");
14
11
  const env_1 = __importDefault(require("../services/env"));
15
12
  const route = (0, express_1.Router)();
16
- const storage = multer_1.default.diskStorage({
17
- destination: function (req, file, cb) {
18
- cb(null, config_1.default.scriptPath);
19
- },
20
- filename: function (req, file, cb) {
21
- cb(null, file.originalname);
22
- },
13
+ const upload = (0, multer_1.default)({
14
+ storage: multer_1.default.memoryStorage(),
15
+ limits: { fileSize: 5 * 1024 * 1024, files: 1 },
23
16
  });
24
- const upload = (0, multer_1.default)({ storage: storage });
25
17
  const labelSchema = celebrate_1.Joi.array()
26
18
  .items(celebrate_1.Joi.string().trim().required())
27
19
  .min(1)
@@ -228,15 +220,24 @@ exports.default = (app) => {
228
220
  }
229
221
  });
230
222
  route.post('/upload', upload.single('env'), async (req, res, next) => {
223
+ var _a;
231
224
  const logger = typedi_1.Container.get('logger');
232
225
  try {
233
226
  const envService = typedi_1.Container.get(env_1.default);
234
- const fileContent = await fs_1.default.promises.readFile(req.file.path, 'utf8');
235
- const parseContent = (0, util_1.safeJSONParse)(fileContent);
227
+ let parseContent;
228
+ try {
229
+ parseContent = JSON.parse(((_a = req.file) === null || _a === void 0 ? void 0 : _a.buffer.toString('utf8')) || '');
230
+ }
231
+ catch (_b) {
232
+ return res.status(400).send({ code: 400, message: (0, i18n_1.t)('参数错误') });
233
+ }
236
234
  const data = Array.isArray(parseContent)
237
235
  ? parseContent
238
236
  : [parseContent];
239
- if (data.every((x) => x.name && x.value)) {
237
+ if (data.length > 0 &&
238
+ data.every((x) => x && typeof x.name === 'string' &&
239
+ /^[a-zA-Z_][0-9a-zA-Z_]*$/.test(x.name) &&
240
+ typeof x.value === 'string' && x.value.length > 0)) {
240
241
  const result = await envService.create(data.map((x) => ({
241
242
  name: x.name,
242
243
  value: x.value,
@@ -7,6 +7,7 @@ const express_1 = require("express");
7
7
  const typedi_1 = require("typedi");
8
8
  const open_1 = __importDefault(require("../services/open"));
9
9
  const celebrate_1 = require("celebrate");
10
+ const express_rate_limit_1 = require("express-rate-limit");
10
11
  const route = (0, express_1.Router)();
11
12
  exports.default = (app) => {
12
13
  app.use('/', route);
@@ -82,7 +83,7 @@ exports.default = (app) => {
82
83
  return next(e);
83
84
  }
84
85
  });
85
- route.get('/auth/token', (0, celebrate_1.celebrate)({
86
+ route.get('/auth/token', (0, express_rate_limit_1.rateLimit)({ windowMs: 15 * 60 * 1000, limit: 100 }), (0, celebrate_1.celebrate)({
86
87
  query: {
87
88
  client_id: celebrate_1.Joi.string().required(),
88
89
  client_secret: celebrate_1.Joi.string().required(),
@@ -36,13 +36,41 @@ const fs = __importStar(require("fs/promises"));
36
36
  const celebrate_1 = require("celebrate");
37
37
  const path_1 = __importStar(require("path"));
38
38
  const script_1 = __importDefault(require("../services/script"));
39
+ const scriptHistory_1 = __importDefault(require("../services/scriptHistory"));
39
40
  const i18n_1 = require("../shared/i18n");
40
41
  const multer_1 = __importDefault(require("multer"));
41
42
  const utils_1 = require("../shared/utils");
43
+ const scriptHistory_2 = require("../shared/scriptHistory");
42
44
  const route = (0, express_1.Router)();
43
45
  function isPathAllowed(targetPath) {
44
46
  const resolved = path_1.default.resolve(targetPath);
45
- return config_1.default.writePathList.some((x) => Boolean((0, fileAccess_1.resolveFileAccess)(x, [resolved], config_1.default.blackFileList)));
47
+ return config_1.default.writePathList.some((x) => Boolean((0, fileAccess_1.resolveFileAccess)(x, [resolved],
48
+ // Panel configuration secrets must not restrict user script filenames.
49
+ path_1.default.resolve(x) === path_1.default.resolve(config_1.default.configPath)
50
+ ? config_1.default.blackFileList
51
+ : [])));
52
+ }
53
+ async function existingWritableRealPath(targetPath) {
54
+ if (!isPathAllowed(targetPath)) {
55
+ throw new scriptHistory_2.ScriptHistoryError(403, '暂无权限');
56
+ }
57
+ const resolved = (0, path_1.resolve)(targetPath);
58
+ for (const writableRoot of config_1.default.writePathList) {
59
+ const root = (0, path_1.resolve)(writableRoot);
60
+ const rootPrefix = root.endsWith(path_1.sep) ? root : root + path_1.sep;
61
+ // Keep an explicit normalized boundary check at the filesystem operation.
62
+ if (!resolved.startsWith(rootPrefix))
63
+ continue;
64
+ const realPath = await fs.realpath(resolved);
65
+ const realRoot = await fs.realpath(root);
66
+ const realRootPrefix = realRoot.endsWith(path_1.sep) ? realRoot : realRoot + path_1.sep;
67
+ if (!realPath.startsWith(realRootPrefix) ||
68
+ !(0, fileAccess_1.resolveFileAccess)(realRoot, [realPath], root === (0, path_1.resolve)(config_1.default.configPath) ? config_1.default.blackFileList : [])) {
69
+ throw new scriptHistory_2.ScriptHistoryError(403, '暂无权限');
70
+ }
71
+ return realPath;
72
+ }
73
+ throw new scriptHistory_2.ScriptHistoryError(403, '暂无权限');
46
74
  }
47
75
  const storage = multer_1.default.diskStorage({
48
76
  destination: function (req, file, cb) {
@@ -72,7 +100,7 @@ exports.default = (app) => {
72
100
  'package-lock.json',
73
101
  ];
74
102
  if (req.query.path) {
75
- if (!(0, fileAccess_1.resolveFileAccess)(config_1.default.scriptPath, [req.query.path], config_1.default.blackFileList)) {
103
+ if (!(0, fileAccess_1.resolveFileAccess)(config_1.default.scriptPath, [req.query.path])) {
76
104
  return res.send({ code: 403, message: (0, i18n_1.t)('暂无权限') });
77
105
  }
78
106
  result = await (0, util_1.readDir)(req.query.path, config_1.default.scriptPath, blacklist);
@@ -113,6 +141,53 @@ exports.default = (app) => {
113
141
  return next(e);
114
142
  }
115
143
  });
144
+ const historyError = (error, res, next) => {
145
+ if (error instanceof scriptHistory_2.ScriptHistoryError) {
146
+ return res.status(error.status).send(Object.assign({ code: error.status, message: (0, i18n_1.t)(error.message) }, (error instanceof scriptHistory_2.HistoryUnavailableError
147
+ ? { historyUnavailable: true, currentHash: error.currentHash }
148
+ : {})));
149
+ }
150
+ return next(error);
151
+ };
152
+ const historyQuery = {
153
+ filename: celebrate_1.Joi.string().required(),
154
+ path: celebrate_1.Joi.string().optional().allow(''),
155
+ };
156
+ route.get('/history', (0, celebrate_1.celebrate)({ query: celebrate_1.Joi.object(historyQuery).unknown(true) }), async (req, res, next) => {
157
+ try {
158
+ const service = typedi_1.Container.get(scriptHistory_1.default);
159
+ const data = await service.list(req.query.path || '', req.query.filename);
160
+ res.send({ code: 200, data });
161
+ }
162
+ catch (error) {
163
+ historyError(error, res, next);
164
+ }
165
+ });
166
+ route.get('/history/detail', (0, celebrate_1.celebrate)({
167
+ query: celebrate_1.Joi.object(Object.assign(Object.assign({}, historyQuery), { id: celebrate_1.Joi.string().uuid().required() })).unknown(true),
168
+ }), async (req, res, next) => {
169
+ try {
170
+ const service = typedi_1.Container.get(scriptHistory_1.default);
171
+ const data = await service.detail(req.query.path || '', req.query.filename, req.query.id);
172
+ res.send({ code: 200, data });
173
+ }
174
+ catch (error) {
175
+ historyError(error, res, next);
176
+ }
177
+ });
178
+ route.put('/history/restore', (0, celebrate_1.celebrate)({
179
+ body: celebrate_1.Joi.object(Object.assign(Object.assign({}, historyQuery), { id: celebrate_1.Joi.string().uuid().required(), expectedHash: celebrate_1.Joi.string().hex().length(64).required() })),
180
+ }), async (req, res, next) => {
181
+ try {
182
+ const { path = '', filename, id, expectedHash } = req.body;
183
+ const service = typedi_1.Container.get(scriptHistory_1.default);
184
+ const data = await service.restore(path, filename, id, expectedHash);
185
+ res.send({ code: 200, data });
186
+ }
187
+ catch (error) {
188
+ historyError(error, res, next);
189
+ }
190
+ });
116
191
  route.get('/:file', (req, res) => {
117
192
  return res.send({
118
193
  code: 410,
@@ -131,6 +206,11 @@ exports.default = (app) => {
131
206
  filename: celebrate_1.Joi.string().required(),
132
207
  path: celebrate_1.Joi.string().optional().allow(''),
133
208
  content: celebrate_1.Joi.string().optional().allow(''),
209
+ skipHistory: celebrate_1.Joi.boolean().optional(),
210
+ expectedHash: celebrate_1.Joi.string()
211
+ .hex()
212
+ .length(64)
213
+ .when('skipHistory', { is: true, then: celebrate_1.Joi.required() }),
134
214
  originFilename: celebrate_1.Joi.string().optional().allow(''),
135
215
  directory: celebrate_1.Joi.string().optional().allow(''),
136
216
  file: celebrate_1.Joi.string().optional().allow(''),
@@ -178,8 +258,56 @@ exports.default = (app) => {
178
258
  if (!isPathAllowed(filePath) || !isPathAllowed(originFilePath)) {
179
259
  return res.send({ code: 403, message: (0, i18n_1.t)('暂无权限') });
180
260
  }
181
- await fs.mkdir(path, { recursive: true });
261
+ // Check the canonical parent directly at the directory creation boundary.
262
+ const parentPath = (0, path_1.resolve)((0, path_1.dirname)(filePath));
263
+ let parentCreated = false;
264
+ for (const writableRoot of config_1.default.writePathList) {
265
+ const root = (0, path_1.resolve)(writableRoot);
266
+ const rootPrefix = root.endsWith(path_1.sep) ? root : root + path_1.sep;
267
+ if (parentPath === root) {
268
+ await fs.mkdir(root, { recursive: true });
269
+ }
270
+ else if (parentPath.startsWith(rootPrefix)) {
271
+ await fs.mkdir(parentPath, { recursive: true });
272
+ }
273
+ else {
274
+ continue;
275
+ }
276
+ parentCreated = true;
277
+ break;
278
+ }
279
+ if (!parentCreated) {
280
+ return res.send({ code: 403, message: (0, i18n_1.t)('暂无权限') });
281
+ }
182
282
  const fileExists = await (0, util_1.fileExist)(filePath);
283
+ if (fileExists && (0, fileAccess_1.resolveFileAccess)(config_1.default.scriptPath, [filePath])) {
284
+ let removeSource = filename !== originFilename;
285
+ let backupSource = '';
286
+ if (removeSource) {
287
+ const [originRealPath, targetRealPath] = await Promise.all([
288
+ existingWritableRealPath(originFilePath),
289
+ existingWritableRealPath(filePath),
290
+ ]);
291
+ // Aliases of one script are an in-place save. Removing the source
292
+ // would also remove the destination behind a target symlink.
293
+ removeSource = originRealPath !== targetRealPath;
294
+ backupSource = originRealPath;
295
+ }
296
+ // Save-as removes the source after committing the destination. Keep
297
+ // its original content too: destination history only protects the
298
+ // file being overwritten, not the source being deleted.
299
+ if (removeSource) {
300
+ await fs.copyFile(backupSource, (0, path_1.join)(config_1.default.bakPath, originFilename.replace(/\//g, '')));
301
+ }
302
+ const service = typedi_1.Container.get(scriptHistory_1.default);
303
+ const data = await service.save(path, filename, content, {
304
+ skipHistory: req.body.skipHistory,
305
+ expectedHash: req.body.expectedHash,
306
+ });
307
+ if (removeSource)
308
+ await (0, util_1.rmPath)(originFilePath);
309
+ return res.send({ code: 200, data });
310
+ }
183
311
  if (fileExists) {
184
312
  await fs.copyFile(originFilePath, (0, path_1.join)(config_1.default.bakPath, originFilename.replace(/\//g, '')));
185
313
  if (filename !== originFilename) {
@@ -190,7 +318,7 @@ exports.default = (app) => {
190
318
  return res.send({ code: 200 });
191
319
  }
192
320
  catch (e) {
193
- return next(e);
321
+ return historyError(e, res, next);
194
322
  }
195
323
  });
196
324
  route.put('/', (0, celebrate_1.celebrate)({
@@ -198,6 +326,11 @@ exports.default = (app) => {
198
326
  filename: celebrate_1.Joi.string().required(),
199
327
  path: celebrate_1.Joi.string().optional().allow(''),
200
328
  content: celebrate_1.Joi.string().required().allow(''),
329
+ skipHistory: celebrate_1.Joi.boolean().optional(),
330
+ expectedHash: celebrate_1.Joi.string()
331
+ .hex()
332
+ .length(64)
333
+ .when('skipHistory', { is: true, then: celebrate_1.Joi.required() }),
201
334
  }),
202
335
  }), async (req, res, next) => {
203
336
  try {
@@ -210,11 +343,15 @@ exports.default = (app) => {
210
343
  message: (0, i18n_1.t)('暂无权限'),
211
344
  });
212
345
  }
213
- await (0, utils_1.writeFileWithLock)(filePath, content);
214
- return res.send({ code: 200 });
346
+ const service = typedi_1.Container.get(scriptHistory_1.default);
347
+ const data = await service.save(path || '', filename, content, {
348
+ skipHistory: req.body.skipHistory,
349
+ expectedHash: req.body.expectedHash,
350
+ });
351
+ return res.send({ code: 200, data });
215
352
  }
216
353
  catch (e) {
217
- return next(e);
354
+ return historyError(e, res, next);
218
355
  }
219
356
  });
220
357
  route.delete('/', (0, celebrate_1.celebrate)({
@@ -317,11 +454,20 @@ exports.default = (app) => {
317
454
  if (!isPathAllowed(filePath)) {
318
455
  return res.send({ code: 403, message: (0, i18n_1.t)('暂无权限') });
319
456
  }
320
- const logPath = (0, path_1.join)(config_1.default.logPath, path, `${name}.swap`);
457
+ const logPath = (0, fileAccess_1.resolveFileAccess)(config_1.default.logPath, [
458
+ path,
459
+ `${name}.swap`,
460
+ ]);
461
+ if (!logPath) {
462
+ return res.send({ code: 403, message: (0, i18n_1.t)('暂无权限') });
463
+ }
321
464
  const scriptService = typedi_1.Container.get(script_1.default);
322
465
  const result = await scriptService.stopScript(filePath, pid);
323
466
  setTimeout(() => {
324
- (0, util_1.rmPath)(logPath);
467
+ const cleanupPath = (0, fileAccess_1.resolveFileAccess)(config_1.default.logPath, [logPath]);
468
+ if (cleanupPath) {
469
+ void (0, util_1.rmPath)(cleanupPath);
470
+ }
325
471
  }, 3000);
326
472
  res.send(result);
327
473
  }
@@ -7,7 +7,7 @@ const express_1 = require("express");
7
7
  const typedi_1 = require("typedi");
8
8
  const subscription_1 = __importDefault(require("../services/subscription"));
9
9
  const celebrate_1 = require("celebrate");
10
- const cron_parser_1 = __importDefault(require("cron-parser"));
10
+ const cronSchedule_1 = require("../shared/cronSchedule");
11
11
  const route = (0, express_1.Router)();
12
12
  exports.default = (app) => {
13
13
  app.use('/subscriptions', route);
@@ -54,8 +54,7 @@ exports.default = (app) => {
54
54
  }), async (req, res, next) => {
55
55
  const logger = typedi_1.Container.get('logger');
56
56
  try {
57
- if (!req.body.schedule ||
58
- cron_parser_1.default.parse(req.body.schedule).hasNext()) {
57
+ if (!req.body.schedule || (0, cronSchedule_1.isValidCronSchedule)(req.body.schedule)) {
59
58
  const subscriptionService = typedi_1.Container.get(subscription_1.default);
60
59
  const data = await subscriptionService.create(req.body);
61
60
  return res.send({ code: 200, data });
@@ -175,9 +174,7 @@ exports.default = (app) => {
175
174
  }), async (req, res, next) => {
176
175
  const logger = typedi_1.Container.get('logger');
177
176
  try {
178
- if (!req.body.schedule ||
179
- typeof req.body.schedule === 'object' ||
180
- cron_parser_1.default.parse(req.body.schedule).hasNext()) {
177
+ if (!req.body.schedule || (0, cronSchedule_1.isValidCronSchedule)(req.body.schedule)) {
181
178
  const subscriptionService = typedi_1.Container.get(subscription_1.default);
182
179
  const data = await subscriptionService.update(req.body);
183
180
  return res.send({ code: 200, data });
@@ -1,10 +1,34 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
27
  };
5
28
  Object.defineProperty(exports, "__esModule", { value: true });
6
29
  const express_1 = require("express");
7
30
  const typedi_1 = require("typedi");
31
+ const fs = __importStar(require("fs/promises"));
8
32
  const config_1 = __importDefault(require("../config"));
9
33
  const system_1 = __importDefault(require("../services/system"));
10
34
  const celebrate_1 = require("celebrate");
@@ -12,9 +36,11 @@ const user_1 = __importDefault(require("../services/user"));
12
36
  const i18n_1 = require("../shared/i18n");
13
37
  const auth_1 = require("../shared/auth");
14
38
  const util_1 = require("../config/util");
39
+ const cronCommand_1 = require("../shared/cronCommand");
15
40
  const dayjs_1 = __importDefault(require("dayjs"));
16
41
  const multer_1 = __importDefault(require("multer"));
17
42
  const logStreamManager_1 = require("../shared/logStreamManager");
43
+ const express_rate_limit_1 = require("express-rate-limit");
18
44
  const route = (0, express_1.Router)();
19
45
  const storage = multer_1.default.diskStorage({
20
46
  destination: function (req, file, cb) {
@@ -199,7 +225,7 @@ exports.default = (app) => {
199
225
  return next(e);
200
226
  }
201
227
  });
202
- route.put('/command-run', (0, celebrate_1.celebrate)({
228
+ route.put('/command-run', (0, express_rate_limit_1.rateLimit)({ windowMs: 60 * 1000, limit: 60 }), (0, celebrate_1.celebrate)({
203
229
  body: celebrate_1.Joi.object({
204
230
  command: celebrate_1.Joi.string().required(),
205
231
  }),
@@ -207,8 +233,14 @@ exports.default = (app) => {
207
233
  try {
208
234
  const systemService = typedi_1.Container.get(system_1.default);
209
235
  const command = req.body.command;
210
- const idStr = `cat ${config_1.default.crontabFile} | grep -E "${command}" | perl -pe "s|.*ID=(.*) ${command}.*|\\1|" | head -1 | awk -F " " '{print $1}' | xargs echo -n`;
211
- let id = await (0, util_1.promiseExec)(idStr);
236
+ const crontab = await fs
237
+ .readFile(config_1.default.crontabFile, 'utf8')
238
+ .catch((e) => {
239
+ if (e.code === 'ENOENT')
240
+ return '';
241
+ throw e;
242
+ });
243
+ const id = (0, cronCommand_1.findCronId)(crontab, command);
212
244
  const uniqPath = await (0, util_1.getUniqPath)(command, id);
213
245
  const logTime = (0, dayjs_1.default)().format('YYYY-MM-DD-HH-mm-ss-SSS');
214
246
  const logPath = `${uniqPath}/${logTime}.log`;
@@ -29,7 +29,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
29
29
  exports.getGrpcCerts = exports.initGrpcCerts = void 0;
30
30
  const child_process_1 = require("child_process");
31
31
  const fs = __importStar(require("fs/promises"));
32
- const os = __importStar(require("os"));
33
32
  const path_1 = __importDefault(require("path"));
34
33
  const index_1 = __importDefault(require("./index"));
35
34
  const util_1 = require("./util");
@@ -42,62 +41,112 @@ const serverCertPath = path_1.default.join(certDir, 'server.crt');
42
41
  const clientKeyPath = path_1.default.join(certDir, 'client.key');
43
42
  const clientCertPath = path_1.default.join(certDir, 'client.crt');
44
43
  let cachedConfig = null;
45
- function run(cmd, execOpts) {
46
- const opts = Object.assign({ stdio: 'pipe', timeout: 30000, encoding: 'utf-8' }, execOpts);
47
- return (0, child_process_1.execSync)(cmd, opts).trim();
48
- }
49
- async function tmpFile(prefix) {
50
- const dir = (await (0, util_1.fileExist)(certDir)) ? certDir : os.tmpdir();
51
- await fs.mkdir(dir, { recursive: true });
52
- return path_1.default.join(dir, `.${prefix}_${Date.now()}_${Math.random().toString(36).slice(2)}.pem`);
44
+ function run(args) {
45
+ return (0, child_process_1.execFileSync)('openssl', args, {
46
+ stdio: 'pipe',
47
+ timeout: 30000,
48
+ encoding: 'utf-8',
49
+ }).trim();
53
50
  }
54
51
  async function generateAllCerts() {
55
52
  logger_1.default.info('[boot] Generating gRPC mTLS certificates...');
56
- const caKeyTmp = await tmpFile('ca_key');
57
- const caCertTmp = await tmpFile('ca_cert');
58
- const serverKeyTmp = await tmpFile('server_key');
59
- const serverCsrTmp = await tmpFile('server_csr');
60
- const serverExtTmp = await tmpFile('server_ext');
61
- const clientKeyTmp = await tmpFile('client_key');
62
- const clientCsrTmp = await tmpFile('client_csr');
63
- const clientExtTmp = await tmpFile('client_ext');
64
- const srlTmp = path_1.default.join(path_1.default.dirname(caKeyTmp), '.grpc_ca.srl');
53
+ await fs.mkdir(certDir, { recursive: true, mode: 0o700 });
54
+ // Atomically reserve a private workspace for keys, CSRs and OpenSSL side files.
55
+ const workspace = await fs.mkdtemp(path_1.default.join(certDir, '.generate-'));
56
+ const caKeyTmp = path_1.default.join(workspace, 'ca.key');
57
+ const caCertTmp = path_1.default.join(workspace, 'ca.crt');
58
+ const serverKeyTmp = path_1.default.join(workspace, 'server.key');
59
+ const serverCsrTmp = path_1.default.join(workspace, 'server.csr');
60
+ const serverExtTmp = path_1.default.join(workspace, 'server.ext');
61
+ const clientKeyTmp = path_1.default.join(workspace, 'client.key');
62
+ const clientCsrTmp = path_1.default.join(workspace, 'client.csr');
63
+ const clientExtTmp = path_1.default.join(workspace, 'client.ext');
65
64
  const cleanup = async () => {
66
- for (const f of [caKeyTmp, caCertTmp, serverKeyTmp, serverCsrTmp, serverExtTmp,
67
- clientKeyTmp, clientCsrTmp, clientExtTmp, srlTmp]) {
68
- try {
69
- await fs.unlink(f);
70
- }
71
- catch (_a) { }
72
- }
65
+ await fs.rm(workspace, { recursive: true, force: true });
73
66
  };
74
67
  try {
75
- // 1. CA(私钥直接存盘,证书写入临时文件供签发使用)
76
- run(`openssl genrsa -out '${caKeyTmp}' 2048 2>/dev/null`);
77
- run(`openssl req -new -x509 -days 3650 -key '${caKeyTmp}' -out '${caCertTmp}' -subj '/CN=qinglong-ca/O=qinglong/C=CN' 2>/dev/null`);
68
+ run(['genrsa', '-out', caKeyTmp, '2048']);
69
+ run([
70
+ 'req',
71
+ '-new',
72
+ '-x509',
73
+ '-days',
74
+ '3650',
75
+ '-key',
76
+ caKeyTmp,
77
+ '-out',
78
+ caCertTmp,
79
+ '-subj',
80
+ '/CN=qinglong-ca/O=qinglong/C=CN',
81
+ ]);
78
82
  const caKey = await fs.readFile(caKeyTmp, 'utf-8');
79
83
  const caCert = await fs.readFile(caCertTmp, 'utf-8');
80
- await fs.mkdir(certDir, { recursive: true });
81
84
  await fs.writeFile(caKeyPath, caKey, { mode: 0o600 });
82
- // 2. 服务端
83
- run(`openssl genrsa -out '${serverKeyTmp}' 2048 2>/dev/null`);
84
- run(`openssl req -new -key '${serverKeyTmp}' -out '${serverCsrTmp}' -subj '/CN=grpc-server' 2>/dev/null`);
85
+ run(['genrsa', '-out', serverKeyTmp, '2048']);
86
+ run([
87
+ 'req',
88
+ '-new',
89
+ '-key',
90
+ serverKeyTmp,
91
+ '-out',
92
+ serverCsrTmp,
93
+ '-subj',
94
+ '/CN=grpc-server',
95
+ ]);
85
96
  await fs.writeFile(serverExtTmp, 'subjectAltName=DNS:localhost,IP:127.0.0.1,IP:::1\n');
86
- const serverCert = run(`openssl x509 -req -days 3650 -in '${serverCsrTmp}' -CA '${caCertTmp}' -CAkey '${caKeyTmp}' -CAcreateserial -extfile '${serverExtTmp}' 2>/dev/null`);
97
+ const serverCert = run([
98
+ 'x509',
99
+ '-req',
100
+ '-days',
101
+ '3650',
102
+ '-in',
103
+ serverCsrTmp,
104
+ '-CA',
105
+ caCertTmp,
106
+ '-CAkey',
107
+ caKeyTmp,
108
+ '-CAserial',
109
+ path_1.default.join(workspace, 'ca.srl'),
110
+ '-CAcreateserial',
111
+ '-extfile',
112
+ serverExtTmp,
113
+ ]);
87
114
  const serverKey = await fs.readFile(serverKeyTmp, 'utf-8');
88
- // 3. 客户端
89
- run(`openssl genrsa -out '${clientKeyTmp}' 2048 2>/dev/null`);
90
- run(`openssl req -new -key '${clientKeyTmp}' -out '${clientCsrTmp}' -subj '/CN=grpc-client' 2>/dev/null`);
115
+ run(['genrsa', '-out', clientKeyTmp, '2048']);
116
+ run([
117
+ 'req',
118
+ '-new',
119
+ '-key',
120
+ clientKeyTmp,
121
+ '-out',
122
+ clientCsrTmp,
123
+ '-subj',
124
+ '/CN=grpc-client',
125
+ ]);
91
126
  await fs.writeFile(clientExtTmp, 'extendedKeyUsage=clientAuth\n');
92
- const clientCert = run(`openssl x509 -req -days 3650 -in '${clientCsrTmp}' -CA '${caCertTmp}' -CAkey '${caKeyTmp}' -CAcreateserial -extfile '${clientExtTmp}' 2>/dev/null`);
127
+ const clientCert = run([
128
+ 'x509',
129
+ '-req',
130
+ '-days',
131
+ '3650',
132
+ '-in',
133
+ clientCsrTmp,
134
+ '-CA',
135
+ caCertTmp,
136
+ '-CAkey',
137
+ caKeyTmp,
138
+ '-CAserial',
139
+ path_1.default.join(workspace, 'ca.srl'),
140
+ '-CAcreateserial',
141
+ '-extfile',
142
+ clientExtTmp,
143
+ ]);
93
144
  const clientKey = await fs.readFile(clientKeyTmp, 'utf-8');
94
- await cleanup();
95
145
  logger_1.default.info('[boot] gRPC mTLS certificates generated successfully');
96
146
  return { caCert, serverCert, serverKey, clientCert, clientKey };
97
147
  }
98
- catch (e) {
148
+ finally {
99
149
  await cleanup();
100
- throw e;
101
150
  }
102
151
  }
103
152
  async function saveCerts(tlsConfig) {
@@ -5,13 +5,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.formatCommand = exports.formatUrl = void 0;
7
7
  const isNil_1 = __importDefault(require("lodash/isNil"));
8
+ const shellQuote_1 = require("../shared/shellQuote");
9
+ const subscriptionPath_1 = require("../shared/subscriptionPath");
8
10
  function formatUrl(doc) {
9
11
  let url = doc.url;
10
12
  let host = '';
11
13
  if (doc.type === 'private-repo') {
12
14
  if (doc.pull_type === 'ssh-key') {
13
15
  host = doc.url.replace(/.*\@([^\:]+)\:.*/, '$1');
14
- url = doc.url.replace(host, doc.alias);
16
+ url = doc.url.replace(host, (0, subscriptionPath_1.getSubscriptionSshAlias)(doc.alias));
15
17
  }
16
18
  else {
17
19
  host = doc.url.replace(/.*\:\/\/([^\/]+)\/.*/, '$1');
@@ -23,16 +25,22 @@ function formatUrl(doc) {
23
25
  }
24
26
  exports.formatUrl = formatUrl;
25
27
  function formatCommand(doc, url) {
26
- let command = `SUB_ID=${doc.id} ql `;
27
- let _url = url || formatUrl(doc).url;
28
- const { type, whitelist, blacklist, dependences, branch, extensions, proxy, autoAddCron, autoDelCron, } = doc;
29
- if (type === 'file') {
30
- command += `raw "${_url}" "${proxy || ''}" "${(0, isNil_1.default)(autoAddCron) ? true : Boolean(autoAddCron)}" "${(0, isNil_1.default)(autoDelCron) ? true : Boolean(autoDelCron)}"`;
31
- }
32
- else {
33
- command += `repo "${_url}" "${whitelist || ''}" "${blacklist || ''}" "${dependences || ''}" "${branch || ''}" "${extensions || ''}" "${proxy || ''}" "${(0, isNil_1.default)(autoAddCron) ? true : Boolean(autoAddCron)}" "${(0, isNil_1.default)(autoDelCron) ? true : Boolean(autoDelCron)}"`;
34
- }
35
- return command;
28
+ const args = doc.type === 'file'
29
+ ? ['raw', url || formatUrl(doc).url, doc.proxy || '']
30
+ : [
31
+ 'repo',
32
+ url || formatUrl(doc).url,
33
+ doc.whitelist || '',
34
+ doc.blacklist || '',
35
+ doc.dependences || '',
36
+ doc.branch || '',
37
+ doc.extensions || '',
38
+ doc.proxy || '',
39
+ ];
40
+ args.push(String((0, isNil_1.default)(doc.autoAddCron) ? true : Boolean(doc.autoAddCron)), String((0, isNil_1.default)(doc.autoDelCron) ? true : Boolean(doc.autoDelCron)));
41
+ return `SUB_ID=${(0, shellQuote_1.shellQuote)(String(doc.id))} ql ${args
42
+ .map((arg) => (0, shellQuote_1.shellQuote)(arg))
43
+ .join(' ')}`;
36
44
  }
37
45
  exports.formatCommand = formatCommand;
38
46
  //# sourceMappingURL=subscription.js.map