hopwire 0.1.9 → 0.2.1
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/dist/account.js +180 -35
- package/dist/agent/redact.js +55 -8
- package/dist/cli.js +172 -58
- package/dist/commands/doctor.js +53 -12
- package/dist/commands/http.js +19 -4
- package/dist/commands/init.js +38 -9
- package/dist/commands/login.js +47 -8
- package/dist/commands/up.js +16 -3
- package/dist/commands/watch.js +33 -4
- package/dist/config-template.js +11 -1
- package/dist/config.js +65 -6
- package/dist/control-plane-client.js +42 -0
- package/dist/inspector/assets/index-CIOfyIbK.js +5 -0
- package/dist/inspector/assets/index-Du4-DCNN.css +2 -0
- package/dist/inspector/index.html +2 -2
- package/dist/inspector-api.js +145 -0
- package/dist/interceptor.js +19 -4
- package/dist/log-follow.js +473 -0
- package/dist/log.js +178 -11
- package/dist/mcp/logs.js +46 -0
- package/dist/mcp/tools.js +136 -3
- package/dist/output.js +38 -0
- package/dist/relay.js +1 -1
- package/dist/serve.js +49 -2
- package/node_modules/@hopwire/protocol/dist/constants.d.ts +10 -0
- package/node_modules/@hopwire/protocol/dist/constants.js +10 -0
- package/node_modules/@hopwire/protocol/dist/index.d.ts +1 -0
- package/node_modules/@hopwire/protocol/dist/index.js +1 -0
- package/node_modules/@hopwire/protocol/dist/inspector.d.ts +18 -0
- package/node_modules/@hopwire/protocol/dist/log-entries.d.ts +72 -0
- package/node_modules/@hopwire/protocol/dist/log-entries.js +147 -0
- package/node_modules/@hopwire/protocol/dist/ws-messages.d.ts +2 -1
- package/package.json +1 -1
- package/dist/inspector/assets/index-D3yNjmSA.js +0 -2
- package/dist/inspector/assets/index-XotF7D1X.css +0 -2
package/dist/account.js
CHANGED
|
@@ -17,19 +17,86 @@ export function isApiToken(value) {
|
|
|
17
17
|
export function accountFile(dataDir) {
|
|
18
18
|
return path.join(dataDir, ACCOUNT_FILE);
|
|
19
19
|
}
|
|
20
|
+
/** The profile a machine has before anyone names one, and the one an older config.json is read as. */
|
|
21
|
+
export const DEFAULT_PROFILE = 'default';
|
|
22
|
+
/** A profile name: a letter or digit, then up to 31 letters, digits, - or _. */
|
|
23
|
+
const PROFILE_PATTERN = /^[A-Za-z0-9][A-Za-z0-9_-]{0,31}$/;
|
|
24
|
+
export function isProfileName(value) {
|
|
25
|
+
return PROFILE_PATTERN.test(value);
|
|
26
|
+
}
|
|
27
|
+
function dataDirOf(ref) {
|
|
28
|
+
return typeof ref === 'string' ? ref : ref.dataDir;
|
|
29
|
+
}
|
|
30
|
+
function namedProfile(ref) {
|
|
31
|
+
return typeof ref === 'string' ? null : ref.profile;
|
|
32
|
+
}
|
|
33
|
+
/** The profile a command uses: the named one, else the current one of the file, else `default`. */
|
|
34
|
+
function profileOf(ref, file) {
|
|
35
|
+
return namedProfile(ref) ?? file?.currentProfile ?? DEFAULT_PROFILE;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The login of the profile; null when there is none. A profile that is named but not on this machine is refused
|
|
39
|
+
* with what to do, so a typo is not taken for a missing login.
|
|
40
|
+
*/
|
|
41
|
+
async function readProfile(ref) {
|
|
42
|
+
const file = await readStored(dataDirOf(ref));
|
|
43
|
+
const name = profileOf(ref, file);
|
|
44
|
+
const stored = file?.profiles.get(name) ?? null;
|
|
45
|
+
if (stored === null && namedProfile(ref) !== null && (file?.profiles.size ?? 0) > 0) {
|
|
46
|
+
throw new HopwireError(`There is no profile "${name}" on this machine; run hopwire login --profile ${name} to create it, or hopwire profiles to list the ones there are.`);
|
|
47
|
+
}
|
|
48
|
+
return { name, stored };
|
|
49
|
+
}
|
|
20
50
|
/** The stored account, null when nobody is logged in. A file that is not what `hopwire login` wrote is refused. */
|
|
21
|
-
export async function readAccount(
|
|
22
|
-
const stored = await
|
|
51
|
+
export async function readAccount(ref) {
|
|
52
|
+
const { stored } = await readProfile(ref);
|
|
23
53
|
return stored === null || stored.apiToken === null
|
|
24
54
|
? null
|
|
25
55
|
: { apiToken: stored.apiToken, controlPlaneUrl: stored.controlPlaneUrl };
|
|
26
56
|
}
|
|
27
57
|
/**
|
|
28
|
-
* The control plane of the last login, also after a logout
|
|
29
|
-
*
|
|
58
|
+
* The control plane of the last login of the profile, also after a logout. A profile that has none yet takes the
|
|
59
|
+
* one of the current profile, so a second organisation at the same control plane needs no --api. Null when this
|
|
60
|
+
* machine was never logged in. A file that is not what `hopwire login` wrote is refused.
|
|
30
61
|
*/
|
|
31
|
-
export async function readControlPlaneUrl(
|
|
32
|
-
|
|
62
|
+
export async function readControlPlaneUrl(ref) {
|
|
63
|
+
const file = await readStored(dataDirOf(ref));
|
|
64
|
+
if (file === null) {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
const stored = file.profiles.get(profileOf(ref, file)) ?? file.profiles.get(file.currentProfile);
|
|
68
|
+
return stored?.controlPlaneUrl ?? null;
|
|
69
|
+
}
|
|
70
|
+
/** The name of the profile a command with this ref uses; no profile has to exist for it. */
|
|
71
|
+
export async function profileName(ref) {
|
|
72
|
+
return profileOf(ref, await readStored(dataDirOf(ref)));
|
|
73
|
+
}
|
|
74
|
+
/** Every profile of the data directory, in the order of their names; empty before the first login. */
|
|
75
|
+
export async function listProfiles(dataDir) {
|
|
76
|
+
const file = await readStored(dataDir);
|
|
77
|
+
if (file === null) {
|
|
78
|
+
return [];
|
|
79
|
+
}
|
|
80
|
+
return [...file.profiles.entries()]
|
|
81
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
82
|
+
.map(([name, stored]) => ({
|
|
83
|
+
name,
|
|
84
|
+
current: name === file.currentProfile,
|
|
85
|
+
loggedIn: stored.apiToken !== null,
|
|
86
|
+
controlPlaneUrl: stored.controlPlaneUrl,
|
|
87
|
+
edgeUrl: stored.edgeUrl,
|
|
88
|
+
organisation: stored.organisation,
|
|
89
|
+
plan: stored.plan,
|
|
90
|
+
}));
|
|
91
|
+
}
|
|
92
|
+
/** Makes a profile the current one. Nothing is contacted. Resolves with false when there is no such profile. */
|
|
93
|
+
export async function useProfile(dataDir, name) {
|
|
94
|
+
const file = await readStored(dataDir);
|
|
95
|
+
if (file === null || !file.profiles.has(name)) {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
await writeStored(dataDir, { ...file, currentProfile: name });
|
|
99
|
+
return true;
|
|
33
100
|
}
|
|
34
101
|
async function readStored(dataDir) {
|
|
35
102
|
const file = accountFile(dataDir);
|
|
@@ -52,15 +119,36 @@ async function readStored(dataDir) {
|
|
|
52
119
|
return stored;
|
|
53
120
|
}
|
|
54
121
|
/**
|
|
55
|
-
* Stores the account, replacing an earlier
|
|
56
|
-
*
|
|
122
|
+
* Stores the account in the profile, replacing an earlier login of that profile, and makes the profile the current
|
|
123
|
+
* one. Other profiles stay as they are. The file is complete and owner-only from the moment it exists. A new login
|
|
124
|
+
* binds the account to the edge its control plane names; without one, the next public start binds it.
|
|
57
125
|
*/
|
|
58
|
-
export async function writeAccount(
|
|
59
|
-
|
|
126
|
+
export async function writeAccount(ref, account, edgeUrl = null, details = { organisation: null, plan: null }) {
|
|
127
|
+
const dataDir = dataDirOf(ref);
|
|
128
|
+
let file;
|
|
129
|
+
try {
|
|
130
|
+
file = await readStored(dataDir);
|
|
131
|
+
}
|
|
132
|
+
catch (error) {
|
|
133
|
+
// A damaged file is replaced, as before profiles: the login is what repairs it
|
|
134
|
+
if (!(error instanceof HopwireError)) {
|
|
135
|
+
throw error;
|
|
136
|
+
}
|
|
137
|
+
file = null;
|
|
138
|
+
}
|
|
139
|
+
const name = profileOf(ref, file);
|
|
140
|
+
const profiles = new Map(file?.profiles ?? []);
|
|
141
|
+
profiles.set(name, {
|
|
142
|
+
...account,
|
|
143
|
+
edgeUrl: edgeUrl === null ? null : new URL(edgeUrl.origin),
|
|
144
|
+
organisation: details.organisation,
|
|
145
|
+
plan: details.plan,
|
|
146
|
+
});
|
|
147
|
+
await writeStored(dataDir, { currentProfile: name, profiles });
|
|
60
148
|
}
|
|
61
149
|
/** The edge the logged-in account is bound to; null when nobody is logged in or no edge is bound yet. */
|
|
62
|
-
export async function boundEdge(
|
|
63
|
-
const stored = await
|
|
150
|
+
export async function boundEdge(ref) {
|
|
151
|
+
const { stored } = await readProfile(ref);
|
|
64
152
|
return stored === null || stored.apiToken === null ? null : stored.edgeUrl;
|
|
65
153
|
}
|
|
66
154
|
/**
|
|
@@ -68,28 +156,40 @@ export async function boundEdge(dataDir) {
|
|
|
68
156
|
* stores the origin of its edge; every later one gets that origin back and has to use the same edge, so a project
|
|
69
157
|
* file or an environment cannot send the account's tokens (and a channel into this machine's services) to an edge
|
|
70
158
|
* of its choosing. The caller of a first start passes the edge the control plane of the account names when it names
|
|
71
|
-
* one (cli.ts, accountEdge). Resolves with the bound edge. A new `hopwire login` clears the binding.
|
|
159
|
+
* one (cli.ts, accountEdge). Resolves with the bound edge. A new `hopwire login` clears the binding. Each profile has
|
|
160
|
+
* its own binding.
|
|
72
161
|
*/
|
|
73
|
-
export async function bindEdge(
|
|
74
|
-
const
|
|
75
|
-
|
|
162
|
+
export async function bindEdge(ref, edgeUrl) {
|
|
163
|
+
const dataDir = dataDirOf(ref);
|
|
164
|
+
const file = await readStored(dataDir);
|
|
165
|
+
const name = profileOf(ref, file);
|
|
166
|
+
const stored = file?.profiles.get(name) ?? null;
|
|
167
|
+
if (file === null || stored === null || stored.apiToken === null) {
|
|
76
168
|
throw new HopwireError('This machine is not logged in; run hopwire login first.');
|
|
77
169
|
}
|
|
78
170
|
if (stored.edgeUrl !== null) {
|
|
79
171
|
return stored.edgeUrl;
|
|
80
172
|
}
|
|
81
|
-
|
|
173
|
+
const profiles = new Map(file.profiles);
|
|
174
|
+
profiles.set(name, { ...stored, edgeUrl: new URL(edgeUrl.origin) });
|
|
175
|
+
await writeStored(dataDir, { ...file, profiles });
|
|
82
176
|
return edgeUrl;
|
|
83
177
|
}
|
|
84
178
|
async function writeStored(dataDir, stored) {
|
|
85
179
|
// A data directory that has to be created holds key material and traffic: owner only
|
|
86
180
|
await mkdir(dataDir, { recursive: true, mode: 0o700 });
|
|
87
181
|
const file = accountFile(dataDir);
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
182
|
+
const profiles = {};
|
|
183
|
+
for (const [name, profile] of [...stored.profiles.entries()].sort(([a], [b]) => a.localeCompare(b))) {
|
|
184
|
+
profiles[name] = {
|
|
185
|
+
...(profile.apiToken === null ? {} : { apiToken: profile.apiToken }),
|
|
186
|
+
controlPlaneUrl: profile.controlPlaneUrl.origin,
|
|
187
|
+
...(profile.edgeUrl === null ? {} : { edgeUrl: profile.edgeUrl.origin }),
|
|
188
|
+
...(profile.organisation === null ? {} : { organisation: profile.organisation }),
|
|
189
|
+
...(profile.plan === null ? {} : { plan: profile.plan }),
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
const content = JSON.stringify({ currentProfile: stored.currentProfile, profiles }, null, 4);
|
|
93
193
|
const draft = `${file}.${process.pid}.${randomBytes(6).toString('hex')}.tmp`;
|
|
94
194
|
await writeFile(draft, `${content}\n`, { mode: 0o600, flag: 'wx' });
|
|
95
195
|
try {
|
|
@@ -101,13 +201,14 @@ async function writeStored(dataDir, stored) {
|
|
|
101
201
|
}
|
|
102
202
|
}
|
|
103
203
|
/**
|
|
104
|
-
* Removes the stored API token and keeps the URL of the control plane it belonged to. A damaged file
|
|
105
|
-
* a whole. Resolves with false when no token was stored.
|
|
204
|
+
* Removes the stored API token of the profile and keeps the URL of the control plane it belonged to. A damaged file
|
|
205
|
+
* is removed as a whole. Resolves with false when no token was stored in the profile.
|
|
106
206
|
*/
|
|
107
|
-
export async function removeAccount(
|
|
108
|
-
|
|
207
|
+
export async function removeAccount(ref) {
|
|
208
|
+
const dataDir = dataDirOf(ref);
|
|
209
|
+
let file;
|
|
109
210
|
try {
|
|
110
|
-
|
|
211
|
+
file = await readStored(dataDir);
|
|
111
212
|
}
|
|
112
213
|
catch (error) {
|
|
113
214
|
if (!(error instanceof HopwireError)) {
|
|
@@ -121,13 +222,19 @@ export async function removeAccount(dataDir) {
|
|
|
121
222
|
});
|
|
122
223
|
return true;
|
|
123
224
|
}
|
|
124
|
-
|
|
225
|
+
const name = profileOf(ref, file);
|
|
226
|
+
const stored = file?.profiles.get(name) ?? null;
|
|
227
|
+
if (file === null || stored === null || stored.apiToken === null) {
|
|
125
228
|
return false;
|
|
126
229
|
}
|
|
127
230
|
// Written anew and renamed into place: the token is in no file any more once this resolves
|
|
128
|
-
|
|
231
|
+
const profiles = new Map(file.profiles);
|
|
232
|
+
profiles.set(name, { ...stored, apiToken: null });
|
|
233
|
+
await writeStored(dataDir, { ...file, profiles });
|
|
129
234
|
return true;
|
|
130
235
|
}
|
|
236
|
+
/** The longest organisation or plan name kept for listing; text of the control plane, cut rather than refused. */
|
|
237
|
+
const DETAIL_MAX = 200;
|
|
131
238
|
function parse(text) {
|
|
132
239
|
let value;
|
|
133
240
|
try {
|
|
@@ -136,11 +243,37 @@ function parse(text) {
|
|
|
136
243
|
catch {
|
|
137
244
|
return null;
|
|
138
245
|
}
|
|
139
|
-
if (
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
246
|
+
if (!isObject(value)) {
|
|
247
|
+
return null;
|
|
248
|
+
}
|
|
249
|
+
// Written by a CLI before profiles: one login, read as the profile `default` and rewritten on the next write
|
|
250
|
+
if (!('profiles' in value)) {
|
|
251
|
+
const stored = parseProfile(value);
|
|
252
|
+
return stored === null
|
|
253
|
+
? null
|
|
254
|
+
: { currentProfile: DEFAULT_PROFILE, profiles: new Map([[DEFAULT_PROFILE, stored]]) };
|
|
255
|
+
}
|
|
256
|
+
if (!isObject(value.profiles) ||
|
|
257
|
+
!('currentProfile' in value) ||
|
|
258
|
+
typeof value.currentProfile !== 'string' ||
|
|
259
|
+
!isProfileName(value.currentProfile)) {
|
|
260
|
+
return null;
|
|
261
|
+
}
|
|
262
|
+
const profiles = new Map();
|
|
263
|
+
for (const [name, entry] of Object.entries(value.profiles)) {
|
|
264
|
+
const stored = isProfileName(name) && isObject(entry) ? parseProfile(entry) : null;
|
|
265
|
+
if (stored === null) {
|
|
266
|
+
return null;
|
|
267
|
+
}
|
|
268
|
+
profiles.set(name, stored);
|
|
269
|
+
}
|
|
270
|
+
return { currentProfile: value.currentProfile, profiles };
|
|
271
|
+
}
|
|
272
|
+
function isObject(value) {
|
|
273
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
274
|
+
}
|
|
275
|
+
function parseProfile(value) {
|
|
276
|
+
if (typeof value.controlPlaneUrl !== 'string') {
|
|
144
277
|
return null;
|
|
145
278
|
}
|
|
146
279
|
// Without a token: logged out. With one, it has to be a token
|
|
@@ -164,5 +297,17 @@ function parse(text) {
|
|
|
164
297
|
}
|
|
165
298
|
edgeUrl = parsed;
|
|
166
299
|
}
|
|
167
|
-
|
|
300
|
+
const organisation = detail(value.organisation);
|
|
301
|
+
const plan = detail(value.plan);
|
|
302
|
+
if (organisation === undefined || plan === undefined) {
|
|
303
|
+
return null;
|
|
304
|
+
}
|
|
305
|
+
return { apiToken, controlPlaneUrl, edgeUrl, organisation, plan };
|
|
306
|
+
}
|
|
307
|
+
/** An optional text of the control plane; undefined when it is there but no text. */
|
|
308
|
+
function detail(value) {
|
|
309
|
+
if (value === undefined) {
|
|
310
|
+
return null;
|
|
311
|
+
}
|
|
312
|
+
return typeof value === 'string' ? value.slice(0, DETAIL_MAX) : undefined;
|
|
168
313
|
}
|
package/dist/agent/redact.js
CHANGED
|
@@ -7,10 +7,13 @@
|
|
|
7
7
|
export const REDACTION_MARKER = '[redacted';
|
|
8
8
|
/** Headers that are always redacted, besides the names that match SENSITIVE_NAME */
|
|
9
9
|
const SENSITIVE_HEADERS = new Set(['authorization', 'proxy-authorization', 'cookie', 'set-cookie', 'x-api-key']);
|
|
10
|
-
/**
|
|
11
|
-
|
|
10
|
+
/**
|
|
11
|
+
* Names of headers that carry credentials or signatures. `hmac` covers Shopify's `X-Shopify-Hmac-SHA256`; `sig` as a
|
|
12
|
+
* word of its own covers PayPal's `PAYPAL-TRANSMISSION-SIG` (and not names such as `x-signal`).
|
|
13
|
+
*/
|
|
14
|
+
const SENSITIVE_NAME = /(token|secret|signature|password|passwd|session|api[-_]?key|auth|hmac|(?:^|[-_])sig(?:[-_]|$))/i;
|
|
12
15
|
/** Names of query parameters, form fields and JSON keys: the header names plus payment data */
|
|
13
|
-
const SENSITIVE_FIELD = /(token|secret|signature|password|passwd|session|api[-_]?key|auth|card|cvc|cvv|iban)/i;
|
|
16
|
+
const SENSITIVE_FIELD = /(token|secret|signature|password|passwd|session|api[-_]?key|auth|hmac|(?:^|[-_])sig(?:[-_]|$)|card|cvc|cvv|iban)/i;
|
|
14
17
|
/** What is shown instead of a value: its length, nothing of its content. */
|
|
15
18
|
export function redacted(value) {
|
|
16
19
|
return `${REDACTION_MARKER}, ${[...value].length} chars]`;
|
|
@@ -32,6 +35,10 @@ export function redactHeaders(headers) {
|
|
|
32
35
|
return [name, value];
|
|
33
36
|
}
|
|
34
37
|
const lower = name.toLowerCase();
|
|
38
|
+
// the time of a signature in a header of its own (Discord's X-Signature-Timestamp): digits only, no secret
|
|
39
|
+
if (lower.endsWith('timestamp') && /^\d{1,13}$/.test(value)) {
|
|
40
|
+
return [name, value];
|
|
41
|
+
}
|
|
35
42
|
if (lower === 'authorization' || lower === 'proxy-authorization') {
|
|
36
43
|
const scheme = /^([A-Za-z][A-Za-z0-9!#$%&'*+.^_`|~-]*) +(.*)$/s.exec(value);
|
|
37
44
|
if (scheme?.[1] !== undefined && scheme[2] !== undefined) {
|
|
@@ -47,12 +54,14 @@ export function redactHeaders(headers) {
|
|
|
47
54
|
/** Names of the parts of a signature header that carry its time, shown when they are digits only */
|
|
48
55
|
const SIGNATURE_TIME_PARTS = new Set(['t', 'ts', 'timestamp']);
|
|
49
56
|
/**
|
|
50
|
-
* A signature header of `name=value` parts separated by commas (Stripe's `t=…,v1=…`, GitHub's
|
|
51
|
-
* value redacted but a timestamp, so an agent can tell whether a
|
|
52
|
-
* Null when the value has another form; the caller then redacts it
|
|
57
|
+
* A signature header of `name=value` parts separated by commas or semicolons (Stripe's `t=…,v1=…`, GitHub's
|
|
58
|
+
* `sha256=…`, Paddle's `ts=…;h1=…`) with every value redacted but a timestamp, so an agent can tell whether a
|
|
59
|
+
* provider's tolerance has passed before it replays. Null when the value has another form; the caller then redacts it
|
|
60
|
+
* as a whole.
|
|
53
61
|
*/
|
|
54
62
|
function redactSignature(value) {
|
|
55
|
-
const
|
|
63
|
+
const separator = value.includes(',') ? ',' : ';';
|
|
64
|
+
const parts = value.split(separator);
|
|
56
65
|
const shown = [];
|
|
57
66
|
for (const part of parts) {
|
|
58
67
|
const match = /^(\s*)([A-Za-z0-9_-]{1,32})=(.*)$/s.exec(part);
|
|
@@ -63,7 +72,7 @@ function redactSignature(value) {
|
|
|
63
72
|
const isTime = SIGNATURE_TIME_PARTS.has(key.toLowerCase()) && /^\d{1,13}$/.test(inner);
|
|
64
73
|
shown.push(`${space}${key}=${isTime ? inner : redacted(inner)}`);
|
|
65
74
|
}
|
|
66
|
-
return shown.join(
|
|
75
|
+
return shown.join(separator);
|
|
67
76
|
}
|
|
68
77
|
/** A path with the values of sensitive query parameters redacted; the rest is kept as it was written. */
|
|
69
78
|
export function redactPath(requestPath) {
|
|
@@ -126,3 +135,41 @@ function decodeComponent(text) {
|
|
|
126
135
|
export function redactJsonText(text) {
|
|
127
136
|
return text.replace(/"((?:[^"\\]|\\.)*)"(\s*:\s*)("(?:[^"\\]|\\.)*"?|[^,}\]\s"{[]+)/g, (whole, key, colon, value) => isRedactedField(key) ? `"${key}"${colon}"${redacted(value.replace(/^"|"$/g, ''))}"` : whole);
|
|
128
137
|
}
|
|
138
|
+
/** Shapes of keys and tokens that are redacted wherever they stand in a log entry */
|
|
139
|
+
const KNOWN_SECRETS = [
|
|
140
|
+
/\b[sr]k_(?:live|test)_[A-Za-z0-9]{6,}/g,
|
|
141
|
+
/\bwhsec_[A-Za-z0-9+/=]{8,}/g,
|
|
142
|
+
/\bgh[pousr]_[A-Za-z0-9]{20,}/g,
|
|
143
|
+
/\bgithub_pat_[A-Za-z0-9_]{20,}/g,
|
|
144
|
+
/\bxox[abprs]-[A-Za-z0-9-]{10,}/g,
|
|
145
|
+
/\bAKIA[0-9A-Z]{16}\b/g,
|
|
146
|
+
/\beyJ[A-Za-z0-9_-]{5,}\.[A-Za-z0-9_-]{5,}\.[A-Za-z0-9_-]{5,}/g,
|
|
147
|
+
];
|
|
148
|
+
/** `Bearer abc…` and `Basic abc…`: the scheme stays, the credential goes */
|
|
149
|
+
const AUTH_SCHEME = /\b(Bearer|Basic|Token)\s+([A-Za-z0-9._~+/=-]{6,})/g;
|
|
150
|
+
/**
|
|
151
|
+
* `password=secret`, `token: abc`, `api_key="x y"` in free text: a sensitive name, `=` or `:`, and a value up to the
|
|
152
|
+
* next space, comma or semicolon (or its closing quote). A value that is already redacted, or a scheme whose
|
|
153
|
+
* credential was, is left alone.
|
|
154
|
+
*/
|
|
155
|
+
const KEY_VALUE = /([A-Za-z0-9_.-]+)(\s*[=:]\s*)(?!\[redacted|(?:Bearer|Basic|Token)\s)("[^"\n]*"|'[^'\n]*'|[^\s,;&}\])"']+)/g;
|
|
156
|
+
/**
|
|
157
|
+
* A log entry as an agent is shown it (phase 15, decision 144): JSON keys and `key=value` / `key: value` pairs with a
|
|
158
|
+
* sensitive name, Bearer and Basic credentials, and the known shapes of keys (Stripe, GitHub, Slack, AWS, JWTs)
|
|
159
|
+
* redacted. Best effort: a log may hold secrets no pattern recognises, and the docs say so.
|
|
160
|
+
*/
|
|
161
|
+
export function redactLogText(text) {
|
|
162
|
+
let shown = redactJsonText(text);
|
|
163
|
+
shown = shown.replace(AUTH_SCHEME, (_whole, scheme, credential) => `${scheme} ${redacted(credential)}`);
|
|
164
|
+
for (const pattern of KNOWN_SECRETS) {
|
|
165
|
+
shown = shown.replace(pattern, (secret) => redacted(secret));
|
|
166
|
+
}
|
|
167
|
+
return shown.replace(KEY_VALUE, (whole, key, separator, value) => {
|
|
168
|
+
if (!isRedactedField(key)) {
|
|
169
|
+
return whole;
|
|
170
|
+
}
|
|
171
|
+
const quote = /^["']/.exec(value)?.[0] ?? '';
|
|
172
|
+
const inner = quote === '' ? value : value.slice(1, -1);
|
|
173
|
+
return `${key}${separator}${quote}${redacted(inner)}${quote}`;
|
|
174
|
+
});
|
|
175
|
+
}
|