huaweicloud-devkit 2.0.0-next.3 → 2.0.0-next.4
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 +3 -1
- package/package.json +3 -1
- package/plugins/huaweicloud-core/.claude-plugin/plugin.json +16 -3
- package/plugins/huaweicloud-core/.codex-plugin/plugin.json +16 -3
- package/plugins/huaweicloud-core/.cursor-plugin/plugin.json +16 -3
- package/plugins/huaweicloud-core/.hermes-plugin/plugin.json +16 -3
- package/plugins/huaweicloud-core/.mcp.json +55 -1
- package/plugins/huaweicloud-core/.workbuddy-plugin/plugin.json +16 -3
- package/plugins/huaweicloud-core/dist/agents/codex-config.js +374 -0
- package/plugins/huaweicloud-core/dist/agents/codex-desktop.js +8 -1
- package/plugins/huaweicloud-core/dist/agents/codex.js +9 -0
- package/plugins/huaweicloud-core/dist/agents/opencode-config.js +141 -86
- package/plugins/huaweicloud-core/dist/agents/opencode.js +106 -39
- package/plugins/huaweicloud-core/dist/approval-gate.js +1185 -0
- package/plugins/huaweicloud-core/dist/approval-ledger.js +2331 -0
- package/plugins/huaweicloud-core/dist/approval-roster.js +89 -0
- package/plugins/huaweicloud-core/dist/approval-types.js +1 -0
- package/plugins/huaweicloud-core/dist/approvals-cli.js +4 -4
- package/plugins/huaweicloud-core/dist/data/icons-manifest.v1.json +23 -5
- package/plugins/huaweicloud-core/dist/dep/receipts.js +65 -5
- package/plugins/huaweicloud-core/dist/hcloud-cli.js +1 -1
- package/plugins/huaweicloud-core/dist/hcloud-plan.js +62 -61
- package/plugins/huaweicloud-core/dist/lib/approval-mode.js +7 -5
- package/plugins/huaweicloud-core/dist/lib/approval-tokens.js +18 -4
- package/plugins/huaweicloud-core/dist/lib/call-trace.js +7 -14
- package/plugins/huaweicloud-core/dist/lib/confirm-language.js +50 -0
- package/plugins/huaweicloud-core/dist/lib/credentials.js +9 -9
- package/plugins/huaweicloud-core/dist/lib/hdkit/hdkit-gate.js +1398 -0
- package/plugins/huaweicloud-core/dist/lib/hdkit/hdkitservice-api.js +362 -32
- package/plugins/huaweicloud-core/dist/lib/hdkit/sandbox-pool.js +6 -16
- package/plugins/huaweicloud-core/dist/lib/opencode-ask-rule.js +224 -78
- package/plugins/huaweicloud-core/dist/lib/opencode-saved-perms.js +282 -0
- package/plugins/huaweicloud-core/dist/lib/supplemental-redaction.js +116 -0
- package/plugins/huaweicloud-core/dist/lib/tool-schema-parts.js +31 -0
- package/plugins/huaweicloud-core/dist/lib/workspace-glob.js +442 -0
- package/plugins/huaweicloud-core/dist/mcp-config-io.js +28 -1
- package/plugins/huaweicloud-core/dist/mcp-protocol.js +68 -16
- package/plugins/huaweicloud-core/dist/mcp-server.js +6506 -912
- package/plugins/huaweicloud-core/dist/mcp-server.js.map +4 -4
- package/plugins/huaweicloud-core/dist/mcp-stdio-transport.js +14 -4
- package/plugins/huaweicloud-core/dist/packs/auth/approval.js +249 -0
- package/plugins/huaweicloud-core/dist/packs/auth/tools.js +44 -44
- package/plugins/huaweicloud-core/dist/packs/huaweicloud-cli/tools.js +85 -130
- package/plugins/huaweicloud-core/dist/packs/obs/approval.js +110 -0
- package/plugins/huaweicloud-core/dist/packs/obs/tools.js +7 -2
- package/plugins/huaweicloud-core/dist/packs/registry.js +19 -1
- package/plugins/huaweicloud-core/dist/packs/sandbox/approval.js +245 -0
- package/plugins/huaweicloud-core/dist/packs/sandbox/handlers-account.js +156 -23
- package/plugins/huaweicloud-core/dist/packs/sandbox/handlers-credentials.js +13 -1
- package/plugins/huaweicloud-core/dist/packs/sandbox/handlers-hwexec.js +136 -0
- package/plugins/huaweicloud-core/dist/packs/sandbox/handlers-session.js +11 -1
- package/plugins/huaweicloud-core/dist/packs/sandbox/handlers-tunnel.js +1 -0
- package/plugins/huaweicloud-core/dist/packs/sandbox/schemas.js +79 -9
- package/plugins/huaweicloud-core/dist/packs/sandbox/tools.js +28 -9
- package/plugins/huaweicloud-core/dist/packs/secrets/approval.js +46 -0
- package/plugins/huaweicloud-core/dist/packs/secrets/tools.js +4 -1
- package/plugins/huaweicloud-core/dist/packs/update/approval.js +79 -0
- package/plugins/huaweicloud-core/dist/packs/update/tools.js +8 -1
- package/plugins/huaweicloud-core/dist/packs/voucher/tools.js +1 -1
- package/plugins/huaweicloud-core/dist/packs-configure.js +74 -0
- package/plugins/huaweicloud-core/dist/safety/classify-text.js +36 -0
- package/plugins/huaweicloud-core/dist/safety/policy.js +2 -0
- package/plugins/huaweicloud-core/dist/sandbox/deploy-check.js +1 -1
- package/plugins/huaweicloud-core/dist/sandbox/devbridge-auth.js +65 -14
- package/plugins/huaweicloud-core/dist/sandbox/devbridge-cleanup.js +422 -0
- package/plugins/huaweicloud-core/dist/sandbox/devbridge-expose-flow.js +66 -16
- package/plugins/huaweicloud-core/dist/sandbox/devbridge-expose.js +13 -10
- package/plugins/huaweicloud-core/dist/sandbox/devbridge-keepalive.js +42 -2
- package/plugins/huaweicloud-core/dist/sandbox/devbridge-key.js +11 -3
- package/plugins/huaweicloud-core/dist/sandbox/devbridge-tunnel.js +6 -0
- package/plugins/huaweicloud-core/dist/sandbox/devbridge-validation.js +10 -1
- package/plugins/huaweicloud-core/dist/sandbox/hwexec-task.js +244 -0
- package/plugins/huaweicloud-core/dist/setup-cli.js +36 -1
- package/plugins/huaweicloud-core/dist/tool-definitions.js +9 -1
- package/plugins/huaweicloud-core/dist/tool-schemas.js +32 -6
- package/plugins/huaweicloud-core/dist/tools.js +76 -9
- package/plugins/huaweicloud-core/dist/update-registry.js +358 -5
- package/plugins/huaweicloud-core/dist/update-upgrade.js +1083 -14
- package/plugins/huaweicloud-core/dist/vendor-gate.js +2 -2
- package/plugins/huaweicloud-core/hooks/huaweicloud-safety.mjs +150 -1
- package/plugins/huaweicloud-core/hooks/huaweicloud-safety.py +53 -3
- package/plugins/huaweicloud-core/openclaw.plugin.json +1 -1
- package/plugins/huaweicloud-core/safety/policy.json +18 -11
- package/plugins/huaweicloud-core/safety/rules/cloud-risk-rules.json +114 -10
- package/plugins/huaweicloud-core/skills/huawei-sandbox/SKILL.md +190 -138
- package/plugins/huaweicloud-core/skills/huawei-sandbox/references/resource-pool.md +36 -12
- package/plugins/huaweicloud-core/skills/huawei-voucher/SKILL.md +2 -0
- package/plugins/huaweicloud-core/skills/huaweicloud-cli-and-auth/SKILL.md +2 -2
- package/plugins/huaweicloud-core/skills/huaweicloud-safety/SKILL.md +11 -7
- package/plugins/huaweicloud-core/skills/huaweicloud-safety/references/approvals.md +74 -29
- package/plugins/huaweicloud-core/skills/huaweicloud-troubleshooting/SKILL.md +21 -19
- package/plugins/huaweicloud-core/src/agents/codex-config.ts +432 -0
- package/plugins/huaweicloud-core/src/agents/codex-desktop.ts +8 -1
- package/plugins/huaweicloud-core/src/agents/codex.ts +10 -0
- package/plugins/huaweicloud-core/src/agents/opencode-config.ts +176 -88
- package/plugins/huaweicloud-core/src/agents/opencode.ts +125 -54
- package/plugins/huaweicloud-core/src/approval-gate.ts +1306 -0
- package/plugins/huaweicloud-core/src/approval-ledger.ts +2446 -0
- package/plugins/huaweicloud-core/src/approval-roster.ts +127 -0
- package/plugins/huaweicloud-core/src/approval-types.ts +237 -0
- package/plugins/huaweicloud-core/src/approvals-cli.ts +4 -4
- package/plugins/huaweicloud-core/src/data/icons-manifest.v1.json +23 -5
- package/plugins/huaweicloud-core/src/dep/receipts.ts +84 -5
- package/plugins/huaweicloud-core/src/hcloud-cli.ts +3 -0
- package/plugins/huaweicloud-core/src/hcloud-plan.ts +68 -70
- package/plugins/huaweicloud-core/src/lib/approval-mode.ts +7 -5
- package/plugins/huaweicloud-core/src/lib/approval-tokens.ts +19 -4
- package/plugins/huaweicloud-core/src/lib/call-trace.ts +7 -17
- package/plugins/huaweicloud-core/src/lib/confirm-language.ts +51 -0
- package/plugins/huaweicloud-core/src/lib/credentials.ts +9 -8
- package/plugins/huaweicloud-core/src/lib/hdkit/hdkit-gate.ts +1428 -0
- package/plugins/huaweicloud-core/src/lib/hdkit/hdkitservice-api.ts +377 -30
- package/plugins/huaweicloud-core/src/lib/hdkit/sandbox-pool.ts +8 -15
- package/plugins/huaweicloud-core/src/lib/opencode-ask-rule.ts +302 -83
- package/plugins/huaweicloud-core/src/lib/opencode-saved-perms.ts +354 -0
- package/plugins/huaweicloud-core/src/lib/supplemental-redaction.ts +129 -0
- package/plugins/huaweicloud-core/src/lib/tool-schema-parts.ts +40 -0
- package/plugins/huaweicloud-core/src/lib/workspace-glob.ts +414 -0
- package/plugins/huaweicloud-core/src/mcp-config-io.ts +27 -1
- package/plugins/huaweicloud-core/src/mcp-protocol.ts +81 -17
- package/plugins/huaweicloud-core/src/mcp-server.ts +7 -7
- package/plugins/huaweicloud-core/src/mcp-stdio-transport.ts +14 -4
- package/plugins/huaweicloud-core/src/packs/auth/approval.ts +302 -0
- package/plugins/huaweicloud-core/src/packs/auth/tools.ts +54 -45
- package/plugins/huaweicloud-core/src/packs/huaweicloud-cli/tools.ts +99 -137
- package/plugins/huaweicloud-core/src/packs/obs/approval.ts +122 -0
- package/plugins/huaweicloud-core/src/packs/obs/tools.ts +7 -2
- package/plugins/huaweicloud-core/src/packs/registry.ts +42 -1
- package/plugins/huaweicloud-core/src/packs/sandbox/approval.ts +296 -0
- package/plugins/huaweicloud-core/src/packs/sandbox/handlers-account.ts +168 -23
- package/plugins/huaweicloud-core/src/packs/sandbox/handlers-credentials.ts +11 -1
- package/plugins/huaweicloud-core/src/packs/sandbox/handlers-hwexec.ts +228 -0
- package/plugins/huaweicloud-core/src/packs/sandbox/handlers-session.ts +11 -1
- package/plugins/huaweicloud-core/src/packs/sandbox/handlers-tunnel.ts +1 -0
- package/plugins/huaweicloud-core/src/packs/sandbox/schemas.ts +100 -8
- package/plugins/huaweicloud-core/src/packs/sandbox/tools.ts +31 -8
- package/plugins/huaweicloud-core/src/packs/secrets/approval.ts +50 -0
- package/plugins/huaweicloud-core/src/packs/secrets/tools.ts +4 -1
- package/plugins/huaweicloud-core/src/packs/update/approval.ts +87 -0
- package/plugins/huaweicloud-core/src/packs/update/tools.ts +8 -1
- package/plugins/huaweicloud-core/src/packs/voucher/tools.ts +1 -1
- package/plugins/huaweicloud-core/src/packs-configure.ts +75 -0
- package/plugins/huaweicloud-core/src/safety/classify-text.ts +40 -0
- package/plugins/huaweicloud-core/src/safety/policy.ts +7 -0
- package/plugins/huaweicloud-core/src/sandbox/deploy-check.ts +1 -1
- package/plugins/huaweicloud-core/src/sandbox/devbridge-auth.ts +74 -14
- package/plugins/huaweicloud-core/src/sandbox/devbridge-cleanup.ts +509 -0
- package/plugins/huaweicloud-core/src/sandbox/devbridge-expose-flow.ts +70 -18
- package/plugins/huaweicloud-core/src/sandbox/devbridge-expose.ts +26 -10
- package/plugins/huaweicloud-core/src/sandbox/devbridge-keepalive.ts +41 -2
- package/plugins/huaweicloud-core/src/sandbox/devbridge-key.ts +9 -3
- package/plugins/huaweicloud-core/src/sandbox/devbridge-tunnel.ts +24 -0
- package/plugins/huaweicloud-core/src/sandbox/devbridge-validation.ts +11 -1
- package/plugins/huaweicloud-core/src/sandbox/hwexec-task.ts +304 -0
- package/plugins/huaweicloud-core/src/setup-cli.ts +39 -1
- package/plugins/huaweicloud-core/src/tool-definitions.ts +9 -1
- package/plugins/huaweicloud-core/src/tool-schemas.ts +39 -6
- package/plugins/huaweicloud-core/src/tools.ts +124 -10
- package/plugins/huaweicloud-core/src/update-registry.ts +340 -5
- package/plugins/huaweicloud-core/src/update-upgrade.ts +1130 -14
- package/plugins/huaweicloud-core/src/vendor-gate.ts +2 -2
|
@@ -158,6 +158,10 @@ export const TOOL_DEFINITIONS_LITERALS = [
|
|
|
158
158
|
name: 'huaweicloud_sandbox_exec_one_shot',
|
|
159
159
|
description: packDescription('huaweicloud_sandbox_exec_one_shot'),
|
|
160
160
|
},
|
|
161
|
+
{
|
|
162
|
+
name: 'huaweicloud_sandbox_exec_poll',
|
|
163
|
+
description: packDescription('huaweicloud_sandbox_exec_poll'),
|
|
164
|
+
},
|
|
161
165
|
{
|
|
162
166
|
name: 'huaweicloud_sandbox_close_session',
|
|
163
167
|
description: packDescription('huaweicloud_sandbox_close_session'),
|
|
@@ -198,6 +202,10 @@ export const TOOL_DEFINITIONS_LITERALS = [
|
|
|
198
202
|
name: 'huaweicloud_sandbox_credentials',
|
|
199
203
|
description: packDescription('huaweicloud_sandbox_credentials'),
|
|
200
204
|
},
|
|
205
|
+
{
|
|
206
|
+
name: 'huaweicloud_sandbox_shutdown',
|
|
207
|
+
description: packDescription('huaweicloud_sandbox_shutdown'),
|
|
208
|
+
},
|
|
201
209
|
{
|
|
202
210
|
name: 'huaweicloud_voucher_status',
|
|
203
211
|
description: packDescription('huaweicloud_voucher_status'),
|
|
@@ -256,7 +264,7 @@ export const TOOL_DEFINITIONS_LITERALS = [
|
|
|
256
264
|
{
|
|
257
265
|
name: 'huaweicloud_packs_configure',
|
|
258
266
|
description:
|
|
259
|
-
'Enable or disable capability packs for THIS session, or reset to the operator posture. Session-scoped: the change lives in this server process only and the next session restart restores the DEVKIT_PACKS posture the operator configured. Takes enable (pack ids to add), disable (pack ids to remove; core cannot be disabled), and reset (drop the session override). The refreshed tool list is pushed to the agent immediately.
|
|
267
|
+
'Enable or disable capability packs for THIS session, or reset to the operator posture. Session-scoped: the change lives in this server process only and the next session restart restores the DEVKIT_PACKS posture the operator configured. Takes enable (pack ids to add), disable (pack ids to remove; core cannot be disabled), and reset (drop the session override). The refreshed tool list is pushed to the agent immediately. Approval-gated in every mode because widening the disclosure face is the consequence: the no-token call returns USER_CONFIRMATION_REQUIRED naming the pack ids with the approve line for the user\u2019s own terminal (token mode), and under DEVKIT_APPROVAL_MODE=ask the server raises the confirmation itself (a form on a capable client, else an ASK_CONFIRMATION_REQUIRED chat round-trip). Stdio agent sessions only — surfaces with no session state are refused outright. The persistent equivalent is the operator CLI (npx huaweicloud-devkit packs enable <ids> --target <agent>); ask the operator to run it and restart the agent.',
|
|
260
268
|
},
|
|
261
269
|
{
|
|
262
270
|
name: 'huaweicloud_gateway_list_tools',
|
|
@@ -3,7 +3,14 @@ import { z } from 'zod';
|
|
|
3
3
|
import type { ToolName } from './tools.ts';
|
|
4
4
|
import type { LooseToolSchema } from './lib/pack-types.ts';
|
|
5
5
|
import { PACK_IDS } from './lib/pack-types.ts';
|
|
6
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
looseObject,
|
|
8
|
+
vendorConfirmed,
|
|
9
|
+
approvalTokenArg,
|
|
10
|
+
approvedByUserArg,
|
|
11
|
+
confirmLanguageArg,
|
|
12
|
+
} from './lib/tool-schema-parts.ts';
|
|
13
|
+
import { APPROVAL_TOOLS } from './approval-roster.ts';
|
|
7
14
|
import { UPDATE_TOOL_SCHEMAS } from './packs/update/tools.ts';
|
|
8
15
|
import { HUAWEICLOUD_OPEN_MCP_TOOL_SCHEMAS } from './packs/huaweicloud-open-mcp/tools.ts';
|
|
9
16
|
import { HUAWEICLOUD_CLI_TOOL_SCHEMAS } from './packs/huaweicloud-cli/tools.ts';
|
|
@@ -39,7 +46,7 @@ const TOOL_SCHEMAS = {
|
|
|
39
46
|
command: z.string().describe('The exact command text to inspect.'),
|
|
40
47
|
}),
|
|
41
48
|
huaweicloud_hook_check_artifacts: z.looseObject({
|
|
42
|
-
artifacts: z.array(artifactsItem),
|
|
49
|
+
artifacts: z.array(artifactsItem).describe('Artifacts to inspect, each with a path and its full text content.'),
|
|
43
50
|
}),
|
|
44
51
|
huaweicloud_hook_check_deploy_plan: z.looseObject({
|
|
45
52
|
plan: z
|
|
@@ -54,11 +61,17 @@ const TOOL_SCHEMAS = {
|
|
|
54
61
|
.describe('Developer intent to route, such as deploy app, use API, debug error, or inspect resources.'),
|
|
55
62
|
}),
|
|
56
63
|
huaweicloud_explain_error: z.looseObject({
|
|
57
|
-
service: z.string().optional(),
|
|
58
|
-
errorCode: z
|
|
64
|
+
service: z.string().optional().describe('Optional service name (e.g., ECS, VPC, APIG) to route the lookup.'),
|
|
65
|
+
errorCode: z
|
|
66
|
+
.string()
|
|
67
|
+
.optional()
|
|
68
|
+
.describe('The error code to explain, such as APIGW.0301 or Ecs.0038. Extracted from message when omitted.'),
|
|
59
69
|
code: z.string().optional().describe('Alias of errorCode, accepted for compatibility. Prefer errorCode.'),
|
|
60
|
-
message: z
|
|
61
|
-
|
|
70
|
+
message: z
|
|
71
|
+
.string()
|
|
72
|
+
.optional()
|
|
73
|
+
.describe('Optional raw error message; the error code is extracted from it when errorCode is not provided.'),
|
|
74
|
+
requestId: z.string().optional().describe('Optional request ID from the failed response, for support escalation.'),
|
|
62
75
|
}),
|
|
63
76
|
huaweicloud_search_docs: z.looseObject({
|
|
64
77
|
query: z.string().describe('Search query across skill descriptions and documentation.'),
|
|
@@ -151,9 +164,29 @@ const TOOL_SCHEMAS = {
|
|
|
151
164
|
.boolean()
|
|
152
165
|
.optional()
|
|
153
166
|
.describe('Drop the session override and return to the operator DEVKIT_PACKS posture.'),
|
|
167
|
+
approvalToken: approvalTokenArg,
|
|
168
|
+
approvedByUser: approvedByUserArg,
|
|
154
169
|
}),
|
|
155
170
|
} satisfies Record<ToolName, LooseToolSchema>;
|
|
156
171
|
|
|
172
|
+
// The confirm-language hint rides every roster tool — the approval
|
|
173
|
+
// domain's parameter, injected HERE rather than in any pack's schema
|
|
174
|
+
// (the field belongs to the roster concept, which is cross-pack; the pack
|
|
175
|
+
// files stay untouched and the pack-boundaries guard stays clean). The
|
|
176
|
+
// roster is a zod-free leaf (approval-roster.ts), so this import adds no
|
|
177
|
+
// pack edge. The extend preserves each schema's loose catchall
|
|
178
|
+
// (probe-verified on zod 4.6.7), so the #530 pass-through leniency is
|
|
179
|
+
// unchanged; the field stays optional everywhere (a missing value must
|
|
180
|
+
// reach the gate, same doctrine as the other approval args). The legacy
|
|
181
|
+
// registry snapshot gate consults DELIBERATE_ADDITIONS for this property
|
|
182
|
+
// on every non-exempt roster tool.
|
|
183
|
+
for (const name of APPROVAL_TOOLS) {
|
|
184
|
+
const base = TOOL_SCHEMAS[name] as LooseToolSchema;
|
|
185
|
+
(TOOL_SCHEMAS as Record<ToolName, LooseToolSchema>)[name] = base.extend({
|
|
186
|
+
confirmLanguage: confirmLanguageArg,
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
|
|
157
190
|
export type ToolSchemaMap = typeof TOOL_SCHEMAS;
|
|
158
191
|
export type ToolSchemaEntry = ToolSchemaMap[ToolName];
|
|
159
192
|
|
|
@@ -30,7 +30,7 @@ import { getRegionalAvailability } from './services/availability.ts';
|
|
|
30
30
|
import type { DistTags } from './update-check.ts';
|
|
31
31
|
// PACK_TOOL_HANDLERS is the central pack dispatch table (its satisfies check
|
|
32
32
|
// pins every PackToolName to a handler).
|
|
33
|
-
import { PACK_TOOL_HANDLERS, isPackToolName, type PackToolName } from './packs/registry.ts';
|
|
33
|
+
import { PACK_TOOL_HANDLERS, isPackToolName, APPROVAL_SPECS, type PackToolName } from './packs/registry.ts';
|
|
34
34
|
// The skill-dir listing lives in the shared zod-free module
|
|
35
35
|
// (src/lib/skill-tree.ts) so the boot guard, the installer's doctor check,
|
|
36
36
|
// the build-time --check stray scan, and validate-package's skill gate all
|
|
@@ -50,6 +50,18 @@ import { toolArgKeys } from './tool-schemas.ts';
|
|
|
50
50
|
// importers (structure-load-path, e2e/cases/core) and dispatchTool calls
|
|
51
51
|
// enforceVendorGate at dispatch's top.
|
|
52
52
|
import { VENDOR_GATED_TOOLS, enforceVendorGate } from './vendor-gate.ts';
|
|
53
|
+
// The dispatch-layer approval gate (the roster, the per-pack specs it
|
|
54
|
+
// resolves from packs/registry.ts's APPROVAL_SPECS merge, the consent
|
|
55
|
+
// ledger, and the envelope faces) lives in the approval-* family;
|
|
56
|
+
// dispatchTool calls runApprovalGate after the vendor gate and before the
|
|
57
|
+
// pack dispatch.
|
|
58
|
+
import {
|
|
59
|
+
recordApprovalDispatchOutcome,
|
|
60
|
+
runApprovalGate,
|
|
61
|
+
type ApprovalConsentScope,
|
|
62
|
+
type ApprovalReuseVia,
|
|
63
|
+
} from './approval-gate.ts';
|
|
64
|
+
import type { ApprovalSpec, ElicitationSeam } from './approval-types.ts';
|
|
53
65
|
// The capability catalog (service_catalog / list_packs / pack_info
|
|
54
66
|
// handlers, the SKILL_OWNER inversion, and the shared pack-refusal
|
|
55
67
|
// helpers) lives in capability-catalog.ts; dispatchTool switches into it.
|
|
@@ -134,6 +146,11 @@ export type ToolArgs = {
|
|
|
134
146
|
workspace_id?: string;
|
|
135
147
|
username?: string;
|
|
136
148
|
timeout_ms?: number;
|
|
149
|
+
// The async-exec claim protocol (huaweicloud_sandbox_exec_one_shot mode:
|
|
150
|
+
// "async" + huaweicloud_sandbox_exec_poll).
|
|
151
|
+
task_id?: string;
|
|
152
|
+
wait_ms?: number;
|
|
153
|
+
tail_bytes?: number;
|
|
137
154
|
local_path?: string;
|
|
138
155
|
remote_path?: string;
|
|
139
156
|
local_dir?: string;
|
|
@@ -150,7 +167,6 @@ export type ToolArgs = {
|
|
|
150
167
|
framework_type?: string;
|
|
151
168
|
source?: string;
|
|
152
169
|
pool_type?: string;
|
|
153
|
-
template_id?: string;
|
|
154
170
|
flavor_id?: string;
|
|
155
171
|
env?: Record<string, unknown>;
|
|
156
172
|
git?: SandboxGitConfig;
|
|
@@ -167,6 +183,8 @@ export type ToolArgs = {
|
|
|
167
183
|
rules?: unknown[];
|
|
168
184
|
expiry_hours?: number;
|
|
169
185
|
keepalive?: boolean;
|
|
186
|
+
// The expose_tunnel selective pre-clean mode (stale default / all / none).
|
|
187
|
+
cleanup_mode?: string;
|
|
170
188
|
tunnel_id?: string;
|
|
171
189
|
tunnels?: unknown[];
|
|
172
190
|
ref?: string;
|
|
@@ -193,6 +211,20 @@ export interface CallToolOptions {
|
|
|
193
211
|
// mcp-protocol.ts. Its absence is the guard that keeps the stateless remote
|
|
194
212
|
// from mutating process-shared pack state through a direct unlisted call.
|
|
195
213
|
onPacksChanged?: () => void;
|
|
214
|
+
// The server-run confirmation seam (PR-1.5): mcp-protocol.ts wires the
|
|
215
|
+
// client's initialize capability record and the SDK elicitInput call here;
|
|
216
|
+
// the approval gate consumes it in ask mode. Absent on the remote face
|
|
217
|
+
// (force-token) and in every non-protocol construction, which is exactly
|
|
218
|
+
// the non-capable T3 path.
|
|
219
|
+
elicitation?: ElicitationSeam;
|
|
220
|
+
// The approval gate's binding value for THIS call, set by dispatchTool
|
|
221
|
+
// when a roster tool's call passed the gate (ask-mode consent, a consumed
|
|
222
|
+
// token, or a classified pass-through). The auth pack's switch and init
|
|
223
|
+
// handlers execute this resolved snapshot — never a post-consent re-read
|
|
224
|
+
// of their backing stores (PR-3b's TOCTOU finding: the shown account must
|
|
225
|
+
// be the executed account). Handlers of tools whose substance lives in
|
|
226
|
+
// the args alone ignore it.
|
|
227
|
+
approvedBinding?: unknown;
|
|
196
228
|
}
|
|
197
229
|
|
|
198
230
|
// Reject invalid numeric args up front instead of silently coercing them to
|
|
@@ -269,16 +301,98 @@ async function dispatchTool(name: ToolName, args: ToolArgs, opts: CallToolOption
|
|
|
269
301
|
if (VENDOR_GATED_TOOLS.has(name)) {
|
|
270
302
|
enforceVendorGate(args, opts);
|
|
271
303
|
}
|
|
272
|
-
//
|
|
273
|
-
//
|
|
274
|
-
//
|
|
275
|
-
//
|
|
276
|
-
//
|
|
277
|
-
//
|
|
278
|
-
|
|
279
|
-
|
|
304
|
+
// The approval gate sits after the vendor gate and before the pack
|
|
305
|
+
// dispatch, spec-driven through the roster (APPROVAL_SPECS keys are
|
|
306
|
+
// exactly the roster names, compile-checked in packs/registry.ts). A
|
|
307
|
+
// non-roster tool pays one table miss here and dispatches unchanged.
|
|
308
|
+
// Envelopes are results, not throws, so a gated refusal reaches the
|
|
309
|
+
// caller as the same JSON the handler would have returned. On a pass
|
|
310
|
+
// the gate's binding rides the handler opts: the call's approved
|
|
311
|
+
// resolution, so credential-consuming handlers execute the snapshot the
|
|
312
|
+
// human confirmed — never a post-consent re-read (PR-3b's TOCTOU
|
|
313
|
+
// finding).
|
|
314
|
+
const approvalSpec = (APPROVAL_SPECS as Partial<Record<ToolName, ApprovalSpec>>)[name];
|
|
315
|
+
let arrivalStamp: number | undefined;
|
|
316
|
+
let arrivalSeq: number | undefined;
|
|
317
|
+
let approvedVia: ApprovalReuseVia | undefined;
|
|
318
|
+
let consentScope: ApprovalConsentScope | undefined;
|
|
319
|
+
let handlerOpts: CallToolOptions = opts;
|
|
320
|
+
if (approvalSpec) {
|
|
321
|
+
const gated = await runApprovalGate(name, approvalSpec, args, opts);
|
|
322
|
+
if (!('pass' in gated)) return gated.envelope;
|
|
323
|
+
arrivalStamp = gated.pass.arrivalStamp;
|
|
324
|
+
arrivalSeq = gated.pass.arrivalSeq;
|
|
325
|
+
approvedVia = gated.pass.approvedVia;
|
|
326
|
+
consentScope = gated.pass.consentScope;
|
|
327
|
+
if (gated.binding !== undefined) handlerOpts = { ...opts, approvedBinding: gated.binding };
|
|
328
|
+
}
|
|
329
|
+
// The delivery-outcome recorder sits at the COMMON dispatch boundary
|
|
330
|
+
// (round 12, finding 3): the gate covers every roster member — pack
|
|
331
|
+
// tools and core tools alike (PR-3c's packs_configure registration is
|
|
332
|
+
// a CORE-tool dispatch through the switch below) — so the recorder must
|
|
333
|
+
// wrap BOTH dispatch legs, or a core-tool registration records its
|
|
334
|
+
// consent arrival but structurally never the delivery outcome the memo
|
|
335
|
+
// keys on. The wrapper hands the gate's spec and the arrival's stamp to
|
|
336
|
+
// the recorder; non-gated tools pay one no-op (the recorder returns
|
|
337
|
+
// without a spec) and dispatch through unchanged.
|
|
338
|
+
try {
|
|
339
|
+
const result = isPackToolName(name)
|
|
340
|
+
? await PACK_TOOL_HANDLERS[name](args, handlerOpts)
|
|
341
|
+
: await dispatchCoreTool(name, args, handlerOpts);
|
|
342
|
+
recordApprovalDispatchOutcome(
|
|
343
|
+
name,
|
|
344
|
+
approvalSpec,
|
|
345
|
+
args,
|
|
346
|
+
opts,
|
|
347
|
+
result,
|
|
348
|
+
undefined,
|
|
349
|
+
arrivalStamp,
|
|
350
|
+
arrivalSeq,
|
|
351
|
+
consentScope,
|
|
352
|
+
);
|
|
353
|
+
// The reuse attribution (PR-3a): a call that rode the retry memo or the
|
|
354
|
+
// session standing grant names it on the response, so the caller can
|
|
355
|
+
// tell a fresh confirmation's execution from a reused consent's. Object
|
|
356
|
+
// results carry the field; every registered tool's handler returns one,
|
|
357
|
+
// and a non-object shape keeps its wire contract untouched.
|
|
358
|
+
if (approvedVia !== undefined && result !== null && typeof result === 'object' && !Array.isArray(result)) {
|
|
359
|
+
(result as Record<string, unknown>).approvedVia = approvedVia;
|
|
360
|
+
}
|
|
361
|
+
return result;
|
|
362
|
+
} catch (error) {
|
|
363
|
+
recordApprovalDispatchOutcome(
|
|
364
|
+
name,
|
|
365
|
+
approvalSpec,
|
|
366
|
+
args,
|
|
367
|
+
opts,
|
|
368
|
+
undefined,
|
|
369
|
+
error,
|
|
370
|
+
arrivalStamp,
|
|
371
|
+
arrivalSeq,
|
|
372
|
+
consentScope,
|
|
373
|
+
);
|
|
374
|
+
// The reuse attribution rides the error path too (interrogate round 2,
|
|
375
|
+
// F11): a memo-reused retry whose handler throws again still names what
|
|
376
|
+
// authorized it — programmatic callers see the marker on the error
|
|
377
|
+
// object; the wire shape of the error itself is unchanged.
|
|
378
|
+
if (approvedVia !== undefined && error instanceof Error) {
|
|
379
|
+
(error as Error & { approvedVia?: ApprovalReuseVia }).approvedVia = approvedVia;
|
|
380
|
+
}
|
|
381
|
+
throw error;
|
|
280
382
|
}
|
|
383
|
+
}
|
|
281
384
|
|
|
385
|
+
// The core-tool dispatch leg: the switch over the core remainder, after
|
|
386
|
+
// the pack guard — extracted so the delivery-outcome wrapper above can
|
|
387
|
+
// cover both dispatch paths from one boundary. The compile-time
|
|
388
|
+
// exhaustiveness never-guard moves with it: `name` is narrowed to
|
|
389
|
+
// Exclude<ToolName, PackToolName> by the call site's isPackToolName
|
|
390
|
+
// branch above.
|
|
391
|
+
async function dispatchCoreTool(
|
|
392
|
+
name: Exclude<ToolName, PackToolName>,
|
|
393
|
+
args: ToolArgs,
|
|
394
|
+
opts: CallToolOptions,
|
|
395
|
+
): Promise<unknown> {
|
|
282
396
|
switch (name) {
|
|
283
397
|
case 'huaweicloud_hook_check_command':
|
|
284
398
|
return hookResult(withUnknownOperationWarn(String(args.command || ''), evaluateCommandRisk(args.command || '')));
|
|
@@ -2,9 +2,13 @@
|
|
|
2
2
|
// spawn) and the undici fetch path honoring HUAWEICLOUD_NPM_REGISTRY. Part of
|
|
3
3
|
// the src/update-* family behind the src/update-check.ts re-export face.
|
|
4
4
|
import { spawn, spawnSync, type ChildProcessWithoutNullStreams } from 'node:child_process';
|
|
5
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
6
|
+
import { homedir } from 'node:os';
|
|
7
|
+
import { dirname, join, relative, sep } from 'node:path';
|
|
5
8
|
|
|
6
9
|
import { fetchWithProxy } from './proxy/proxy-agent.ts';
|
|
7
10
|
import { asText } from './lib/as-text.ts';
|
|
11
|
+
import { workspaceMemberMatch } from './lib/workspace-glob.ts';
|
|
8
12
|
import type { DistTags } from './update-semver.ts';
|
|
9
13
|
|
|
10
14
|
const NPM_BIN = process.platform === 'win32' ? 'npm.cmd' : 'npm';
|
|
@@ -46,15 +50,346 @@ function debugLog(message: string): void {
|
|
|
46
50
|
}
|
|
47
51
|
}
|
|
48
52
|
|
|
53
|
+
// One spelling rule serves every env channel for the pre-flight and the child:
|
|
54
|
+
// trimmed non-empty exact-case first, else the first trimmed non-empty
|
|
55
|
+
// case/padding variant; whitespace counts as unset.
|
|
56
|
+
export function resolveEnvSpelling(env: NodeJS.ProcessEnv, canonical: string): string | undefined {
|
|
57
|
+
const spellings = Object.entries(env)
|
|
58
|
+
.filter(([key, value]) => key.trim().toUpperCase() === canonical && Boolean(value?.trim()))
|
|
59
|
+
.map(([key, value]) => [key, value!.trim()] as const);
|
|
60
|
+
const exact = spellings.find(([key]) => key === canonical);
|
|
61
|
+
return (exact ?? spellings[0])?.[1];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function resolveRegistryOverride(env: NodeJS.ProcessEnv = process.env): string | undefined {
|
|
65
|
+
return resolveEnvSpelling(env, 'HUAWEICLOUD_NPM_REGISTRY');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// The npm auth channels resolve through the same exact-case-first collapse as
|
|
69
|
+
// the child env, so duplicate/variant spellings cannot split the halves.
|
|
70
|
+
function resolveNpmAuthToken(env: NodeJS.ProcessEnv = process.env): string | undefined {
|
|
71
|
+
return resolveEnvSpelling(env, 'NPM_TOKEN') ?? resolveEnvSpelling(env, 'NODE_AUTH_TOKEN');
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// npm reads the userconfig by a case-insensitive `npm_config_userconfig` key,
|
|
75
|
+
// trims the value, and skips an empty value (falling back to ~/.npmrc). An
|
|
76
|
+
// empty spelling is skipped rather than terminal, so a set sibling still wins;
|
|
77
|
+
// a whitespace-only value trims to an empty path, which is not the fallback.
|
|
78
|
+
function resolveUserconfig(env: NodeJS.ProcessEnv = process.env): string | undefined {
|
|
79
|
+
const raw = Object.entries(env)
|
|
80
|
+
.filter(([key, value]) => key.toUpperCase() === 'NPM_CONFIG_USERCONFIG' && value !== '')
|
|
81
|
+
.at(-1)?.[1];
|
|
82
|
+
if (raw === undefined) return undefined;
|
|
83
|
+
// npm applies envReplace to every config value before path resolution, so a
|
|
84
|
+
// CI env block (`${CI_PROJECT_DIR}/.npmrc`) reaches the same file.
|
|
85
|
+
return expandTilde(envReplace(raw.trim(), env));
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function expandTilde(value: string): string {
|
|
89
|
+
if (value === '~') return homedir();
|
|
90
|
+
return value.startsWith('~/') ? join(homedir(), value.slice(2)) : value;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function workspaceGlobs(value: unknown): string[] {
|
|
94
|
+
const declared = Array.isArray(value)
|
|
95
|
+
? value
|
|
96
|
+
: value && typeof value === 'object' && Array.isArray((value as Record<string, unknown>).packages)
|
|
97
|
+
? ((value as Record<string, unknown>).packages as unknown[])
|
|
98
|
+
: [];
|
|
99
|
+
// npm rejects a non-string entry (EWORKSPACESCONFIG / a .match TypeError), so
|
|
100
|
+
// the declaration yields no members instead of promoting where npm crashes.
|
|
101
|
+
if (declared.some((entry) => typeof entry !== 'string')) return [];
|
|
102
|
+
return declared as string[];
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function packageJsonWorkspaces(path: string): string[] | undefined {
|
|
106
|
+
try {
|
|
107
|
+
const parsed: unknown = JSON.parse(readFileSync(path, 'utf8'));
|
|
108
|
+
if (!parsed || typeof parsed !== 'object') return undefined;
|
|
109
|
+
const globs = workspaceGlobs((parsed as Record<string, unknown>).workspaces);
|
|
110
|
+
return globs.length ? globs : undefined;
|
|
111
|
+
} catch {
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function isWorkspaceMember(root: string, member: string, globs: string[]): boolean {
|
|
117
|
+
return workspaceMemberMatch(relative(root, member).split(sep).join('/'), globs);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function localPrefix(start: string = process.cwd()): string {
|
|
121
|
+
// npm's localPrefix starts at the nearest package dir and moves to an
|
|
122
|
+
// ancestor only when that ancestor's workspace map actually contains the
|
|
123
|
+
// package dir (mapWorkspaces membership, not the mere presence of the field).
|
|
124
|
+
let nearest: string | undefined;
|
|
125
|
+
let current = start;
|
|
126
|
+
for (;;) {
|
|
127
|
+
const manifest = join(current, 'package.json');
|
|
128
|
+
if (existsSync(manifest)) {
|
|
129
|
+
if (!nearest) {
|
|
130
|
+
nearest = current;
|
|
131
|
+
} else {
|
|
132
|
+
const globs = packageJsonWorkspaces(manifest);
|
|
133
|
+
if (globs && isWorkspaceMember(current, nearest, globs)) return current;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
const parent = dirname(current);
|
|
137
|
+
if (parent === current) return nearest ?? start;
|
|
138
|
+
current = parent;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// Env spellings for npm's own config keys are case-insensitive; npm takes the
|
|
143
|
+
// last non-empty one when both are exported (an empty value is skipped, not
|
|
144
|
+
// terminal).
|
|
145
|
+
function envValue(env: NodeJS.ProcessEnv, name: string): string | undefined {
|
|
146
|
+
const values = Object.entries(env)
|
|
147
|
+
.filter(([key]) => key.toUpperCase() === name)
|
|
148
|
+
.map(([, value]) => value?.trim())
|
|
149
|
+
.filter((value): value is string => Boolean(value));
|
|
150
|
+
return values.at(-1);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function globalconfigPath(env: NodeJS.ProcessEnv): string {
|
|
154
|
+
const globalconfig = envValue(env, 'NPM_CONFIG_GLOBALCONFIG');
|
|
155
|
+
if (globalconfig) return expandTilde(globalconfig);
|
|
156
|
+
const prefix = envValue(env, 'NPM_CONFIG_PREFIX') || env.PREFIX || dirname(dirname(process.execPath));
|
|
157
|
+
return join(prefix, 'etc', 'npmrc');
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function stripIniQuotes(value: string): string {
|
|
161
|
+
const trimmed = value.trim();
|
|
162
|
+
const quoted = /^(['"])([\s\S]*)\1$/.exec(trimmed);
|
|
163
|
+
return quoted ? quoted[2]! : trimmed;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// npm's ini value rule: a quoted value keeps its comment characters; an
|
|
167
|
+
// unquoted value ends at the first unescaped `;` or `#` (with or without
|
|
168
|
+
// whitespace) and backslash escapes those three characters.
|
|
169
|
+
function parseIniValue(raw: string): string {
|
|
170
|
+
const trimmed = raw.trim();
|
|
171
|
+
if (/^(['"])[\s\S]*\1$/.test(trimmed)) return stripIniQuotes(trimmed);
|
|
172
|
+
let out = '';
|
|
173
|
+
let escaped = false;
|
|
174
|
+
for (const char of trimmed) {
|
|
175
|
+
if (escaped) {
|
|
176
|
+
out += ';#\\'.includes(char) ? char : `\\${char}`;
|
|
177
|
+
escaped = false;
|
|
178
|
+
} else if (char === ';' || char === '#') {
|
|
179
|
+
break;
|
|
180
|
+
} else if (char === '\\') {
|
|
181
|
+
escaped = true;
|
|
182
|
+
} else {
|
|
183
|
+
out += char;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
if (escaped) out += '\\';
|
|
187
|
+
return out.trim();
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// npm's env-replace: `\${...}` stays literal, `${VAR}` interpolates the exact
|
|
191
|
+
// env name (unset keeps the literal), and `${VAR?}` resolves empty when unset.
|
|
192
|
+
function envReplace(value: string, env: NodeJS.ProcessEnv): string {
|
|
193
|
+
let out = '';
|
|
194
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
195
|
+
const char = value[index]!;
|
|
196
|
+
if (char === '\\' && value[index + 1] === '$') {
|
|
197
|
+
out += '$';
|
|
198
|
+
index += 1;
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
if (char === '$' && value[index + 1] === '{') {
|
|
202
|
+
const end = value.indexOf('}', index + 2);
|
|
203
|
+
if (end < 0) {
|
|
204
|
+
out += char;
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
let name = value.slice(index + 2, end);
|
|
208
|
+
const optional = name.endsWith('?');
|
|
209
|
+
if (optional) name = name.slice(0, -1);
|
|
210
|
+
const resolved = env[name];
|
|
211
|
+
out += resolved !== undefined ? resolved : optional ? '' : value.slice(index, end + 1);
|
|
212
|
+
index = end;
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
out += char;
|
|
216
|
+
}
|
|
217
|
+
return out;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function parseNpmrc(text: string, env: NodeJS.ProcessEnv): Map<string, string> {
|
|
221
|
+
const entries = new Map<string, string>();
|
|
222
|
+
for (const line of text.split(/\r?\n/)) {
|
|
223
|
+
const trimmed = line.trim();
|
|
224
|
+
if (!trimmed || trimmed.startsWith(';') || trimmed.startsWith('#')) continue;
|
|
225
|
+
const match = /^([^=]+?)\s*=\s*(.*)$/.exec(trimmed);
|
|
226
|
+
if (!match) continue;
|
|
227
|
+
const value = parseIniValue(match[2]!);
|
|
228
|
+
entries.set(match[1]!.trim(), envReplace(value, env));
|
|
229
|
+
}
|
|
230
|
+
return entries;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// npm's auth cascade: env-spread nerf keys, the project/workspace root
|
|
234
|
+
// (nearest package.json), the userconfig, then the global config. Unreadable
|
|
235
|
+
// files contribute nothing.
|
|
236
|
+
function configLayers(env: NodeJS.ProcessEnv): Array<Map<string, string>> {
|
|
237
|
+
const layers: Array<Map<string, string>> = [];
|
|
238
|
+
const spread = new Map<string, string>();
|
|
239
|
+
for (const [key, value] of Object.entries(env)) {
|
|
240
|
+
const body = /^npm_config_(.*)$/i.exec(key)?.[1];
|
|
241
|
+
if (body?.startsWith('//') && value !== undefined) spread.set(body, envReplace(value, env));
|
|
242
|
+
}
|
|
243
|
+
if (spread.size) layers.push(spread);
|
|
244
|
+
const userconfig = resolveUserconfig(env);
|
|
245
|
+
const files = [join(localPrefix(), '.npmrc'), userconfig ?? join(homedir(), '.npmrc'), globalconfigPath(env)];
|
|
246
|
+
for (const file of files) {
|
|
247
|
+
try {
|
|
248
|
+
layers.push(parseNpmrc(readFileSync(file, 'utf8'), env));
|
|
249
|
+
} catch {
|
|
250
|
+
// an unreadable config layer contributes no keys
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
return layers;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function basicHeader(username: string | undefined, password: string | undefined): string | undefined {
|
|
257
|
+
if (!username || password === undefined) return undefined;
|
|
258
|
+
const decoded = (() => {
|
|
259
|
+
try {
|
|
260
|
+
return Buffer.from(password, 'base64').toString('utf8');
|
|
261
|
+
} catch {
|
|
262
|
+
return password;
|
|
263
|
+
}
|
|
264
|
+
})();
|
|
265
|
+
return `Basic ${Buffer.from(`${username}:${decoded}`).toString('base64')}`;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// The pre-flight auth header for a registry, resolved the way npm resolves it:
|
|
269
|
+
// walk every nerf-dart path level through to the host root (both the slash and
|
|
270
|
+
// slash-less keys), and take the first token/basic entry found across the
|
|
271
|
+
// config cascade. A falsy candidate is skipped, never terminal (npm's hasAuth
|
|
272
|
+
// tests truthiness and keeps walking), while an explicitly empty key's layer
|
|
273
|
+
// suppresses the lower layers; when the whole walk finds no auth and some level
|
|
274
|
+
// named an empty auth key, `null` tells the caller not to resurrect the env
|
|
275
|
+
// token.
|
|
276
|
+
function npmrcAuthHeader(registry: string, env: NodeJS.ProcessEnv = process.env): string | undefined | null {
|
|
277
|
+
let url: URL;
|
|
278
|
+
try {
|
|
279
|
+
url = new URL(registry);
|
|
280
|
+
} catch {
|
|
281
|
+
return undefined;
|
|
282
|
+
}
|
|
283
|
+
const host = `//${url.host}`;
|
|
284
|
+
const path = url.pathname.replace(/\/+$/, '');
|
|
285
|
+
const levels: string[] = [];
|
|
286
|
+
let current = path;
|
|
287
|
+
for (;;) {
|
|
288
|
+
levels.push(current);
|
|
289
|
+
if (!current) break;
|
|
290
|
+
const next = current.replace(/\/[^/]*$/, '');
|
|
291
|
+
if (next === current) break;
|
|
292
|
+
current = next;
|
|
293
|
+
}
|
|
294
|
+
const layers = configLayers(env);
|
|
295
|
+
// `undefined` means no layer names the key; an empty string is that layer's
|
|
296
|
+
// explicit (falsy) value.
|
|
297
|
+
const lookup = (key: string): string | undefined => {
|
|
298
|
+
for (const layer of layers) {
|
|
299
|
+
if (layer.has(key)) return layer.get(key);
|
|
300
|
+
}
|
|
301
|
+
return undefined;
|
|
302
|
+
};
|
|
303
|
+
let sawExplicitEmpty = false;
|
|
304
|
+
for (const level of levels) {
|
|
305
|
+
const prefix = `${host}${level}`;
|
|
306
|
+
const token = lookup(`${prefix}/:_authToken`) ?? lookup(`${prefix}:_authToken`);
|
|
307
|
+
if (token) return token.startsWith('Bearer ') ? token : `Bearer ${token}`;
|
|
308
|
+
if (token === '') sawExplicitEmpty = true;
|
|
309
|
+
const auth = lookup(`${prefix}/:_auth`) ?? lookup(`${prefix}:_auth`);
|
|
310
|
+
if (auth) return `Basic ${auth}`;
|
|
311
|
+
if (auth === '') sawExplicitEmpty = true;
|
|
312
|
+
const username = lookup(`${prefix}/:username`);
|
|
313
|
+
const password = lookup(`${prefix}/:_password`);
|
|
314
|
+
if (username && password) {
|
|
315
|
+
const basic = basicHeader(username, password);
|
|
316
|
+
if (basic) return basic;
|
|
317
|
+
}
|
|
318
|
+
if (username !== undefined && password !== undefined && (!username || !password)) sawExplicitEmpty = true;
|
|
319
|
+
}
|
|
320
|
+
return sawExplicitEmpty ? null : undefined;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// The pre-flight follows redirects the way npm's registry client does: the same
|
|
324
|
+
// Authorization header travels to a same-host target leg (port or scheme
|
|
325
|
+
// changes re-authenticate), and a hostname change drops it.
|
|
326
|
+
async function fetchFollowingAuth(url: string, init: RequestInit) {
|
|
327
|
+
let current = url;
|
|
328
|
+
const originalHost = new URL(url).hostname;
|
|
329
|
+
for (let hop = 0; hop < 5; hop += 1) {
|
|
330
|
+
const hopInit = { ...init, redirect: 'manual' as const };
|
|
331
|
+
// npm re-authenticates a same-host redirect (port/scheme changes) but strips
|
|
332
|
+
// the Authorization when the hostname changes.
|
|
333
|
+
if (new URL(current).hostname !== originalHost) delete hopInit.headers;
|
|
334
|
+
const response = await fetchWithProxy(current, hopInit);
|
|
335
|
+
const location = response.status >= 300 && response.status < 400 ? response.headers.get('location') : undefined;
|
|
336
|
+
if (!location) return response;
|
|
337
|
+
try {
|
|
338
|
+
current = new URL(location, current).toString();
|
|
339
|
+
} catch {
|
|
340
|
+
return response;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
return null;
|
|
344
|
+
}
|
|
345
|
+
|
|
49
346
|
export async function queryDistTagsFetch({ timeoutMs = 15000 }: { timeoutMs?: number } = {}): Promise<DistTags | null> {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
347
|
+
// One normalization with upgradeEnv: a padded or case-variant spelling is
|
|
348
|
+
// usable and a whitespace-only value is unset, so the pre-flight and the
|
|
349
|
+
// child pin cannot disagree.
|
|
350
|
+
let configured = resolveRegistryOverride();
|
|
351
|
+
// A registry override may carry URL credentials; npm only falls back to them
|
|
352
|
+
// when the config cascade yields no header, and it sends the raw
|
|
353
|
+
// (percent-encoded) userinfo as Basic.
|
|
354
|
+
let urlAuth: string | undefined;
|
|
355
|
+
if (configured) {
|
|
356
|
+
try {
|
|
357
|
+
const parsed = new URL(configured);
|
|
358
|
+
if (parsed.username || parsed.password) {
|
|
359
|
+
urlAuth = `Basic ${Buffer.from(`${parsed.username}:${parsed.password}`).toString('base64')}`;
|
|
360
|
+
parsed.username = '';
|
|
361
|
+
parsed.password = '';
|
|
362
|
+
configured = parsed.toString();
|
|
363
|
+
}
|
|
364
|
+
} catch {
|
|
365
|
+
// a malformed URL degrades like npm's own registry parse
|
|
366
|
+
}
|
|
53
367
|
}
|
|
368
|
+
let registry = 'https://registry.npmjs.org';
|
|
369
|
+
// The request URL is built by appending `/-/package/…`, so a trailing slash
|
|
370
|
+
// on the override would produce a doubled path.
|
|
371
|
+
if (configured) registry = configured.replace(/\/+$/, '');
|
|
54
372
|
const controller = new AbortController();
|
|
55
373
|
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
374
|
+
// The pre-flight query needs the same mirror auth the child npm gets: a
|
|
375
|
+
// token-guarded private mirror 401s the packument without it, aborting the
|
|
376
|
+
// act before the spawn. The token is read for this request only; the env
|
|
377
|
+
// shaping of the child is a separate concern.
|
|
378
|
+
// npm's own order: the config cascade's named channel first (an .npmrc value
|
|
379
|
+
// may interpolate NODE_AUTH_TOKEN even when a stale NPM_TOKEN is exported),
|
|
380
|
+
// then the URL userinfo, then the env tokens, then no auth.
|
|
381
|
+
const token = resolveNpmAuthToken();
|
|
382
|
+
const configAuth = npmrcAuthHeader(registry);
|
|
383
|
+
// `null` is an explicitly empty config token: no env fallback (npm's merged
|
|
384
|
+
// value is empty and it sends nothing); URL userinfo still applies, as it
|
|
385
|
+
// does for npm when no Authorization header was set.
|
|
386
|
+
const authorization = configAuth ?? urlAuth ?? (configAuth === null || !token ? undefined : `Bearer ${token}`);
|
|
387
|
+
const init: RequestInit = {
|
|
388
|
+
signal: controller.signal,
|
|
389
|
+
...(authorization ? { headers: { authorization } } : {}),
|
|
390
|
+
};
|
|
56
391
|
try {
|
|
57
|
-
const resp = await
|
|
392
|
+
const resp = await fetchFollowingAuth(`${registry}/-/package/huaweicloud-devkit/dist-tags`, init);
|
|
58
393
|
clearTimeout(timer);
|
|
59
394
|
if (!resp || !resp.ok) return null;
|
|
60
395
|
return toDistTags(await resp.json().catch(() => null));
|
|
@@ -103,7 +438,7 @@ export function queryDistTags({
|
|
|
103
438
|
// delegation closes that inconsistency, so every default consumer
|
|
104
439
|
// (getCachedUpdateInfo, upgradePackage) honors the operator/E2E override
|
|
105
440
|
// without spawning npm.
|
|
106
|
-
if (
|
|
441
|
+
if (resolveRegistryOverride()) {
|
|
107
442
|
return queryDistTagsFetch({ timeoutMs });
|
|
108
443
|
}
|
|
109
444
|
return new Promise<DistTags | null>((resolve) => {
|