@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.
- package/back/protos/cron.proto +4 -0
- package/docs/development/node-cron-migration.md +49 -0
- package/docs/development/task-startup.md +78 -0
- package/docs/releases/2.22.0.md +8 -0
- package/docs/releases/2.23.0.md +33 -0
- package/docs/script-history.md +53 -0
- package/package.json +9 -8
- package/scripts/benchmark-script-history.cjs +39 -0
- package/scripts/publish-static.sh +34 -0
- package/scripts/sync-static.sh +20 -0
- package/scripts/wait-for-mirror.sh +214 -0
- package/shell/api.sh +56 -70
- package/shell/node_path_cache.sh +10 -2
- package/shell/otask.sh +7 -9
- package/shell/preload/sitecustomize.js +6 -2
- package/shell/preload/sitecustomize.py +4 -2
- package/shell/share.sh +1 -1
- package/shell/task-timeout.sh +63 -0
- package/shell/task.sh +8 -6
- package/shell/update.sh +40 -95
- package/shell/upgrade.cjs +8 -0
- package/static/build/api/config.js +2 -6
- package/static/build/api/env.js +15 -14
- package/static/build/api/open.js +2 -1
- package/static/build/api/script.js +155 -9
- package/static/build/api/subscription.js +3 -6
- package/static/build/api/system.js +35 -3
- package/static/build/config/grpcCerts.js +89 -40
- package/static/build/config/subscription.js +19 -11
- package/static/build/config/util.js +6 -4
- package/static/build/loaders/express.js +2 -0
- package/static/build/loaders/initData.js +20 -30
- package/static/build/loaders/initTask.js +11 -1
- package/static/build/middlewares/uniqueConstraintError.js +15 -0
- package/static/build/protos/cron.js +63 -2
- package/static/build/schedule/addCron.js +45 -63
- package/static/build/schedule/client.js +19 -1
- package/static/build/schedule/delCron.js +1 -1
- package/static/build/schedule/setConcurrency.js +27 -0
- package/static/build/services/config.js +2 -3
- package/static/build/services/cron.js +36 -10
- package/static/build/services/env.js +1 -1
- package/static/build/services/grpc.js +2 -1
- package/static/build/services/log.js +1 -1
- package/static/build/services/open.js +106 -31
- package/static/build/services/schedule.js +20 -10
- package/static/build/services/script.js +1 -1
- package/static/build/services/scriptHistory.js +30 -0
- package/static/build/services/sshKey.js +39 -14
- package/static/build/services/subscription.js +104 -53
- package/static/build/services/system.js +67 -32
- package/static/build/services/user.js +15 -10
- package/static/build/shared/cronCommand.js +17 -0
- package/static/build/shared/cronSchedule.js +101 -0
- package/static/build/shared/cronScheduler.js +73 -0
- package/static/build/shared/i18n.js +3 -2
- package/static/build/shared/logPath.js +15 -0
- package/static/build/shared/loginThrottle.js +45 -0
- package/static/build/shared/pLimit.js +19 -6
- package/static/build/shared/runCron.js +13 -4
- package/static/build/shared/scriptHistory.js +445 -0
- package/static/build/shared/shellQuote.js +11 -0
- package/static/build/shared/subscriptionMutationLock.js +34 -0
- package/static/build/shared/subscriptionPath.js +41 -0
- package/static/build/shared/subscriptionStorage.js +98 -0
- package/static/build/shared/utils.js +2 -1
- package/static/build/token.js +3 -14
- package/static/build/validation/schedule.js +3 -18
- package/static/dist/292.264757f0.async.js +1 -0
- package/static/dist/3512.95e41255.async.js +1 -0
- package/static/dist/index.html +2 -2
- package/static/dist/preload_helper.01fcd534.js +1 -0
- package/static/dist/src__pages__dependence__modal.0fa8b421.async.js +1 -0
- package/static/dist/src__pages__diff__index.b94e5dc5.async.js +1 -0
- package/static/dist/src__pages__script__editModal.afabf073.async.js +1 -0
- package/static/dist/src__pages__script__editNameModal.789ad87b.async.js +1 -0
- package/static/dist/src__pages__script__historyDrawer.8c58d958.async.js +1 -0
- package/static/dist/src__pages__script__historyDrawer.a449193a.chunk.css +1 -0
- package/static/dist/src__pages__script__index.ab360776.chunk.css +1 -0
- package/static/dist/src__pages__script__index.fc575945.async.js +1 -0
- package/static/dist/src__pages__script__saveModal.8fca1a22.async.js +1 -0
- package/static/dist/src__pages__script__saveWithHistory.99ece830.async.js +1 -0
- package/static/dist/src__pages__subscription__modal.30868ba8.async.js +1 -0
- package/static/dist/{umi.fc019050.js → umi.3a24a9fa.js} +1 -1
- package/test/back/advisory-input-security.test.cjs +416 -0
- package/test/back/code-scanning-security.test.cjs +189 -0
- package/test/back/cron-macros.test.cjs +51 -0
- package/test/back/cron-schedule-compatibility.test.cjs +69 -0
- package/test/back/cron-scheduler-routing.test.cjs +59 -0
- package/test/back/draft-path-security.test.cjs +235 -0
- package/test/back/env-import-security.test.cjs +91 -0
- package/test/back/execution-lifecycle.test.cjs +1 -1
- package/test/back/file-access-scope.test.cjs +180 -0
- package/test/back/grpc-mtls-security.test.cjs +136 -0
- package/test/back/http-security.test.cjs +105 -5
- package/test/back/legacy-cron-recovery.test.cjs +76 -0
- package/test/back/linux-mirror-result.test.cjs +176 -0
- package/test/back/listen-security.test.cjs +1 -0
- package/test/back/mirror-status.test.cjs +257 -0
- package/test/back/node-cron-migration.test.cjs +65 -0
- package/test/back/node-path-cache.test.cjs +29 -0
- package/test/back/node-path-lock.test.cjs +6 -2
- package/test/back/preload-lazy-notify.test.cjs +168 -0
- package/test/back/queued-cron-invalidation.test.cjs +98 -0
- package/test/back/restart-instance-recovery.test.cjs +116 -0
- package/test/back/scheduler-bulk-recovery.test.cjs +40 -0
- package/test/back/scheduler-concurrency.test.cjs +411 -0
- package/test/back/scheduler-reconciliation.test.cjs +58 -4
- package/test/back/script-file-access.test.cjs +210 -0
- package/test/back/script-history-api.test.cjs +551 -0
- package/test/back/script-history.test.cjs +383 -0
- package/test/back/shell-api-parsing.test.cjs +4 -4
- package/test/back/shell-translation-output.test.cjs +16 -0
- package/test/back/shell-upgrade.test.cjs +312 -0
- package/test/back/shellApiToken.test.cjs +151 -0
- package/test/back/ssh-config-home.test.cjs +113 -0
- package/test/back/static-mirror-trigger.test.cjs +441 -0
- package/test/back/static-mirror.test.cjs +179 -0
- package/test/back/stop-race.test.cjs +5 -1
- package/test/back/subscription-mutation-lock.test.cjs +83 -0
- package/test/back/subscription-review-regression.test.cjs +607 -0
- package/test/back/subscription-storage-regression.test.cjs +553 -0
- package/test/back/system-dependence-cache.test.cjs +1 -0
- package/test/back/system-export-history.test.cjs +119 -0
- package/test/back/systemTokenRenewal.test.cjs +497 -0
- package/test/back/task-shell-lifecycle.test.cjs +194 -0
- package/test/back/unique-constraint-error.test.cjs +72 -0
- package/test/back/upgrade-guards.test.cjs +285 -0
- package/test/back/upgrade-lifecycle.test.cjs +288 -0
- package/test/back/upgrade-transaction.test.cjs +133 -0
- package/test/back/user-security.test.cjs +102 -1
- package/test/back/wpush-grpc.test.cjs +1 -0
- package/test/fixtures/legacy-cron.json +1371 -0
- package/test/front/diff-file-routing.test.cjs +126 -0
- package/test/front/script-history-save.test.cjs +121 -0
- package/test/helpers/upgrade-fixture.cjs +30 -0
- package/version.yaml +14 -19
- package/static/dist/2096.6d98fd12.async.js +0 -1
- package/static/dist/4859.97d6653c.async.js +0 -1
- package/static/dist/6707.801b886b.async.js +0 -1
- package/static/dist/preload_helper.cf7de3fa.js +0 -1
- package/static/dist/src__pages__dependence__modal.a29c1a0f.async.js +0 -1
- package/static/dist/src__pages__diff__index.1714b941.async.js +0 -1
- package/static/dist/src__pages__script__editModal.22acdf7c.async.js +0 -1
- package/static/dist/src__pages__script__editNameModal.1a521cc0.async.js +0 -1
- package/static/dist/src__pages__script__index.60b6f827.chunk.css +0 -1
- package/static/dist/src__pages__script__index.a18b2692.async.js +0 -1
- package/static/dist/src__pages__script__saveModal.8965b74c.async.js +0 -1
- package/static/dist/src__pages__subscription__modal.38991e1e.async.js +0 -1
- /package/static/dist/{4859.1b962b29.chunk.css → 292.1b962b29.chunk.css} +0 -0
|
@@ -0,0 +1,416 @@
|
|
|
1
|
+
const assert = require('node:assert/strict');
|
|
2
|
+
const test = require('node:test');
|
|
3
|
+
const fs = require('node:fs');
|
|
4
|
+
const path = require('node:path');
|
|
5
|
+
const os = require('node:os');
|
|
6
|
+
const vm = require('node:vm');
|
|
7
|
+
const { execFileSync } = require('node:child_process');
|
|
8
|
+
const load = require('../helpers/load-security-module.cjs');
|
|
9
|
+
const decorators = { Service: () => (x) => x, Inject: () => () => {} };
|
|
10
|
+
const logger = { info() {}, warn() {}, error() {} };
|
|
11
|
+
const translate = { t: (x) => x, tf: (x) => x };
|
|
12
|
+
function temporary(t) {
|
|
13
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'ql-advisory-'));
|
|
14
|
+
t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
|
|
15
|
+
return dir;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
test('environment values remain literal when the generated JavaScript loads', async () => {
|
|
19
|
+
const output = {};
|
|
20
|
+
const Env = load(path.resolve('back/services/env.ts'), {
|
|
21
|
+
typedi: decorators,
|
|
22
|
+
'../config': { envFile: 'sh', jsEnvFile: 'js', pyEnvFile: 'py' },
|
|
23
|
+
'../data/env': { EnvStatus: { normal: 0 } },
|
|
24
|
+
'../data': {},
|
|
25
|
+
'../shared/utils': {
|
|
26
|
+
writeFileWithLock: async (file, text) => (output[file] = text),
|
|
27
|
+
},
|
|
28
|
+
}).default;
|
|
29
|
+
const service = new Env(logger);
|
|
30
|
+
const values = [
|
|
31
|
+
'${globalThis.injected = true}',
|
|
32
|
+
'quotes\'"` \\ newline\n中文\u2028',
|
|
33
|
+
'${1 + 2}',
|
|
34
|
+
];
|
|
35
|
+
service.envs = async () =>
|
|
36
|
+
values.map((value, i) => ({ name: `VALUE_${i}`, value }));
|
|
37
|
+
await service.set_envs();
|
|
38
|
+
const context = { process: { env: {} } };
|
|
39
|
+
vm.runInNewContext(output.js, context);
|
|
40
|
+
assert.equal(context.injected, undefined);
|
|
41
|
+
values.forEach((value, i) =>
|
|
42
|
+
assert.equal(context.process.env[`VALUE_${i}`], value),
|
|
43
|
+
);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test('subscription shell parameters round trip without expansion or command substitution', () => {
|
|
47
|
+
const { formatCommand } = load(path.resolve('back/config/subscription.ts'));
|
|
48
|
+
const payload =
|
|
49
|
+
'a $(printf INJECTED) `printf INJECTED` " ; echo BAD; # \' 中文\nnext';
|
|
50
|
+
for (const type of ['file', 'repo']) {
|
|
51
|
+
const fields =
|
|
52
|
+
type === 'file'
|
|
53
|
+
? ['url', 'proxy']
|
|
54
|
+
: [
|
|
55
|
+
'url',
|
|
56
|
+
'whitelist',
|
|
57
|
+
'blacklist',
|
|
58
|
+
'dependences',
|
|
59
|
+
'branch',
|
|
60
|
+
'extensions',
|
|
61
|
+
'proxy',
|
|
62
|
+
];
|
|
63
|
+
for (const field of fields) {
|
|
64
|
+
const doc = {
|
|
65
|
+
id: 12,
|
|
66
|
+
type,
|
|
67
|
+
url: 'https://example.invalid/repo',
|
|
68
|
+
[field]: payload,
|
|
69
|
+
};
|
|
70
|
+
const command = formatCommand(doc);
|
|
71
|
+
const stdout = execFileSync('/bin/bash', [
|
|
72
|
+
'-c',
|
|
73
|
+
`ql() { printf '%s\\0' "$@"; }; ${command}`,
|
|
74
|
+
]);
|
|
75
|
+
const args = stdout.toString().split('\0').slice(0, -1);
|
|
76
|
+
assert.equal(
|
|
77
|
+
args[fields.indexOf(field) + 1],
|
|
78
|
+
payload,
|
|
79
|
+
`${type}:${field}`,
|
|
80
|
+
);
|
|
81
|
+
assert.equal(args[0], type === 'file' ? 'raw' : 'repo');
|
|
82
|
+
assert.equal(args.length, fields.length + 3);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test('SSH alias traversal and symlinks cannot write or remove outside files', async (t) => {
|
|
88
|
+
const base = temporary(t);
|
|
89
|
+
const root = path.join(base, 'ssh');
|
|
90
|
+
fs.mkdirSync(root);
|
|
91
|
+
const outside = path.join(base, 'outside');
|
|
92
|
+
fs.writeFileSync(outside, 'keep');
|
|
93
|
+
fs.symlinkSync(outside, path.join(root, 'link'));
|
|
94
|
+
const Ssh = load(path.resolve('back/services/sshKey.ts'), {
|
|
95
|
+
typedi: decorators,
|
|
96
|
+
'../config': { sshdPath: root },
|
|
97
|
+
'../config/util': {
|
|
98
|
+
rmPath: (p) => fs.promises.rm(p, { recursive: true, force: true }),
|
|
99
|
+
},
|
|
100
|
+
'../shared/utils': {
|
|
101
|
+
writeFileWithLock: (p, value) => fs.promises.writeFile(p, value),
|
|
102
|
+
},
|
|
103
|
+
}).default;
|
|
104
|
+
const service = Object.assign(Object.create(Ssh.prototype), {
|
|
105
|
+
sshPath: root,
|
|
106
|
+
logger,
|
|
107
|
+
});
|
|
108
|
+
for (const alias of [
|
|
109
|
+
'../outside',
|
|
110
|
+
'..',
|
|
111
|
+
'.',
|
|
112
|
+
'/tmp/outside',
|
|
113
|
+
'link',
|
|
114
|
+
'x\nHost *',
|
|
115
|
+
]) {
|
|
116
|
+
await service.generatePrivateKeyFile(alias, 'overwrite');
|
|
117
|
+
await service.removePrivateKeyFile(alias);
|
|
118
|
+
await assert.rejects(service.generateSingleSshConfig(alias, 'github.com'));
|
|
119
|
+
assert.equal(fs.readFileSync(outside, 'utf8'), 'keep');
|
|
120
|
+
}
|
|
121
|
+
await service.generatePrivateKeyFile('仓库-main', 'private-key');
|
|
122
|
+
await service.generateSingleSshConfig(
|
|
123
|
+
'仓库-main',
|
|
124
|
+
'github.com',
|
|
125
|
+
'127.0.0.1:1080',
|
|
126
|
+
);
|
|
127
|
+
assert.equal(
|
|
128
|
+
fs.readFileSync(path.join(root, '仓库-main'), 'utf8').trim(),
|
|
129
|
+
'private-key',
|
|
130
|
+
);
|
|
131
|
+
await service.generatePrivateKeyFile('~global_default', 'global-key');
|
|
132
|
+
await service.generateGlobalSshConfig('~global_default');
|
|
133
|
+
assert.match(
|
|
134
|
+
fs.readFileSync(path.join(root, '~global_default.config'), 'utf8'),
|
|
135
|
+
/Host \*/,
|
|
136
|
+
);
|
|
137
|
+
await assert.rejects(
|
|
138
|
+
service.generateSingleSshConfig('safe', 'host\nProxyCommand bad'),
|
|
139
|
+
);
|
|
140
|
+
await assert.rejects(
|
|
141
|
+
service.generateSingleSshConfig('safe', 'host', '$(printf BAD)'),
|
|
142
|
+
);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
function cronService(config, updates = []) {
|
|
146
|
+
const Cron = load(path.resolve('back/services/cron.ts'), {
|
|
147
|
+
typedi: decorators,
|
|
148
|
+
'../config': config,
|
|
149
|
+
'../data/cron': {
|
|
150
|
+
CrontabStatus: { running: 0, idle: 1, queued: 3 },
|
|
151
|
+
CrontabModel: { update: async (v) => updates.push(v) },
|
|
152
|
+
},
|
|
153
|
+
'../data/runningInstance': { RunningInstanceModel: {}, InstanceStatus: {} },
|
|
154
|
+
'../config/util': { fileExist: async (p) => fs.existsSync(p) },
|
|
155
|
+
'../config/const': {},
|
|
156
|
+
'../schedule/client': {},
|
|
157
|
+
'../shared/pLimit': {},
|
|
158
|
+
'../shared/utils': {},
|
|
159
|
+
'../shared/i18n': translate,
|
|
160
|
+
'../shared/logStreamManager': {},
|
|
161
|
+
'../shared/cronSchedule': {},
|
|
162
|
+
}).default;
|
|
163
|
+
return new Cron(logger);
|
|
164
|
+
}
|
|
165
|
+
function subscriptionService(config, model = {}) {
|
|
166
|
+
const Subscription = load(path.resolve('back/services/subscription.ts'), {
|
|
167
|
+
typedi: decorators,
|
|
168
|
+
'../config': config,
|
|
169
|
+
'../data/subscription': {
|
|
170
|
+
SubscriptionModel: model,
|
|
171
|
+
SubscriptionStatus: {},
|
|
172
|
+
},
|
|
173
|
+
'../data/cron': {},
|
|
174
|
+
'../config/util': { fileExist: async (p) => fs.existsSync(p) },
|
|
175
|
+
'../config/const': {},
|
|
176
|
+
'../shared/i18n': translate,
|
|
177
|
+
'../shared/pLimit': {},
|
|
178
|
+
'../shared/logStreamManager': {},
|
|
179
|
+
'./schedule': {},
|
|
180
|
+
'./sock': {},
|
|
181
|
+
'./sshKey': {},
|
|
182
|
+
'./cron': {},
|
|
183
|
+
}).default;
|
|
184
|
+
return new Subscription(logger, {}, {}, {}, {});
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
test('stored cron/subscription paths cannot read or enumerate outside the log root', async (t) => {
|
|
188
|
+
const base = temporary(t);
|
|
189
|
+
const root = path.join(base, 'log');
|
|
190
|
+
fs.mkdirSync(root);
|
|
191
|
+
fs.mkdirSync(path.join(base, 'outside'));
|
|
192
|
+
const secret = path.join(base, 'outside', 'secret');
|
|
193
|
+
fs.writeFileSync(secret, 'SECRET');
|
|
194
|
+
fs.symlinkSync(path.dirname(secret), path.join(root, 'escape'));
|
|
195
|
+
fs.writeFileSync(path.join(root, 'normal.log'), 'NORMAL');
|
|
196
|
+
const updates = [];
|
|
197
|
+
const cron = cronService({ logPath: root }, updates);
|
|
198
|
+
const subscription = subscriptionService(
|
|
199
|
+
{ logPath: root },
|
|
200
|
+
{ update: async (v) => updates.push(v) },
|
|
201
|
+
);
|
|
202
|
+
for (const log_path of [secret, '../outside/secret', 'escape/secret']) {
|
|
203
|
+
cron.getDb = subscription.getDb = async () => ({ log_path, status: 1 });
|
|
204
|
+
await assert.rejects(cron.log(1), /outside the log directory/);
|
|
205
|
+
await assert.rejects(cron.logs(1), /outside the log directory/);
|
|
206
|
+
await assert.rejects(subscription.logs(1), /outside the log directory/);
|
|
207
|
+
await assert.rejects(
|
|
208
|
+
cron.status({ ids: [1], status: 0, log_path }),
|
|
209
|
+
/outside the log directory/,
|
|
210
|
+
);
|
|
211
|
+
await assert.rejects(
|
|
212
|
+
subscription.status({ ids: [1], status: 0, log_path }),
|
|
213
|
+
/outside the log directory/,
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
assert.equal(updates.length, 0);
|
|
217
|
+
cron.getDb = subscription.getDb = async () => ({
|
|
218
|
+
log_path: 'normal.log',
|
|
219
|
+
status: 1,
|
|
220
|
+
});
|
|
221
|
+
assert.equal((await cron.log(1)).content, 'NORMAL');
|
|
222
|
+
assert.ok((await cron.logs(1)).some((x) => x.filename === 'normal.log'));
|
|
223
|
+
assert.ok(
|
|
224
|
+
(await subscription.logs(1)).some((x) => x.filename === 'normal.log'),
|
|
225
|
+
);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
test('subscription aliases are checked before persistence and forced deletion', async (t) => {
|
|
229
|
+
const base = temporary(t);
|
|
230
|
+
for (const dir of ['scripts', 'repo']) fs.mkdirSync(path.join(base, dir));
|
|
231
|
+
let writes = 0;
|
|
232
|
+
const service = subscriptionService(
|
|
233
|
+
{
|
|
234
|
+
scriptPath: path.join(base, 'scripts'),
|
|
235
|
+
repoPath: path.join(base, 'repo'),
|
|
236
|
+
},
|
|
237
|
+
{
|
|
238
|
+
findAll: async () => [{ alias: '../victim' }],
|
|
239
|
+
destroy: async () => writes++,
|
|
240
|
+
},
|
|
241
|
+
);
|
|
242
|
+
service.insert = service.updateDb = async () => writes++;
|
|
243
|
+
await assert.rejects(
|
|
244
|
+
service.create({ alias: '../victim' }),
|
|
245
|
+
/Invalid subscription alias/,
|
|
246
|
+
);
|
|
247
|
+
await assert.rejects(
|
|
248
|
+
service.update({ alias: '../victim' }),
|
|
249
|
+
/Invalid subscription alias/,
|
|
250
|
+
);
|
|
251
|
+
await assert.rejects(
|
|
252
|
+
service.remove([1], { force: true }),
|
|
253
|
+
/Invalid subscription alias/,
|
|
254
|
+
);
|
|
255
|
+
assert.equal(writes, 0);
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
function systemService(config, overrides = {}) {
|
|
259
|
+
const System = load(path.resolve('back/services/system.ts'), {
|
|
260
|
+
typedi: decorators,
|
|
261
|
+
'../config': config,
|
|
262
|
+
'../config/const': {},
|
|
263
|
+
'../data/dependence': {
|
|
264
|
+
DependenceModel: { findAll: async () => [] },
|
|
265
|
+
DependenceTypes: {},
|
|
266
|
+
DependenceStatus: {},
|
|
267
|
+
},
|
|
268
|
+
'../data/system': {},
|
|
269
|
+
'../shared/pLimit': {},
|
|
270
|
+
'../schedule/client': {},
|
|
271
|
+
'../shared/i18n': { ...translate, setLang() {} },
|
|
272
|
+
'./notify': {},
|
|
273
|
+
'./schedule': {},
|
|
274
|
+
'./sock': {},
|
|
275
|
+
'../config/util': {},
|
|
276
|
+
...overrides,
|
|
277
|
+
}).default;
|
|
278
|
+
return new System(logger, {}, {});
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
test('backup rejects unrecognized paths before spawning; valid export uses argv without a shell', async (t) => {
|
|
282
|
+
const base = temporary(t);
|
|
283
|
+
const calls = [];
|
|
284
|
+
const service = systemService(
|
|
285
|
+
{
|
|
286
|
+
dataPath: path.join(base, 'data'),
|
|
287
|
+
dataTgzFile: path.join(base, 'backup file.tgz'),
|
|
288
|
+
},
|
|
289
|
+
{
|
|
290
|
+
child_process: {
|
|
291
|
+
execFile: (...args) => {
|
|
292
|
+
calls.push(args.slice(0, -1));
|
|
293
|
+
args.at(-1)(null, '', '');
|
|
294
|
+
},
|
|
295
|
+
},
|
|
296
|
+
'../config/util': {
|
|
297
|
+
promiseExec: () => assert.fail('must not invoke shell'),
|
|
298
|
+
},
|
|
299
|
+
},
|
|
300
|
+
);
|
|
301
|
+
let downloaded = false;
|
|
302
|
+
const res = {
|
|
303
|
+
status() {
|
|
304
|
+
return this;
|
|
305
|
+
},
|
|
306
|
+
send(body) {
|
|
307
|
+
return body;
|
|
308
|
+
},
|
|
309
|
+
download() {
|
|
310
|
+
downloaded = true;
|
|
311
|
+
},
|
|
312
|
+
};
|
|
313
|
+
for (const entry of [
|
|
314
|
+
'$(printf BAD)',
|
|
315
|
+
'../outside',
|
|
316
|
+
'/etc',
|
|
317
|
+
'scripts;echo BAD',
|
|
318
|
+
'--checkpoint-action=exec=bad',
|
|
319
|
+
]) {
|
|
320
|
+
assert.equal((await service.exportData(res, [entry])).code, 400);
|
|
321
|
+
}
|
|
322
|
+
assert.equal(calls.length, 0);
|
|
323
|
+
await service.exportData(res, ['base', 'scripts', 'config', 'scripts']);
|
|
324
|
+
assert.equal(calls.length, 1);
|
|
325
|
+
assert.equal(calls[0][0], 'tar');
|
|
326
|
+
assert.deepEqual(calls[0][1].slice(2), [
|
|
327
|
+
'--',
|
|
328
|
+
'data/db',
|
|
329
|
+
'data/upload',
|
|
330
|
+
'data/scripts',
|
|
331
|
+
'data/config',
|
|
332
|
+
]);
|
|
333
|
+
assert.equal(calls[0][2].shell, undefined);
|
|
334
|
+
assert.equal(downloaded, true);
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
test('proxy, language and mirror settings stay literal at their shell boundaries', async (t) => {
|
|
338
|
+
const base = temporary(t);
|
|
339
|
+
const commands = [];
|
|
340
|
+
const service = systemService(
|
|
341
|
+
{
|
|
342
|
+
dependenceProxyFile: path.join(base, 'proxy.sh'),
|
|
343
|
+
langEnvFile: path.join(base, 'lang.sh'),
|
|
344
|
+
},
|
|
345
|
+
{
|
|
346
|
+
'../config/util': {
|
|
347
|
+
promiseExec: async (command) => commands.push(command),
|
|
348
|
+
},
|
|
349
|
+
},
|
|
350
|
+
);
|
|
351
|
+
service.getSystemConfig = async () => ({ info: {} });
|
|
352
|
+
service.updateAuthDb = async () => {};
|
|
353
|
+
service.scheduleService = { runTask: (command) => commands.push(command) };
|
|
354
|
+
const value = 'x\'$(printf BAD)`printf BAD`"; echo BAD; #';
|
|
355
|
+
await service.updateDependenceProxy({ dependenceProxy: value });
|
|
356
|
+
await service.updateLanguage({ lang: value });
|
|
357
|
+
const stdout = execFileSync('/bin/bash', [
|
|
358
|
+
'-c',
|
|
359
|
+
'source "$1"; source "$2"; printf "%s\\0%s" "$http_proxy" "$QL_LANG"',
|
|
360
|
+
'bash',
|
|
361
|
+
path.join(base, 'proxy.sh'),
|
|
362
|
+
path.join(base, 'lang.sh'),
|
|
363
|
+
]).toString();
|
|
364
|
+
assert.equal(stdout, value + '\0' + value);
|
|
365
|
+
await service.updatePythonMirror({ pythonMirror: value });
|
|
366
|
+
await service.updateNodeMirror({ nodeMirror: value });
|
|
367
|
+
for (const command of commands) {
|
|
368
|
+
const stdout = execFileSync('/bin/bash', [
|
|
369
|
+
'-c',
|
|
370
|
+
`pip3() { printf '%s\\0' "$@"; }; pnpm() { printf '%s\\0' "$@"; }; ${command}`,
|
|
371
|
+
]).toString();
|
|
372
|
+
assert.equal(stdout.split('\0').at(-2), value);
|
|
373
|
+
}
|
|
374
|
+
assert.equal((await service.reloadSystem('$(printf BAD)')).code, 400);
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
test('the ignored-log sentinel remains supported without exposing device files', async (t) => {
|
|
378
|
+
const root = temporary(t);
|
|
379
|
+
const cron = cronService({ logPath: root });
|
|
380
|
+
cron.getDb = async () => ({ log_path: '/dev/null', log_name: '/dev/null' });
|
|
381
|
+
assert.equal((await cron.log(1)).status, 'ignored');
|
|
382
|
+
assert.deepEqual(await cron.logs(1), []);
|
|
383
|
+
await cron.status({ ids: [], status: 0, log_path: '/dev/null' });
|
|
384
|
+
await assert.rejects(
|
|
385
|
+
cron.status({ ids: [], status: 0, log_path: '/dev/zero' }),
|
|
386
|
+
);
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
test('normal backup creates an archive containing the selected data directories', async (t) => {
|
|
390
|
+
const base = temporary(t);
|
|
391
|
+
const dataPath = path.join(base, 'data');
|
|
392
|
+
for (const name of ['db', 'upload', 'scripts'])
|
|
393
|
+
fs.mkdirSync(path.join(dataPath, name), { recursive: true });
|
|
394
|
+
fs.writeFileSync(path.join(dataPath, 'scripts/example.js'), 'literal script');
|
|
395
|
+
const dataTgzFile = path.join(base, 'backup with spaces.tgz');
|
|
396
|
+
const service = systemService({ dataPath, dataTgzFile });
|
|
397
|
+
let downloaded;
|
|
398
|
+
await service.exportData(
|
|
399
|
+
{
|
|
400
|
+
download(file) {
|
|
401
|
+
downloaded = file;
|
|
402
|
+
},
|
|
403
|
+
send(error) {
|
|
404
|
+
assert.fail(JSON.stringify(error));
|
|
405
|
+
},
|
|
406
|
+
},
|
|
407
|
+
['scripts'],
|
|
408
|
+
);
|
|
409
|
+
assert.equal(downloaded, dataTgzFile);
|
|
410
|
+
const content = execFileSync('tar', [
|
|
411
|
+
'-xzOf',
|
|
412
|
+
dataTgzFile,
|
|
413
|
+
'data/scripts/example.js',
|
|
414
|
+
]).toString();
|
|
415
|
+
assert.equal(content, 'literal script');
|
|
416
|
+
});
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
const assert = require('node:assert/strict');
|
|
2
|
+
const test = require('node:test');
|
|
3
|
+
const fs = require('node:fs');
|
|
4
|
+
const os = require('node:os');
|
|
5
|
+
const path = require('node:path');
|
|
6
|
+
const { execFileSync } = require('node:child_process');
|
|
7
|
+
const express = require('express');
|
|
8
|
+
const load = require('../helpers/load-security-module.cjs');
|
|
9
|
+
|
|
10
|
+
test('Python environment generation escapes backslashes and triple quotes together', async () => {
|
|
11
|
+
const output = {};
|
|
12
|
+
const Env = load(path.resolve('back/services/env.ts'), {
|
|
13
|
+
typedi: { Service: () => (x) => x, Inject: () => () => {} },
|
|
14
|
+
'../config': { envFile: 'sh', jsEnvFile: 'js', pyEnvFile: 'py' },
|
|
15
|
+
'../data/env': { EnvStatus: { normal: 0 } },
|
|
16
|
+
'../data': {},
|
|
17
|
+
'../shared/utils': {
|
|
18
|
+
writeFileWithLock: async (file, text) => (output[file] = text),
|
|
19
|
+
},
|
|
20
|
+
}).default;
|
|
21
|
+
const values = [
|
|
22
|
+
"\\'''; injected = True; #",
|
|
23
|
+
"'''",
|
|
24
|
+
'\\\\\\',
|
|
25
|
+
'quotes\'"`\n中文\u2028',
|
|
26
|
+
'${1+2}',
|
|
27
|
+
];
|
|
28
|
+
const service = new Env({});
|
|
29
|
+
service.envs = async () =>
|
|
30
|
+
values.map((value, i) => ({ name: `VALUE_${i}`, value }));
|
|
31
|
+
await service.set_envs();
|
|
32
|
+
const result = execFileSync(
|
|
33
|
+
'python3',
|
|
34
|
+
[
|
|
35
|
+
'-c',
|
|
36
|
+
output.py +
|
|
37
|
+
'\nimport json\nprint(json.dumps([os.environ["VALUE_"+str(i)] for i in range(5)]))\nassert "injected" not in globals()',
|
|
38
|
+
],
|
|
39
|
+
{ encoding: 'utf8' },
|
|
40
|
+
);
|
|
41
|
+
assert.deepEqual(JSON.parse(result), values);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test('cron lookup treats regex and shell metacharacters as literal command text', () => {
|
|
45
|
+
const { findCronId } = load(path.resolve('back/shared/cronCommand.ts'));
|
|
46
|
+
const command = 'task file[1].js --value "$(touch ignored)"';
|
|
47
|
+
const crontab =
|
|
48
|
+
`* * * * * real_time=false no_tee=true ID=17 ${command}\n` +
|
|
49
|
+
'* * * * * real_time=false no_tee=true ID=18 task other.js\n';
|
|
50
|
+
assert.equal(findCronId(crontab, command), '17');
|
|
51
|
+
assert.equal(findCronId(crontab, 'task file1.js'), '');
|
|
52
|
+
assert.equal(findCronId(crontab, '.*'), '');
|
|
53
|
+
assert.equal(findCronId(crontab, 'task other.js'), '18');
|
|
54
|
+
assert.equal(findCronId('', command), '');
|
|
55
|
+
assert.equal(findCronId(crontab, ''), '');
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test('command route does not execute input while looking up the cron id', async (t) => {
|
|
59
|
+
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'ql-command-'));
|
|
60
|
+
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
|
|
61
|
+
const marker = path.join(root, 'unexpected');
|
|
62
|
+
const command = `task demo.js "$(touch ${marker})"`;
|
|
63
|
+
const crontabFile = path.join(root, 'crontab');
|
|
64
|
+
fs.writeFileSync(
|
|
65
|
+
crontabFile,
|
|
66
|
+
`* * * * * real_time=false no_tee=true ID=42 ${command}\n`,
|
|
67
|
+
);
|
|
68
|
+
const seen = [];
|
|
69
|
+
const app = express.Router();
|
|
70
|
+
load(path.resolve('back/api/system.ts'), {
|
|
71
|
+
'../config': { crontabFile, tmpPath: root },
|
|
72
|
+
'../services/system': {},
|
|
73
|
+
'../services/user': {},
|
|
74
|
+
'../shared/i18n': { t: (x) => x },
|
|
75
|
+
'../shared/logStreamManager': { logStreamManager: {} },
|
|
76
|
+
typedi: {
|
|
77
|
+
Container: { get: () => ({ run: async (value) => seen.push(value) }) },
|
|
78
|
+
},
|
|
79
|
+
'../config/util': {
|
|
80
|
+
getUniqPath: async (_command, id) => {
|
|
81
|
+
assert.equal(id, '42');
|
|
82
|
+
return 'test';
|
|
83
|
+
},
|
|
84
|
+
promiseExec: async (cmd) => {
|
|
85
|
+
return execFileSync('/bin/sh', ['-c', cmd], { encoding: 'utf8' });
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
}).default(app);
|
|
89
|
+
const router = app.stack.find((x) => x.name === 'router').handle;
|
|
90
|
+
const route = router.stack.find(
|
|
91
|
+
(x) => x.route?.path === '/command-run',
|
|
92
|
+
).route;
|
|
93
|
+
await route.stack
|
|
94
|
+
.at(-1)
|
|
95
|
+
.handle({ body: { command } }, { setHeader() {} }, (e) => {
|
|
96
|
+
throw e;
|
|
97
|
+
});
|
|
98
|
+
assert.equal(fs.existsSync(marker), false);
|
|
99
|
+
assert.equal(seen.length, 1);
|
|
100
|
+
assert.equal(seen[0].command, command);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test('command execution is limited before running across both API mounts', async (t) => {
|
|
104
|
+
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'ql-command-limit-'));
|
|
105
|
+
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
|
|
106
|
+
let runs = 0;
|
|
107
|
+
const app = express();
|
|
108
|
+
app.use(express.json());
|
|
109
|
+
const router = express.Router();
|
|
110
|
+
load(path.resolve('back/api/system.ts'), {
|
|
111
|
+
'../config': { crontabFile: path.join(root, 'missing'), tmpPath: root },
|
|
112
|
+
'../services/system': {},
|
|
113
|
+
'../services/user': {},
|
|
114
|
+
'../shared/i18n': { t: (x) => x },
|
|
115
|
+
'../shared/logStreamManager': {
|
|
116
|
+
logStreamManager: { closeStream: async () => {} },
|
|
117
|
+
},
|
|
118
|
+
'../config/util': {
|
|
119
|
+
getUniqPath: async () => 'test',
|
|
120
|
+
handleLogPath: async () => path.join(root, 'unused.log'),
|
|
121
|
+
},
|
|
122
|
+
typedi: {
|
|
123
|
+
Container: {
|
|
124
|
+
get: () => ({
|
|
125
|
+
run: async (value, callbacks) => {
|
|
126
|
+
runs++;
|
|
127
|
+
await callbacks.onEnd();
|
|
128
|
+
},
|
|
129
|
+
}),
|
|
130
|
+
},
|
|
131
|
+
},
|
|
132
|
+
}).default(router);
|
|
133
|
+
app.use('/api', router);
|
|
134
|
+
app.use('/open', router);
|
|
135
|
+
const server = await new Promise((resolve) => {
|
|
136
|
+
const s = app.listen(0, '127.0.0.1', () => resolve(s));
|
|
137
|
+
});
|
|
138
|
+
t.after(() => new Promise((resolve) => server.close(resolve)));
|
|
139
|
+
const base = `http://127.0.0.1:${server.address().port}`;
|
|
140
|
+
for (let i = 0; i < 61; i++) {
|
|
141
|
+
const response = await fetch(
|
|
142
|
+
`${base}/${i % 2 ? 'api' : 'open'}/system/command-run`,
|
|
143
|
+
{
|
|
144
|
+
method: 'PUT',
|
|
145
|
+
headers: { 'Content-Type': 'application/json' },
|
|
146
|
+
body: JSON.stringify({ command: 'echo safe' }),
|
|
147
|
+
},
|
|
148
|
+
);
|
|
149
|
+
await response.text();
|
|
150
|
+
assert.equal(response.status, i < 60 ? 200 : 429, `request ${i + 1}`);
|
|
151
|
+
}
|
|
152
|
+
assert.equal(runs, 60);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
test('anonymous token exchange is limited per source across both API mounts', async (t) => {
|
|
156
|
+
let attempts = 0;
|
|
157
|
+
const app = express();
|
|
158
|
+
const router = express.Router();
|
|
159
|
+
load(path.resolve('back/api/open.ts'), {
|
|
160
|
+
'../services/open': {},
|
|
161
|
+
typedi: {
|
|
162
|
+
Container: {
|
|
163
|
+
get: () => ({
|
|
164
|
+
authToken: async () => {
|
|
165
|
+
attempts++;
|
|
166
|
+
return { code: 401 };
|
|
167
|
+
},
|
|
168
|
+
}),
|
|
169
|
+
},
|
|
170
|
+
},
|
|
171
|
+
}).default(router);
|
|
172
|
+
app.use('/api', router);
|
|
173
|
+
app.use('/open', router);
|
|
174
|
+
const server = await new Promise((resolve) => {
|
|
175
|
+
const s = app.listen(0, '127.0.0.1', () => resolve(s));
|
|
176
|
+
});
|
|
177
|
+
t.after(() => new Promise((resolve) => server.close(resolve)));
|
|
178
|
+
const base = `http://127.0.0.1:${server.address().port}`;
|
|
179
|
+
for (let i = 0; i < 101; i++) {
|
|
180
|
+
const response = await fetch(
|
|
181
|
+
`${base}/${
|
|
182
|
+
i % 2 ? 'api' : 'open'
|
|
183
|
+
}/auth/token?client_id=id&client_secret=wrong`,
|
|
184
|
+
);
|
|
185
|
+
await response.text();
|
|
186
|
+
assert.equal(response.status, i < 100 ? 200 : 429, `request ${i + 1}`);
|
|
187
|
+
}
|
|
188
|
+
assert.equal(attempts, 100);
|
|
189
|
+
});
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
const test = require('node:test');
|
|
2
|
+
const assert = require('node:assert/strict');
|
|
3
|
+
const { createCronJob } = require('../../back/shared/cronScheduler');
|
|
4
|
+
const { isValidCronSchedule } = require('../../back/shared/cronSchedule');
|
|
5
|
+
|
|
6
|
+
// node-cron caches timezone formatters; model each deployment in a fresh process.
|
|
7
|
+
if (!process.env.QL_MACRO_TEST_TZ) {
|
|
8
|
+
for (const tz of ['UTC', 'Asia/Shanghai']) {
|
|
9
|
+
test(`macro execution in ${tz}`, () => {
|
|
10
|
+
const result = require('node:child_process').spawnSync(process.execPath,
|
|
11
|
+
['-r', 'ts-node/register/transpile-only', '--test', __filename],
|
|
12
|
+
{ env: { ...process.env, TZ: tz, QL_MACRO_TEST_TZ: tz }, encoding: 'utf8' });
|
|
13
|
+
assert.equal(result.status, 0, result.stdout + result.stderr);
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
for (const tz of process.env.QL_MACRO_TEST_TZ ? [process.env.QL_MACRO_TEST_TZ] : []) {
|
|
18
|
+
for (const [macro, start, next] of [
|
|
19
|
+
['@annually', [2026, 11, 31, 23, 59, 59], [[2027, 0, 1, 0, 0, 0], [2028, 0, 1, 0, 0, 0]]],
|
|
20
|
+
['@midnight', [2026, 8, 27, 23, 59, 59], [[2026, 8, 28, 0, 0, 0], [2026, 8, 29, 0, 0, 0]]],
|
|
21
|
+
['@minutely', [2026, 8, 27, 12, 34, 59], [[2026, 8, 27, 12, 35, 0], [2026, 8, 27, 12, 36, 0]]],
|
|
22
|
+
]) {
|
|
23
|
+
test(`${macro} fires at the expected local times in ${tz}`, async (t) => {
|
|
24
|
+
const previousTZ = process.env.TZ;
|
|
25
|
+
process.env.TZ = tz;
|
|
26
|
+
t.after(() => { if (previousTZ === undefined) delete process.env.TZ; else process.env.TZ = previousTZ; });
|
|
27
|
+
t.mock.timers.enable({ apis: ['Date', 'setTimeout', 'setInterval'], now: new Date(...start) });
|
|
28
|
+
const expected = next.map((parts) => new Date(...parts).getTime());
|
|
29
|
+
const actual = [];
|
|
30
|
+
assert.equal(isValidCronSchedule(` ${macro} `), true);
|
|
31
|
+
const job = createCronJob(macro, (date) => actual.push(date.getTime()), {
|
|
32
|
+
name: macro, logger: { warn() {}, error() {} },
|
|
33
|
+
});
|
|
34
|
+
t.after(() => job.cancel());
|
|
35
|
+
for (const time of expected) {
|
|
36
|
+
t.mock.timers.tick(time - Date.now() - 1);
|
|
37
|
+
await new Promise(setImmediate);
|
|
38
|
+
assert.equal(actual.includes(time), false, 'must not fire early');
|
|
39
|
+
t.mock.timers.tick(1);
|
|
40
|
+
await new Promise(setImmediate);
|
|
41
|
+
}
|
|
42
|
+
assert.deepEqual(actual, expected);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
test('new aliases do not enable unknown macros or raw parser extensions', () => {
|
|
48
|
+
for (const schedule of ['@annuallyx', '@midnightx', '@minutelyx', '@secondly', '@reboot', 'H * * * *', '0 /5 * * * *']) {
|
|
49
|
+
assert.equal(isValidCronSchedule(schedule), false, schedule);
|
|
50
|
+
}
|
|
51
|
+
});
|