@indigoai-us/hq-cli 5.345.217 → 5.345.219
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/CHANGELOG.md +105 -0
- package/README.md +1 -1
- package/assets/bot-workers/setup/skills/add-a-pack.md +3 -3
- package/assets/scaffold/core/scripts/hq-session.sh +4 -0
- package/dist/command-catalog.generated.d.ts +69 -0
- package/dist/command-catalog.generated.js +83 -0
- package/dist/commands/agents.d.ts +22 -0
- package/dist/commands/agents.js +92 -1
- package/dist/commands/cloud.d.ts +1 -1
- package/dist/commands/cloud.js +1 -1
- package/dist/commands/core-hook-registration-plan.d.ts +8 -0
- package/dist/commands/core-hook-registration-plan.js +21 -0
- package/dist/commands/core-hq-session-native.js +3 -0
- package/dist/commands/core.js +9 -0
- package/dist/commands/doctor.js +4 -0
- package/dist/commands/install-global.js +1 -1
- package/dist/commands/lanes-shared.d.ts +2 -0
- package/dist/commands/lanes-watch.js +28 -1
- package/dist/commands/lanes.d.ts +2 -0
- package/dist/commands/lanes.js +99 -9
- package/dist/commands/members.js +1 -1
- package/dist/commands/pack-start-bot.d.ts +1 -1
- package/dist/commands/pack-start-bot.js +2 -2
- package/dist/commands/pkg-update.js +1 -1
- package/dist/commands/reindex.js +6 -0
- package/dist/commands/sync-narrow.js +1 -1
- package/dist/commands/team-sync.js +1 -1
- package/dist/data/command-states.json +8 -1
- package/dist/index.js +8 -0
- package/dist/lib/anywhere-person-setting.d.ts +2 -2
- package/dist/lib/anywhere-person-setting.js +2 -2
- package/dist/lib/billing/usage-cost.js +5 -2
- package/dist/lib/bot/pack-link.d.ts +2 -2
- package/dist/lib/bot/pack-link.js +2 -2
- package/dist/lib/daemon/anywhere-listener.d.ts +33 -1
- package/dist/lib/daemon/anywhere-listener.js +86 -4
- package/dist/lib/daemon/hook-env.d.ts +32 -0
- package/dist/lib/daemon/hook-env.js +51 -0
- package/dist/lib/daemon/machine-check-owner.d.ts +21 -0
- package/dist/lib/daemon/machine-check-owner.js +95 -0
- package/dist/lib/daemon/machine-check-registration.d.ts +4 -0
- package/dist/lib/daemon/machine-check-registration.js +64 -0
- package/dist/lib/daemon/machine-checks.d.ts +144 -0
- package/dist/lib/daemon/machine-checks.js +412 -0
- package/dist/lib/daemon/ops/policy.js +5 -3
- package/dist/lib/daemon/run.js +37 -5
- package/dist/lib/daemon/server.d.ts +2 -0
- package/dist/lib/daemon/server.js +6 -0
- package/dist/lib/daemon/state-file.d.ts +12 -1
- package/dist/lib/daemon/state-file.js +23 -1
- package/dist/lib/doctor/checks/fleet-hook-core.d.ts +4 -0
- package/dist/lib/doctor/checks/fleet-hook-core.js +103 -0
- package/dist/lib/doctor/checks/host-core.d.ts +2 -0
- package/dist/lib/doctor/checks/host-core.js +81 -11
- package/dist/lib/doctor/checks/machine-checks.d.ts +16 -0
- package/dist/lib/doctor/checks/machine-checks.js +103 -0
- package/dist/lib/doctor/checks/sync-runtime.js +3 -3
- package/dist/lib/doctor/registry.js +4 -0
- package/dist/lib/doctor/types.d.ts +4 -0
- package/dist/lib/environment/credential-shapes.d.ts +61 -0
- package/dist/lib/environment/credential-shapes.js +333 -0
- package/dist/lib/environment/profile.js +4 -16
- package/dist/lib/hook-engine/canary-gate.d.ts +64 -0
- package/dist/lib/hook-engine/canary-gate.js +147 -0
- package/dist/lib/hook-engine/coverage.d.ts +15 -0
- package/dist/lib/hook-engine/coverage.js +130 -0
- package/dist/lib/hook-engine/fleet-registration.d.ts +30 -0
- package/dist/lib/hook-engine/fleet-registration.js +132 -0
- package/dist/lib/hook-engine/reconcile.js +297 -116
- package/dist/lib/hook-engine/registration-identity.d.ts +33 -0
- package/dist/lib/hook-engine/registration-identity.js +97 -0
- package/dist/lib/hook-engine/registration.d.ts +1 -23
- package/dist/lib/hook-engine/registration.js +1 -52
- package/dist/lib/hook-engine/resolve.d.ts +2 -0
- package/dist/lib/hook-engine/resolve.js +7 -3
- package/dist/lib/install/claude-targets.js +28 -15
- package/dist/lib/install/codex-pack-contributions.js +1 -1
- package/dist/lib/install/codex-targets.js +24 -11
- package/dist/lib/install/hook-target.d.ts +2 -2
- package/dist/lib/install/hook-target.js +7 -5
- package/dist/lib/lanes/canonical-host.d.ts +8 -1
- package/dist/lib/lanes/canonical-host.js +9 -3
- package/dist/lib/lanes/machine-identity.d.ts +15 -0
- package/dist/lib/lanes/machine-identity.js +26 -0
- package/dist/lib/lanes/spawn.js +17 -3
- package/dist/lib/packages/setup-block.d.ts +2 -2
- package/dist/lib/packages/setup-block.js +2 -2
- package/dist/lib/packages/start-bot.d.ts +1 -1
- package/dist/lib/packages/start-bot.js +1 -1
- package/dist/main.js +1 -1
- package/dist/types.d.ts +1 -1
- package/dist/utils/callback-port-busy.d.ts +1 -1
- package/dist/utils/callback-port-busy.js +4 -4
- package/dist/utils/core-next-flag.d.ts +4 -0
- package/dist/utils/core-next-flag.js +22 -0
- package/dist/utils/core-next-kill.d.ts +16 -0
- package/dist/utils/core-next-kill.js +35 -0
- package/dist/utils/hook-trust.js +4 -1
- package/dist/utils/host-registration.d.ts +80 -17
- package/dist/utils/host-registration.js +194 -47
- package/package.json +8 -8
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Credential shapes in environment profile text.
|
|
3
|
+
*
|
|
4
|
+
* What counts as a credential is decided by SECRET_PATTERNS in
|
|
5
|
+
* src/lib/core-utils/secret-patterns.ts, the list `hq core secret-scan` uses.
|
|
6
|
+
* This module does not keep a second set of shapes. It applies the shared
|
|
7
|
+
* expressions, unchanged, to bounded windows of the text, so that:
|
|
8
|
+
*
|
|
9
|
+
* - the result for a string is the result the shared list gives for it
|
|
10
|
+
* (`sharedShapesIn`, checked against `matchingSecretPatterns` in
|
|
11
|
+
* credential-equivalence.test.ts), and
|
|
12
|
+
* - the time is linear in the length of the string. Applied to one long
|
|
13
|
+
* string as a whole, the JWT expression restarts at every candidate and
|
|
14
|
+
* rescans to the end, which took 29 s for a 320 KB note.
|
|
15
|
+
*
|
|
16
|
+
* Windows.
|
|
17
|
+
*
|
|
18
|
+
* - Every expression except JWT is run on consecutive chunks of CHUNK
|
|
19
|
+
* characters that overlap by OVERLAP. A match of any of those expressions
|
|
20
|
+
* has a shortest form of at most 41 characters from its start (a prefix
|
|
21
|
+
* and a minimum count), so every match start lies in a chunk that holds
|
|
22
|
+
* that shortest form. The cost of one chunk is bounded by its size.
|
|
23
|
+
* - A JWT match is three dot-separated runs of [A-Za-z0-9_-]. Only the
|
|
24
|
+
* minimum counts (20, 20, 10) matter to the expression, so a long run can
|
|
25
|
+
* be cut after 64 characters without changing the answer. Each window is
|
|
26
|
+
* one piece of a first segment (at most 320 characters), the first 64
|
|
27
|
+
* characters of the next segment and the first 64 of the one after.
|
|
28
|
+
*
|
|
29
|
+
* Exemptions. Profile text is sentences, hostnames and labels, and a match
|
|
30
|
+
* rejects the profile. Three exemptions accept ordinary text that matches the
|
|
31
|
+
* shared list: a Slack prefix named in a sentence, a host name that starts
|
|
32
|
+
* with the key prefix letters, and an Authorization header followed by a
|
|
33
|
+
* secret's NAME. Each fails closed: it reads the whole white-space-delimited
|
|
34
|
+
* token and accepts only a closed form. Nothing else the shared list rejects
|
|
35
|
+
* is accepted here, and some ordinary text is rejected as the price of that.
|
|
36
|
+
*
|
|
37
|
+
* There is no exemption for a private key header. Any string that holds a
|
|
38
|
+
* BEGIN ... PRIVATE KEY line is rejected, as on main, with or without key
|
|
39
|
+
* material after it, because the body can sit in another string of the same
|
|
40
|
+
* profile. To mention a key in a profile, name its type in words.
|
|
41
|
+
*
|
|
42
|
+
* One rule is added to the shared list: a URL with an inline password.
|
|
43
|
+
*/
|
|
44
|
+
import { SECRET_PATTERN_BOUNDARY, SECRET_PATTERNS } from "../core-utils/secret-patterns.js";
|
|
45
|
+
const URL_PASSWORD = "URL with an inline password";
|
|
46
|
+
const CHUNK = 4096;
|
|
47
|
+
const OVERLAP = 256;
|
|
48
|
+
const JWT_NAME = "JWT";
|
|
49
|
+
const JWT_PIECE = 256;
|
|
50
|
+
const JWT_EXTENSION = 64;
|
|
51
|
+
const JWT_SEGMENT_CAP = 64;
|
|
52
|
+
// "(^|[^a-zA-Z0-9])" without the start-of-text alternative.
|
|
53
|
+
const BOUNDARY_CHARACTER = SECRET_PATTERN_BOUNDARY.replace("^|", "");
|
|
54
|
+
const COMPILED = SECRET_PATTERNS.map(({ pattern, name }) => ({
|
|
55
|
+
name,
|
|
56
|
+
whole: new RegExp(`${SECRET_PATTERN_BOUNDARY}${pattern}`),
|
|
57
|
+
first: new RegExp(`${SECRET_PATTERN_BOUNDARY}(?:${pattern})`, "g"),
|
|
58
|
+
later: new RegExp(`${BOUNDARY_CHARACTER}(?:${pattern})`, "g"),
|
|
59
|
+
afterCharacter: new RegExp(`${BOUNDARY_CHARACTER}(?:${pattern})`),
|
|
60
|
+
}));
|
|
61
|
+
/**
|
|
62
|
+
* Call `visit` with the start and end of each match of one shared expression,
|
|
63
|
+
* found chunk by chunk. `start` is the first character of the token, after the
|
|
64
|
+
* boundary character. Stops when `visit` returns true and returns true.
|
|
65
|
+
*/
|
|
66
|
+
function someChunkedMatch(shape, text, visit) {
|
|
67
|
+
for (let from = 0;; from += CHUNK - OVERLAP) {
|
|
68
|
+
const expression = from === 0 ? shape.first : shape.later;
|
|
69
|
+
// A later chunk begins one character early so its first position has a real boundary character.
|
|
70
|
+
const base = from === 0 ? 0 : from - 1;
|
|
71
|
+
const chunk = text.slice(base, from + CHUNK);
|
|
72
|
+
expression.lastIndex = 0;
|
|
73
|
+
for (let match = expression.exec(chunk); match !== null; match = expression.exec(chunk)) {
|
|
74
|
+
const boundary = match[1]?.length ?? 0;
|
|
75
|
+
if (visit(base + match.index + boundary, base + match.index + match[0].length))
|
|
76
|
+
return true;
|
|
77
|
+
if (match[0].length === 0)
|
|
78
|
+
expression.lastIndex += 1;
|
|
79
|
+
}
|
|
80
|
+
if (from + CHUNK >= text.length)
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
const isDigit = (c) => c >= 48 && c <= 57;
|
|
85
|
+
const isUpper = (c) => c >= 65 && c <= 90;
|
|
86
|
+
const isLower = (c) => c >= 97 && c <= 122;
|
|
87
|
+
const isAlnum = (c) => isDigit(c) || isUpper(c) || isLower(c);
|
|
88
|
+
const DOT = 46;
|
|
89
|
+
const DASH = 45;
|
|
90
|
+
const UNDERSCORE = 95;
|
|
91
|
+
const isJwtSegmentCharacter = (c) => isAlnum(c) || c === UNDERSCORE || c === DASH;
|
|
92
|
+
/** Whether the shared JWT expression matches anywhere in `text`, using bounded windows. */
|
|
93
|
+
function jwtPresent(shape, text) {
|
|
94
|
+
let at = 0;
|
|
95
|
+
while (at < text.length) {
|
|
96
|
+
// One maximal run of JWT characters and dots.
|
|
97
|
+
while (at < text.length && !isJwtSegmentCharacter(text.charCodeAt(at)) && text.charCodeAt(at) !== DOT)
|
|
98
|
+
at += 1;
|
|
99
|
+
const runStart = at;
|
|
100
|
+
const segments = [];
|
|
101
|
+
let segmentStart = at;
|
|
102
|
+
for (; at < text.length; at += 1) {
|
|
103
|
+
const c = text.charCodeAt(at);
|
|
104
|
+
if (c === DOT) {
|
|
105
|
+
segments.push([segmentStart, at]);
|
|
106
|
+
segmentStart = at + 1;
|
|
107
|
+
}
|
|
108
|
+
else if (!isJwtSegmentCharacter(c))
|
|
109
|
+
break;
|
|
110
|
+
}
|
|
111
|
+
if (at > runStart)
|
|
112
|
+
segments.push([segmentStart, at]);
|
|
113
|
+
// The expression has two literal dots, so a match needs two segments after its first.
|
|
114
|
+
for (let index = 0; index + 2 < segments.length; index += 1) {
|
|
115
|
+
const [firstStart, firstEnd] = segments[index];
|
|
116
|
+
const [secondStart, secondEnd] = segments[index + 1];
|
|
117
|
+
const [thirdStart, thirdEnd] = segments[index + 2];
|
|
118
|
+
const tail = `.${text.slice(secondStart, Math.min(secondEnd, secondStart + JWT_SEGMENT_CAP))}.${text.slice(thirdStart, Math.min(thirdEnd, thirdStart + JWT_SEGMENT_CAP))}`;
|
|
119
|
+
for (let piece = firstStart; piece < firstEnd; piece += JWT_PIECE) {
|
|
120
|
+
const before = piece > 0 ? text[piece - 1] : "";
|
|
121
|
+
const window = before + text.slice(piece, Math.min(firstEnd, piece + JWT_PIECE + JWT_EXTENSION)) + tail;
|
|
122
|
+
// With a character in front, the start of the window is not the start of the text.
|
|
123
|
+
if ((before === "" ? shape.whole : shape.afterCharacter).test(window))
|
|
124
|
+
return true;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
/** Read the run of characters satisfying `inClass` from `from`, at most `cap` characters. null when the run is longer. */
|
|
131
|
+
function runFrom(text, from, inClass, cap) {
|
|
132
|
+
let end = from;
|
|
133
|
+
while (end < text.length && end - from <= cap && inClass(text.charCodeAt(end)))
|
|
134
|
+
end += 1;
|
|
135
|
+
return end - from > cap ? null : text.slice(from, end);
|
|
136
|
+
}
|
|
137
|
+
/*
|
|
138
|
+
* Exemptions fail closed. Each one is decided on the whole white-space
|
|
139
|
+
* delimited token the match starts and accepts only a closed form. Anything
|
|
140
|
+
* else the shared list rejects is rejected, at the cost of some ordinary text
|
|
141
|
+
* being rejected too.
|
|
142
|
+
*/
|
|
143
|
+
const CLOSERS = ".,;\"')`";
|
|
144
|
+
/**
|
|
145
|
+
* The text from `start` to the next white space or the end, without one
|
|
146
|
+
* closing punctuation mark at its very end. null when it is longer than 255
|
|
147
|
+
* characters. Punctuation anywhere else stays in the token, so "joined to more
|
|
148
|
+
* characters" is always visible to the rule that reads it.
|
|
149
|
+
*/
|
|
150
|
+
function tokenFrom(text, start) {
|
|
151
|
+
let end = start;
|
|
152
|
+
while (end < text.length && end - start <= 255 && text.charCodeAt(end) > 32)
|
|
153
|
+
end += 1;
|
|
154
|
+
if (end - start > 255)
|
|
155
|
+
return null;
|
|
156
|
+
const token = text.slice(start, end);
|
|
157
|
+
return token.length > 1 && CLOSERS.includes(token[token.length - 1]) ? token.slice(0, -1) : token;
|
|
158
|
+
}
|
|
159
|
+
const SLACK_PROSE_WORDS = new Set([
|
|
160
|
+
"style", "styled", "prefix", "prefixed", "type", "typed", "token", "tokens", "format", "kind", "like", "shaped", "based", "family",
|
|
161
|
+
]);
|
|
162
|
+
/**
|
|
163
|
+
* Exemption 1. A sentence that names a Slack token prefix, such as the prefix,
|
|
164
|
+
* a hyphen and the word "style". Exempt only when the whole token is the
|
|
165
|
+
* prefix, a hyphen and one word from a closed list.
|
|
166
|
+
*/
|
|
167
|
+
function slackPrefixInProse(text, start) {
|
|
168
|
+
const token = tokenFrom(text, start);
|
|
169
|
+
return token !== null && SLACK_PROSE_WORDS.has(token.slice(5));
|
|
170
|
+
}
|
|
171
|
+
const HOST_SUFFIXES = new Set([
|
|
172
|
+
"example", "internal", "local", "localhost", "test", "invalid", "lan", "corp", "arpa", "svc",
|
|
173
|
+
"com", "net", "org", "io", "dev", "ai", "co", "app", "cloud",
|
|
174
|
+
]);
|
|
175
|
+
/**
|
|
176
|
+
* Exemptions 2 and 3. A host name that starts with the letters and hyphen of
|
|
177
|
+
* the OpenAI key prefix, in free text or in an identifier field such as
|
|
178
|
+
* declared.aliases. Exempt only when the whole token is a dotted host name in
|
|
179
|
+
* lower case whose last label is in a closed list of suffixes, with at least
|
|
180
|
+
* two plain words and no piece that could be key material: letters only up to
|
|
181
|
+
* 15 characters, digits only up to 5, letters and digits mixed up to 8.
|
|
182
|
+
*
|
|
183
|
+
* Known limit: material deliberately shaped as such a host name and ending in
|
|
184
|
+
* a listed suffix is accepted, for example the prefix, two random 15-letter
|
|
185
|
+
* pieces and ".com". No key format is written that way. The behaviour is
|
|
186
|
+
* pinned by a test named as a known limit in credential-exemptions.test.ts.
|
|
187
|
+
*/
|
|
188
|
+
function hostnameWithKeyPrefix(text, start) {
|
|
189
|
+
const token = tokenFrom(text, start);
|
|
190
|
+
if (token === null || !/^[a-z0-9]+(?:[.-][a-z0-9]+)*$/.test(token))
|
|
191
|
+
return false;
|
|
192
|
+
const labels = token.split(".");
|
|
193
|
+
if (labels.length < 2 || !HOST_SUFFIXES.has(labels[labels.length - 1]))
|
|
194
|
+
return false;
|
|
195
|
+
let words = 0;
|
|
196
|
+
for (const piece of token.split(/[.-]/).slice(1)) {
|
|
197
|
+
const letters = /[a-z]/.test(piece);
|
|
198
|
+
const digits = /[0-9]/.test(piece);
|
|
199
|
+
if (piece.length > (letters && digits ? 8 : letters ? 15 : 5))
|
|
200
|
+
return false;
|
|
201
|
+
if (letters && !digits && piece.length >= 3)
|
|
202
|
+
words += 1;
|
|
203
|
+
}
|
|
204
|
+
return words >= 2;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Exemption 4. An Authorization header whose value is a secret's NAME and not
|
|
208
|
+
* a value. Exempt only when the whole token after the scheme word is upper
|
|
209
|
+
* snake case: capitals, digits and underscores, starting with a letter, with
|
|
210
|
+
* at least one underscore, at most 64 characters, and every piece between
|
|
211
|
+
* underscores either at most 4 characters or holding a letter from G to Z and
|
|
212
|
+
* at most 24 characters. Hex with underscores is therefore not a NAME. A NAME
|
|
213
|
+
* joined to anything (plus, slash, equals, dot, hyphen, colon, more
|
|
214
|
+
* characters) is not exempt, because the token is read up to white space.
|
|
215
|
+
*
|
|
216
|
+
* Known limit: material deliberately written as upper snake case with pieces
|
|
217
|
+
* that fit this rule is accepted, for example 32 upper-case hex in groups of
|
|
218
|
+
* four joined by underscores. No token format is written that way. The
|
|
219
|
+
* behaviour is pinned by a test named as a known limit in
|
|
220
|
+
* credential-exemptions.test.ts.
|
|
221
|
+
*
|
|
222
|
+
* There is no placeholder list. A header followed by a lower-case placeholder
|
|
223
|
+
* of hyphen-joined words was exempt in an earlier version; it cannot be told
|
|
224
|
+
* from token material without guessing, so it is rejected.
|
|
225
|
+
*/
|
|
226
|
+
function bearerSecretName(text, start) {
|
|
227
|
+
const token = tokenFrom(text, start + "Bearer ".length);
|
|
228
|
+
if (token === null || token.length > 64 || !/^[A-Z][A-Z0-9]*(?:_[A-Z0-9]+)+$/.test(token))
|
|
229
|
+
return false;
|
|
230
|
+
return token.split("_").every((piece) => piece.length <= 4 || (piece.length <= 24 && /[G-Z]/.test(piece)));
|
|
231
|
+
}
|
|
232
|
+
const EXEMPTIONS = {
|
|
233
|
+
"Slack token": slackPrefixInProse,
|
|
234
|
+
"OpenAI/Stripe key": hostnameWithKeyPrefix,
|
|
235
|
+
"Bearer token": bearerSecretName,
|
|
236
|
+
};
|
|
237
|
+
const isHostCharacter = (c) => isAlnum(c) || c === DOT || c === DASH;
|
|
238
|
+
/** Whether a host-like name starts at `from`: letters, digits, dots and hyphens, with a dot, or "localhost". */
|
|
239
|
+
function hostAt(text, from) {
|
|
240
|
+
const host = runFrom(text, from, isHostCharacter, 253);
|
|
241
|
+
if (host === null)
|
|
242
|
+
return true;
|
|
243
|
+
return host === "localhost" || (host.length >= 3 && host.includes(".") && isAlnum(host.charCodeAt(0)));
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* scheme://user:password@host, including an empty user name. A user name
|
|
247
|
+
* alone, a port, and a colon or at-sign in the path or query are not
|
|
248
|
+
* passwords.
|
|
249
|
+
*
|
|
250
|
+
* The authority normally ends at a slash, question mark, hash, comma, space,
|
|
251
|
+
* quote or angle bracket. A password may itself contain one of those. So when
|
|
252
|
+
* the text up to that point has a colon that is not followed by a port
|
|
253
|
+
* number, the scan looks up to 128 characters further for an at-sign followed
|
|
254
|
+
* by a host name, and allows at most one space on the way, so that a sentence
|
|
255
|
+
* with an email address later in it is not taken for a password.
|
|
256
|
+
*
|
|
257
|
+
* Not covered: a URL with no scheme (two slashes alone), and a token used as
|
|
258
|
+
* the user name with no colon, which is reported only when it has a shape
|
|
259
|
+
* from the shared list.
|
|
260
|
+
*/
|
|
261
|
+
function urlWithPassword(text) {
|
|
262
|
+
for (let at = text.indexOf("://"); at !== -1; at = text.indexOf("://", at + 3)) {
|
|
263
|
+
const start = at + 3;
|
|
264
|
+
let end = start;
|
|
265
|
+
let colon = -1;
|
|
266
|
+
let lastAt = -1;
|
|
267
|
+
for (; end < text.length; end += 1) {
|
|
268
|
+
const c = text.charCodeAt(end);
|
|
269
|
+
if (c === 47 || c === 63 || c === 35 || c === 44 || c <= 32 || c === 34 || c === 39 || c === 60 || c === 62)
|
|
270
|
+
break;
|
|
271
|
+
if (c === 58 && colon === -1)
|
|
272
|
+
colon = end;
|
|
273
|
+
if (c === 64)
|
|
274
|
+
lastAt = end;
|
|
275
|
+
}
|
|
276
|
+
if (lastAt !== -1) {
|
|
277
|
+
// user:password@host inside the authority. The password is not empty.
|
|
278
|
+
if (colon !== -1 && colon < lastAt - 1)
|
|
279
|
+
return true;
|
|
280
|
+
continue;
|
|
281
|
+
}
|
|
282
|
+
if (colon === -1 || text.charCodeAt(start) === 91)
|
|
283
|
+
continue; // no colon, or an IPv6 literal
|
|
284
|
+
const afterColon = text.slice(colon + 1, end);
|
|
285
|
+
if (afterColon === "" || /^[0-9]{1,5}$/.test(afterColon))
|
|
286
|
+
continue; // nothing, or a port
|
|
287
|
+
let spaces = 0;
|
|
288
|
+
const limit = Math.min(text.length, colon + 1 + 128);
|
|
289
|
+
for (let index = end; index < limit; index += 1) {
|
|
290
|
+
const c = text.charCodeAt(index);
|
|
291
|
+
if (c === 10 || c === 13)
|
|
292
|
+
break;
|
|
293
|
+
if (c === 32 && (spaces += 1) > 1)
|
|
294
|
+
break;
|
|
295
|
+
if (c === 64) {
|
|
296
|
+
if (hostAt(text, index + 1))
|
|
297
|
+
return true;
|
|
298
|
+
break;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
return false;
|
|
303
|
+
}
|
|
304
|
+
export const CREDENTIAL_SHAPE_NAMES = [...COMPILED.map((shape) => shape.name), URL_PASSWORD];
|
|
305
|
+
function sharedShapePresent(shape, text, withExemptions) {
|
|
306
|
+
if (shape.name === JWT_NAME)
|
|
307
|
+
return jwtPresent(shape, text);
|
|
308
|
+
const exempt = withExemptions ? EXEMPTIONS[shape.name] : undefined;
|
|
309
|
+
if (!exempt)
|
|
310
|
+
return someChunkedMatch(shape, text, () => true);
|
|
311
|
+
return someChunkedMatch(shape, text, (start, end) => !exempt(text, start, end));
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* The shared shape names that match `text`, in the shared list's order: the
|
|
315
|
+
* same answer as `matchingSecretPatterns(text)`, computed on bounded windows.
|
|
316
|
+
*/
|
|
317
|
+
export function sharedShapesIn(text) {
|
|
318
|
+
return COMPILED.filter((shape) => sharedShapePresent(shape, text, false)).map((shape) => shape.name);
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* The credential shapes present in `text` for the profile loader: the shared
|
|
322
|
+
* shapes, less the six ordinary-text exemptions, plus a URL with an inline
|
|
323
|
+
* password. Returns names, never the matched text.
|
|
324
|
+
*/
|
|
325
|
+
export function credentialShapesIn(text) {
|
|
326
|
+
if (text.length < 6)
|
|
327
|
+
return [];
|
|
328
|
+
const found = COMPILED.filter((shape) => sharedShapePresent(shape, text, true)).map((shape) => shape.name);
|
|
329
|
+
if (urlWithPassword(text))
|
|
330
|
+
found.push(URL_PASSWORD);
|
|
331
|
+
return found;
|
|
332
|
+
}
|
|
333
|
+
//# sourceMappingURL=credential-shapes.js.map
|
|
@@ -17,7 +17,7 @@ import { readFileSync } from "node:fs";
|
|
|
17
17
|
import { dirname, join } from "node:path";
|
|
18
18
|
import { fileURLToPath } from "node:url";
|
|
19
19
|
import Ajv2020 from "ajv/dist/2020.js";
|
|
20
|
-
import {
|
|
20
|
+
import { credentialShapesIn } from "./credential-shapes.js";
|
|
21
21
|
const HERE = dirname(fileURLToPath(import.meta.url));
|
|
22
22
|
export const ENVIRONMENT_PROFILE_SCHEMA_VERSION = "hq-environment-profile.v1";
|
|
23
23
|
export const ENVIRONMENT_PROFILE_SCHEMA_ID = "urn:hq:schema:hq-environment-profile:v1";
|
|
@@ -101,18 +101,6 @@ function printable(value, limit) {
|
|
|
101
101
|
}
|
|
102
102
|
/** A property name is printed only when it looks like a field name. */
|
|
103
103
|
const PLAIN_FIELD_NAME = /^[A-Za-z_][A-Za-z0-9_]{0,40}$/;
|
|
104
|
-
// The shapes `hq core secret-scan` uses (src/lib/core-utils/secret-patterns.ts),
|
|
105
|
-
// compiled once. Each needs a known credential prefix, so ordinary sentences
|
|
106
|
-
// that mention a token or a key do not match.
|
|
107
|
-
const CREDENTIAL_SHAPES = SECRET_PATTERNS.map(({ pattern, name }) => ({
|
|
108
|
-
name,
|
|
109
|
-
expression: new RegExp(`${SECRET_PATTERN_BOUNDARY}${pattern}`),
|
|
110
|
-
}));
|
|
111
|
-
function credentialShapes(text) {
|
|
112
|
-
if (text.length < 6)
|
|
113
|
-
return [];
|
|
114
|
-
return CREDENTIAL_SHAPES.flatMap(({ name, expression }) => (expression.test(text) ? [name] : []));
|
|
115
|
-
}
|
|
116
104
|
function pointerSegment(key) {
|
|
117
105
|
return key.replace(/~/g, "~0").replace(/\//g, "~1");
|
|
118
106
|
}
|
|
@@ -123,7 +111,7 @@ function pointerSegment(key) {
|
|
|
123
111
|
*/
|
|
124
112
|
function credentialIssues(value, at, out) {
|
|
125
113
|
if (typeof value === "string") {
|
|
126
|
-
const shapes =
|
|
114
|
+
const shapes = credentialShapesIn(value);
|
|
127
115
|
if (shapes.length > 0)
|
|
128
116
|
out.push({ path: at, message: `holds a string shaped like a credential (${shapes.join(", ")})` });
|
|
129
117
|
return;
|
|
@@ -135,7 +123,7 @@ function credentialIssues(value, at, out) {
|
|
|
135
123
|
if (value === null || typeof value !== "object")
|
|
136
124
|
return;
|
|
137
125
|
for (const [key, child] of Object.entries(value)) {
|
|
138
|
-
const shapes =
|
|
126
|
+
const shapes = credentialShapesIn(key);
|
|
139
127
|
if (shapes.length > 0) {
|
|
140
128
|
out.push({ path: at, message: `has a property name shaped like a credential (${shapes.join(", ")})` });
|
|
141
129
|
continue;
|
|
@@ -164,7 +152,7 @@ function issueFrom(error) {
|
|
|
164
152
|
const params = error.params;
|
|
165
153
|
const extra = params.additionalProperty ?? params.unevaluatedProperty;
|
|
166
154
|
if (typeof extra === "string") {
|
|
167
|
-
message = PLAIN_FIELD_NAME.test(extra) &&
|
|
155
|
+
message = PLAIN_FIELD_NAME.test(extra) && credentialShapesIn(extra).length === 0
|
|
168
156
|
? `must not have property "${extra}"`
|
|
169
157
|
: "must not have an unexpected property";
|
|
170
158
|
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/** The canary result record, under the home folder. */
|
|
2
|
+
export declare const HOST_CANARY_RECORD: string;
|
|
3
|
+
/** Schema of [`HOST_CANARY_RECORD`]. */
|
|
4
|
+
export declare const CANARY_RECORD_SCHEMA = "hq.host-canary-result.v1";
|
|
5
|
+
/** The hosts the canary runs and the switch covers. */
|
|
6
|
+
export declare const CANARY_HOSTS: readonly ["claude", "codex", "grok"];
|
|
7
|
+
export type CanaryHost = (typeof CANARY_HOSTS)[number];
|
|
8
|
+
/** The oldest canary run the gate accepts: seven days. */
|
|
9
|
+
export declare const CANARY_MAX_AGE_MS: number;
|
|
10
|
+
/** The override: switch events without a passing canary. */
|
|
11
|
+
export declare const SKIP_CANARY_GATE_ENV = "HQ_SKIP_HOST_CANARY_GATE";
|
|
12
|
+
/** The hq-core gate script whose hash a canary run is bound to. */
|
|
13
|
+
export declare const GATE_SCRIPT: string;
|
|
14
|
+
/** One host's last standard canary run. */
|
|
15
|
+
export interface CanaryHostResult {
|
|
16
|
+
passed: boolean;
|
|
17
|
+
/** When the run finished (ISO 8601). */
|
|
18
|
+
at: string;
|
|
19
|
+
/** The hq-hook version the run's registrations called; null when no event reached the new core. */
|
|
20
|
+
engineVersion: string | null;
|
|
21
|
+
/** SHA-256 of the root's hq-core gate script; null when the root has none. */
|
|
22
|
+
gateSha256: string | null;
|
|
23
|
+
}
|
|
24
|
+
export interface CanaryRecord {
|
|
25
|
+
schema: typeof CANARY_RECORD_SCHEMA;
|
|
26
|
+
hosts: Partial<Record<CanaryHost, CanaryHostResult>>;
|
|
27
|
+
}
|
|
28
|
+
/** SHA-256 of `<root>/.claude/hooks/policy-enforcement-gate.sh`, or null when it is absent. */
|
|
29
|
+
export declare function gateScriptSha256(root: string): string | null;
|
|
30
|
+
/** True when the override is set in `env`. */
|
|
31
|
+
export declare function canaryGateSkipped(env?: NodeJS.ProcessEnv): boolean;
|
|
32
|
+
/** The record at `<home>/.hq/state/host-canary.json`; null when there is none. Throws when it cannot be read or is not a record. */
|
|
33
|
+
export declare function readCanaryRecord(home: string): CanaryRecord | null;
|
|
34
|
+
export interface CanaryExpectation {
|
|
35
|
+
host: string;
|
|
36
|
+
/** The hq-hook version the switch would register. */
|
|
37
|
+
engineVersion: string;
|
|
38
|
+
/** The current root's gate hash ([`gateScriptSha256`]). */
|
|
39
|
+
gateSha256: string | null;
|
|
40
|
+
now?: number;
|
|
41
|
+
maxAgeMs?: number;
|
|
42
|
+
platform?: NodeJS.Platform;
|
|
43
|
+
}
|
|
44
|
+
/** Why `record` does not let `host` switch, or null when it does. */
|
|
45
|
+
export declare function canaryRefusal(record: CanaryRecord | null, expected: CanaryExpectation): string | null;
|
|
46
|
+
export interface CanaryGateOptions extends Omit<CanaryExpectation, 'gateSha256'> {
|
|
47
|
+
home: string;
|
|
48
|
+
root: string;
|
|
49
|
+
env?: NodeJS.ProcessEnv;
|
|
50
|
+
}
|
|
51
|
+
export interface CanaryGateResult {
|
|
52
|
+
allowed: boolean;
|
|
53
|
+
/** Why the canary does not allow the switch; null when it does. */
|
|
54
|
+
reason: string | null;
|
|
55
|
+
/** True when the override let a refused switch go on. */
|
|
56
|
+
skipped: boolean;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* The gate for one host and HQ root: allowed when the record has a current
|
|
60
|
+
* passing canary for it, or when the override is set (then `skipped` is true
|
|
61
|
+
* and `reason` says what was bypassed). A record that cannot be read refuses.
|
|
62
|
+
*/
|
|
63
|
+
export declare function checkHostCanary(options: CanaryGateOptions): CanaryGateResult;
|
|
64
|
+
//# sourceMappingURL=canary-gate.d.ts.map
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The host canary gate (hq-core-rust US-019 criterion 4).
|
|
3
|
+
*
|
|
4
|
+
* `scripts/host-canary.mjs` runs a real Claude Code, Codex or Grok Build
|
|
5
|
+
* session against the hook core in a disposable copy of an HQ root. Before
|
|
6
|
+
* any event on a host is moved to the new core (the registration switch,
|
|
7
|
+
* US-017, and the flip, US-026) or the old core is deleted (US-027/028), a
|
|
8
|
+
* passing canary for that host must exist for:
|
|
9
|
+
*
|
|
10
|
+
* - the hq-hook engine version the switch would register (`hq-hook --version`);
|
|
11
|
+
* - the hq-core policy gate this HQ root ships
|
|
12
|
+
* (`.claude/hooks/policy-enforcement-gate.sh`, by SHA-256, the same hash
|
|
13
|
+
* the engine pins its gate port to; `null` when the root has none);
|
|
14
|
+
* - a run no older than [`CANARY_MAX_AGE_MS`].
|
|
15
|
+
*
|
|
16
|
+
* The canary keeps one small record per machine at
|
|
17
|
+
* `~/.hq/state/host-canary.json` ([`HOST_CANARY_RECORD`]): per host, whether
|
|
18
|
+
* the last standard run passed, when, and what it ran against. No paths,
|
|
19
|
+
* prompts, hook payloads, output or secrets go in it. The last run of a host
|
|
20
|
+
* replaces its entry, so a failure after a pass refuses again.
|
|
21
|
+
*
|
|
22
|
+
* Windows is never allowed: the canary does not run there yet, and the
|
|
23
|
+
* registration switch keeps Windows on the old core (US-022).
|
|
24
|
+
*
|
|
25
|
+
* The override, `HQ_SKIP_HOST_CANARY_GATE=1` (or `hq reindex
|
|
26
|
+
* --skip-host-canary-gate`), lets the switch go on without a passing canary
|
|
27
|
+
* and prints a warning that says so. The canary itself sets it for the
|
|
28
|
+
* reindex it runs on its disposable root: that reindex is what the canary
|
|
29
|
+
* tests.
|
|
30
|
+
*/
|
|
31
|
+
import { createHash } from 'node:crypto';
|
|
32
|
+
import * as fs from 'node:fs';
|
|
33
|
+
import * as path from 'node:path';
|
|
34
|
+
/** The canary result record, under the home folder. */
|
|
35
|
+
export const HOST_CANARY_RECORD = path.join('.hq', 'state', 'host-canary.json');
|
|
36
|
+
/** Schema of [`HOST_CANARY_RECORD`]. */
|
|
37
|
+
export const CANARY_RECORD_SCHEMA = 'hq.host-canary-result.v1';
|
|
38
|
+
/** The hosts the canary runs and the switch covers. */
|
|
39
|
+
export const CANARY_HOSTS = ['claude', 'codex', 'grok'];
|
|
40
|
+
/** The oldest canary run the gate accepts: seven days. */
|
|
41
|
+
export const CANARY_MAX_AGE_MS = 7 * 24 * 60 * 60 * 1000;
|
|
42
|
+
/** Clock skew tolerated for a run recorded in the future. */
|
|
43
|
+
const FUTURE_SKEW_MS = 5 * 60 * 1000;
|
|
44
|
+
/** The override: switch events without a passing canary. */
|
|
45
|
+
export const SKIP_CANARY_GATE_ENV = 'HQ_SKIP_HOST_CANARY_GATE';
|
|
46
|
+
/** The hq-core gate script whose hash a canary run is bound to. */
|
|
47
|
+
export const GATE_SCRIPT = path.join('.claude', 'hooks', 'policy-enforcement-gate.sh');
|
|
48
|
+
/** SHA-256 of `<root>/.claude/hooks/policy-enforcement-gate.sh`, or null when it is absent. */
|
|
49
|
+
export function gateScriptSha256(root) {
|
|
50
|
+
let bytes;
|
|
51
|
+
try {
|
|
52
|
+
bytes = fs.readFileSync(path.join(root, GATE_SCRIPT));
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
if (error.code === 'ENOENT')
|
|
56
|
+
return null;
|
|
57
|
+
throw error;
|
|
58
|
+
}
|
|
59
|
+
return createHash('sha256').update(bytes).digest('hex');
|
|
60
|
+
}
|
|
61
|
+
/** True when the override is set in `env`. */
|
|
62
|
+
export function canaryGateSkipped(env = process.env) {
|
|
63
|
+
return /^(1|true|yes|on)$/i.test((env[SKIP_CANARY_GATE_ENV] ?? '').trim());
|
|
64
|
+
}
|
|
65
|
+
/** The record at `<home>/.hq/state/host-canary.json`; null when there is none. Throws when it cannot be read or is not a record. */
|
|
66
|
+
export function readCanaryRecord(home) {
|
|
67
|
+
const file = path.join(home, HOST_CANARY_RECORD);
|
|
68
|
+
let text;
|
|
69
|
+
try {
|
|
70
|
+
text = fs.readFileSync(file, 'utf8');
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
if (error.code === 'ENOENT')
|
|
74
|
+
return null;
|
|
75
|
+
throw new Error(`${file} cannot be read (${error.code ?? 'error'})`);
|
|
76
|
+
}
|
|
77
|
+
let doc;
|
|
78
|
+
try {
|
|
79
|
+
doc = JSON.parse(text);
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
throw new Error(`${file} is not JSON`);
|
|
83
|
+
}
|
|
84
|
+
const record = doc;
|
|
85
|
+
if (!record || record.schema !== CANARY_RECORD_SCHEMA || !record.hosts || typeof record.hosts !== 'object' || Array.isArray(record.hosts)) {
|
|
86
|
+
throw new Error(`${file} is not a ${CANARY_RECORD_SCHEMA} record`);
|
|
87
|
+
}
|
|
88
|
+
return record;
|
|
89
|
+
}
|
|
90
|
+
/** Why `record` does not let `host` switch, or null when it does. */
|
|
91
|
+
export function canaryRefusal(record, expected) {
|
|
92
|
+
const { host, engineVersion, gateSha256 } = expected;
|
|
93
|
+
const platform = expected.platform ?? process.platform;
|
|
94
|
+
if (platform === 'win32')
|
|
95
|
+
return 'Windows host canaries are not supported yet';
|
|
96
|
+
if (!CANARY_HOSTS.includes(host))
|
|
97
|
+
return `${host} is not a host the canary runs`;
|
|
98
|
+
if (!record)
|
|
99
|
+
return `no host canary result (${HOST_CANARY_RECORD} is missing)`;
|
|
100
|
+
const result = record.hosts[host];
|
|
101
|
+
if (!result || typeof result !== 'object')
|
|
102
|
+
return `no host canary result for ${host}`;
|
|
103
|
+
if (result.passed !== true)
|
|
104
|
+
return `the last ${host} canary failed`;
|
|
105
|
+
if (result.engineVersion === null)
|
|
106
|
+
return `the last ${host} canary reached no event on the new core`;
|
|
107
|
+
if (result.engineVersion !== engineVersion) {
|
|
108
|
+
return `the last ${host} canary ran hq-hook ${String(result.engineVersion)}, not ${engineVersion}`;
|
|
109
|
+
}
|
|
110
|
+
if (result.gateSha256 !== gateSha256) {
|
|
111
|
+
return `the last ${host} canary ran hq-core gate ${short(result.gateSha256)}, not ${short(gateSha256)}`;
|
|
112
|
+
}
|
|
113
|
+
const at = typeof result.at === 'string' ? Date.parse(result.at) : Number.NaN;
|
|
114
|
+
if (!Number.isFinite(at))
|
|
115
|
+
return `the last ${host} canary has no valid time`;
|
|
116
|
+
const now = expected.now ?? Date.now();
|
|
117
|
+
const maxAgeMs = expected.maxAgeMs ?? CANARY_MAX_AGE_MS;
|
|
118
|
+
if (at > now + FUTURE_SKEW_MS)
|
|
119
|
+
return `the last ${host} canary is dated in the future (${result.at})`;
|
|
120
|
+
if (now - at > maxAgeMs) {
|
|
121
|
+
return `the last ${host} canary is from ${result.at}, older than ${Math.round(maxAgeMs / 3_600_000)} hours`;
|
|
122
|
+
}
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
function short(hash) {
|
|
126
|
+
return typeof hash === 'string' ? hash.slice(0, 12) : 'none';
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* The gate for one host and HQ root: allowed when the record has a current
|
|
130
|
+
* passing canary for it, or when the override is set (then `skipped` is true
|
|
131
|
+
* and `reason` says what was bypassed). A record that cannot be read refuses.
|
|
132
|
+
*/
|
|
133
|
+
export function checkHostCanary(options) {
|
|
134
|
+
let reason;
|
|
135
|
+
try {
|
|
136
|
+
reason = canaryRefusal(readCanaryRecord(options.home), { ...options, gateSha256: gateScriptSha256(options.root) });
|
|
137
|
+
}
|
|
138
|
+
catch (error) {
|
|
139
|
+
reason = error.message;
|
|
140
|
+
}
|
|
141
|
+
if (!reason)
|
|
142
|
+
return { allowed: true, reason: null, skipped: false };
|
|
143
|
+
if (canaryGateSkipped(options.env))
|
|
144
|
+
return { allowed: true, reason, skipped: true };
|
|
145
|
+
return { allowed: false, reason, skipped: false };
|
|
146
|
+
}
|
|
147
|
+
//# sourceMappingURL=canary-gate.js.map
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { CoreNextHost, CoreNextTarget } from '../../utils/host-registration.js';
|
|
2
|
+
/** Stands for the hook list when it cannot be read; never covered. */
|
|
3
|
+
export declare const UNREADABLE = "(the old dispatcher hook list could not be read)";
|
|
4
|
+
/** The hooks the old dispatcher runs for `event` on `host` in this HQ root; null when they cannot be read exactly. */
|
|
5
|
+
export declare function shellHooks(root: string, host: CoreNextHost, event: string): string[] | null;
|
|
6
|
+
/** The old dispatcher's hooks for `event` on `host` that the engine does not cover. */
|
|
7
|
+
export declare function uncoveredHooks(root: string, host: CoreNextHost, event: string, covers: Readonly<Record<string, readonly string[]>> | undefined): string[];
|
|
8
|
+
/**
|
|
9
|
+
* `target` narrowed to the events it covers in full on `host` in this HQ
|
|
10
|
+
* root; null when none is covered (every event stays on the old dispatcher),
|
|
11
|
+
* or when the target's host canary gate refuses `host` (US-019 criterion 4:
|
|
12
|
+
* no event on that host switches).
|
|
13
|
+
*/
|
|
14
|
+
export declare function forHost(target: CoreNextTarget | null, root: string, host: CoreNextHost): CoreNextTarget | null;
|
|
15
|
+
//# sourceMappingURL=coverage.d.ts.map
|