@xkwdstore/agent 2.0.2 → 2.0.3

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.
@@ -1,98 +1,98 @@
1
- 'use strict';
2
-
3
- /**
4
- * Agent config persistence with protected long-lived credentials (Phase 10/11).
5
- *
6
- * The on-disk config.json NEVER contains a plaintext long-lived credential. The
7
- * long-lived values — the legacy device token (pre-Phase-10) and the Phase 10
8
- * rotating refresh credential — are protected at rest (see secretStore — DPAPI
9
- * on Windows) and stored under `deviceTokenProtected` / `deviceRefreshProtected`.
10
- * In memory, the config exposes `deviceToken` / `deviceRefresh` as plaintext for
11
- * the connection logic; those fields are stripped before writing.
12
- *
13
- * The short-lived device access JWT (`deviceAccessToken`, ~15m) is low-value and
14
- * short-lived, so it is stored as plaintext — its theft is bounded by its expiry
15
- * and it is useless without the refresh credential that re-issues it.
16
- *
17
- * Legacy pre-Phase-11 configs that stored a plaintext `deviceToken` string are
18
- * migrated transparently: loadConfig reads it into memory as plaintext, and the
19
- * next saveConfig replaces it with a protected envelope.
20
- */
21
-
22
- const fs = require('fs');
23
- const { protectSecret, unprotectSecret } = require('./secretStore');
24
-
25
- const DEFAULTS = {
26
- serverUrl: null,
27
- deviceToken: null, // legacy 365d JWT (pre-Phase-10 devices), kept until migration
28
- deviceAccessToken: null, // short-lived two-level access JWT (Phase 10)
29
- deviceRefresh: null, // long-lived rotating opaque refresh (Phase 10)
30
- deviceId: null, // Phase 14: this device's immutable UUID (not a secret)
31
- profileRoot: null,
32
- browserChannel: 'chromium',
33
- headless: false,
34
- maxSessions: 5,
35
- };
36
-
37
- // Long-lived secrets that must be DPAPI-protected at rest. The short-lived
38
- // access token is intentionally NOT in this list.
39
- const PROTECTED_FIELDS = [
40
- { plain: 'deviceToken', envelope: 'deviceTokenProtected' },
41
- { plain: 'deviceRefresh', envelope: 'deviceRefreshProtected' },
42
- ];
43
-
44
- /**
45
- * Load and return an in-memory config (plaintext long-lived credentials).
46
- * Returns defaults if the file is missing or unreadable — credentials are null
47
- * in that case.
48
- */
49
- function loadConfig(configPath) {
50
- let raw;
51
- try {
52
- raw = JSON.parse(fs.readFileSync(configPath, 'utf8'));
53
- } catch {
54
- return { ...DEFAULTS };
55
- }
56
- const cfg = { ...DEFAULTS, ...raw };
57
-
58
- for (const { plain, envelope } of PROTECTED_FIELDS) {
59
- if (raw[envelope]) {
60
- // Protected envelope → unwrap to plaintext in memory.
61
- cfg[plain] = unprotectSecret(raw[envelope]);
62
- } else if (typeof raw[plain] === 'string' && raw[plain]) {
63
- // Legacy plaintext value — kept in memory, migrated to protected on save.
64
- cfg[plain] = raw[plain];
65
- } else {
66
- cfg[plain] = null;
67
- }
68
- // Never carry the protected envelope (or a stale plaintext field) in memory.
69
- delete cfg[envelope];
70
- }
71
-
72
- // deviceAccessToken is plaintext (short-lived). Coerce to null if absent.
73
- if (typeof cfg.deviceAccessToken !== 'string' || !cfg.deviceAccessToken) {
74
- cfg.deviceAccessToken = null;
75
- }
76
-
77
- return cfg;
78
- }
79
-
80
- /**
81
- * Persist config. Long-lived credentials are protected and stored under their
82
- * `*Protected` envelope keys; the plaintext fields are never written to disk.
83
- */
84
- function saveConfig(configPath, config) {
85
- const persist = { ...config };
86
- for (const { plain, envelope } of PROTECTED_FIELDS) {
87
- if (persist[plain] && typeof persist[plain] === 'string') {
88
- persist[envelope] = protectSecret(persist[plain]);
89
- }
90
- // Ensure the plaintext long-lived credential is never written to disk,
91
- // whether or not one is present.
92
- delete persist[plain];
93
- }
94
- // deviceAccessToken (short-lived) is written as plaintext by design.
95
- fs.writeFileSync(configPath, JSON.stringify(persist, null, 2), 'utf8');
96
- }
97
-
1
+ 'use strict';
2
+
3
+ /**
4
+ * Agent config persistence with protected long-lived credentials (Phase 10/11).
5
+ *
6
+ * The on-disk config.json NEVER contains a plaintext long-lived credential. The
7
+ * long-lived values — the legacy device token (pre-Phase-10) and the Phase 10
8
+ * rotating refresh credential — are protected at rest (see secretStore — DPAPI
9
+ * on Windows) and stored under `deviceTokenProtected` / `deviceRefreshProtected`.
10
+ * In memory, the config exposes `deviceToken` / `deviceRefresh` as plaintext for
11
+ * the connection logic; those fields are stripped before writing.
12
+ *
13
+ * The short-lived device access JWT (`deviceAccessToken`, ~15m) is low-value and
14
+ * short-lived, so it is stored as plaintext — its theft is bounded by its expiry
15
+ * and it is useless without the refresh credential that re-issues it.
16
+ *
17
+ * Legacy pre-Phase-11 configs that stored a plaintext `deviceToken` string are
18
+ * migrated transparently: loadConfig reads it into memory as plaintext, and the
19
+ * next saveConfig replaces it with a protected envelope.
20
+ */
21
+
22
+ const fs = require('fs');
23
+ const { protectSecret, unprotectSecret } = require('./secretStore');
24
+
25
+ const DEFAULTS = {
26
+ serverUrl: null,
27
+ deviceToken: null, // legacy 365d JWT (pre-Phase-10 devices), kept until migration
28
+ deviceAccessToken: null, // short-lived two-level access JWT (Phase 10)
29
+ deviceRefresh: null, // long-lived rotating opaque refresh (Phase 10)
30
+ deviceId: null, // Phase 14: this device's immutable UUID (not a secret)
31
+ profileRoot: null,
32
+ browserChannel: 'chromium',
33
+ headless: false,
34
+ maxSessions: 5,
35
+ };
36
+
37
+ // Long-lived secrets that must be DPAPI-protected at rest. The short-lived
38
+ // access token is intentionally NOT in this list.
39
+ const PROTECTED_FIELDS = [
40
+ { plain: 'deviceToken', envelope: 'deviceTokenProtected' },
41
+ { plain: 'deviceRefresh', envelope: 'deviceRefreshProtected' },
42
+ ];
43
+
44
+ /**
45
+ * Load and return an in-memory config (plaintext long-lived credentials).
46
+ * Returns defaults if the file is missing or unreadable — credentials are null
47
+ * in that case.
48
+ */
49
+ function loadConfig(configPath) {
50
+ let raw;
51
+ try {
52
+ raw = JSON.parse(fs.readFileSync(configPath, 'utf8'));
53
+ } catch {
54
+ return { ...DEFAULTS };
55
+ }
56
+ const cfg = { ...DEFAULTS, ...raw };
57
+
58
+ for (const { plain, envelope } of PROTECTED_FIELDS) {
59
+ if (raw[envelope]) {
60
+ // Protected envelope → unwrap to plaintext in memory.
61
+ cfg[plain] = unprotectSecret(raw[envelope]);
62
+ } else if (typeof raw[plain] === 'string' && raw[plain]) {
63
+ // Legacy plaintext value — kept in memory, migrated to protected on save.
64
+ cfg[plain] = raw[plain];
65
+ } else {
66
+ cfg[plain] = null;
67
+ }
68
+ // Never carry the protected envelope (or a stale plaintext field) in memory.
69
+ delete cfg[envelope];
70
+ }
71
+
72
+ // deviceAccessToken is plaintext (short-lived). Coerce to null if absent.
73
+ if (typeof cfg.deviceAccessToken !== 'string' || !cfg.deviceAccessToken) {
74
+ cfg.deviceAccessToken = null;
75
+ }
76
+
77
+ return cfg;
78
+ }
79
+
80
+ /**
81
+ * Persist config. Long-lived credentials are protected and stored under their
82
+ * `*Protected` envelope keys; the plaintext fields are never written to disk.
83
+ */
84
+ function saveConfig(configPath, config) {
85
+ const persist = { ...config };
86
+ for (const { plain, envelope } of PROTECTED_FIELDS) {
87
+ if (persist[plain] && typeof persist[plain] === 'string') {
88
+ persist[envelope] = protectSecret(persist[plain]);
89
+ }
90
+ // Ensure the plaintext long-lived credential is never written to disk,
91
+ // whether or not one is present.
92
+ delete persist[plain];
93
+ }
94
+ // deviceAccessToken (short-lived) is written as plaintext by design.
95
+ fs.writeFileSync(configPath, JSON.stringify(persist, null, 2), 'utf8');
96
+ }
97
+
98
98
  module.exports = { loadConfig, saveConfig, DEFAULTS };