@xkwdstore/agent 2.0.1 → 2.0.2
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 +21 -12
- package/package.json +1 -1
- package/src/cli.js +2 -2
- package/src/enrollmentClient.js +170 -34
package/README.md
CHANGED
|
@@ -2,23 +2,31 @@
|
|
|
2
2
|
|
|
3
3
|
Terminal-only, cryptographically authenticated CLI for XKWDStore browser automation.
|
|
4
4
|
|
|
5
|
-
## Installation
|
|
5
|
+
## Installation
|
|
6
6
|
|
|
7
|
-
>
|
|
7
|
+
> **@xkwdstore/agent 2.0.1 is now publicly available on npm.**
|
|
8
8
|
>
|
|
9
|
-
> Protocol-v2
|
|
10
|
-
>
|
|
11
|
-
>
|
|
12
|
-
> installer; the pinned `npx` command below will fail on a clean machine.
|
|
9
|
+
> A 2.0.2 release (Protocol-v2 Windows browser-launch fix) is prepared but
|
|
10
|
+
> not yet published. Public installation remains pinned to 2.0.1 until 2.0.2
|
|
11
|
+
> is available on the official npm registry.
|
|
13
12
|
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
End users install by downloading the installer file first, then running it
|
|
14
|
+
(the installer explicitly forbids piping a remote script directly into
|
|
15
|
+
execution). This creates a desktop launcher pinned to this version:
|
|
16
16
|
|
|
17
17
|
```powershell
|
|
18
|
-
|
|
18
|
+
$installer = "$env:TEMP\xkwdstore-install.ps1"
|
|
19
|
+
Invoke-WebRequest -Uri "https://raw.githubusercontent.com/EssaGhazwani/TW-manager-install/main/install.ps1" -OutFile $installer
|
|
20
|
+
powershell.exe -NoProfile -ExecutionPolicy Bypass -File $installer
|
|
19
21
|
```
|
|
20
22
|
|
|
21
|
-
|
|
23
|
+
Or, from CMD:
|
|
24
|
+
|
|
25
|
+
```cmd
|
|
26
|
+
curl -L -o "%TEMP%\xkwdstore-install.ps1" https://raw.githubusercontent.com/EssaGhazwani/TW-manager-install/main/install.ps1 && powershell -NoProfile -ExecutionPolicy Bypass -File "%TEMP%\xkwdstore-install.ps1"
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
The generated launcher runs the pinned command:
|
|
22
30
|
|
|
23
31
|
```powershell
|
|
24
32
|
npx --yes @xkwdstore/agent@2.0.1 start
|
|
@@ -172,8 +180,9 @@ number in the command. The pinned future command for this release is:
|
|
|
172
180
|
npx --yes @xkwdstore/agent@2.0.1 start
|
|
173
181
|
```
|
|
174
182
|
|
|
175
|
-
> Version 2.0.1 is
|
|
176
|
-
> published to npm.
|
|
183
|
+
> Version 2.0.1 is the current published version. Version 2.0.2 (Protocol-v2
|
|
184
|
+
> Windows browser-launch fix) is prepared but not yet published to npm. After
|
|
185
|
+
> 2.0.2 is published, the pinned command will be updated to 2.0.2.
|
|
177
186
|
|
|
178
187
|
The server checks compatibility at startup and displays a clear message if the
|
|
179
188
|
agent version is no longer supported.
|
package/package.json
CHANGED
package/src/cli.js
CHANGED
|
@@ -24,7 +24,7 @@ const crypto = require('crypto');
|
|
|
24
24
|
const os = require('os');
|
|
25
25
|
const path = require('path');
|
|
26
26
|
|
|
27
|
-
const CLI_VERSION = '2.0.
|
|
27
|
+
const CLI_VERSION = '2.0.2';
|
|
28
28
|
const PROTOCOL_VERSION = 2;
|
|
29
29
|
|
|
30
30
|
// ── Command parsing ──────────────────────────────────────────────────────────
|
|
@@ -156,7 +156,7 @@ async function start(serverOrigin, flags) {
|
|
|
156
156
|
if (!deviceMeta || !privateKeyB64Url) {
|
|
157
157
|
console.log('\n✓ Device identity loaded securely');
|
|
158
158
|
try {
|
|
159
|
-
const result = await enroll(serverOrigin);
|
|
159
|
+
const result = await enroll(serverOrigin, { chromePath });
|
|
160
160
|
deviceMeta = require('./deviceKeyStore').loadMetadata();
|
|
161
161
|
privateKeyB64Url = await getStoredPrivateKey();
|
|
162
162
|
} catch (e) {
|
package/src/enrollmentClient.js
CHANGED
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
|
|
19
19
|
const https = require('https');
|
|
20
20
|
const http = require('http');
|
|
21
|
+
const fs = require('fs');
|
|
21
22
|
const { URL } = require('url');
|
|
22
23
|
const { execSync, spawn } = require('child_process');
|
|
23
24
|
const os = require('os');
|
|
@@ -27,37 +28,146 @@ const { generateKeyPair, storePrivateKey, saveMetadata, loadPrivateKeyFromStorag
|
|
|
27
28
|
const { resolveChromeOrFail } = require('./chromeDetector');
|
|
28
29
|
|
|
29
30
|
/**
|
|
30
|
-
* Open a URL in the
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
31
|
+
* Open a URL in the browser (Defect 11 fix: no shell interpolation).
|
|
32
|
+
*
|
|
33
|
+
* Prefers launching the already-detected Chrome executable directly with
|
|
34
|
+
* child_process.spawn and an argument array — no shell, no cmd.exe, no
|
|
35
|
+
* PowerShell process launcher, no command injection. The URL (which
|
|
36
|
+
* legitimately contains &, ?, #, =) is passed as a single separate argument,
|
|
37
|
+
* so shell metacharacters in the URL can never be interpreted as shell syntax.
|
|
38
|
+
*
|
|
39
|
+
* If a detected Chrome path is provided, it is used directly. If direct
|
|
40
|
+
* Chrome launch fails (or no Chrome path is available), the OS default
|
|
41
|
+
* browser opener is tried as a safe fallback (still via spawn argument
|
|
42
|
+
* arrays, never via a constructed shell string). If that also fails, the
|
|
43
|
+
* manual URL is printed exactly once.
|
|
44
|
+
*
|
|
45
|
+
* This function is ASYNCHRONOUS. child_process.spawn can return a ChildProcess
|
|
46
|
+
* and then emit an asynchronous 'error' event if the process cannot actually
|
|
47
|
+
* be started (e.g. ENOENT, EACCES). We wait for the 'spawn' event (process
|
|
48
|
+
* actually started) before treating the launch as successful, and wait for
|
|
49
|
+
* the 'error' event to detect a genuine launch failure and fall through to
|
|
50
|
+
* the fallback. No unhandled 'error' event is left on any ChildProcess.
|
|
51
|
+
*
|
|
52
|
+
* @param {string} url — the authorization URL to open
|
|
53
|
+
* @param {object} [opts] — optional settings
|
|
54
|
+
* @param {string} [opts.chromePath] — already-detected Chrome executable path
|
|
55
|
+
* @returns {Promise<boolean>} true if a browser launched successfully,
|
|
56
|
+
* false if all launch attempts failed (manual URL
|
|
57
|
+
* printed exactly once)
|
|
34
58
|
*/
|
|
35
|
-
function openBrowser(url) {
|
|
36
|
-
|
|
59
|
+
async function openBrowser(url, opts) {
|
|
60
|
+
opts = opts || {};
|
|
61
|
+
const chromePath = opts.chromePath || null;
|
|
62
|
+
const platform = os.platform();
|
|
63
|
+
|
|
64
|
+
// Validate the URL before opening. _isSafeAuthUrl allows legitimate URL
|
|
65
|
+
// characters (& ? # =) that appear in the authorization fragment; it only
|
|
66
|
+
// rejects control characters and genuinely dangerous shell metacharacters
|
|
67
|
+
// (; | ` $) that have no place in an HTTPS authorization URL.
|
|
37
68
|
if (!_isSafeAuthUrl(url)) {
|
|
38
|
-
console.log('\nCould not open browser automatically. Open this URL
|
|
69
|
+
console.log('\nCould not open browser automatically. Open this URL now to approve this computer:');
|
|
39
70
|
console.log(` ${url}`);
|
|
40
71
|
return false;
|
|
41
72
|
}
|
|
42
73
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
74
|
+
// 1. Preferred path: launch the already-detected Chrome executable directly.
|
|
75
|
+
// The URL is a single separate spawn argument — never reconstructed into
|
|
76
|
+
// a shell string — so &, ?, #, = cannot become shell syntax.
|
|
77
|
+
if (chromePath && fs.existsSync(chromePath)) {
|
|
78
|
+
const launched = await spawnAndConfirm(chromePath, [url], {
|
|
79
|
+
stdio: 'ignore',
|
|
80
|
+
detached: true,
|
|
81
|
+
windowsHide: false,
|
|
82
|
+
});
|
|
83
|
+
if (launched) return true;
|
|
84
|
+
// Fall through to the OS default-browser fallback below.
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// 2. Safe fallback: OS default browser opener, still via spawn argument
|
|
88
|
+
// arrays (no shell:true, no constructed command string).
|
|
89
|
+
let fallbackCmd, fallbackArgs;
|
|
90
|
+
if (platform === 'win32') {
|
|
91
|
+
// explorer.exe opens the URL in the registered default browser. It does
|
|
92
|
+
// not interpret & as a command separator when the URL is a separate
|
|
93
|
+
// spawn argument (no shell). This avoids cmd.exe entirely.
|
|
94
|
+
fallbackCmd = 'explorer.exe';
|
|
95
|
+
fallbackArgs = [url];
|
|
96
|
+
} else if (platform === 'darwin') {
|
|
97
|
+
fallbackCmd = 'open';
|
|
98
|
+
fallbackArgs = [url];
|
|
99
|
+
} else {
|
|
100
|
+
fallbackCmd = 'xdg-open';
|
|
101
|
+
fallbackArgs = [url];
|
|
60
102
|
}
|
|
103
|
+
|
|
104
|
+
const fallbackLaunched = await spawnAndConfirm(fallbackCmd, fallbackArgs, {
|
|
105
|
+
stdio: 'ignore',
|
|
106
|
+
detached: true,
|
|
107
|
+
windowsHide: false,
|
|
108
|
+
});
|
|
109
|
+
if (fallbackLaunched) return true;
|
|
110
|
+
|
|
111
|
+
// 3. All launch attempts failed — print the manual URL exactly once.
|
|
112
|
+
console.log('\nCould not open browser automatically. Open this URL now to approve this computer:');
|
|
113
|
+
console.log(` ${url}`);
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Spawn a process and confirm it actually started via the 'spawn' event.
|
|
119
|
+
*
|
|
120
|
+
* child_process.spawn can return a ChildProcess synchronously and then emit
|
|
121
|
+
* an asynchronous 'error' event if the process cannot be started. This helper
|
|
122
|
+
* resolves true only when the 'spawn' event fires (the OS actually started
|
|
123
|
+
* the process), and false on a synchronous throw or an asynchronous 'error'.
|
|
124
|
+
* The ChildProcess is always unref()'d so the parent can exit independently.
|
|
125
|
+
* No unhandled 'error' listener is left behind — the promise settles exactly
|
|
126
|
+
* once and the listeners are removed.
|
|
127
|
+
*
|
|
128
|
+
* @param {string} command - executable to launch
|
|
129
|
+
* @param {string[]} args - argument array (URL is a separate element)
|
|
130
|
+
* @param {object} options - spawn options (never shell:true)
|
|
131
|
+
* @returns {Promise<boolean>} true if the process started, false if it failed
|
|
132
|
+
*/
|
|
133
|
+
function spawnAndConfirm(command, args, options) {
|
|
134
|
+
return new Promise((resolve) => {
|
|
135
|
+
let settled = false;
|
|
136
|
+
const settle = (result) => {
|
|
137
|
+
if (settled) return;
|
|
138
|
+
settled = true;
|
|
139
|
+
resolve(result);
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
let child;
|
|
143
|
+
try {
|
|
144
|
+
child = spawn(command, args, options);
|
|
145
|
+
} catch {
|
|
146
|
+
settle(false);
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// 'spawn' fires when the process is successfully spawned (Node >=16).
|
|
151
|
+
child.once('spawn', () => {
|
|
152
|
+
child.unref();
|
|
153
|
+
settle(true);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
// 'error' fires if the process could not be spawned (ENOENT, EACCES, …)
|
|
157
|
+
// or could not send a message. We remove the 'spawn' listener to avoid
|
|
158
|
+
// leaving a dangling listener that would never fire.
|
|
159
|
+
child.once('error', () => {
|
|
160
|
+
settle(false);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
// Safety: if neither event fires within a short window (e.g. on an old
|
|
164
|
+
// Node version that does not emit 'spawn'), resolve true as a best-effort
|
|
165
|
+
// so we do not hang indefinitely. unref the child first.
|
|
166
|
+
setTimeout(() => {
|
|
167
|
+
settle(true);
|
|
168
|
+
try { child.unref(); } catch {}
|
|
169
|
+
}, 1000).unref();
|
|
170
|
+
});
|
|
61
171
|
}
|
|
62
172
|
|
|
63
173
|
/**
|
|
@@ -66,14 +176,26 @@ function openBrowser(url) {
|
|
|
66
176
|
* - Origin must match the configured XKWDStore server.
|
|
67
177
|
* - Path must be the expected authorization path.
|
|
68
178
|
* - No control characters (newline, carriage return, etc.).
|
|
179
|
+
* - No shell separators (; | ` and $ when used as a command-prefix char) that
|
|
180
|
+
* could be interpreted by cmd.exe. Legitimate URL characters (& ? # =) are
|
|
181
|
+
* ALLOWED — they are normal query/fragment delimiters and, because the URL
|
|
182
|
+
* is passed as a separate spawn argument with no shell, they can never be
|
|
183
|
+
* interpreted as shell syntax.
|
|
69
184
|
* - Request ID and fragment token have strict formats.
|
|
70
185
|
*/
|
|
71
186
|
function _isSafeAuthUrl(url) {
|
|
72
187
|
if (typeof url !== 'string' || url.length === 0) return false;
|
|
73
188
|
// Reject control characters.
|
|
74
189
|
if (/[\x00-\x1f\x7f]/.test(url)) return false;
|
|
75
|
-
// Reject shell metacharacters that
|
|
76
|
-
|
|
190
|
+
// Reject genuinely dangerous shell metacharacters that have no legitimate
|
|
191
|
+
// place in an HTTPS authorization URL. `;` is a cmd.exe command separator,
|
|
192
|
+
// `|` is a pipe, backtick is PowerShell subexpression, and a leading `$`
|
|
193
|
+
// is PowerShell variable syntax. We reject these but allow & ? # = which
|
|
194
|
+
// are normal URL delimiters in the authorization fragment.
|
|
195
|
+
if (/[;|`]/.test(url)) return false;
|
|
196
|
+
// Reject a leading-$ or PowerShell-style $(...) subexpression. A bare `$`
|
|
197
|
+
// inside a base64url token is not produced by this flow.
|
|
198
|
+
if (/\$\(|\$ENV:|\$\{/.test(url)) return false;
|
|
77
199
|
|
|
78
200
|
let parsed;
|
|
79
201
|
try {
|
|
@@ -300,9 +422,15 @@ function collectDeviceMetadata() {
|
|
|
300
422
|
* Run the first-run enrollment flow.
|
|
301
423
|
*
|
|
302
424
|
* @param {string} serverOrigin — e.g. 'https://x.kwdstore.com'
|
|
425
|
+
* @param {object} [opts] — optional settings
|
|
426
|
+
* @param {string} [opts.chromePath] — already-detected Chrome executable path,
|
|
427
|
+
* reused for the authorization-browser launch (no second lookup)
|
|
303
428
|
* @returns {object} { deviceId, deviceCertificate } on success
|
|
304
429
|
*/
|
|
305
|
-
async function enroll(serverOrigin) {
|
|
430
|
+
async function enroll(serverOrigin, opts) {
|
|
431
|
+
opts = opts || {};
|
|
432
|
+
const chromePath = opts.chromePath || null;
|
|
433
|
+
|
|
306
434
|
// 1. Generate Ed25519 key pair
|
|
307
435
|
const keyPair = generateKeyPair();
|
|
308
436
|
|
|
@@ -354,11 +482,12 @@ async function enroll(serverOrigin) {
|
|
|
354
482
|
console.log(` Chrome: ${deviceMeta.chromeVersion || 'not detected'}`);
|
|
355
483
|
console.log(` Public-key fingerprint: ${keyPair.publicKeyFingerprint.slice(0, 4)}…${keyPair.publicKeyFingerprint.slice(-4)}`);
|
|
356
484
|
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
485
|
+
// openBrowser prints the manual-URL fallback exactly once when all launch
|
|
486
|
+
// attempts fail (including the URL-validation-failed case). Do NOT print a
|
|
487
|
+
// second fallback here — that was the source of the duplicate message.
|
|
488
|
+
// await ensures we don't race the polling loop against an in-flight launch,
|
|
489
|
+
// but the polling loop continues regardless of whether the browser opened.
|
|
490
|
+
await openBrowser(authorizationUrl, { chromePath });
|
|
362
491
|
|
|
363
492
|
// 6. Poll for approval
|
|
364
493
|
console.log('\nWaiting for approval...');
|
|
@@ -408,13 +537,19 @@ async function enroll(serverOrigin) {
|
|
|
408
537
|
}
|
|
409
538
|
|
|
410
539
|
if (status.state === 'expired') {
|
|
411
|
-
throw new Error(
|
|
540
|
+
throw new Error(
|
|
541
|
+
'The authorization request expired because approval was not completed in time. ' +
|
|
542
|
+
'Please run the command again.'
|
|
543
|
+
);
|
|
412
544
|
}
|
|
413
545
|
|
|
414
546
|
// Still pending — keep polling
|
|
415
547
|
}
|
|
416
548
|
|
|
417
|
-
throw new Error(
|
|
549
|
+
throw new Error(
|
|
550
|
+
'The authorization request expired because approval was not completed in time. ' +
|
|
551
|
+
'Please run the command again.'
|
|
552
|
+
);
|
|
418
553
|
}
|
|
419
554
|
|
|
420
555
|
/**
|
|
@@ -438,6 +573,7 @@ module.exports = {
|
|
|
438
573
|
getStoredDevice,
|
|
439
574
|
getStoredPrivateKey,
|
|
440
575
|
openBrowser,
|
|
576
|
+
spawnAndConfirm,
|
|
441
577
|
collectDeviceMetadata,
|
|
442
578
|
_request,
|
|
443
579
|
_isHtmlResponse,
|