@bitkyc08/opencodex 2.10.0 → 2.10.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/AGENTS_INSTALL.md +77 -0
- package/README.md +4 -10
- package/bin/ocx.mjs +71 -18
- package/gui/dist/assets/index-Cd6_PBKn.css +1 -0
- package/gui/dist/assets/index-ChZQsmBY.js +70 -0
- package/gui/dist/index.html +2 -2
- package/gui/dist/provider-icons/alibaba-color.svg +1 -1
- package/gui/dist/provider-icons/antigravity-color.svg +1 -1
- package/gui/dist/provider-icons/claude-color.svg +1 -1
- package/gui/dist/provider-icons/cline-color.svg +16 -0
- package/gui/dist/provider-icons/cloudflare-ai-gateway-color.svg +1 -1
- package/gui/dist/provider-icons/copilot-color.svg +1 -1
- package/gui/dist/provider-icons/cursor-color.svg +1 -1
- package/gui/dist/provider-icons/deepseek-color.svg +1 -1
- package/gui/dist/provider-icons/firepass-color.svg +1 -1
- package/gui/dist/provider-icons/fireworks-color.svg +1 -1
- package/gui/dist/provider-icons/gemini-color.svg +1 -1
- package/gui/dist/provider-icons/github-copilot-color.svg +1 -1
- package/gui/dist/provider-icons/gitlab-duo-color.svg +1 -1
- package/gui/dist/provider-icons/grok.svg +1 -1
- package/gui/dist/provider-icons/groq-color.svg +1 -1
- package/gui/dist/provider-icons/huggingface-color.svg +1 -1
- package/gui/dist/provider-icons/kimi-color.svg +1 -1
- package/gui/dist/provider-icons/kiro-color.svg +2 -2
- package/gui/dist/provider-icons/lm-studio-color.svg +1 -1
- package/gui/dist/provider-icons/mistral-color.svg +1 -1
- package/gui/dist/provider-icons/moonshot-color.svg +1 -1
- package/gui/dist/provider-icons/nvidia-color.svg +1 -1
- package/gui/dist/provider-icons/ollama-color.svg +1 -1
- package/gui/dist/provider-icons/openai.svg +1 -1
- package/gui/dist/provider-icons/opencode.svg +2 -1
- package/gui/dist/provider-icons/openrouter-color.svg +1 -1
- package/gui/dist/provider-icons/pi.svg +2 -2
- package/gui/dist/provider-icons/qianfan-color.svg +1 -1
- package/gui/dist/provider-icons/qwen-portal-color.svg +1 -1
- package/gui/dist/provider-icons/vercel-ai-gateway-color.svg +1 -1
- package/gui/dist/provider-icons/vllm-color.svg +1 -1
- package/gui/dist/provider-icons/xiaomi-color.svg +1 -1
- package/package.json +8 -4
- package/src/adapters/anthropic.ts +208 -14
- package/src/adapters/base.ts +16 -5
- package/src/adapters/cursor/effort-map.ts +3 -2
- package/src/adapters/cursor/framing.ts +39 -0
- package/src/adapters/cursor/live-transport.ts +105 -95
- package/src/adapters/cursor/native-exec.ts +32 -6
- package/src/adapters/cursor/protobuf-request.ts +20 -15
- package/src/adapters/cursor/request-builder.ts +21 -7
- package/src/adapters/cursor/types.ts +7 -0
- package/src/adapters/google-antigravity-replay.ts +237 -21
- package/src/adapters/google-truncation.ts +11 -0
- package/src/adapters/google.ts +50 -9
- package/src/adapters/identity.ts +39 -6
- package/src/adapters/kiro-errors.ts +11 -0
- package/src/adapters/kiro-events.ts +19 -1
- package/src/adapters/kiro-thinking.ts +10 -2
- package/src/adapters/kiro-tools.ts +10 -1
- package/src/adapters/kiro.ts +37 -11
- package/src/adapters/openai-chat.ts +284 -83
- package/src/adapters/openai-responses.ts +182 -24
- package/src/bridge.ts +177 -7
- package/src/chat/outbound.ts +78 -23
- package/src/claude/agents-inject.ts +27 -5
- package/src/claude/inbound.ts +11 -1
- package/src/claude/model-info.ts +13 -10
- package/src/claude/outbound.ts +17 -0
- package/src/cli/account-api.ts +24 -0
- package/src/cli/account-auth.ts +31 -6
- package/src/cli/account-main.ts +317 -0
- package/src/cli/account.ts +5 -0
- package/src/cli/claude.ts +2 -1
- package/src/cli/doctor.ts +93 -22
- package/src/cli/export-command.ts +26 -12
- package/src/cli/help.ts +8 -6
- package/src/cli/index.ts +56 -22
- package/src/cli/integrations.ts +84 -1
- package/src/cli/observe.ts +54 -1
- package/src/cli/opencode.ts +2 -1
- package/src/cli/provider-runtime.ts +18 -1
- package/src/cli/route-policy.ts +92 -0
- package/src/cli/runtime-api.ts +6 -3
- package/src/cli/star-prompt.ts +71 -15
- package/src/cli/status.ts +1 -1
- package/src/cli/v2.ts +36 -9
- package/src/clients/config-export.ts +687 -10
- package/src/codex/account-lifecycle.ts +30 -5
- package/src/codex/account-usability.ts +22 -2
- package/src/codex/admission.ts +255 -0
- package/src/codex/auth-api.ts +427 -140
- package/src/codex/auth-context.ts +155 -30
- package/src/codex/autostart-health.ts +8 -1
- package/src/codex/catalog/account-models.ts +62 -0
- package/src/codex/catalog/aggregation.ts +14 -1
- package/src/codex/catalog/bundled.ts +282 -32
- package/src/codex/catalog/filesystem-evidence.ts +302 -0
- package/src/codex/catalog/metadata.ts +51 -6
- package/src/codex/catalog/parsing.ts +6 -3
- package/src/codex/catalog/provider-fetch.ts +576 -41
- package/src/codex/catalog/sync.ts +505 -66
- package/src/codex/catalog-admission.ts +197 -0
- package/src/codex/catalog-write-serialization.ts +241 -0
- package/src/codex/catalog.ts +2 -1
- package/src/codex/codex-write-lock.ts +372 -0
- package/src/codex/convergence-types.ts +593 -0
- package/src/codex/convergence.ts +441 -0
- package/src/codex/desired-state.ts +177 -0
- package/src/codex/features.ts +52 -8
- package/src/codex/generation.ts +202 -0
- package/src/codex/history-job.ts +257 -0
- package/src/codex/history-lock.ts +241 -0
- package/src/codex/history-migration-guardian.ts +18 -5
- package/src/codex/history-provider.ts +9 -2
- package/src/codex/history-transition.ts +105 -0
- package/src/codex/history-worker.ts +176 -0
- package/src/codex/inject-coordination.ts +245 -0
- package/src/codex/inject.ts +605 -124
- package/src/codex/integration-record.ts +266 -0
- package/src/codex/internal/catalog-writer.ts +203 -0
- package/src/codex/internal/history-writer.ts +80 -0
- package/src/codex/journal.ts +10 -1
- package/src/codex/main-account-cache.ts +24 -0
- package/src/codex/management-convergence.ts +114 -0
- package/src/codex/native-main-admission.ts +47 -0
- package/src/codex/native-main-auth-temp.ts +187 -0
- package/src/codex/native-main-claim.ts +167 -0
- package/src/codex/native-main-lock-file.ts +158 -0
- package/src/codex/native-main-owner.ts +315 -0
- package/src/codex/native-profile-api.ts +247 -0
- package/src/codex/native-profile-manager.ts +1512 -0
- package/src/codex/native-profile-processes.ts +121 -0
- package/src/codex/native-profile-recovery.ts +99 -0
- package/src/codex/native-profile-stage-store.ts +387 -0
- package/src/codex/native-profile-startup.ts +340 -0
- package/src/codex/native-profile-store.ts +855 -0
- package/src/codex/native-profile-types.ts +120 -0
- package/src/codex/native-residue.ts +557 -0
- package/src/codex/project-config-warnings.ts +18 -4
- package/src/codex/prompt-journal.ts +311 -0
- package/src/codex/prompt-layers.ts +967 -0
- package/src/codex/prompt-lock.ts +143 -0
- package/src/codex/quota-rejection.ts +224 -0
- package/src/codex/quota.ts +86 -3
- package/src/codex/routing.ts +299 -62
- package/src/codex/runtime.ts +159 -38
- package/src/codex/shim.ts +39 -13
- package/src/codex/subagent-model-fallback.ts +73 -12
- package/src/codex/transition-state.ts +604 -0
- package/src/codex/upstream-host-health.ts +70 -0
- package/src/codex/user-identity.ts +266 -0
- package/src/codex/write-coordination.ts +114 -0
- package/src/config.ts +562 -26
- package/src/generated/jawcode-model-metadata.ts +2 -2
- package/src/grok/inject.ts +15 -4
- package/src/grok/inspect.ts +45 -0
- package/src/images/loop.ts +113 -20
- package/src/integrations/config-io.ts +151 -0
- package/src/integrations/journal.ts +315 -0
- package/src/integrations/merge.ts +135 -0
- package/src/integrations/native/ownership-preflight.ts +165 -0
- package/src/integrations/ownership.ts +111 -0
- package/src/integrations/registry.ts +101 -0
- package/src/integrations/serialize.ts +235 -0
- package/src/integrations/state.ts +290 -0
- package/src/integrations/store.ts +103 -0
- package/src/integrations/writer.ts +492 -0
- package/src/lib/bounded-body.ts +46 -8
- package/src/lib/bun-runtime.ts +110 -1
- package/src/lib/bun-stream-caps.ts +2 -1
- package/src/lib/redact.ts +407 -2
- package/src/lib/shadow-call.ts +24 -0
- package/src/lib/translator-budget.ts +10 -0
- package/src/lib/upstream-reachability.ts +91 -0
- package/src/lib/upstream-retry.ts +154 -2
- package/src/lib/windows-secret-acl.ts +212 -11
- package/src/lib/winsw.ts +9 -3
- package/src/oauth/index.ts +61 -3
- package/src/oauth/key-providers.ts +4 -0
- package/src/oauth/kiro.ts +50 -6
- package/src/oauth/store.ts +31 -6
- package/src/oauth/token-guardian.ts +9 -3
- package/src/providers/codex-capacity.ts +288 -0
- package/src/providers/derive.ts +33 -1
- package/src/providers/free-directory.ts +3 -1
- package/src/providers/key-failover.ts +71 -3
- package/src/providers/openai-sidecar.ts +64 -4
- package/src/providers/openai-virtual-models.ts +1 -0
- package/src/providers/quota.ts +334 -26
- package/src/providers/registry.ts +284 -16
- package/src/providers/xai-transport.ts +11 -4
- package/src/responses/compaction.ts +8 -1
- package/src/responses/hosted-tool-policy.ts +9 -0
- package/src/responses/parser.ts +44 -2
- package/src/responses/reasoning-envelope.ts +9 -1
- package/src/responses/reasoning-replay-cache.ts +105 -0
- package/src/responses/spill-store.ts +45 -8
- package/src/responses/state.ts +161 -17
- package/src/router.ts +302 -16
- package/src/routing/analytics.ts +377 -0
- package/src/routing/capability.ts +204 -0
- package/src/routing/cost.ts +77 -0
- package/src/routing/evaluator.ts +444 -0
- package/src/routing/health.ts +401 -0
- package/src/routing/history/cursor.ts +43 -0
- package/src/routing/history/indexer.ts +590 -0
- package/src/routing/history/schema.ts +72 -0
- package/src/routing/profile.ts +423 -0
- package/src/routing/quota.ts +124 -0
- package/src/routing/request-evidence.ts +45 -0
- package/src/routing/trace.ts +686 -0
- package/src/server/auth-cors.ts +46 -6
- package/src/server/chat-completions.ts +28 -13
- package/src/server/claude-messages.ts +23 -15
- package/src/server/gui-static.ts +39 -10
- package/src/server/images.ts +10 -1
- package/src/server/index.ts +238 -52
- package/src/server/lifecycle.ts +228 -9
- package/src/server/live.ts +14 -3
- package/src/server/management/agent-settings-routes.ts +64 -14
- package/src/server/management/combo-routes.ts +5 -5
- package/src/server/management/config-routes.ts +1 -1
- package/src/server/management/context.ts +42 -2
- package/src/server/management/integration-routes.ts +538 -0
- package/src/server/management/logs-usage-routes.ts +1 -1
- package/src/server/management/model-routes.ts +32 -113
- package/src/server/management/model-rows.ts +117 -0
- package/src/server/management/native-integration-routes.ts +587 -0
- package/src/server/management/oauth-account-routes.ts +1 -1
- package/src/server/management/provider-routes.ts +218 -117
- package/src/server/management/request-history-routes.ts +191 -0
- package/src/server/management/routing-analytics-routes.ts +74 -0
- package/src/server/management/routing-profile-routes.ts +177 -0
- package/src/server/management/shared.ts +2 -2
- package/src/server/management/sidebar-routes.ts +47 -31
- package/src/server/management/sync-response.ts +69 -0
- package/src/server/management/system-restart.ts +276 -24
- package/src/server/management/system-routes.ts +4 -0
- package/src/server/management-api.ts +84 -9
- package/src/server/management-auth.ts +43 -5
- package/src/server/relay-eager.ts +82 -42
- package/src/server/relay.ts +120 -6
- package/src/server/request-log.ts +26 -6
- package/src/server/responses/collaboration.ts +63 -8
- package/src/server/responses/compact.ts +272 -41
- package/src/server/responses/core.ts +730 -132
- package/src/server/responses/fetch-helpers.ts +15 -1
- package/src/server/responses-item-id-repair.ts +32 -3
- package/src/server/responses-json-events.ts +52 -0
- package/src/server/responses-snapshot-repair.ts +621 -0
- package/src/server/search.ts +51 -6
- package/src/server/sse-payload-rewrite.ts +89 -12
- package/src/server/startup-health-cache.ts +7 -1
- package/src/server/ws-bridge.ts +11 -17
- package/src/service-manager-probe.ts +297 -0
- package/src/service.ts +222 -32
- package/src/tray/windows-tray.ps1 +9 -0
- package/src/tray/windows.ts +15 -7
- package/src/types.ts +194 -14
- package/src/update/index.ts +13 -13
- package/src/update/job.ts +24 -21
- package/src/update/notify.ts +7 -3
- package/src/usage/cost.ts +0 -0
- package/src/usage/expected-prices.ts +129 -10
- package/src/usage/log.ts +50 -15
- package/src/usage/summary.ts +4 -4
- package/src/vision/index.ts +6 -1
- package/src/web-search/loop.ts +161 -34
- package/gui/dist/assets/index-OY43ubAq.css +0 -1
- package/gui/dist/assets/index-YwNnKZcL.js +0 -67
- package/gui/dist/provider-icons/antigravity.svg +0 -1
- package/gui/dist/provider-icons/claude.svg +0 -1
- package/gui/dist/provider-icons/copilot.svg +0 -1
- package/gui/dist/provider-icons/cursor.svg +0 -2
- package/gui/dist/provider-icons/gemini.svg +0 -1
- package/gui/dist/provider-icons/grok-color.svg +0 -1
- package/gui/dist/provider-icons/kiro.svg +0 -14
package/src/lib/bun-runtime.ts
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
* back to `process.execPath` (which is itself Bun when run via `bun src/cli/index.ts`).
|
|
12
12
|
*/
|
|
13
13
|
import { createRequire } from "node:module";
|
|
14
|
+
import { realpathSync } from "node:fs";
|
|
14
15
|
import { dirname, join, resolve } from "node:path";
|
|
15
16
|
import { isRealBunBinary } from "./bun-binary-validator.mjs";
|
|
16
17
|
|
|
@@ -20,12 +21,120 @@ const require = createRequire(import.meta.url);
|
|
|
20
21
|
|
|
21
22
|
const BUN_OVERRIDE_ENV = "OPENCODEX_BUN_PATH";
|
|
22
23
|
|
|
24
|
+
/**
|
|
25
|
+
* Env marker stamped by whichever launcher selected the Bun binary, then read back
|
|
26
|
+
* inside the launched process.
|
|
27
|
+
*
|
|
28
|
+
* Provenance has to travel with the launch because it cannot be recovered afterwards:
|
|
29
|
+
* resolving it at report time answers "what would this shell pick now", not "what was
|
|
30
|
+
* this service started with", and those differ exactly when the answer matters.
|
|
31
|
+
*/
|
|
32
|
+
export const BUN_RUNTIME_SOURCE_ENV = "OCX_BUN_RUNTIME_SOURCE";
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The binary the marker was minted for. Stamped beside the source so a reader can tell
|
|
36
|
+
* whether a marker still describes the process holding it, without re-deriving the
|
|
37
|
+
* selection from an environment that may no longer contain it.
|
|
38
|
+
*/
|
|
39
|
+
export const BUN_RUNTIME_PATH_ENV = "OCX_BUN_RUNTIME_PATH";
|
|
40
|
+
|
|
41
|
+
export type BunRuntimeSource = "override" | "bundled" | "process";
|
|
42
|
+
|
|
43
|
+
/** The only provenance values any surface may accept off the wire or out of the env. */
|
|
44
|
+
export const BUN_RUNTIME_SOURCES: readonly BunRuntimeSource[] = ["override", "bundled", "process"];
|
|
45
|
+
|
|
23
46
|
export type DurableBunRuntime = {
|
|
24
47
|
path: string;
|
|
25
|
-
source:
|
|
48
|
+
source: BunRuntimeSource;
|
|
26
49
|
overrideEnv: typeof BUN_OVERRIDE_ENV;
|
|
27
50
|
};
|
|
28
51
|
|
|
52
|
+
/**
|
|
53
|
+
* The provenance this process was launched with, or `undefined` when nothing
|
|
54
|
+
* trustworthy is recorded.
|
|
55
|
+
*
|
|
56
|
+
* Deliberately never falls back to `durableBunRuntime()`. A service installed before
|
|
57
|
+
* the marker existed has no provenance, and guessing one from the current environment
|
|
58
|
+
* would report a confident wrong answer — "unknown" is the honest result and callers
|
|
59
|
+
* are expected to say so. Values outside the allowlist are treated as absent rather
|
|
60
|
+
* than passed through.
|
|
61
|
+
*/
|
|
62
|
+
export function reportedBunRuntimeSource(
|
|
63
|
+
env: NodeJS.ProcessEnv = process.env,
|
|
64
|
+
): BunRuntimeSource | undefined {
|
|
65
|
+
const raw = env[BUN_RUNTIME_SOURCE_ENV]?.trim();
|
|
66
|
+
const source = BUN_RUNTIME_SOURCES.find(candidate => candidate === raw);
|
|
67
|
+
if (!source) return undefined;
|
|
68
|
+
// Source and binary are a PAIR: without a recorded path that names THIS
|
|
69
|
+
// executable, the marker describes some other launch and must not be
|
|
70
|
+
// reported (a bare OCX_BUN_RUNTIME_SOURCE inherited from an unrelated
|
|
71
|
+
// parent would otherwise claim a confident wrong origin).
|
|
72
|
+
const recordedPath = env[BUN_RUNTIME_PATH_ENV]?.trim();
|
|
73
|
+
if (!recordedPath || !samePath(recordedPath, process.execPath)) return undefined;
|
|
74
|
+
return source;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Env pair a launcher stamps for the binary it just selected. */
|
|
78
|
+
export function bunRuntimeProvenanceEnv(runtime: DurableBunRuntime): Record<string, string> {
|
|
79
|
+
return { [BUN_RUNTIME_SOURCE_ENV]: runtime.source, [BUN_RUNTIME_PATH_ENV]: runtime.path };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Child environment for a proxy started with `process.execPath` — the runtime this
|
|
84
|
+
* process is already using.
|
|
85
|
+
*
|
|
86
|
+
* These launchers re-exec the current runtime rather than resolving a binary, so the
|
|
87
|
+
* provenance to report is whatever launched THIS process. Without this the marker would
|
|
88
|
+
* be silently dropped on `ocx ensure`, GUI start, restart, and update-relaunch, and the
|
|
89
|
+
* service would report an unknown origin it actually knows.
|
|
90
|
+
*
|
|
91
|
+
* An inherited marker is carried forward only when the binary it was minted for is the
|
|
92
|
+
* one about to be re-executed. Inheritance travels down a process tree, so a marker can
|
|
93
|
+
* outlive its binary — something started under a marked process but running a different
|
|
94
|
+
* Bun would otherwise relaunch the daemon with a provenance contradicting the binary
|
|
95
|
+
* actually serving it. The check compares the recorded path rather than re-deriving the
|
|
96
|
+
* selection, because a service installed with a shell-local override keeps neither that
|
|
97
|
+
* shell nor its `OPENCODEX_BUN_PATH`, and re-deriving would demote a correct `override`
|
|
98
|
+
* to `process` on its first relaunch.
|
|
99
|
+
*/
|
|
100
|
+
export function withProcessRuntimeProvenance(
|
|
101
|
+
env: NodeJS.ProcessEnv,
|
|
102
|
+
): NodeJS.ProcessEnv {
|
|
103
|
+
return { ...env, ...bunRuntimeProvenanceEnv(currentRuntimeProvenance(env)) };
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Provenance for `process.execPath`: the inherited claim when it was minted for this
|
|
108
|
+
* exact executable, otherwise what this executable actually is.
|
|
109
|
+
*/
|
|
110
|
+
function currentRuntimeProvenance(env: NodeJS.ProcessEnv): DurableBunRuntime {
|
|
111
|
+
const claimed = reportedBunRuntimeSource(env);
|
|
112
|
+
const claimedPath = env[BUN_RUNTIME_PATH_ENV]?.trim();
|
|
113
|
+
if (claimed && claimedPath && samePath(claimedPath, process.execPath)) {
|
|
114
|
+
return { path: process.execPath, source: claimed, overrideEnv: BUN_OVERRIDE_ENV };
|
|
115
|
+
}
|
|
116
|
+
// No marker that describes this binary: report what is running. One resolution
|
|
117
|
+
// supplies both halves so the pair can never disagree.
|
|
118
|
+
const runtime = durableBunRuntime();
|
|
119
|
+
return samePath(runtime.path, process.execPath)
|
|
120
|
+
? runtime
|
|
121
|
+
: { path: process.execPath, source: "process", overrideEnv: BUN_OVERRIDE_ENV };
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Same file, allowing for the aliases a path can pick up between launch and relaunch:
|
|
126
|
+
* symlinks/junctions, mapped drives, and Windows case differences. Falls back to a
|
|
127
|
+
* lexical comparison when a path cannot be resolved (it may be gone).
|
|
128
|
+
*/
|
|
129
|
+
function samePath(left: string, right: string): boolean {
|
|
130
|
+
const canonical = (value: string): string => {
|
|
131
|
+
let resolved = value;
|
|
132
|
+
try { resolved = realpathSync(value); } catch { /* keep the literal path */ }
|
|
133
|
+
return process.platform === "win32" ? resolved.toLowerCase() : resolved;
|
|
134
|
+
};
|
|
135
|
+
return canonical(left) === canonical(right);
|
|
136
|
+
}
|
|
137
|
+
|
|
29
138
|
/**
|
|
30
139
|
* Absolute path to the bundled Bun binary, or null if the `bun` dependency is
|
|
31
140
|
* not installed/resolvable (or only the un-downloaded placeholder is present).
|
|
@@ -6,7 +6,8 @@
|
|
|
6
6
|
* PR #32120, merged 2026-06-21). No RELEASED Bun version is proven to carry
|
|
7
7
|
* that fix yet, so `MIN_FIXED_BUN_VERSION` is null: every runtime is
|
|
8
8
|
* "known-bad" until a bundle-bump commit sets it. Windows no-rewrite traffic
|
|
9
|
-
* follows this runtime/config decision
|
|
9
|
+
* follows this runtime/config decision, preserving the explicit legacy-tee
|
|
10
|
+
* safety pin. Darwin no-rewrite traffic stays on tee
|
|
10
11
|
* for `auto` regardless of runtime capability and reaches eager relay only via
|
|
11
12
|
* explicit `streamMode: "eager-relay"` opt-in (see
|
|
12
13
|
* devlog/_plan/260731_macos_rss_retention/100_darwin_eager_optin.md).
|
package/src/lib/redact.ts
CHANGED
|
@@ -2,8 +2,413 @@ export const REDACTED_SECRET = "[REDACTED]";
|
|
|
2
2
|
|
|
3
3
|
const SENSITIVE_KEY_PATTERN = /^(?:authorization|proxy-authorization|cookie|set-cookie|set-cookie2|api[-_]?key|x-api-key|x-goog-api-key|x-amz-security-token|access[-_]?token|refresh[-_]?token|id[-_]?token|token|secret|client[-_]?secret|password|profile[-_]?arn)$/i;
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Colon-labelled credential headers echoed back inside an error body
|
|
7
|
+
* (`x-api-key: <value>`), which the `key=value` rules never match.
|
|
8
|
+
*
|
|
9
|
+
* This is one pass with an explicit decision rather than a stack of regexes
|
|
10
|
+
* that have to reason about each other's output. Three earlier attempts failed
|
|
11
|
+
* exactly there: exempting `Bearer` let anything the Bearer rule could not
|
|
12
|
+
* parse escape both rules; trusting the public `[REDACTED]` marker let a
|
|
13
|
+
* suffix ride along behind it; and splitting into two ordered patterns had the
|
|
14
|
+
* second eat the first one's result.
|
|
15
|
+
*
|
|
16
|
+
* The rule: the value after the label is a credential and gets masked to
|
|
17
|
+
* end-of-line. There is no "keep the readable part" exception, because every
|
|
18
|
+
* round of review found another way to hide a credential inside whatever the
|
|
19
|
+
* previous round chose to preserve — a second label, a repeated `Bearer`
|
|
20
|
+
* scheme, a third token two levels deep. Preserving attacker-controlled text
|
|
21
|
+
* next to a credential is the bug; the scheme word is not worth it.
|
|
22
|
+
*
|
|
23
|
+
* `Bearer` survives only as a fixed prefix on `authorization` /
|
|
24
|
+
* `proxy-authorization`, where it says which scheme failed and carries nothing
|
|
25
|
+
* from the input. `[REDACTED]` is a PUBLIC string an upstream can emit too, so
|
|
26
|
+
* its presence never grants trust.
|
|
27
|
+
*
|
|
28
|
+
* The label boundary is matched over a NORMALIZED VIEW: colon confusables and
|
|
29
|
+
* invisible format characters are folded for matching only, with offsets mapped
|
|
30
|
+
* back so unrelated text keeps its original bytes. Folding the string itself
|
|
31
|
+
* rewrote innocent diagnostics (`ratio∶1` became `ratio:1`).
|
|
32
|
+
*/
|
|
33
|
+
// Every letter position also accepts \u0001, the placeholder the fold emits for
|
|
34
|
+
// an unresolved HTML named reference: `authorⅈzation` is the label with one
|
|
35
|
+
// character we cannot name, and that is still the label.
|
|
36
|
+
const CREDENTIAL_HEADER_LABEL_RAW = "x-api-key|x-goog-api-key|x-amz-security-token|api[_-]?key|apiKey|access[_-]?token|accessToken|refresh[_-]?token|refreshToken|id[_-]?token|client[_-]?secret|clientSecret|authorization|proxy-authorization|cookie|set-cookie|password|secret|token";
|
|
37
|
+
|
|
38
|
+
const CREDENTIAL_HEADER_LABEL = CREDENTIAL_HEADER_LABEL_RAW
|
|
39
|
+
.replace(/(?<![\[\\])([A-Za-z])(?![\]\-])/g, "[$1\u0001]");
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Characters that render as a colon separator. Folded to `:` in the matching
|
|
43
|
+
* view so a look-alike cannot hide a header from the label pattern.
|
|
44
|
+
*/
|
|
45
|
+
const COLON_CONFUSABLES = new Set([
|
|
46
|
+
"\uFF1A", "\uFE55", "\uFE13", "\uA789", "\u02D0", "\u2236",
|
|
47
|
+
"\u205A", "\u0589", "\u1361", "\u16EC", "\u1803", "\u2982", "\u2AF6", "\uFE30",
|
|
48
|
+
]);
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Characters dropped from the matching view: anything with no visible width
|
|
52
|
+
* that could split a label into pieces the pattern no longer recognizes.
|
|
53
|
+
* `\p{Default_Ignorable_Code_Point}` is the systematic answer — it covers the
|
|
54
|
+
* zero-width set, the bidi isolates and marks, the Mongolian vowel separator,
|
|
55
|
+
* and the variation selectors in one property instead of a list that review
|
|
56
|
+
* keeps finding another member of. `\p{Cf}` and combining marks are folded too.
|
|
57
|
+
*/
|
|
58
|
+
const INVISIBLE_FORMAT = /[\p{Default_Ignorable_Code_Point}\p{Cf}\p{Mn}\p{Me}]/u;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* HTML named character references.
|
|
62
|
+
*
|
|
63
|
+
* A hand-picked list is a coverage promise nobody can keep — review found
|
|
64
|
+
* `ⅈ`, `ⅇ`, and `ⅆ` decoding to compatibility letters that
|
|
65
|
+
* NFKD already maps onto `i`, `e`, and `d`, and the WHATWG table holds roughly
|
|
66
|
+
* 2200 entries. Neither Bun nor Node exposes that table, and pulling in a
|
|
67
|
+
* dependency to spell a header name is the wrong trade for this path.
|
|
68
|
+
*
|
|
69
|
+
* So names are not resolved at all. A named reference sitting inside a
|
|
70
|
+
* credential label is folded to a single placeholder character of unknown
|
|
71
|
+
* identity, and the label alternation accepts that placeholder wherever a
|
|
72
|
+
* letter may appear. Every named entity is covered, present and future,
|
|
73
|
+
* without claiming to know what any of them mean.
|
|
74
|
+
*/
|
|
75
|
+
const NAMED_ENTITY_PLACEHOLDER = "\u0001";
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* The handful of named references that spell a SEPARATOR rather than a letter.
|
|
79
|
+
* These have to resolve exactly, because the placeholder stands in for a letter
|
|
80
|
+
* position and a separator is structure, not a character of the name.
|
|
81
|
+
*/
|
|
82
|
+
const SEPARATOR_ENTITIES = new Map<string, string>([
|
|
83
|
+
["colon", ":"], ["semi", ";"], ["equals", "="], ["quot", '"'], ["apos", "'"],
|
|
84
|
+
["lt", "<"], ["gt", ">"], ["amp", "&"], ["sol", "/"], ["lowbar", "_"],
|
|
85
|
+
["hyphen", "-"], ["dash", "-"], ["ndash", "-"], ["mdash", "-"], ["minus", "-"],
|
|
86
|
+
["period", "."], ["comma", ","], ["num", "#"], ["nbsp", " "],
|
|
87
|
+
]);
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Latin look-alikes for the ASCII letters that appear in credential labels.
|
|
91
|
+
* Cyrillic `а`/`е`, Greek `ο`, fullwidth forms and the mathematical alphabets
|
|
92
|
+
* all render as the label to a human, so the matching view folds them back.
|
|
93
|
+
* NFKD handles the width/font variants; this table covers the cross-script
|
|
94
|
+
* homoglyphs NFKD deliberately leaves alone.
|
|
95
|
+
*/
|
|
96
|
+
const LETTER_CONFUSABLES = new Map<string, string>([
|
|
97
|
+
// Cyrillic
|
|
98
|
+
["\u0430", "a"], ["\u0435", "e"], ["\u043E", "o"], ["\u0440", "p"], ["\u0441", "c"],
|
|
99
|
+
["\u0445", "x"], ["\u0443", "y"], ["\u04BB", "h"], ["\u0455", "s"], ["\u0456", "i"],
|
|
100
|
+
["\u0458", "j"], ["\u043A", "k"], ["\u0442", "t"], ["\u0432", "b"], ["\u043C", "m"],
|
|
101
|
+
["\u043D", "h"], ["\u0501", "d"], ["\u0503", "g"], ["\u051B", "q"], ["\u051D", "w"],
|
|
102
|
+
["\u04CF", "l"], ["\u0261", "g"], ["\u04AB", "c"], ["\u04BD", "e"], ["\u0459", "k"],
|
|
103
|
+
// Greek
|
|
104
|
+
["\u03B1", "a"], ["\u03BF", "o"], ["\u03C1", "p"], ["\u03BD", "v"], ["\u03BA", "k"],
|
|
105
|
+
["\u03B5", "e"], ["\u03C4", "t"], ["\u03B9", "i"], ["\u03C5", "u"], ["\u03C7", "x"],
|
|
106
|
+
["\u03B7", "n"], ["\u03BC", "u"], ["\u03C3", "o"], ["\u03B2", "b"], ["\u03B3", "y"],
|
|
107
|
+
// Latin extended / other
|
|
108
|
+
["\u0131", "i"], ["\u0269", "i"], ["\u1D0F", "o"], ["\u0280", "r"], ["\u01BF", "p"],
|
|
109
|
+
["\u0578", "n"], ["\u057D", "u"], ["\u0585", "o"], ["\u0581", "g"], ["\u2044", "/"],
|
|
110
|
+
]);
|
|
111
|
+
|
|
112
|
+
// `\b` is the wrong left boundary for a header name: it matches after a `-` or
|
|
113
|
+
// `_`, so `not-authorization:` and `internal_token:` were treated as the
|
|
114
|
+
// credential labels they merely end with. Requiring a non-identifier character
|
|
115
|
+
// (or start of input) keeps the match to whole field names.
|
|
116
|
+
//
|
|
117
|
+
// The optional quotes around the label matter: a serialized headers object
|
|
118
|
+
// (`{"x-api-key":"<secret>"}`) puts a closing quote between the name and the
|
|
119
|
+
// colon, so a bare `label:` pattern never saw it. The pre-existing JSON rules
|
|
120
|
+
// below only listed a few field names and did not share this label grammar,
|
|
121
|
+
// which is how ordinary JSON serialization — no homoglyphs, no attacker
|
|
122
|
+
// alphabet — walked a credential straight through.
|
|
123
|
+
const COLON_LABELLED_CREDENTIAL = new RegExp(
|
|
124
|
+
`(?<![A-Za-z0-9_-])["']?(?:${CREDENTIAL_HEADER_LABEL})["']?[^\\S\\r\\n]*:`,
|
|
125
|
+
"gi",
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Framings other than `label: value` that carry the same credential names.
|
|
130
|
+
*
|
|
131
|
+
* An upstream error body is not always a header dump. It can echo the request
|
|
132
|
+
* as a form-encoded string, an XML element, or a multipart part header, and a
|
|
133
|
+
* colon-only matcher sees none of those. Each entry masks the value with the
|
|
134
|
+
* terminator its own grammar defines, so the surrounding structure survives.
|
|
135
|
+
*/
|
|
136
|
+
const OTHER_FRAMED_CREDENTIALS: Array<[RegExp, string]> = [
|
|
137
|
+
// URL query / form-encoded: `authorization=<value>` up to `&` or `;`.
|
|
138
|
+
// Unconditionally to the separator — a quoted value is NOT allowed to end it
|
|
139
|
+
// early, or `authorization="decoy"<secret>&model=…` leaks the suffix.
|
|
140
|
+
[
|
|
141
|
+
new RegExp(`(?<![A-Za-z0-9_-])(?:${CREDENTIAL_HEADER_LABEL})=[^&;\\r\\n]*`, "gi"),
|
|
142
|
+
"=",
|
|
143
|
+
],
|
|
144
|
+
// XML/HTML. A tag qualifies when its NAME is a credential (optionally
|
|
145
|
+
// namespace-qualified), or when a whole `name`/`key`/`id` attribute names one
|
|
146
|
+
// — `data-name` does not count, or `<field data-name="authorization">` loses
|
|
147
|
+
// harmless status text.
|
|
148
|
+
//
|
|
149
|
+
// Once a tag qualifies, the mask keeps only the tag name and runs to END OF
|
|
150
|
+
// INPUT. Two stopping points were tried and both leaked: the CLOSING TAG
|
|
151
|
+
// (same-name nesting ended the mask at the inner `</authorization>`, and a
|
|
152
|
+
// self-closing tag had none), then END OF LINE (an opening tag may legally
|
|
153
|
+
// span lines, so `<authorization\n value="…">` left the credential on the
|
|
154
|
+
// next line). XML has no line discipline to borrow, so there is no boundary
|
|
155
|
+
// left worth trusting.
|
|
156
|
+
//
|
|
157
|
+
// Whitespace is allowed around an attribute `=`, which XML permits and an
|
|
158
|
+
// echo may well reproduce.
|
|
159
|
+
[
|
|
160
|
+
new RegExp(
|
|
161
|
+
`(<[^\\S\\r\\n]*(?:[A-Za-z_][\\w.-]*:)?(?:${CREDENTIAL_HEADER_LABEL})(?=[\\s/>]))[\\s\\S]*`,
|
|
162
|
+
"gi",
|
|
163
|
+
),
|
|
164
|
+
"element",
|
|
165
|
+
],
|
|
166
|
+
[
|
|
167
|
+
new RegExp(
|
|
168
|
+
`(<[^\\S\\r\\n]*(?:[A-Za-z_][\\w.-]*:)?[A-Za-z_][\\w:.-]*)(?=[^>]*?(?<![\\w:.-])(?:name|key|id)[^\\S]*=[^\\S]*["']?(?:${CREDENTIAL_HEADER_LABEL})["']?(?=[\\s/>]))[\\s\\S]*`,
|
|
169
|
+
"gi",
|
|
170
|
+
),
|
|
171
|
+
"element",
|
|
172
|
+
],
|
|
173
|
+
// Multipart part: everything from a credential-named part header to the end
|
|
174
|
+
// of the input. Part-based, not line-based — a body can span lines and the
|
|
175
|
+
// blank line is often missing in a malformed echo.
|
|
176
|
+
//
|
|
177
|
+
// It deliberately does NOT stop at the first `--`: the boundary token is
|
|
178
|
+
// attacker-controlled text, so a body line starting `--not-the-boundary`
|
|
179
|
+
// ended the mask and exposed everything after it. Consuming the remainder
|
|
180
|
+
// costs trailing context in one framing and closes the bypass.
|
|
181
|
+
[
|
|
182
|
+
new RegExp(
|
|
183
|
+
`(name=["']?(?:${CREDENTIAL_HEADER_LABEL})["']?[^\\r\\n]*\\r?\\n(?:\\r?\\n)?)([\\s\\S]+)`,
|
|
184
|
+
"gi",
|
|
185
|
+
),
|
|
186
|
+
"multipart",
|
|
187
|
+
],
|
|
188
|
+
];
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* These run over the FOLDED view too, then map back to the original string.
|
|
192
|
+
*
|
|
193
|
+
* Matching the raw text meant a percent-encoded form key (`author%69zation=`)
|
|
194
|
+
* and an XML character reference (`name="authorization"`) were invisible,
|
|
195
|
+
* even though both spell the credential name to anything that parses the body.
|
|
196
|
+
* The fold already decodes those, so the grammars are applied there and the
|
|
197
|
+
* mask is written back at the corresponding original offsets.
|
|
198
|
+
*/
|
|
199
|
+
function maskOtherFramings(value: string): string {
|
|
200
|
+
// Same union rule as the header pass: decoding may add coverage, never
|
|
201
|
+
// remove it.
|
|
202
|
+
return maskOtherFramingsOnce(maskOtherFramingsOnce(value, true), false);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function maskOtherFramingsOnce(value: string, decodeEscapes: boolean): string {
|
|
206
|
+
let current = value;
|
|
207
|
+
for (const [pattern, kind] of OTHER_FRAMED_CREDENTIALS) {
|
|
208
|
+
const { folded, map } = foldForMatching(current, decodeEscapes);
|
|
209
|
+
pattern.lastIndex = 0;
|
|
210
|
+
let out = "";
|
|
211
|
+
let cursor = 0;
|
|
212
|
+
let match: RegExpExecArray | null;
|
|
213
|
+
while ((match = pattern.exec(folded)) !== null) {
|
|
214
|
+
const start = map[match.index] ?? current.length;
|
|
215
|
+
const end = map[match.index + match[0].length] ?? current.length;
|
|
216
|
+
if (start < cursor) continue;
|
|
217
|
+
const head = (() => {
|
|
218
|
+
if (kind === "=") {
|
|
219
|
+
const eq = match[0].indexOf("=");
|
|
220
|
+
const headEnd = map[match.index + eq + 1] ?? end;
|
|
221
|
+
return current.slice(start, headEnd);
|
|
222
|
+
}
|
|
223
|
+
const captured = match[1] ?? "";
|
|
224
|
+
const headEnd = map[match.index + captured.length] ?? end;
|
|
225
|
+
return current.slice(start, headEnd);
|
|
226
|
+
})();
|
|
227
|
+
const body = current.slice(start + head.length, end);
|
|
228
|
+
if (kind === "multipart" && !body.trim()) continue;
|
|
229
|
+
out += current.slice(cursor, start) + head + REDACTED_SECRET;
|
|
230
|
+
cursor = end;
|
|
231
|
+
if (pattern.lastIndex === match.index) pattern.lastIndex += 1;
|
|
232
|
+
}
|
|
233
|
+
current = out + current.slice(cursor);
|
|
234
|
+
}
|
|
235
|
+
return current;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Build a folded copy plus an index map back to the original string, so the
|
|
240
|
+
* match runs on normalized text while the output keeps every byte the match did
|
|
241
|
+
* not cover.
|
|
242
|
+
*/
|
|
243
|
+
function foldForMatching(value: string, decodeEscapes = true): { folded: string; map: number[] } {
|
|
244
|
+
let folded = "";
|
|
245
|
+
const map: number[] = [];
|
|
246
|
+
// Serialization escapes are ALIASES for the label, not decoration: a JSON
|
|
247
|
+
// `\u0069`, a percent-encoded `%69`, and an XML `i` all spell the same
|
|
248
|
+
// field name to whatever parses the body, while spelling something else to a
|
|
249
|
+
// literal matcher. Decode them into the matching view (one folded character
|
|
250
|
+
// per escape, with the whole escape mapped back to its start) so
|
|
251
|
+
// `author\u0069zation`, `author%69zation`, and `authorization` are the
|
|
252
|
+
// label they claim to be.
|
|
253
|
+
const decodeEscape = (at: number): { ch: string; width: number } | null => {
|
|
254
|
+
// JSON `\uXXXX`, INCLUDING a surrogate pair. Decoding the halves
|
|
255
|
+
// independently left `\uD835\uDD69` as two lone surrogates, so the
|
|
256
|
+
// mathematical letter they spell was never normalized as one code point.
|
|
257
|
+
const json = /^\\u([0-9a-fA-F]{4})/.exec(value.slice(at, at + 6));
|
|
258
|
+
if (json) {
|
|
259
|
+
const high = parseInt(json[1]!, 16);
|
|
260
|
+
if (high >= 0xd800 && high <= 0xdbff) {
|
|
261
|
+
const low = /^\\u([0-9a-fA-F]{4})/.exec(value.slice(at + 6, at + 12));
|
|
262
|
+
const lowCode = low ? parseInt(low[1]!, 16) : NaN;
|
|
263
|
+
if (lowCode >= 0xdc00 && lowCode <= 0xdfff) {
|
|
264
|
+
return { ch: String.fromCharCode(high, lowCode), width: 12 };
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
return { ch: String.fromCharCode(high), width: 6 };
|
|
268
|
+
}
|
|
269
|
+
// Percent encoding is UTF-8: consecutive `%XX` bytes form ONE character.
|
|
270
|
+
// Decoding each byte on its own turned `%D0%B5` into two unrelated
|
|
271
|
+
// Latin-1 characters instead of the Cyrillic `е` the fold would have
|
|
272
|
+
// recognized.
|
|
273
|
+
const pct = /^(?:%[0-9a-fA-F]{2})+/.exec(value.slice(at, at + 24));
|
|
274
|
+
if (pct) {
|
|
275
|
+
try {
|
|
276
|
+
const decoded = decodeURIComponent(pct[0]);
|
|
277
|
+
if (decoded.length >= 1) {
|
|
278
|
+
// Consume only the bytes that produced the FIRST character, so the
|
|
279
|
+
// rest of the sequence is decoded on the next iteration.
|
|
280
|
+
const first = String.fromCodePoint(decoded.codePointAt(0)!);
|
|
281
|
+
const bytes = new TextEncoder().encode(first).length;
|
|
282
|
+
return { ch: first, width: bytes * 3 };
|
|
283
|
+
}
|
|
284
|
+
} catch {
|
|
285
|
+
const single = parseInt(pct[0].slice(1, 3), 16);
|
|
286
|
+
return { ch: String.fromCharCode(single), width: 3 };
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
const xml = /^&#(x[0-9a-fA-F]{1,6}|[0-9]{1,7});/.exec(value.slice(at, at + 11));
|
|
290
|
+
if (xml) {
|
|
291
|
+
const raw = xml[1]!;
|
|
292
|
+
const code = raw[0] === "x" || raw[0] === "X"
|
|
293
|
+
? parseInt(raw.slice(1), 16)
|
|
294
|
+
: parseInt(raw, 10);
|
|
295
|
+
if (Number.isFinite(code) && code > 0 && code <= 0x10ffff) {
|
|
296
|
+
return { ch: String.fromCodePoint(code), width: xml[0].length };
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
// HTML named references. `:` and the other separator names are
|
|
300
|
+
// resolved exactly; anything else folds to the opaque placeholder so the
|
|
301
|
+
// label still matches without pretending to know the character.
|
|
302
|
+
const named = /^&([A-Za-z][A-Za-z0-9]{1,31});/.exec(value.slice(at, at + 34));
|
|
303
|
+
if (named) {
|
|
304
|
+
const separator = SEPARATOR_ENTITIES.get(named[1]!.toLowerCase());
|
|
305
|
+
return { ch: separator ?? NAMED_ENTITY_PLACEHOLDER, width: named[0].length };
|
|
306
|
+
}
|
|
307
|
+
return null;
|
|
308
|
+
};
|
|
309
|
+
// Iterate by CODE POINT, not UTF-16 code unit: a supplementary character
|
|
310
|
+
// (mathematical letters, variation selectors above the BMP) is two units, so
|
|
311
|
+
// a per-unit loop hands each half to the property tests separately and
|
|
312
|
+
// neither half matches anything. `𝕩-api-key` and a U+E0100 inside a label
|
|
313
|
+
// both walked straight past the fold that way.
|
|
314
|
+
let i = 0;
|
|
315
|
+
while (i < value.length) {
|
|
316
|
+
const escaped = decodeEscapes ? decodeEscape(i) : null;
|
|
317
|
+
const ch = escaped ? escaped.ch : String.fromCodePoint(value.codePointAt(i)!);
|
|
318
|
+
const width = escaped ? escaped.width : ch.length;
|
|
319
|
+
if (INVISIBLE_FORMAT.test(ch)) {
|
|
320
|
+
i += width;
|
|
321
|
+
continue;
|
|
322
|
+
}
|
|
323
|
+
const mapped = COLON_CONFUSABLES.has(ch)
|
|
324
|
+
? ":"
|
|
325
|
+
: LETTER_CONFUSABLES.get(ch.toLowerCase())
|
|
326
|
+
// NFKD collapses fullwidth, circled, and mathematical letter variants
|
|
327
|
+
// onto their ASCII base.
|
|
328
|
+
?? (ch.normalize("NFKD").length === 1 ? ch.normalize("NFKD") : ch);
|
|
329
|
+
// One folded unit per source code point keeps the offset map aligned; a
|
|
330
|
+
// multi-unit fold would desynchronize it, so those keep the original.
|
|
331
|
+
folded += mapped.length === 1 ? mapped : ch;
|
|
332
|
+
// One map entry per EMITTED UTF-16 unit. An escaped supplementary
|
|
333
|
+
// character emits two units, and giving it one entry desynchronized every
|
|
334
|
+
// later offset — the mask then landed mid-token and left part of the
|
|
335
|
+
// credential behind.
|
|
336
|
+
const emittedText = mapped.length === 1 ? mapped : ch;
|
|
337
|
+
for (let k = 0; k < emittedText.length; k += 1) map.push(i);
|
|
338
|
+
i += width;
|
|
339
|
+
}
|
|
340
|
+
map.push(value.length);
|
|
341
|
+
return { folded, map };
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Run the header rule over BOTH matching views and take the union.
|
|
346
|
+
*
|
|
347
|
+
* Decoding may only ADD coverage. Applying it unconditionally removed some:
|
|
348
|
+
* `𝕩x-api-key: <secret>` decoded to `𝕩x-api-key:`, which folds to
|
|
349
|
+
* `xx-api-key:` and no longer matches the label boundary — so a decode-only
|
|
350
|
+
* view masked LESS than the plain view did. Running both and masking whatever
|
|
351
|
+
* either one finds makes the direction of the change one-way.
|
|
352
|
+
*/
|
|
353
|
+
function maskCredentialHeaders(value: string): string {
|
|
354
|
+
const decoded = maskCredentialHeadersOnce(value, true);
|
|
355
|
+
return maskCredentialHeadersOnce(decoded, false);
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
function maskCredentialHeadersOnce(value: string, decodeEscapes: boolean): string {
|
|
359
|
+
const { folded, map } = foldForMatching(value, decodeEscapes);
|
|
360
|
+
COLON_LABELLED_CREDENTIAL.lastIndex = 0;
|
|
361
|
+
let out = "";
|
|
362
|
+
let cursor = 0;
|
|
363
|
+
let match: RegExpExecArray | null;
|
|
364
|
+
while ((match = COLON_LABELLED_CREDENTIAL.exec(folded)) !== null) {
|
|
365
|
+
const start = map[match.index] ?? value.length;
|
|
366
|
+
const afterLabel = map[match.index + match[0].length] ?? value.length;
|
|
367
|
+
if (start < cursor) continue;
|
|
368
|
+
const lineEnd = (() => {
|
|
369
|
+
const nl = value.slice(afterLabel).search(/[\r\n]/);
|
|
370
|
+
return nl === -1 ? value.length : afterLabel + nl;
|
|
371
|
+
})();
|
|
372
|
+
// THE VALUE ALWAYS RUNS TO END-OF-LINE. There is no early termination and
|
|
373
|
+
// no attempt to preserve sibling fields.
|
|
374
|
+
//
|
|
375
|
+
// Three attempts tried to be smarter, and each one leaked: stop at the
|
|
376
|
+
// first closing quote; stop at a closing quote followed by punctuation;
|
|
377
|
+
// stop only when the LABEL was quoted. The third still leaked on an
|
|
378
|
+
// unmatched opening quote (`"x-api-key: "decoy",<secret>`) and on a
|
|
379
|
+
// correctly quoted key whose value quote was a decoy
|
|
380
|
+
// (`{"x-api-key":"decoy"<secret>}`).
|
|
381
|
+
//
|
|
382
|
+
// The pattern is the lesson: any rule that stops early is reading
|
|
383
|
+
// attacker-controlled text to decide where a secret ends, and the attacker
|
|
384
|
+
// gets to write that text. Losing the siblings in a serialized object
|
|
385
|
+
// makes a diagnostic less pretty; stopping early makes it leak. Monotonic
|
|
386
|
+
// and blunt wins.
|
|
387
|
+
const valueEnd = lineEnd;
|
|
388
|
+
const rawValue = value.slice(afterLabel, valueEnd);
|
|
389
|
+
if (!rawValue.trim()) continue;
|
|
390
|
+
// Keep the original separator spacing so a diagnostic still reads as
|
|
391
|
+
// `header: [REDACTED]` rather than `header:[REDACTED]`.
|
|
392
|
+
const gap = /^[^\S\r\n]*/.exec(rawValue)?.[0] ?? "";
|
|
393
|
+
const quote = "";
|
|
394
|
+
// `Bearer` is a fixed prefix, reproduced from a literal — never copied from
|
|
395
|
+
// the input — and only where an auth scheme is meaningful.
|
|
396
|
+
const label = match[0].replace(/[^\S\r\n]*:$/, "").trim();
|
|
397
|
+
const isAuthHeader = /^(?:proxy-)?authorization$/i.test(label);
|
|
398
|
+
const prefix = isAuthHeader && new RegExp(`^[^\\S\\r\\n]*${quote}?Bearer[^\\S\\r\\n]`, "i").test(rawValue)
|
|
399
|
+
? "Bearer "
|
|
400
|
+
: "";
|
|
401
|
+
out += value.slice(cursor, afterLabel) + gap + quote + prefix + REDACTED_SECRET + quote;
|
|
402
|
+
cursor = valueEnd;
|
|
403
|
+
}
|
|
404
|
+
return out + value.slice(cursor);
|
|
405
|
+
}
|
|
406
|
+
|
|
5
407
|
const SECRET_VALUE_PATTERNS: Array<[RegExp, string]> = [
|
|
6
|
-
|
|
408
|
+
// A Bearer token outside a labelled header (prose, JSON fragments, logs).
|
|
409
|
+
// Horizontal whitespace only: `\s+` crossed line boundaries and masked the
|
|
410
|
+
// first word of the NEXT line when a header was quoted with a trailing break.
|
|
411
|
+
[/\b(Bearer)([^\S\r\n]+)[A-Za-z0-9._~+/=-]{8,}\b/gi, `$1$2${REDACTED_SECRET}`],
|
|
7
412
|
[/\b(sk-[A-Za-z0-9][A-Za-z0-9._-]{6,})\b/g, REDACTED_SECRET],
|
|
8
413
|
// GitHub tokens (classic + fine-grained + OAuth/refresh): ghp_/gho_/ghu_/ghs_/ghr_/github_pat_.
|
|
9
414
|
[/\b(gh[pousr]_[A-Za-z0-9_]{8,}|github_pat_[A-Za-z0-9_]{20,})\b/g, REDACTED_SECRET],
|
|
@@ -31,7 +436,7 @@ function isSensitiveKey(key: string): boolean {
|
|
|
31
436
|
}
|
|
32
437
|
|
|
33
438
|
export function redactSecretString(value: string): string {
|
|
34
|
-
let redacted = value;
|
|
439
|
+
let redacted = maskOtherFramings(maskCredentialHeaders(value));
|
|
35
440
|
for (const [pattern, replacement] of SECRET_VALUE_PATTERNS) {
|
|
36
441
|
redacted = redacted.replace(pattern, replacement);
|
|
37
442
|
}
|
package/src/lib/shadow-call.ts
CHANGED
|
@@ -28,3 +28,27 @@ export function isShadowSourceModel(modelId: string, configured?: unknown): bool
|
|
|
28
28
|
if (modelId.includes("/")) return false;
|
|
29
29
|
return shadowSourceModels(configured).some(prefix => modelId.startsWith(prefix));
|
|
30
30
|
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Decide whether a matching source model should use the opt-in intercept.
|
|
34
|
+
*
|
|
35
|
+
* Codex 0.145.0+ identifies normal user turns and maintenance requests in
|
|
36
|
+
* x-codex-turn-metadata. Only an explicit normal turn bypasses interception;
|
|
37
|
+
* missing or unrecognized metadata retains the legacy opt-in prefix behavior.
|
|
38
|
+
*/
|
|
39
|
+
export function shouldInterceptShadowCall(
|
|
40
|
+
modelId: string,
|
|
41
|
+
configured: unknown,
|
|
42
|
+
headers: Headers,
|
|
43
|
+
): boolean {
|
|
44
|
+
if (!isShadowSourceModel(modelId, configured)) return false;
|
|
45
|
+
const rawMetadata = headers.get("x-codex-turn-metadata");
|
|
46
|
+
if (rawMetadata === null) return true;
|
|
47
|
+
|
|
48
|
+
try {
|
|
49
|
+
const parsed = JSON.parse(rawMetadata) as { request_kind?: unknown };
|
|
50
|
+
return parsed?.request_kind !== "turn";
|
|
51
|
+
} catch {
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -315,6 +315,16 @@ export function translatorObservedOverflowCount(): number {
|
|
|
315
315
|
return aggregateOverflows;
|
|
316
316
|
}
|
|
317
317
|
|
|
318
|
+
/** Test-only: proves owned default budgets are disposed on every stream-death path. */
|
|
319
|
+
export function translatorLiveBudgetCountForTests(): number {
|
|
320
|
+
return liveBudgets.size;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/** Test-only: proves no charge survives against a disposed budget (cancel race). */
|
|
324
|
+
export function translatorAggregateCurrentBytesForTests(): number {
|
|
325
|
+
return aggregateCurrentBytes;
|
|
326
|
+
}
|
|
327
|
+
|
|
318
328
|
/** Clears process-wide translator diagnostics and disposes leaked test budgets. */
|
|
319
329
|
export function resetTranslatorAggregateForTests(): void {
|
|
320
330
|
for (const budget of liveBudgets) budget.dispose();
|