@whyour/qinglong 2.21.0-8 → 2.22.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/.dockerignore +13 -0
- package/.env.example +7 -0
- package/AGENTS.md +2 -2
- package/README-en.md +24 -0
- package/README.md +24 -0
- package/back/protos/api.proto +6 -1
- package/back/protos/cron.proto +4 -1
- package/docker/Dockerfile +31 -12
- package/docker/Dockerfile.310 +31 -12
- package/docker/Dockerfile.debian +22 -14
- package/docker/Dockerfile.debian310 +24 -14
- package/docker/build-manifest.cjs +28 -0
- package/docker/verify-build.cjs +49 -0
- package/docs/releases/2.22.0-validation.md +49 -0
- package/docs/releases/2.22.0.md +54 -0
- package/ecosystem.config.js +10 -1
- package/package.json +11 -6
- package/sample/config.sample.sh +10 -0
- package/sample/notify.js +54 -0
- package/sample/notify.py +37 -0
- package/scripts/benchmark-execution.cjs +96 -0
- package/scripts/write-build-info.cjs +27 -0
- package/shell/api.sh +34 -13
- package/shell/check.sh +59 -19
- package/shell/lang/en.sh +6 -0
- package/shell/lang/zh.sh +6 -0
- package/shell/node_path_cache.sh +128 -0
- package/shell/otask.sh +7 -1
- package/shell/preload/sitecustomize.js +5 -1
- package/shell/share.sh +25 -8
- package/shell/start.sh +6 -3
- package/shell/task.sh +13 -3
- package/shell/update.sh +65 -4
- package/static/build/api/clientIp.js +42 -0
- package/static/build/api/config.js +8 -16
- package/static/build/api/cron.js +38 -2
- package/static/build/api/dashboard.js +37 -0
- package/static/build/api/health.js +2 -2
- package/static/build/api/index.js +4 -0
- package/static/build/api/log.js +30 -19
- package/static/build/api/retention.js +63 -0
- package/static/build/api/script.js +23 -22
- package/static/build/api/subscription.js +16 -2
- package/static/build/api/system.js +25 -2
- package/static/build/api/user.js +75 -15
- package/static/build/app.js +54 -45
- package/static/build/bootstrap/database.js +51 -0
- package/static/build/config/const.js +1 -0
- package/static/build/config/share.js +5 -4
- package/static/build/config/util.js +133 -38
- package/static/build/data/cron.js +1 -0
- package/static/build/data/notify.js +11 -1
- package/static/build/loaders/app.js +2 -0
- package/static/build/loaders/db.js +3 -33
- package/static/build/loaders/deps.js +29 -0
- package/static/build/loaders/express.js +30 -21
- package/static/build/loaders/initData.js +10 -7
- package/static/build/loaders/sock.js +72 -6
- package/static/build/middlewares/monitoring.js +12 -33
- package/static/build/middlewares/protectedPathCase.js +17 -0
- package/static/build/protos/api.js +56 -2
- package/static/build/protos/cron.js +21 -4
- package/static/build/protos/health.js +1 -1
- package/static/build/schedule/addCron.js +9 -0
- package/static/build/schedule/api.js +5 -2
- package/static/build/schedule/client.js +38 -6
- package/static/build/schedule/health.js +72 -7
- package/static/build/services/config.js +5 -42
- package/static/build/services/cron.js +338 -199
- package/static/build/services/dependence.js +47 -0
- package/static/build/services/grpc.js +1 -1
- package/static/build/services/health.js +4 -7
- package/static/build/services/http.js +8 -2
- package/static/build/services/log.js +2 -3
- package/static/build/services/metrics.js +46 -14
- package/static/build/services/notify.js +26 -0
- package/static/build/services/retention.js +123 -0
- package/static/build/services/schedule.js +57 -33
- package/static/build/services/script.js +4 -5
- package/static/build/services/subscription.js +55 -34
- package/static/build/services/system.js +37 -9
- package/static/build/services/user.js +213 -25
- package/static/build/shared/auth.js +18 -5
- package/static/build/shared/authMutation.js +17 -0
- package/static/build/shared/childProcess.js +82 -0
- package/static/build/shared/clientIp.js +27 -0
- package/static/build/shared/errors.js +9 -0
- package/static/build/shared/fileAccess.js +60 -0
- package/static/build/shared/i18n.js +12 -5
- package/static/build/shared/logReader.js +85 -0
- package/static/build/shared/logStreamManager.js +80 -60
- package/static/build/shared/password.js +30 -0
- package/static/build/shared/retention.js +58 -0
- package/static/build/shared/runCron.js +28 -15
- package/static/build/shared/schedulerMutationLock.js +44 -0
- package/static/build/shared/schedulerReadiness.js +82 -0
- package/static/build/shared/schemaMigrations.js +58 -0
- package/static/build/shared/startupProcess.js +48 -0
- package/static/build/shared/store.js +7 -0
- package/static/build/shared/trustProxy.js +119 -0
- package/static/build/validation/schedule.js +3 -3
- package/static/dist/{1885.e1ea09f6.async.js → 1885.b8f427bf.async.js} +1 -1
- package/static/dist/2031.6af40025.async.js +1 -0
- package/static/dist/2693.51c2636b.async.js +1 -0
- package/static/dist/{2890.9e373f5b.async.js → 2890.add55e85.async.js} +1 -1
- package/static/dist/2901.2ca95295.async.js +1 -0
- package/static/dist/445.a65ff63b.async.js +1 -0
- package/static/dist/4736.1d2b5cb6.async.js +1 -0
- package/static/dist/{4859.6592cebb.async.js → 4859.97d6653c.async.js} +1 -1
- package/static/dist/{6013.4e38de36.async.js → 6013.db535339.async.js} +1 -1
- package/static/dist/6212.9e6d9913.async.js +1 -0
- package/static/dist/{6339.ab305e96.async.js → 6431.4603ad0e.async.js} +1 -1
- package/static/dist/{6872.42f939f1.async.js → 6872.a503ef55.async.js} +1 -1
- package/static/dist/7231.f7e5b668.async.js +1 -0
- package/static/dist/{7355.f9f263d8.async.js → 7355.fca746f5.async.js} +1 -1
- package/static/dist/{7508.7dcee91c.async.js → 7508.d90eac63.async.js} +1 -1
- package/static/dist/{7984.594296a5.async.js → 7984.eaac7402.async.js} +1 -1
- package/static/dist/8844.a0fef08e.async.js +1 -0
- package/static/dist/index.html +2 -2
- package/static/dist/layouts__index.633d3553.async.js +1 -0
- package/static/dist/preload_helper.cf7de3fa.js +1 -0
- package/static/dist/{src__pages__config__index.a22ff7dc.async.js → src__pages__config__index.9aba4054.async.js} +1 -1
- package/static/dist/src__pages__crontab__detail.86b2eebb.async.js +1 -0
- package/static/dist/src__pages__crontab__index.ae4e06f2.async.js +1 -0
- package/static/dist/{src__pages__crontab__logModal.831d5e4b.async.js → src__pages__crontab__logModal.930535e2.async.js} +1 -1
- package/static/dist/src__pages__crontab__modal.a2c8fdcc.async.js +1 -0
- package/static/dist/src__pages__crontab__viewCreateModal.7b4f9cbe.async.js +1 -0
- package/static/dist/src__pages__crontab__viewManageModal.cd454ea0.async.js +1 -0
- package/static/dist/src__pages__dashboard__index.243c5a35.async.js +1 -0
- package/static/dist/src__pages__dashboard__taskResultModal.7795d6e1.async.js +1 -0
- package/static/dist/src__pages__dependence__index.874d501f.async.js +1 -0
- package/static/dist/src__pages__dependence__logModal.154efe7a.async.js +1 -0
- package/static/dist/src__pages__dependence__modal.a29c1a0f.async.js +1 -0
- package/static/dist/{src__pages__diff__index.013b7c75.async.js → src__pages__diff__index.1714b941.async.js} +1 -1
- package/static/dist/src__pages__env__editNameModal.c13d47d2.async.js +1 -0
- package/static/dist/src__pages__env__index.fd6fd2b0.async.js +1 -0
- package/static/dist/src__pages__env__modal.4d71adb3.async.js +1 -0
- package/static/dist/src__pages__error__index.aae50c94.async.js +1 -0
- package/static/dist/src__pages__initialization__index.82a2e967.async.js +1 -0
- package/static/dist/src__pages__log__index.71250e0d.async.js +1 -0
- package/static/dist/{src__pages__login__index.8c813eb1.async.js → src__pages__login__index.a0fd5ed8.async.js} +1 -1
- package/static/dist/src__pages__script__editModal.22acdf7c.async.js +1 -0
- package/static/dist/src__pages__script__editNameModal.1a521cc0.async.js +1 -0
- package/static/dist/src__pages__script__index.a18b2692.async.js +1 -0
- package/static/dist/src__pages__script__renameModal.0ab029e7.async.js +1 -0
- package/static/dist/src__pages__script__saveModal.8965b74c.async.js +1 -0
- package/static/dist/src__pages__script__setting.35ad4f02.async.js +1 -0
- package/static/dist/src__pages__setting__appModal.7eaeb9d3.async.js +1 -0
- package/static/dist/{src__pages__setting__checkUpdate.34d773f2.async.js → src__pages__setting__checkUpdate.d175b154.async.js} +1 -1
- package/static/dist/src__pages__setting__clientIp.3a8711ed.async.js +1 -0
- package/static/dist/src__pages__setting__dependence.9c0f749b.async.js +1 -0
- package/static/dist/src__pages__setting__index.cb397741.async.js +1 -0
- package/static/dist/src__pages__setting__loginLog.e0a1f54d.async.js +1 -0
- package/static/dist/src__pages__setting__notification.2ffe4e5a.async.js +1 -0
- package/static/dist/src__pages__setting__other.3d0b8492.async.js +1 -0
- package/static/dist/{src__pages__setting__progress.8823ac90.async.js → src__pages__setting__progress.2ccae3d7.async.js} +1 -1
- package/static/dist/src__pages__setting__security.e2d71ebb.async.js +1 -0
- package/static/dist/src__pages__setting__systemLog.b3046955.async.js +1 -0
- package/static/dist/src__pages__subscription__index.43086a06.async.js +1 -0
- package/static/dist/src__pages__subscription__logModal.cdba5139.async.js +1 -0
- package/static/dist/src__pages__subscription__modal.38991e1e.async.js +1 -0
- package/static/dist/{umi.a54a59f6.js → umi.fc019050.js} +1 -1
- package/test/back/auth-security.test.cjs +74 -0
- package/test/back/build-provenance.test.cjs +150 -0
- package/test/back/cron-log-name.test.cjs +110 -0
- package/test/back/cron-validation.test.cjs +45 -0
- package/test/back/dashboard-failures.test.cjs +173 -0
- package/test/back/database-bootstrap.test.cjs +193 -0
- package/test/back/dependence-cache-status.test.cjs +125 -0
- package/test/back/deprecated-file-routes.test.cjs +82 -0
- package/test/back/ecosystem.test.cjs +28 -0
- package/test/back/env-name-parsing.test.cjs +96 -0
- package/test/back/execution-lifecycle.test.cjs +236 -0
- package/test/back/file-access-security.test.cjs +47 -0
- package/test/back/fileTreeListing.test.cjs +57 -0
- package/test/back/grpc-cron-response.test.cjs +60 -0
- package/test/back/http-exclusive-listen.test.cjs +31 -0
- package/test/back/http-security.test.cjs +213 -0
- package/test/back/listen-security.test.cjs +67 -0
- package/test/back/log-path-security.test.cjs +158 -0
- package/test/back/logReader.test.cjs +72 -0
- package/test/back/manual-execution.test.cjs +85 -0
- package/test/back/manual-stop-claim.test.cjs +220 -0
- package/test/back/metrics.test.cjs +34 -0
- package/test/back/monitoring.test.cjs +73 -0
- package/test/back/node-path-cache.test.cjs +173 -0
- package/test/back/node-path-lock.test.cjs +178 -0
- package/test/back/npm-publication.test.cjs +70 -0
- package/test/back/primary-apm.test.cjs +24 -0
- package/test/back/process-exit-state.test.cjs +81 -0
- package/test/back/process-stop-signal.test.cjs +77 -0
- package/test/back/protected-path-case.test.cjs +58 -0
- package/test/back/qlServiceLogCommand.test.cjs +128 -0
- package/test/back/retention.test.cjs +131 -0
- package/test/back/role-loading.test.cjs +118 -0
- package/test/back/schedule-health.test.cjs +86 -0
- package/test/back/scheduler-file-lock.test.cjs +51 -0
- package/test/back/scheduler-mutation.test.cjs +312 -0
- package/test/back/scheduler-readiness.test.cjs +85 -0
- package/test/back/scheduler-reconciliation.test.cjs +152 -0
- package/test/back/scheduler-rpc-timeout.test.cjs +49 -0
- package/test/back/schema-migrations.test.cjs +127 -0
- package/test/back/shell-api-parsing.test.cjs +184 -0
- package/test/back/sock-polling.test.cjs +297 -0
- package/test/back/sock-security.test.cjs +72 -0
- package/test/back/start-env-preservation.test.cjs +53 -0
- package/test/back/stop-failure-feedback.test.cjs +158 -0
- package/test/back/stop-race.test.cjs +167 -0
- package/test/back/subscription-cleanup.test.cjs +53 -0
- package/test/back/subscription-stop-state.test.cjs +66 -0
- package/test/back/system-dependence-cache.test.cjs +126 -0
- package/test/back/task-time.test.cjs +116 -0
- package/test/back/user-api.test.cjs +99 -0
- package/test/back/user-security.test.cjs +248 -0
- package/test/back/worker-apm.test.cjs +84 -0
- package/test/back/wpush-grpc.test.cjs +115 -0
- package/test/front/cron-log-pagination.test.cjs +214 -0
- package/test/front/http-auth.test.cjs +152 -0
- package/test/front/http-error.test.cjs +29 -0
- package/test/helpers/load-security-module.cjs +42 -0
- package/version.yaml +19 -34
- package/CLAUDE.md +0 -43
- package/static/dist/1147.62793604.async.js +0 -1
- package/static/dist/134.54df9e38.async.js +0 -1
- package/static/dist/2208.85285476.async.js +0 -1
- package/static/dist/2286.f371c743.async.js +0 -1
- package/static/dist/3191.545ab873.async.js +0 -1
- package/static/dist/4902.555f92ab.async.js +0 -1
- package/static/dist/5520.ff2cfd6b.async.js +0 -1
- package/static/dist/6431.c50479be.async.js +0 -1
- package/static/dist/8826.db34ad8d.async.js +0 -1
- package/static/dist/layouts__index.a7a2bfe0.async.js +0 -1
- package/static/dist/preload_helper.3048fe5b.js +0 -1
- package/static/dist/src__pages__crontab__detail.b98f0084.async.js +0 -1
- package/static/dist/src__pages__crontab__index.68e78a33.async.js +0 -1
- package/static/dist/src__pages__crontab__modal.f7041c7c.async.js +0 -1
- package/static/dist/src__pages__crontab__viewCreateModal.4d589f66.async.js +0 -1
- package/static/dist/src__pages__crontab__viewManageModal.0e317746.async.js +0 -1
- package/static/dist/src__pages__dashboard__index.58177357.async.js +0 -1
- package/static/dist/src__pages__dependence__index.b7bbc795.async.js +0 -1
- package/static/dist/src__pages__dependence__logModal.0681830b.async.js +0 -1
- package/static/dist/src__pages__dependence__modal.f7bb47ba.async.js +0 -1
- package/static/dist/src__pages__env__editNameModal.48715576.async.js +0 -1
- package/static/dist/src__pages__env__index.452cdc47.async.js +0 -1
- package/static/dist/src__pages__env__modal.e1234834.async.js +0 -1
- package/static/dist/src__pages__error__index.dedb2b90.async.js +0 -1
- package/static/dist/src__pages__initialization__index.22620a77.async.js +0 -1
- package/static/dist/src__pages__log__index.9c66639f.async.js +0 -1
- package/static/dist/src__pages__script__editModal.cbf4ec0e.async.js +0 -1
- package/static/dist/src__pages__script__editNameModal.532a3044.async.js +0 -1
- package/static/dist/src__pages__script__index.1fc20f22.async.js +0 -1
- package/static/dist/src__pages__script__renameModal.5cdde919.async.js +0 -1
- package/static/dist/src__pages__script__saveModal.5ffed35f.async.js +0 -1
- package/static/dist/src__pages__script__setting.1a82c6d7.async.js +0 -1
- package/static/dist/src__pages__setting__appModal.43b7758a.async.js +0 -1
- package/static/dist/src__pages__setting__dependence.9c04c79a.async.js +0 -1
- package/static/dist/src__pages__setting__index.7e070838.async.js +0 -1
- package/static/dist/src__pages__setting__loginLog.cea6056c.async.js +0 -1
- package/static/dist/src__pages__setting__notification.bac481c2.async.js +0 -1
- package/static/dist/src__pages__setting__other.707cfafe.async.js +0 -1
- package/static/dist/src__pages__setting__security.b7235753.async.js +0 -1
- package/static/dist/src__pages__setting__systemLog.b81a8c1d.async.js +0 -1
- package/static/dist/src__pages__subscription__index.d0ab2abc.async.js +0 -1
- package/static/dist/src__pages__subscription__logModal.3864b37f.async.js +0 -1
- package/static/dist/src__pages__subscription__modal.562448e2.async.js +0 -1
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
const assert = require('node:assert/strict');
|
|
2
|
+
const test = require('node:test');
|
|
3
|
+
const fs = require('node:fs/promises');
|
|
4
|
+
const path = require('node:path');
|
|
5
|
+
const os = require('node:os');
|
|
6
|
+
const { Sequelize, QueryTypes } = require('sequelize');
|
|
7
|
+
const { migrateSchema } = require('../../back/shared/schemaMigrations');
|
|
8
|
+
const load = require('../helpers/load-security-module.cjs');
|
|
9
|
+
|
|
10
|
+
async function createLegacy(storage = ':memory:', omitEnvs = false) {
|
|
11
|
+
const database = new Sequelize({
|
|
12
|
+
dialect: 'sqlite',
|
|
13
|
+
storage,
|
|
14
|
+
logging: false,
|
|
15
|
+
});
|
|
16
|
+
for (const table of [
|
|
17
|
+
'CrontabViews',
|
|
18
|
+
'Subscriptions',
|
|
19
|
+
'Crontabs',
|
|
20
|
+
...(omitEnvs ? [] : ['Envs']),
|
|
21
|
+
]) {
|
|
22
|
+
await database.query(
|
|
23
|
+
`CREATE TABLE "${table}" (id INTEGER PRIMARY KEY, name TEXT)`,
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
await database.query(
|
|
27
|
+
'INSERT INTO "Crontabs" (id, name) VALUES (1, \'keep-me\')',
|
|
28
|
+
);
|
|
29
|
+
return database;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
test('legacy database upgrades without losing rows and repeated migration is idempotent', async (t) => {
|
|
33
|
+
const database = await createLegacy();
|
|
34
|
+
t.after(() => database.close());
|
|
35
|
+
await migrateSchema(database);
|
|
36
|
+
await migrateSchema(database);
|
|
37
|
+
const rows = await database.query('SELECT * FROM "Crontabs"', {
|
|
38
|
+
type: QueryTypes.SELECT,
|
|
39
|
+
});
|
|
40
|
+
assert.equal(rows[0].name, 'keep-me');
|
|
41
|
+
assert.equal(rows[0].queued_token, null);
|
|
42
|
+
assert.ok(Object.hasOwn(rows[0], 'allow_multiple_instances'));
|
|
43
|
+
const applied = await database.query('SELECT id FROM "SchemaMigrations"', {
|
|
44
|
+
type: QueryTypes.SELECT,
|
|
45
|
+
});
|
|
46
|
+
assert.equal(applied.length, 15);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test('migration failure rolls back added columns and can be retried after repair', async (t) => {
|
|
50
|
+
const database = await createLegacy(':memory:', true);
|
|
51
|
+
t.after(() => database.close());
|
|
52
|
+
await assert.rejects(migrateSchema(database));
|
|
53
|
+
const columns = await database.getQueryInterface().describeTable('Crontabs');
|
|
54
|
+
assert.equal(Object.hasOwn(columns, 'work_dir'), false);
|
|
55
|
+
await database.query(
|
|
56
|
+
'CREATE TABLE "Envs" (id INTEGER PRIMARY KEY, name TEXT)',
|
|
57
|
+
);
|
|
58
|
+
await migrateSchema(database);
|
|
59
|
+
assert.ok(
|
|
60
|
+
Object.hasOwn(
|
|
61
|
+
await database.getQueryInterface().describeTable('Crontabs'),
|
|
62
|
+
'work_dir',
|
|
63
|
+
),
|
|
64
|
+
);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test('offline pre-upgrade backup restores the legacy schema and data', async (t) => {
|
|
68
|
+
const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'ql-migration-'));
|
|
69
|
+
t.after(() => fs.rm(dir, { recursive: true, force: true }));
|
|
70
|
+
const file = path.join(dir, 'database.sqlite');
|
|
71
|
+
const backup = path.join(dir, 'before.sqlite');
|
|
72
|
+
let database = await createLegacy(file);
|
|
73
|
+
await database.close();
|
|
74
|
+
await fs.copyFile(file, backup);
|
|
75
|
+
database = new Sequelize({
|
|
76
|
+
dialect: 'sqlite',
|
|
77
|
+
storage: file,
|
|
78
|
+
logging: false,
|
|
79
|
+
});
|
|
80
|
+
await migrateSchema(database);
|
|
81
|
+
await database.close();
|
|
82
|
+
await fs.copyFile(backup, file);
|
|
83
|
+
database = new Sequelize({
|
|
84
|
+
dialect: 'sqlite',
|
|
85
|
+
storage: file,
|
|
86
|
+
logging: false,
|
|
87
|
+
});
|
|
88
|
+
try {
|
|
89
|
+
assert.equal(
|
|
90
|
+
Object.hasOwn(
|
|
91
|
+
await database.getQueryInterface().describeTable('Crontabs'),
|
|
92
|
+
'work_dir',
|
|
93
|
+
),
|
|
94
|
+
false,
|
|
95
|
+
);
|
|
96
|
+
const rows = await database.query('SELECT name FROM "Crontabs"', {
|
|
97
|
+
type: QueryTypes.SELECT,
|
|
98
|
+
});
|
|
99
|
+
assert.equal(rows[0].name, 'keep-me');
|
|
100
|
+
} finally {
|
|
101
|
+
await database.close();
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test('database loader rejects initialization failure rather than allowing workers to start', async () => {
|
|
106
|
+
const mocks = { './logger': { error() {}, info() {} } };
|
|
107
|
+
for (const [file, model] of [
|
|
108
|
+
['env', 'EnvModel'],
|
|
109
|
+
['cron', 'CrontabModel'],
|
|
110
|
+
['dependence', 'DependenceModel'],
|
|
111
|
+
['open', 'AppModel'],
|
|
112
|
+
['system', 'SystemModel'],
|
|
113
|
+
['subscription', 'SubscriptionModel'],
|
|
114
|
+
['cronView', 'CrontabViewModel'],
|
|
115
|
+
['cronStats', 'CrontabStatModel'],
|
|
116
|
+
['runningInstance', 'RunningInstanceModel'],
|
|
117
|
+
])
|
|
118
|
+
mocks[`../data/${file}`] = { [model]: { sync: async () => {} } };
|
|
119
|
+
mocks['../data'] = { sequelize: {} };
|
|
120
|
+
mocks['../shared/schemaMigrations'] = {
|
|
121
|
+
migrateSchema: async () => {
|
|
122
|
+
throw new Error('SQLITE_FULL');
|
|
123
|
+
},
|
|
124
|
+
};
|
|
125
|
+
const initialize = load(path.resolve('back/loaders/db.ts'), mocks).default;
|
|
126
|
+
await assert.rejects(initialize(), /SQLITE_FULL/);
|
|
127
|
+
});
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
const test = require('node:test');
|
|
2
|
+
const assert = require('node:assert/strict');
|
|
3
|
+
const fs = require('node:fs');
|
|
4
|
+
const os = require('node:os');
|
|
5
|
+
const path = require('node:path');
|
|
6
|
+
const { spawnSync } = require('node:child_process');
|
|
7
|
+
const source = fs
|
|
8
|
+
.readFileSync(path.resolve('shell/api.sh'), 'utf8')
|
|
9
|
+
.replace(/\nget_token\s*$/, '\n');
|
|
10
|
+
const realJq = spawnSync('which', ['jq'], { encoding: 'utf8' }).stdout.trim();
|
|
11
|
+
function fixture(t) {
|
|
12
|
+
assert.ok(realJq, 'jq is required');
|
|
13
|
+
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'ql-api-parse-'));
|
|
14
|
+
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
|
|
15
|
+
const bin = path.join(root, 'bin');
|
|
16
|
+
fs.mkdirSync(bin);
|
|
17
|
+
fs.writeFileSync(
|
|
18
|
+
path.join(bin, 'jq'),
|
|
19
|
+
'#!/bin/bash\nprintf "call\\n" >> "$JQ_CALLS"\nexec "$REAL_JQ" "$@"\n',
|
|
20
|
+
{ mode: 0o755 },
|
|
21
|
+
);
|
|
22
|
+
const token = path.join(root, 'token.json'),
|
|
23
|
+
response = path.join(root, 'response.json'),
|
|
24
|
+
calls = path.join(root, 'calls');
|
|
25
|
+
const run = (code) =>
|
|
26
|
+
spawnSync(
|
|
27
|
+
'/bin/bash',
|
|
28
|
+
[
|
|
29
|
+
'-euc',
|
|
30
|
+
source +
|
|
31
|
+
'\ncreate_token(){ __ql_token__=generated; generated=1; }; curl(){ cat "$RESPONSE_FILE"; };\n' +
|
|
32
|
+
code,
|
|
33
|
+
],
|
|
34
|
+
{
|
|
35
|
+
encoding: 'utf8',
|
|
36
|
+
env: {
|
|
37
|
+
...process.env,
|
|
38
|
+
PATH: bin + ':' + process.env.PATH,
|
|
39
|
+
REAL_JQ: realJq,
|
|
40
|
+
JQ_CALLS: calls,
|
|
41
|
+
file_auth_token: token,
|
|
42
|
+
RESPONSE_FILE: response,
|
|
43
|
+
ql_port: '5700',
|
|
44
|
+
__ql_token__: 'test-token',
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
);
|
|
48
|
+
return {
|
|
49
|
+
token,
|
|
50
|
+
response,
|
|
51
|
+
run,
|
|
52
|
+
count: () =>
|
|
53
|
+
fs.existsSync(calls)
|
|
54
|
+
? fs.readFileSync(calls, 'utf8').trim().split('\n').length
|
|
55
|
+
: 0,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
test('valid token cache is read with one jq and reused without generation', (t) => {
|
|
59
|
+
const f = fixture(t);
|
|
60
|
+
fs.writeFileSync(
|
|
61
|
+
f.token,
|
|
62
|
+
JSON.stringify({
|
|
63
|
+
value: 'header.payload.signature',
|
|
64
|
+
expiration: 4102444800,
|
|
65
|
+
}),
|
|
66
|
+
);
|
|
67
|
+
const r = f.run(
|
|
68
|
+
'get_token; printf "%s:%s" "$__ql_token__" "${generated:-0}"',
|
|
69
|
+
);
|
|
70
|
+
assert.equal(r.status, 0, r.stderr);
|
|
71
|
+
assert.equal(r.stdout, 'header.payload.signature:0');
|
|
72
|
+
assert.equal(f.count(), 1);
|
|
73
|
+
});
|
|
74
|
+
test('expired, corrupt, missing or header-unsafe token cache refreshes under errexit', (t) => {
|
|
75
|
+
const f = fixture(t);
|
|
76
|
+
for (const content of [
|
|
77
|
+
undefined,
|
|
78
|
+
'broken',
|
|
79
|
+
'{}',
|
|
80
|
+
JSON.stringify({ value: 'old', expiration: 1 }),
|
|
81
|
+
JSON.stringify({ value: 'old', expiration: '4102444800' }),
|
|
82
|
+
JSON.stringify({ value: '', expiration: 4102444800 }),
|
|
83
|
+
JSON.stringify({ value: 'bad\ninjected', expiration: 4102444800 }),
|
|
84
|
+
JSON.stringify({ value: 'bad\n', expiration: 4102444800 }),
|
|
85
|
+
JSON.stringify({ value: 'bad\rinjected', expiration: 4102444800 }),
|
|
86
|
+
]) {
|
|
87
|
+
if (content === undefined) fs.rmSync(f.token, { force: true });
|
|
88
|
+
else fs.writeFileSync(f.token, content);
|
|
89
|
+
const r = f.run('get_token; printf "%s:%s" "$__ql_token__" "$generated"');
|
|
90
|
+
assert.equal(r.status, 0, r.stderr);
|
|
91
|
+
assert.equal(r.stdout, 'generated:1');
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
test('success with a message still parses once and stays silent', (t) => {
|
|
95
|
+
const f = fixture(t);
|
|
96
|
+
fs.writeFileSync(f.response, JSON.stringify({ code: 200, message: 'ok' }));
|
|
97
|
+
const r = f.run('update_cron 1 0 123 log 1');
|
|
98
|
+
assert.equal(r.status, 0, r.stderr);
|
|
99
|
+
assert.equal(r.stdout, '');
|
|
100
|
+
assert.equal(f.count(), 1);
|
|
101
|
+
});
|
|
102
|
+
test('status and statistics errors preserve multiline Unicode messages', (t) => {
|
|
103
|
+
const f = fixture(t);
|
|
104
|
+
fs.writeFileSync(
|
|
105
|
+
f.response,
|
|
106
|
+
JSON.stringify({ code: 500, message: '写入失败\n请重试 "原任务"' }),
|
|
107
|
+
);
|
|
108
|
+
for (const command of [
|
|
109
|
+
'update_cron 1 1 123 log 1 2 7',
|
|
110
|
+
'record_cron_stat 1 7 2',
|
|
111
|
+
]) {
|
|
112
|
+
const r = f.run(command);
|
|
113
|
+
assert.equal(r.status, 0, r.stderr);
|
|
114
|
+
assert.equal(r.stdout, '写入失败\n请重试 "原任务"\n');
|
|
115
|
+
}
|
|
116
|
+
assert.equal(f.count(), 2);
|
|
117
|
+
});
|
|
118
|
+
test('non-JSON errors fall back to raw text and absent messages retain legacy null', (t) => {
|
|
119
|
+
const f = fixture(t);
|
|
120
|
+
fs.writeFileSync(f.response, 'upstream unavailable');
|
|
121
|
+
let r = f.run('update_cron 1 0 123 log 1');
|
|
122
|
+
assert.equal(r.status, 0);
|
|
123
|
+
assert.equal(r.stdout, 'upstream unavailable\n');
|
|
124
|
+
fs.writeFileSync(f.response, '{"code":500}');
|
|
125
|
+
r = f.run('record_cron_stat 1 7 2');
|
|
126
|
+
assert.equal(r.status, 0);
|
|
127
|
+
assert.equal(r.stdout, 'null\n');
|
|
128
|
+
});
|
|
129
|
+
test('statistics without a task id perform no JSON parsing', (t) => {
|
|
130
|
+
const f = fixture(t);
|
|
131
|
+
const r = f.run('record_cron_stat "" 0 1 || true');
|
|
132
|
+
assert.equal(r.status, 0, r.stderr);
|
|
133
|
+
assert.equal(f.count(), 0);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
test('canonical success resets prior errors and matches jq missing-message semantics', (t) => {
|
|
137
|
+
const f = fixture(t);
|
|
138
|
+
fs.writeFileSync(f.response, '{"code":200}');
|
|
139
|
+
const r = f.run(
|
|
140
|
+
'code=500; message=old; ql_parse_status_response "$(cat "$RESPONSE_FILE")"; printf "%s|%s" "$code" "$message"; record_cron_stat 1 0 1',
|
|
141
|
+
);
|
|
142
|
+
assert.equal(r.status, 0, r.stderr);
|
|
143
|
+
assert.equal(r.stdout, '200|null');
|
|
144
|
+
assert.equal(f.count(), 0);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
test('noncanonical JSON always uses jq, including whitespace, duplicate keys and embedded success text', (t) => {
|
|
148
|
+
const f = fixture(t);
|
|
149
|
+
const cases = [
|
|
150
|
+
[' {"code":200} ', '200|null'],
|
|
151
|
+
['{"code": 200}', '200|null'],
|
|
152
|
+
['{"code":200,"message":"完成\\n第二行"}', '200|完成\n第二行'],
|
|
153
|
+
['{"code":200,"data":{"code":500}}', '200|null'],
|
|
154
|
+
['{"code":"200"}', '200|null'],
|
|
155
|
+
['{"code":200,"code":500,"message":"error"}', '500|error'],
|
|
156
|
+
['{"code":500,"message":"{\\"code\\":200}"}', '500|{"code":200}'],
|
|
157
|
+
['{"code":500}', '500|null'],
|
|
158
|
+
];
|
|
159
|
+
for (const [body, expected] of cases) {
|
|
160
|
+
fs.writeFileSync(f.response, body);
|
|
161
|
+
const r = f.run(
|
|
162
|
+
'ql_parse_status_response "$(cat "$RESPONSE_FILE")"; printf "%s|%s" "$code" "$message"',
|
|
163
|
+
);
|
|
164
|
+
assert.equal(r.status, 0, r.stderr);
|
|
165
|
+
assert.equal(r.stdout, expected);
|
|
166
|
+
}
|
|
167
|
+
assert.equal(f.count(), cases.length);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
test('success-shaped invalid documents retain the raw-response error fallback', (t) => {
|
|
171
|
+
const f = fixture(t);
|
|
172
|
+
const cases = [
|
|
173
|
+
'{"code":200}trailing',
|
|
174
|
+
'{"code":200',
|
|
175
|
+
'<html>{"code":200}</html>',
|
|
176
|
+
];
|
|
177
|
+
for (const body of cases) {
|
|
178
|
+
fs.writeFileSync(f.response, body);
|
|
179
|
+
const r = f.run('update_cron 1 0 123 log 1');
|
|
180
|
+
assert.equal(r.status, 0, r.stderr);
|
|
181
|
+
assert.equal(r.stdout, body + '\n');
|
|
182
|
+
}
|
|
183
|
+
assert.equal(f.count(), cases.length);
|
|
184
|
+
});
|
|
@@ -0,0 +1,297 @@
|
|
|
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 { EventEmitter } = require('node:events');
|
|
6
|
+
const ts = require('typescript');
|
|
7
|
+
const jwt = require('jsonwebtoken');
|
|
8
|
+
const { isValidToken } = require('../../back/shared/auth');
|
|
9
|
+
|
|
10
|
+
async function setup() {
|
|
11
|
+
const secret = 'polling-test';
|
|
12
|
+
const token = jwt.sign({}, secret, { algorithm: 'HS384', expiresIn: '1h' });
|
|
13
|
+
let auth = { token };
|
|
14
|
+
let read = async () => auth;
|
|
15
|
+
let reads = 0;
|
|
16
|
+
const validations = [];
|
|
17
|
+
let onConnection;
|
|
18
|
+
const clients = new Set();
|
|
19
|
+
const timers = new Set();
|
|
20
|
+
const mocks = {
|
|
21
|
+
sockjs: {
|
|
22
|
+
createServer: () => ({
|
|
23
|
+
on: (_, fn) => {
|
|
24
|
+
onConnection = fn;
|
|
25
|
+
},
|
|
26
|
+
installHandlers() {},
|
|
27
|
+
}),
|
|
28
|
+
},
|
|
29
|
+
typedi: {
|
|
30
|
+
Container: {
|
|
31
|
+
get: () => ({
|
|
32
|
+
addClient: (c) => clients.add(c),
|
|
33
|
+
removeClient: (c) => clients.delete(c),
|
|
34
|
+
}),
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
'../services/sock': class {},
|
|
38
|
+
'../config/util': { getPlatform: (agent) => agent || 'desktop' },
|
|
39
|
+
'../shared/store': {
|
|
40
|
+
shareStore: {
|
|
41
|
+
getAuthInfo: () => {
|
|
42
|
+
reads++;
|
|
43
|
+
return read();
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
'../shared/auth': {
|
|
48
|
+
isValidToken: (...args) => {
|
|
49
|
+
validations.push({ token: args[1], platform: args[2] });
|
|
50
|
+
return isValidToken(...args);
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
'../config': { baseUrl: '', jwt: { secret } },
|
|
54
|
+
};
|
|
55
|
+
const source = fs.readFileSync(
|
|
56
|
+
path.join(__dirname, '../../back/loaders/sock.ts'),
|
|
57
|
+
'utf8',
|
|
58
|
+
);
|
|
59
|
+
const { outputText } = ts.transpileModule(source, {
|
|
60
|
+
compilerOptions: {
|
|
61
|
+
module: ts.ModuleKind.CommonJS,
|
|
62
|
+
target: ts.ScriptTarget.ES2020,
|
|
63
|
+
esModuleInterop: true,
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
const module = { exports: {} };
|
|
67
|
+
new Function(
|
|
68
|
+
'require',
|
|
69
|
+
'module',
|
|
70
|
+
'exports',
|
|
71
|
+
'setInterval',
|
|
72
|
+
'clearInterval',
|
|
73
|
+
outputText,
|
|
74
|
+
)(
|
|
75
|
+
(name) => {
|
|
76
|
+
assert.ok(Object.hasOwn(mocks, name), name);
|
|
77
|
+
return mocks[name];
|
|
78
|
+
},
|
|
79
|
+
module,
|
|
80
|
+
module.exports,
|
|
81
|
+
(callback, delay) => {
|
|
82
|
+
assert.equal(delay, 1000);
|
|
83
|
+
const timer = { callback, unref() {} };
|
|
84
|
+
timers.add(timer);
|
|
85
|
+
return timer;
|
|
86
|
+
},
|
|
87
|
+
(timer) => timers.delete(timer),
|
|
88
|
+
);
|
|
89
|
+
await module.exports.default({ server: {} });
|
|
90
|
+
function connection(value = token) {
|
|
91
|
+
const conn = new EventEmitter();
|
|
92
|
+
Object.assign(conn, {
|
|
93
|
+
headers: {},
|
|
94
|
+
pathname: '/api/ws/a/b/websocket',
|
|
95
|
+
url: `/api/ws/a/b/websocket?token=${value}`,
|
|
96
|
+
write() {},
|
|
97
|
+
close(code) {
|
|
98
|
+
this.closeCode = code;
|
|
99
|
+
this.emit('close');
|
|
100
|
+
},
|
|
101
|
+
});
|
|
102
|
+
return conn;
|
|
103
|
+
}
|
|
104
|
+
return {
|
|
105
|
+
token,
|
|
106
|
+
secret,
|
|
107
|
+
clients,
|
|
108
|
+
timers,
|
|
109
|
+
validations,
|
|
110
|
+
connection,
|
|
111
|
+
connect: (c) => onConnection(c),
|
|
112
|
+
get reads() {
|
|
113
|
+
return reads;
|
|
114
|
+
},
|
|
115
|
+
setAuth(value) {
|
|
116
|
+
auth = value;
|
|
117
|
+
},
|
|
118
|
+
setRead(fn) {
|
|
119
|
+
read = fn;
|
|
120
|
+
},
|
|
121
|
+
tick: () => Promise.all([...timers].map((t) => t.callback())),
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function deferred() {
|
|
126
|
+
let resolve, reject;
|
|
127
|
+
const promise = new Promise((a, b) => {
|
|
128
|
+
resolve = a;
|
|
129
|
+
reject = b;
|
|
130
|
+
});
|
|
131
|
+
return { promise, resolve, reject };
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
test('multiple sockets share one periodic read, revoke together, and stop polling when empty', async () => {
|
|
135
|
+
const h = await setup();
|
|
136
|
+
assert.equal(h.timers.size, 0);
|
|
137
|
+
const sockets = Array.from({ length: 20 }, () => h.connection());
|
|
138
|
+
await Promise.all(sockets.map(h.connect));
|
|
139
|
+
assert.equal(h.reads, 20, 'initial authentication remains per connection');
|
|
140
|
+
assert.equal(h.timers.size, 1);
|
|
141
|
+
await h.tick();
|
|
142
|
+
assert.equal(h.reads, 21);
|
|
143
|
+
assert.equal(h.clients.size, 20);
|
|
144
|
+
h.setAuth({ token: '' });
|
|
145
|
+
await h.tick();
|
|
146
|
+
assert.ok(sockets.every((c) => c.closeCode === '401'));
|
|
147
|
+
assert.equal(h.clients.size, 0);
|
|
148
|
+
assert.equal(h.timers.size, 0);
|
|
149
|
+
const c = h.connection();
|
|
150
|
+
h.setAuth({ token: h.token });
|
|
151
|
+
await h.connect(c);
|
|
152
|
+
assert.equal(h.timers.size, 1);
|
|
153
|
+
c.close();
|
|
154
|
+
assert.equal(h.timers.size, 0);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
test('slow reads do not overlap and old snapshots do not reject newly accepted sockets', async () => {
|
|
158
|
+
const h = await setup();
|
|
159
|
+
const old = h.connection();
|
|
160
|
+
await h.connect(old);
|
|
161
|
+
const pending = deferred();
|
|
162
|
+
h.setRead(() => pending.promise);
|
|
163
|
+
const tick = h.tick();
|
|
164
|
+
await h.tick();
|
|
165
|
+
assert.equal(h.reads, 2);
|
|
166
|
+
old.close();
|
|
167
|
+
h.setRead(async () => ({ token: h.token }));
|
|
168
|
+
const fresh = h.connection();
|
|
169
|
+
await h.connect(fresh);
|
|
170
|
+
pending.resolve({ token: '' });
|
|
171
|
+
await tick;
|
|
172
|
+
assert.equal(h.clients.has(fresh), true);
|
|
173
|
+
assert.equal(fresh.closeCode, undefined);
|
|
174
|
+
await h.tick();
|
|
175
|
+
assert.equal(h.reads, 4);
|
|
176
|
+
fresh.close();
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
test('periodic store failures disconnect the checked sessions', async () => {
|
|
180
|
+
const h = await setup();
|
|
181
|
+
const c = h.connection();
|
|
182
|
+
await h.connect(c);
|
|
183
|
+
h.setRead(async () => {
|
|
184
|
+
throw new Error('database unavailable');
|
|
185
|
+
});
|
|
186
|
+
await h.tick();
|
|
187
|
+
assert.equal(c.closeCode, '401');
|
|
188
|
+
assert.equal(h.clients.size, 0);
|
|
189
|
+
assert.equal(h.timers.size, 0);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
test('closed connections cannot be added after initial authentication finishes', async () => {
|
|
193
|
+
const h = await setup();
|
|
194
|
+
const pending = deferred();
|
|
195
|
+
h.setRead(() => pending.promise);
|
|
196
|
+
const c = h.connection();
|
|
197
|
+
const connecting = h.connect(c);
|
|
198
|
+
c.close();
|
|
199
|
+
pending.resolve({ token: h.token });
|
|
200
|
+
await connecting;
|
|
201
|
+
assert.equal(h.clients.size, 0);
|
|
202
|
+
assert.equal(h.timers.size, 0);
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
test('initial store errors reject the connection without creating a timer', async () => {
|
|
206
|
+
const h = await setup();
|
|
207
|
+
h.setRead(async () => {
|
|
208
|
+
throw new Error('database unavailable');
|
|
209
|
+
});
|
|
210
|
+
const c = h.connection();
|
|
211
|
+
await h.connect(c);
|
|
212
|
+
assert.equal(c.closeCode, '401');
|
|
213
|
+
assert.equal(h.clients.size, 0);
|
|
214
|
+
assert.equal(h.timers.size, 0);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
test('each periodic check still validates JWT expiry and platform session membership', async () => {
|
|
218
|
+
const h = await setup();
|
|
219
|
+
const expired = h.connection(
|
|
220
|
+
jwt.sign({}, h.secret, { algorithm: 'HS384', expiresIn: -1 }),
|
|
221
|
+
);
|
|
222
|
+
await h.connect(expired);
|
|
223
|
+
assert.equal(expired.closeCode, '404');
|
|
224
|
+
const c = h.connection();
|
|
225
|
+
await h.connect(c);
|
|
226
|
+
h.setAuth({ tokens: { desktop: [{ value: h.token, expiration: 1 }] } });
|
|
227
|
+
await h.tick();
|
|
228
|
+
assert.equal(c.closeCode, '401');
|
|
229
|
+
assert.equal(h.timers.size, 0);
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
test('an accepted JWT is disconnected when it expires without a store change', async () => {
|
|
233
|
+
const h = await setup();
|
|
234
|
+
const c = h.connection();
|
|
235
|
+
await h.connect(c);
|
|
236
|
+
const now = Date.now;
|
|
237
|
+
try {
|
|
238
|
+
Date.now = () => now() + 2 * 60 * 60 * 1000;
|
|
239
|
+
await h.tick();
|
|
240
|
+
} finally {
|
|
241
|
+
Date.now = now;
|
|
242
|
+
}
|
|
243
|
+
assert.equal(c.closeCode, '401');
|
|
244
|
+
assert.equal(h.timers.size, 0);
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
test('same token and platform share validation only within the current round', async () => {
|
|
248
|
+
const h = await setup();
|
|
249
|
+
const sockets = Array.from({ length: 20 }, () => h.connection());
|
|
250
|
+
await Promise.all(sockets.map(h.connect));
|
|
251
|
+
assert.equal(
|
|
252
|
+
h.validations.length,
|
|
253
|
+
20,
|
|
254
|
+
'initial authentication stays independent',
|
|
255
|
+
);
|
|
256
|
+
h.validations.length = 0;
|
|
257
|
+
await h.tick();
|
|
258
|
+
assert.equal(h.validations.length, 1);
|
|
259
|
+
await h.tick();
|
|
260
|
+
assert.equal(h.validations.length, 2, 'new rounds revalidate');
|
|
261
|
+
h.setAuth({ token: '' });
|
|
262
|
+
await h.tick();
|
|
263
|
+
assert.equal(
|
|
264
|
+
h.validations.length,
|
|
265
|
+
3,
|
|
266
|
+
'invalid results are also shared for this round',
|
|
267
|
+
);
|
|
268
|
+
assert.ok(sockets.every((c) => c.closeCode === '401'));
|
|
269
|
+
assert.equal(h.timers.size, 0);
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
test('validation results never cross tokens or platforms', async () => {
|
|
273
|
+
const h = await setup();
|
|
274
|
+
const other = jwt.sign({ account: 'other' }, h.secret, {
|
|
275
|
+
algorithm: 'HS384',
|
|
276
|
+
expiresIn: '1h',
|
|
277
|
+
});
|
|
278
|
+
h.setAuth({
|
|
279
|
+
tokens: {
|
|
280
|
+
desktop: [{ value: h.token }, { value: other }],
|
|
281
|
+
mobile: [{ value: h.token }],
|
|
282
|
+
},
|
|
283
|
+
});
|
|
284
|
+
const desktop = h.connection();
|
|
285
|
+
const mobile = h.connection();
|
|
286
|
+
mobile.headers['user-agent'] = 'mobile';
|
|
287
|
+
const separate = h.connection(other);
|
|
288
|
+
await Promise.all([desktop, mobile, separate].map(h.connect));
|
|
289
|
+
h.validations.length = 0;
|
|
290
|
+
h.setAuth({ tokens: { desktop: [{ value: h.token }], mobile: [] } });
|
|
291
|
+
await h.tick();
|
|
292
|
+
assert.equal(h.validations.length, 3);
|
|
293
|
+
assert.equal(desktop.closeCode, undefined);
|
|
294
|
+
assert.equal(mobile.closeCode, '401');
|
|
295
|
+
assert.equal(separate.closeCode, '401');
|
|
296
|
+
desktop.close();
|
|
297
|
+
});
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
const assert = require('node:assert/strict');
|
|
2
|
+
const test = require('node:test');
|
|
3
|
+
const path = require('node:path');
|
|
4
|
+
const { EventEmitter } = require('node:events');
|
|
5
|
+
const jwt = require('jsonwebtoken');
|
|
6
|
+
const load = require('../helpers/load-security-module.cjs');
|
|
7
|
+
|
|
8
|
+
test('WebSocket connections reject expired tokens and close when sessions are revoked', async () => {
|
|
9
|
+
const secret = 'sock-test';
|
|
10
|
+
const valid = jwt.sign({}, secret, { algorithm: 'HS384', expiresIn: '1h' });
|
|
11
|
+
const expired = jwt.sign({}, secret, { algorithm: 'HS384', expiresIn: -1 });
|
|
12
|
+
let auth = { token: valid, tokens: { desktop: [{ value: expired }] } };
|
|
13
|
+
let onConnection;
|
|
14
|
+
const clients = new Set();
|
|
15
|
+
const Sock = class {};
|
|
16
|
+
const loader = load(path.join(__dirname, '../../back/loaders/sock.ts'), {
|
|
17
|
+
sockjs: {
|
|
18
|
+
createServer: () => ({
|
|
19
|
+
on: (_event, fn) => {
|
|
20
|
+
onConnection = fn;
|
|
21
|
+
},
|
|
22
|
+
installHandlers() {},
|
|
23
|
+
}),
|
|
24
|
+
},
|
|
25
|
+
typedi: {
|
|
26
|
+
Container: {
|
|
27
|
+
get: () => ({
|
|
28
|
+
addClient: (c) => clients.add(c),
|
|
29
|
+
removeClient: (c) => clients.delete(c),
|
|
30
|
+
}),
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
'../services/sock': Sock,
|
|
34
|
+
'../config': { jwt: { secret }, baseUrl: '' },
|
|
35
|
+
'../config/util': { getPlatform: () => 'desktop' },
|
|
36
|
+
'../shared/store': { shareStore: { getAuthInfo: async () => auth } },
|
|
37
|
+
}).default;
|
|
38
|
+
await loader({ server: {} });
|
|
39
|
+
const connection = (token) => {
|
|
40
|
+
const c = new EventEmitter();
|
|
41
|
+
Object.assign(c, {
|
|
42
|
+
headers: {},
|
|
43
|
+
pathname: '/api/ws/a/b/websocket',
|
|
44
|
+
url: `/api/ws/a/b/websocket?token=${token}`,
|
|
45
|
+
close() {
|
|
46
|
+
this.closed = true;
|
|
47
|
+
this.emit('close');
|
|
48
|
+
},
|
|
49
|
+
write() {},
|
|
50
|
+
});
|
|
51
|
+
return c;
|
|
52
|
+
};
|
|
53
|
+
const rejected = connection(expired);
|
|
54
|
+
await onConnection(rejected);
|
|
55
|
+
assert.equal(rejected.closed, true);
|
|
56
|
+
assert.equal(clients.size, 0);
|
|
57
|
+
const accepted = connection(valid);
|
|
58
|
+
await onConnection(accepted);
|
|
59
|
+
assert.equal(clients.size, 1);
|
|
60
|
+
auth = { token: '', tokens: {} };
|
|
61
|
+
await new Promise((resolve, reject) => {
|
|
62
|
+
const timeout = setTimeout(() => {
|
|
63
|
+
accepted.close();
|
|
64
|
+
reject(new Error('revoked socket remained open'));
|
|
65
|
+
}, 3000);
|
|
66
|
+
accepted.once('close', () => {
|
|
67
|
+
clearTimeout(timeout);
|
|
68
|
+
resolve();
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
assert.equal(clients.size, 0);
|
|
72
|
+
});
|