@goodandready/dsh-cron 0.1.24 → 0.2.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/README.md +117 -82
- package/docs/README.ru.md +133 -85
- package/docs/README.zh.md +140 -82
- package/docs/design/DESIGN.md +44 -36
- package/lib/chat-start.js +2 -2
- package/lib/client.js +647 -409
- package/lib/http-utils.js +81 -0
- package/lib/index.js +342 -384
- package/lib/integrations.js +0 -1
- package/lib/prompt.js +42 -40
- package/lib/runner.js +48 -34
- package/lib/scheduler.js +33 -32
- package/lib/store.js +1 -1
- package/lib/telegram.js +27 -13
- package/package.json +1 -1
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTTP helpers shared by the dsh-cron REST routes.
|
|
3
|
+
* The plugin is served from the local DSH web UI, so mutating endpoints
|
|
4
|
+
* accept only same-origin browser requests (Issue #86) and bounded bodies.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export const MAX_BODY_BYTES = 1024 * 1024;
|
|
8
|
+
export const SCRIPT_CONFIRM_HEADER = 'x-dsh-cron-confirm';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Parse a JSON request body with a hard size cap.
|
|
12
|
+
* Rejects with err.statusCode = 413 when the body exceeds the limit.
|
|
13
|
+
*/
|
|
14
|
+
export function parseJsonBody(req, limit = MAX_BODY_BYTES) {
|
|
15
|
+
return new Promise((resolve, reject) => {
|
|
16
|
+
let data = '';
|
|
17
|
+
let settled = false;
|
|
18
|
+
const fail = (err) => {
|
|
19
|
+
if (settled) return;
|
|
20
|
+
settled = true;
|
|
21
|
+
reject(err);
|
|
22
|
+
};
|
|
23
|
+
req.on('data', (chunk) => {
|
|
24
|
+
if (settled) return;
|
|
25
|
+
data += chunk;
|
|
26
|
+
if (data.length > limit) {
|
|
27
|
+
fail(Object.assign(new Error('Request body exceeds the allowed size'), { statusCode: 413 }));
|
|
28
|
+
req.destroy();
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
req.on('end', () => {
|
|
32
|
+
if (settled) return;
|
|
33
|
+
settled = true;
|
|
34
|
+
try {
|
|
35
|
+
resolve(data ? JSON.parse(data) : {});
|
|
36
|
+
} catch (err) {
|
|
37
|
+
reject(err);
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
req.on('error', fail);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Detect cross-origin (drive-by/CSRF) requests.
|
|
46
|
+
* Browsers attach Origin to every non-GET request and Sec-Fetch-Site on
|
|
47
|
+
* modern fetches; plain local API clients (curl, server-side calls) send
|
|
48
|
+
* neither and stay allowed.
|
|
49
|
+
*/
|
|
50
|
+
export function isCrossOrigin(req) {
|
|
51
|
+
const headers = req.headers || {};
|
|
52
|
+
const origin = headers.origin;
|
|
53
|
+
if (origin !== undefined) {
|
|
54
|
+
if (origin === 'null') return true;
|
|
55
|
+
try {
|
|
56
|
+
const parsed = new URL(origin);
|
|
57
|
+
const host = headers.host || '';
|
|
58
|
+
if (parsed.host && parsed.host !== host) return true;
|
|
59
|
+
} catch (err) {
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
const site = headers['sec-fetch-site'];
|
|
64
|
+
if (site && site !== 'same-origin' && site !== 'none') return true;
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Extract only whitelisted task fields from a PATCH body so that
|
|
70
|
+
* service-owned state (stats, timestamps) cannot be overwritten (Issue #90).
|
|
71
|
+
*/
|
|
72
|
+
export function pickPatchableFields(body, allowed) {
|
|
73
|
+
const patch = {};
|
|
74
|
+
if (!body || typeof body !== 'object') return patch;
|
|
75
|
+
for (const key of allowed) {
|
|
76
|
+
if (Object.prototype.hasOwnProperty.call(body, key)) {
|
|
77
|
+
patch[key] = body[key];
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return patch;
|
|
81
|
+
}
|