@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,69 @@
|
|
|
1
|
+
const test = require('node:test');
|
|
2
|
+
const assert = require('node:assert/strict');
|
|
3
|
+
const { isValidCronSchedule } = require('../../back/shared/cronSchedule');
|
|
4
|
+
const { createCronJob } = require('../../back/shared/cronScheduler');
|
|
5
|
+
const legacy = require('../fixtures/legacy-cron.json');
|
|
6
|
+
const logger = { warn() {}, error() {} };
|
|
7
|
+
const flush = () => new Promise(setImmediate);
|
|
8
|
+
|
|
9
|
+
test('validation preserves legacy syntax except the explicitly added macros', () => {
|
|
10
|
+
const addedMacros = new Set(['@annually', '@midnight', '@minutely']);
|
|
11
|
+
assert.ok(legacy.validation.length > 250);
|
|
12
|
+
for (const { schedule, accepted } of legacy.validation) {
|
|
13
|
+
assert.equal(isValidCronSchedule(schedule), accepted || addedMacros.has(schedule), schedule);
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
for (const sample of legacy.times) {
|
|
18
|
+
test(`calendar compatibility: ${sample.schedule} from ${sample.currentDate}`, async (t) => {
|
|
19
|
+
const previousTZ = process.env.TZ;
|
|
20
|
+
process.env.TZ = 'UTC';
|
|
21
|
+
t.after(() => { if (previousTZ === undefined) delete process.env.TZ; else process.env.TZ = previousTZ; });
|
|
22
|
+
t.mock.timers.enable({ apis: ['Date', 'setTimeout', 'setInterval'], now: new Date(sample.currentDate) });
|
|
23
|
+
const actual = [];
|
|
24
|
+
const job = createCronJob(sample.schedule, (date) => actual.push(date.toISOString()), { name: 'calendar', logger });
|
|
25
|
+
t.after(() => job.cancel());
|
|
26
|
+
for (const expected of sample.next) {
|
|
27
|
+
t.mock.timers.tick(new Date(expected).getTime() - Date.now());
|
|
28
|
+
await flush();
|
|
29
|
+
}
|
|
30
|
+
assert.deepEqual(actual, sample.next);
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
test('late callbacks catch up exactly once and cancellation destroys native tasks', async (t) => {
|
|
35
|
+
t.mock.timers.enable({ apis: ['Date', 'setTimeout', 'setInterval'], now: new Date('2026-09-27T00:00:00Z') });
|
|
36
|
+
const native = require('node-cron');
|
|
37
|
+
const before = native.getTasks().size;
|
|
38
|
+
const dates = [], warnings = [];
|
|
39
|
+
const job = createCronJob('* * * * * *', (date) => dates.push(date.getTime()), {
|
|
40
|
+
name: 'late', logger: { warn: (...args) => warnings.push(args), error() {} },
|
|
41
|
+
});
|
|
42
|
+
t.after(() => job.cancel());
|
|
43
|
+
t.mock.timers.tick(8000);
|
|
44
|
+
await flush();
|
|
45
|
+
assert.equal(dates.length, 8);
|
|
46
|
+
assert.equal(new Set(dates).size, 8);
|
|
47
|
+
assert.ok(warnings.length > 0);
|
|
48
|
+
job.cancel();
|
|
49
|
+
t.mock.timers.tick(5000);
|
|
50
|
+
await flush();
|
|
51
|
+
assert.equal(dates.length, 8);
|
|
52
|
+
assert.equal(native.getTasks().size, before);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test('callback rejection is logged and future executions continue', async (t) => {
|
|
56
|
+
t.mock.timers.enable({ apis: ['Date', 'setTimeout', 'setInterval'], now: new Date('2026-09-27T00:00:00Z') });
|
|
57
|
+
const errors = [];
|
|
58
|
+
let count = 0;
|
|
59
|
+
const job = createCronJob('* * * * * *', async () => { count++; throw Error('callback failed'); }, {
|
|
60
|
+
name: 'failure', logger: { warn() {}, error: (...args) => errors.push(args) },
|
|
61
|
+
});
|
|
62
|
+
t.after(() => job.cancel());
|
|
63
|
+
t.mock.timers.tick(1000);
|
|
64
|
+
await flush();
|
|
65
|
+
t.mock.timers.tick(1000);
|
|
66
|
+
await flush();
|
|
67
|
+
assert.equal(count, 2);
|
|
68
|
+
assert.equal(errors.length, 2);
|
|
69
|
+
});
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
const test = require('node:test');
|
|
2
|
+
const assert = require('node:assert/strict');
|
|
3
|
+
const fs = require('node:fs');
|
|
4
|
+
const ts = require('typescript');
|
|
5
|
+
const { ScheduleType } = require('../../back/interface/schedule');
|
|
6
|
+
|
|
7
|
+
test('system and node modes route extended cron once and keep portable rules in crontab', async () => {
|
|
8
|
+
const source = fs.readFileSync('back/services/cron.ts', 'utf8');
|
|
9
|
+
const begin = source.indexOf(' private isNodeCron(');
|
|
10
|
+
const end = source.indexOf(' private async getLogName', begin);
|
|
11
|
+
const saveBegin = source.indexOf(' private async setCrontab(');
|
|
12
|
+
const saveEnd = source.indexOf(' public importCrontab(', saveBegin);
|
|
13
|
+
const js = ts.transpileModule(`class Fixture {${source.slice(begin, end)}${source.slice(saveBegin, saveEnd)}}; module.exports = Fixture;`, {
|
|
14
|
+
compilerOptions: { target: ts.ScriptTarget.ES2020 },
|
|
15
|
+
}).outputText;
|
|
16
|
+
for (const mode of ['system', 'node']) {
|
|
17
|
+
const module = { exports: {} };
|
|
18
|
+
let file = '', installs = 0;
|
|
19
|
+
new Function('module', 'process', 'execSync', 'ScheduleType', 'config', 'writeFileWithLock', 'CrontabModel', js)(
|
|
20
|
+
module, { env: { QL_SCHEDULER: mode } }, () => { installs++; }, ScheduleType,
|
|
21
|
+
{ crontabFile: '/unused' }, async (_, data) => { file = data; }, { update: async () => {} },
|
|
22
|
+
);
|
|
23
|
+
const service = new module.exports();
|
|
24
|
+
service.makeCommand = (row) => `task ${row.id}.js`;
|
|
25
|
+
const rows = [
|
|
26
|
+
{ id: 'plain', schedule: '*/5 0-23 * * 1,3' },
|
|
27
|
+
{ id: 'spaces', schedule: ' 0\t0 * * * ' },
|
|
28
|
+
{ id: 'numeric-step', schedule: '0/5 * * * *' },
|
|
29
|
+
{ id: 'sunday-seven', schedule: '0 0 * * 7' },
|
|
30
|
+
{ id: 'day-step', schedule: '0 0 */2 * *' },
|
|
31
|
+
{ id: 'restricted-days', schedule: '0 0 1-31 * 1' },
|
|
32
|
+
{ id: 'restricted-week', schedule: '0 0 1 * 0-6' },
|
|
33
|
+
{ id: 'annually', schedule: '@annually' },
|
|
34
|
+
{ id: 'midnight', schedule: '@midnight' },
|
|
35
|
+
{ id: 'minutely', schedule: '@minutely' },
|
|
36
|
+
{ id: 'seconds', schedule: '0 0 * * * *' },
|
|
37
|
+
{ id: 'question', schedule: '0 0 * * ?' },
|
|
38
|
+
{ id: 'last', schedule: '0 0 L * *' },
|
|
39
|
+
{ id: 'nth', schedule: '0 0 * * 1#2' },
|
|
40
|
+
{ id: 'named', schedule: '0 0 * JAN MON' },
|
|
41
|
+
{ id: 'macro', schedule: '@daily' },
|
|
42
|
+
{ id: 'short', schedule: '*' },
|
|
43
|
+
{ id: 'extra', schedule: '* * * * *', extra_schedules: [{ schedule: '0 * * * *' }] },
|
|
44
|
+
{ id: 'once', schedule: '@once' },
|
|
45
|
+
{ id: 'boot', schedule: '@boot' },
|
|
46
|
+
];
|
|
47
|
+
for (const row of rows) {
|
|
48
|
+
const special = ['once', 'boot'].includes(row.id);
|
|
49
|
+
const portable = ['plain', 'spaces'].includes(row.id);
|
|
50
|
+
assert.equal(service.shouldUseCronClient(row), !special && (mode === 'node' || !portable), `${mode}: ${row.id}`);
|
|
51
|
+
}
|
|
52
|
+
await service.setCrontab({ data: rows, total: rows.length });
|
|
53
|
+
assert.equal(installs, mode === 'system' ? 1 : 0);
|
|
54
|
+
for (const id of ['numeric-step', 'sunday-seven', 'day-step', 'restricted-days', 'restricted-week', 'annually', 'midnight', 'minutely', 'seconds', 'question', 'last', 'nth', 'named', 'macro', 'short', 'extra', 'once', 'boot']) {
|
|
55
|
+
const line = file.split('\n').find((line) => line.endsWith(`task ${id}.js`));
|
|
56
|
+
assert.ok(line.startsWith('# '), `${mode}: ${id} cannot also run from system crontab`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
});
|
|
@@ -0,0 +1,235 @@
|
|
|
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 express = require('express');
|
|
7
|
+
const load = require('../helpers/load-security-module.cjs');
|
|
8
|
+
|
|
9
|
+
function fixture(t, api = 'script') {
|
|
10
|
+
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'ql-draft-path-'));
|
|
11
|
+
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
|
|
12
|
+
for (const dir of [
|
|
13
|
+
'scripts',
|
|
14
|
+
'config/grpc',
|
|
15
|
+
'log',
|
|
16
|
+
'tmp',
|
|
17
|
+
'bak',
|
|
18
|
+
'outside',
|
|
19
|
+
]) {
|
|
20
|
+
fs.mkdirSync(path.join(root, dir), { recursive: true });
|
|
21
|
+
}
|
|
22
|
+
const config = { blackFileList: ['auth.json', 'token.json', 'grpc'] };
|
|
23
|
+
for (const [key, dir] of Object.entries({
|
|
24
|
+
scriptPath: 'scripts',
|
|
25
|
+
configPath: 'config',
|
|
26
|
+
logPath: 'log',
|
|
27
|
+
tmpPath: 'tmp',
|
|
28
|
+
bakPath: 'bak',
|
|
29
|
+
}))
|
|
30
|
+
config[key] = path.join(root, dir);
|
|
31
|
+
config.writePathList = [config.scriptPath, config.configPath];
|
|
32
|
+
const stopped = [];
|
|
33
|
+
const removed = [];
|
|
34
|
+
const service = {
|
|
35
|
+
stopScript: async (...args) => {
|
|
36
|
+
stopped.push(args);
|
|
37
|
+
return { code: 200 };
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
const app = express.Router();
|
|
41
|
+
load(path.resolve(`back/api/${api}.ts`), {
|
|
42
|
+
'../config': config,
|
|
43
|
+
'../config/const': {},
|
|
44
|
+
'../services/script': {},
|
|
45
|
+
'../services/scriptHistory': {},
|
|
46
|
+
'../services/config': {},
|
|
47
|
+
'../shared/i18n': { t: (x) => x },
|
|
48
|
+
typedi: { Container: { get: () => service } },
|
|
49
|
+
'../shared/utils': {
|
|
50
|
+
writeFileWithLock: (p, s) => fs.promises.writeFile(p, s),
|
|
51
|
+
},
|
|
52
|
+
'../config/util': {
|
|
53
|
+
fileExist: async (p) => fs.existsSync(p),
|
|
54
|
+
rmPath: async (p) => {
|
|
55
|
+
removed.push(p);
|
|
56
|
+
await fs.promises.rm(p, { recursive: true, force: true });
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
}).default(app);
|
|
60
|
+
const router = app.stack.find((x) => x.name === 'router').handle;
|
|
61
|
+
const invoke = async (method, url, body) => {
|
|
62
|
+
const route = router.stack.find(
|
|
63
|
+
(x) => x.route?.path === url && x.route.methods[method],
|
|
64
|
+
).route;
|
|
65
|
+
let result;
|
|
66
|
+
await route.stack
|
|
67
|
+
.at(-1)
|
|
68
|
+
.handle({ body }, { send: (x) => (result = x) }, (e) => {
|
|
69
|
+
throw e;
|
|
70
|
+
});
|
|
71
|
+
return result;
|
|
72
|
+
};
|
|
73
|
+
return { root, config, stopped, removed, invoke };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
test('script creation only creates the validated destination parent', async (t) => {
|
|
77
|
+
const { root, config, invoke } = fixture(t);
|
|
78
|
+
const outside = path.join(root, 'outside', 'unrequested');
|
|
79
|
+
const result = await invoke('post', '/', {
|
|
80
|
+
path: `${config.scriptPath}/../outside/unrequested`,
|
|
81
|
+
filename: '../../scripts/safe.txt',
|
|
82
|
+
content: 'safe',
|
|
83
|
+
});
|
|
84
|
+
assert.equal(result.code, 200);
|
|
85
|
+
assert.equal(
|
|
86
|
+
fs.readFileSync(path.join(config.scriptPath, 'safe.txt'), 'utf8'),
|
|
87
|
+
'safe',
|
|
88
|
+
);
|
|
89
|
+
assert.equal(fs.existsSync(outside), false);
|
|
90
|
+
assert.equal(
|
|
91
|
+
(
|
|
92
|
+
await invoke('post', '/', {
|
|
93
|
+
path: `${config.scriptPath}/../../escape`,
|
|
94
|
+
filename: 'poc.txt',
|
|
95
|
+
content: 'blocked',
|
|
96
|
+
})
|
|
97
|
+
).code,
|
|
98
|
+
403,
|
|
99
|
+
);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test('script creation supports root and nested parents in both writable directories', async (t) => {
|
|
103
|
+
const { config, invoke } = fixture(t);
|
|
104
|
+
for (const root of [config.scriptPath, config.configPath]) {
|
|
105
|
+
for (const filename of ['ordinary.js', 'nested/deeper/script.js']) {
|
|
106
|
+
assert.equal(
|
|
107
|
+
(await invoke('post', '/', {
|
|
108
|
+
path: root,
|
|
109
|
+
filename,
|
|
110
|
+
content: 'safe',
|
|
111
|
+
})).code,
|
|
112
|
+
200,
|
|
113
|
+
);
|
|
114
|
+
assert.equal(fs.readFileSync(path.join(root, filename), 'utf8'), 'safe');
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
test('script parent creation rejects sibling prefixes and external symlinks', async (t) => {
|
|
120
|
+
const { root, config, invoke } = fixture(t);
|
|
121
|
+
const sibling = config.scriptPath + '-sibling';
|
|
122
|
+
const outside = path.join(root, 'outside');
|
|
123
|
+
fs.mkdirSync(sibling);
|
|
124
|
+
fs.symlinkSync(outside, path.join(config.scriptPath, 'escape'));
|
|
125
|
+
for (const parent of [sibling, path.join(config.scriptPath, 'escape')]) {
|
|
126
|
+
assert.equal(
|
|
127
|
+
(await invoke('post', '/', {
|
|
128
|
+
path: parent,
|
|
129
|
+
filename: 'nested/poc.js',
|
|
130
|
+
content: 'blocked',
|
|
131
|
+
})).code,
|
|
132
|
+
403,
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
assert.equal(fs.existsSync(path.join(sibling, 'nested')), false);
|
|
136
|
+
assert.equal(fs.existsSync(path.join(outside, 'nested')), false);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
test('stopping scripts rejects logs outside the log root before any side effect', async (t) => {
|
|
140
|
+
const { root, config, stopped, invoke } = fixture(t);
|
|
141
|
+
const callbacks = [];
|
|
142
|
+
t.mock.method(global, 'setTimeout', (fn) => {
|
|
143
|
+
callbacks.push(fn);
|
|
144
|
+
return {};
|
|
145
|
+
});
|
|
146
|
+
fs.mkdirSync(path.join(config.configPath, 'victim.swap'));
|
|
147
|
+
fs.mkdirSync(path.join(config.scriptPath, 'linked'));
|
|
148
|
+
fs.symlinkSync(
|
|
149
|
+
path.join(root, 'outside'),
|
|
150
|
+
path.join(config.logPath, 'linked'),
|
|
151
|
+
);
|
|
152
|
+
for (const dir of ['../config', 'linked']) {
|
|
153
|
+
assert.equal(
|
|
154
|
+
(await invoke('put', '/stop', { filename: 'victim.js', path: dir })).code,
|
|
155
|
+
403,
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
assert.equal(stopped.length, 0);
|
|
159
|
+
assert.equal(callbacks.length, 0);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
test('log cleanup revalidates delayed paths and still removes ordinary debug logs', async (t) => {
|
|
163
|
+
const { root, config, removed, stopped, invoke } = fixture(t);
|
|
164
|
+
const callbacks = [];
|
|
165
|
+
t.mock.method(global, 'setTimeout', (fn) => {
|
|
166
|
+
callbacks.push(fn);
|
|
167
|
+
return {};
|
|
168
|
+
});
|
|
169
|
+
fs.mkdirSync(path.join(config.scriptPath, 'nested'));
|
|
170
|
+
fs.mkdirSync(path.join(config.logPath, 'nested'));
|
|
171
|
+
fs.mkdirSync(path.join(root, 'outside', 'victim.swap'));
|
|
172
|
+
fs.mkdirSync(path.join(config.logPath, 'normal.swap'));
|
|
173
|
+
assert.equal(
|
|
174
|
+
(await invoke('put', '/stop', { filename: 'normal.js' })).code,
|
|
175
|
+
200,
|
|
176
|
+
);
|
|
177
|
+
assert.equal(
|
|
178
|
+
(await invoke('put', '/stop', { filename: 'victim.js', path: 'nested' }))
|
|
179
|
+
.code,
|
|
180
|
+
200,
|
|
181
|
+
);
|
|
182
|
+
fs.rmdirSync(path.join(config.logPath, 'nested'));
|
|
183
|
+
fs.symlinkSync(
|
|
184
|
+
path.join(root, 'outside'),
|
|
185
|
+
path.join(config.logPath, 'nested'),
|
|
186
|
+
);
|
|
187
|
+
callbacks.forEach((fn) => fn());
|
|
188
|
+
assert.equal(stopped.length, 2);
|
|
189
|
+
assert.deepEqual(removed, [path.join(config.logPath, 'normal.swap')]);
|
|
190
|
+
assert.equal(fs.existsSync(path.join(root, 'outside', 'victim.swap')), true);
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
test('draft configuration traversal and normalized secret names cannot write files', async (t) => {
|
|
194
|
+
const { config, invoke } = fixture(t, 'config');
|
|
195
|
+
fs.writeFileSync(path.join(config.configPath, 'auth.json'), 'secret');
|
|
196
|
+
fs.symlinkSync(
|
|
197
|
+
path.join(config.configPath, 'auth.json'),
|
|
198
|
+
path.join(config.configPath, 'alias'),
|
|
199
|
+
);
|
|
200
|
+
for (const name of [
|
|
201
|
+
'auth.json',
|
|
202
|
+
'sub/../auth.json',
|
|
203
|
+
'sub/../token.json',
|
|
204
|
+
'grpc/client.key',
|
|
205
|
+
'../../shell/preload/env.sh',
|
|
206
|
+
'../scripts/poc.js',
|
|
207
|
+
'data/scripts/../config/auth.json',
|
|
208
|
+
'alias',
|
|
209
|
+
]) {
|
|
210
|
+
assert.equal(
|
|
211
|
+
(await invoke('post', '/save', { name, content: 'changed' })).code,
|
|
212
|
+
403,
|
|
213
|
+
name,
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
assert.equal(
|
|
217
|
+
fs.readFileSync(path.join(config.configPath, 'auth.json'), 'utf8'),
|
|
218
|
+
'secret',
|
|
219
|
+
);
|
|
220
|
+
assert.equal(
|
|
221
|
+
(await invoke('post', '/save', { name: 'config.sh', content: 'normal' }))
|
|
222
|
+
.code,
|
|
223
|
+
200,
|
|
224
|
+
);
|
|
225
|
+
assert.equal(
|
|
226
|
+
(
|
|
227
|
+
await invoke('post', '/save', {
|
|
228
|
+
name: 'data/scripts/normal.js',
|
|
229
|
+
content: 'normal',
|
|
230
|
+
})
|
|
231
|
+
).code,
|
|
232
|
+
403,
|
|
233
|
+
);
|
|
234
|
+
assert.equal(fs.existsSync(path.join(config.scriptPath, 'normal.js')), false);
|
|
235
|
+
});
|
|
@@ -0,0 +1,91 @@
|
|
|
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 express = require('express');
|
|
7
|
+
const load = require('../helpers/load-security-module.cjs');
|
|
8
|
+
|
|
9
|
+
test('environment import never writes upload filenames, including rejected files', async (t) => {
|
|
10
|
+
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'ql-env-import-'));
|
|
11
|
+
fs.writeFileSync(path.join(root, 'sendNotify.js'), 'ORIGINAL');
|
|
12
|
+
const created = [];
|
|
13
|
+
const Env = class {};
|
|
14
|
+
const app = express();
|
|
15
|
+
load(path.resolve('back/api/env.ts'), {
|
|
16
|
+
'../config': { scriptPath: root },
|
|
17
|
+
'../config/util': {
|
|
18
|
+
safeJSONParse: (value) => {
|
|
19
|
+
try {
|
|
20
|
+
return JSON.parse(value);
|
|
21
|
+
} catch {
|
|
22
|
+
return {};
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
'../services/env': Env,
|
|
27
|
+
'../shared/i18n': { t: (s) => s },
|
|
28
|
+
typedi: {
|
|
29
|
+
Container: {
|
|
30
|
+
get: () => ({
|
|
31
|
+
create: async (values) => {
|
|
32
|
+
created.push(values);
|
|
33
|
+
return values;
|
|
34
|
+
},
|
|
35
|
+
}),
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
}).default(app);
|
|
39
|
+
app.use((err, _req, res, _next) =>
|
|
40
|
+
res.status(400).json({ code: 400, message: err.message }),
|
|
41
|
+
);
|
|
42
|
+
const server = app.listen(0, '127.0.0.1');
|
|
43
|
+
t.after(async () => {
|
|
44
|
+
await new Promise((resolve) => server.close(resolve));
|
|
45
|
+
fs.rmSync(root, { recursive: true, force: true });
|
|
46
|
+
});
|
|
47
|
+
await new Promise((resolve, reject) => {
|
|
48
|
+
server.on('listening', resolve);
|
|
49
|
+
server.on('error', reject);
|
|
50
|
+
});
|
|
51
|
+
const upload = async (content, filename = 'sendNotify.js') => {
|
|
52
|
+
const form = new FormData();
|
|
53
|
+
form.append('env', new Blob([content]), filename);
|
|
54
|
+
const response = await fetch(
|
|
55
|
+
`http://127.0.0.1:${server.address().port}/envs/upload`,
|
|
56
|
+
{ method: 'POST', body: form },
|
|
57
|
+
);
|
|
58
|
+
return response.json();
|
|
59
|
+
};
|
|
60
|
+
for (const payload of [
|
|
61
|
+
'globalThis.injected=true;',
|
|
62
|
+
'null',
|
|
63
|
+
'[]',
|
|
64
|
+
'[{"name":"BAD;CODE","value":"x"}]',
|
|
65
|
+
'[{"name":"X","value":{}}]',
|
|
66
|
+
]) {
|
|
67
|
+
assert.equal((await upload(payload)).code, 400);
|
|
68
|
+
assert.equal(
|
|
69
|
+
fs.readFileSync(path.join(root, 'sendNotify.js'), 'utf8'),
|
|
70
|
+
'ORIGINAL',
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
assert.equal((await upload('x'.repeat(5 * 1024 * 1024 + 1))).code, 400);
|
|
74
|
+
assert.equal(created.length, 0);
|
|
75
|
+
assert.equal(
|
|
76
|
+
(
|
|
77
|
+
await upload(
|
|
78
|
+
'[{"name":"TOKEN","value":"literal ${value}","remarks":"ok"}]',
|
|
79
|
+
'../sendNotify.js',
|
|
80
|
+
)
|
|
81
|
+
).code,
|
|
82
|
+
200,
|
|
83
|
+
);
|
|
84
|
+
assert.equal(created.length, 1);
|
|
85
|
+
assert.equal(created[0][0].value, 'literal ${value}');
|
|
86
|
+
assert.deepEqual(fs.readdirSync(root), ['sendNotify.js']);
|
|
87
|
+
assert.equal(
|
|
88
|
+
fs.readFileSync(path.join(root, 'sendNotify.js'), 'utf8'),
|
|
89
|
+
'ORIGINAL',
|
|
90
|
+
);
|
|
91
|
+
});
|
|
@@ -0,0 +1,180 @@
|
|
|
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 express = require('express');
|
|
7
|
+
const load = require('../helpers/load-security-module.cjs');
|
|
8
|
+
|
|
9
|
+
function fixture(t, customData = false) {
|
|
10
|
+
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'ql-access-scope-'));
|
|
11
|
+
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
|
|
12
|
+
const data = path.join(root, customData ? 'custom-data' : 'data');
|
|
13
|
+
const config = {
|
|
14
|
+
rootPath: root,
|
|
15
|
+
configPath: path.join(data, 'config'),
|
|
16
|
+
scriptPath: path.join(data, 'scripts'),
|
|
17
|
+
logPath: path.join(data, 'log'),
|
|
18
|
+
blackFileList: ['token.json', 'auth.json', 'grpc', 'env.js'],
|
|
19
|
+
};
|
|
20
|
+
for (const directory of [
|
|
21
|
+
config.configPath,
|
|
22
|
+
config.scriptPath,
|
|
23
|
+
config.logPath,
|
|
24
|
+
])
|
|
25
|
+
fs.mkdirSync(directory, { recursive: true });
|
|
26
|
+
const secret = path.join(config.configPath, 'token.json');
|
|
27
|
+
fs.writeFileSync(secret, 'PANEL-SECRET');
|
|
28
|
+
for (const directory of [
|
|
29
|
+
config.configPath,
|
|
30
|
+
config.scriptPath,
|
|
31
|
+
config.logPath,
|
|
32
|
+
])
|
|
33
|
+
fs.symlinkSync(secret, path.join(directory, 'secret-link'));
|
|
34
|
+
const mocks = {
|
|
35
|
+
'../config': config,
|
|
36
|
+
typedi: { Service: () => (x) => x, Inject: () => () => {} },
|
|
37
|
+
'../config/util': {
|
|
38
|
+
getFileContentByName: (p) => fs.promises.readFile(p, 'utf8'),
|
|
39
|
+
rmPath: (p) => fs.promises.rm(p),
|
|
40
|
+
removeAnsi: (s) => s,
|
|
41
|
+
},
|
|
42
|
+
'../shared/utils': {
|
|
43
|
+
writeFileWithLock: (p, s) => fs.promises.writeFile(p, s),
|
|
44
|
+
},
|
|
45
|
+
'../shared/i18n': { t: (s) => s },
|
|
46
|
+
'../config/const': {},
|
|
47
|
+
'../data/runningInstance': {
|
|
48
|
+
InstanceStatus: { running: 'running' },
|
|
49
|
+
RunningInstanceModel: { findOne: async () => null },
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
return { config, mocks, secret };
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function api(name, service, mocks) {
|
|
56
|
+
mocks.typedi = { Container: { get: () => service } };
|
|
57
|
+
const app = express.Router();
|
|
58
|
+
load(path.resolve(`back/api/${name}.ts`), mocks).default(app);
|
|
59
|
+
const router = app.stack.find((layer) => layer.name === 'router').handle;
|
|
60
|
+
return async (method, url, values) => {
|
|
61
|
+
const handler = router.stack
|
|
62
|
+
.find((layer) => layer.route?.path === url && layer.route.methods[method])
|
|
63
|
+
.route.stack.at(-1).handle;
|
|
64
|
+
let result;
|
|
65
|
+
await handler(
|
|
66
|
+
{ query: values, body: values },
|
|
67
|
+
{
|
|
68
|
+
send: (value) => {
|
|
69
|
+
result = value;
|
|
70
|
+
},
|
|
71
|
+
download: (p) => {
|
|
72
|
+
result = { code: 200, data: fs.readFileSync(p, 'utf8') };
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
(error) => {
|
|
76
|
+
throw error;
|
|
77
|
+
},
|
|
78
|
+
);
|
|
79
|
+
return result;
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
for (const customData of [false, true]) {
|
|
84
|
+
test(`config APIs reject script compatibility paths (custom=${customData})`, async (t) => {
|
|
85
|
+
const { config, mocks, secret } = fixture(t, customData);
|
|
86
|
+
const Config = load(path.resolve('back/services/config.ts'), mocks).default;
|
|
87
|
+
mocks['../services/config'] = Config;
|
|
88
|
+
const invoke = api('config', new Config(), mocks);
|
|
89
|
+
const legacy = path.join(config.rootPath, 'data/scripts');
|
|
90
|
+
if (customData) {
|
|
91
|
+
fs.mkdirSync(legacy, { recursive: true });
|
|
92
|
+
fs.writeFileSync(path.join(legacy, 'notify.py'), 'legacy');
|
|
93
|
+
}
|
|
94
|
+
for (const filename of [
|
|
95
|
+
'token.json',
|
|
96
|
+
'auth.json',
|
|
97
|
+
'grpc/env.js',
|
|
98
|
+
'notify.py',
|
|
99
|
+
]) {
|
|
100
|
+
const file = path.join(config.scriptPath, filename);
|
|
101
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
102
|
+
fs.writeFileSync(file, 'initial');
|
|
103
|
+
const name = `data/scripts/${filename}`;
|
|
104
|
+
assert.equal((await invoke('get', '/detail', { path: name })).code, 403);
|
|
105
|
+
assert.equal(
|
|
106
|
+
(await invoke('post', '/save', { name, content: 'updated' })).code,
|
|
107
|
+
403,
|
|
108
|
+
);
|
|
109
|
+
assert.equal(fs.readFileSync(file, 'utf8'), 'initial');
|
|
110
|
+
}
|
|
111
|
+
if (customData)
|
|
112
|
+
assert.equal(
|
|
113
|
+
fs.readFileSync(path.join(legacy, 'notify.py'), 'utf8'),
|
|
114
|
+
'legacy',
|
|
115
|
+
);
|
|
116
|
+
for (const name of [
|
|
117
|
+
'token.json',
|
|
118
|
+
'grpc/client.key',
|
|
119
|
+
'secret-link',
|
|
120
|
+
'data/scripts/secret-link',
|
|
121
|
+
'data/scripts/../config/token.json',
|
|
122
|
+
secret,
|
|
123
|
+
]) {
|
|
124
|
+
assert.equal(
|
|
125
|
+
(await invoke('get', '/detail', { path: name })).code,
|
|
126
|
+
403,
|
|
127
|
+
name,
|
|
128
|
+
);
|
|
129
|
+
assert.equal(
|
|
130
|
+
(await invoke('post', '/save', { name, content: 'overwrite' })).code,
|
|
131
|
+
403,
|
|
132
|
+
name,
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
assert.equal(
|
|
136
|
+
(await invoke('post', '/save', { name: 'normal.txt', content: 'normal' }))
|
|
137
|
+
.code,
|
|
138
|
+
200,
|
|
139
|
+
);
|
|
140
|
+
assert.equal(
|
|
141
|
+
(await invoke('get', '/detail', { path: 'normal.txt' })).data,
|
|
142
|
+
'normal',
|
|
143
|
+
);
|
|
144
|
+
assert.equal(fs.readFileSync(secret, 'utf8'), 'PANEL-SECRET');
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
test('log APIs allow configuration-like names while retaining directory boundaries', async (t) => {
|
|
149
|
+
const { config, mocks, secret } = fixture(t);
|
|
150
|
+
const Log = load(path.resolve('back/services/log.ts'), mocks).default;
|
|
151
|
+
mocks['../services/log'] = Log;
|
|
152
|
+
const invoke = api('log', new Log(), mocks);
|
|
153
|
+
for (const filename of ['token.json', 'grpc/run.log', 'env.js/run.log']) {
|
|
154
|
+
const file = path.join(config.logPath, filename);
|
|
155
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
156
|
+
fs.writeFileSync(file, 'log content');
|
|
157
|
+
const values = {
|
|
158
|
+
path: path.dirname(filename) === '.' ? '' : path.dirname(filename),
|
|
159
|
+
file: path.basename(filename),
|
|
160
|
+
filename: path.basename(filename),
|
|
161
|
+
};
|
|
162
|
+
assert.equal((await invoke('get', '/detail', values)).data, 'log content');
|
|
163
|
+
assert.equal(
|
|
164
|
+
(await invoke('post', '/download', values)).data,
|
|
165
|
+
'log content',
|
|
166
|
+
);
|
|
167
|
+
assert.equal((await invoke('delete', '/', values)).code, 200);
|
|
168
|
+
assert.equal(fs.existsSync(file), false);
|
|
169
|
+
}
|
|
170
|
+
for (const filename of ['../config/token.json', 'secret-link', secret]) {
|
|
171
|
+
const values = { path: '', file: filename, filename };
|
|
172
|
+
for (const [method, route] of [
|
|
173
|
+
['get', '/detail'],
|
|
174
|
+
['post', '/download'],
|
|
175
|
+
['delete', '/'],
|
|
176
|
+
])
|
|
177
|
+
assert.equal((await invoke(method, route, values)).code, 403, filename);
|
|
178
|
+
}
|
|
179
|
+
assert.equal(fs.readFileSync(secret, 'utf8'), 'PANEL-SECRET');
|
|
180
|
+
});
|