From 870a188254cffe3c863b0a243971bd75523ed4ed Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sun, 22 Nov 2026 00:02:28 -0800 Subject: [PATCH 1/9] fix: redact known credential values from tool output --- .../native/connections/credential-store.ts | 34 +++- .../altimate/native/connections/registry.ts | 8 + .../src/altimate/tool-output-credentials.ts | 151 ++++++++++++++++++ .../opencode/src/altimate/tool-zod-compat.ts | 10 +- packages/opencode/src/session/index.ts | 8 + packages/opencode/src/session/message-v2.ts | 19 ++- packages/opencode/src/session/prompt.ts | 44 +++-- packages/opencode/src/session/tools.ts | 41 +++-- packages/opencode/src/tool/bash.ts | 8 +- packages/opencode/src/tool/read.ts | 7 + packages/opencode/src/tool/tool.ts | 13 +- .../session/tool-credential-output.test.ts | 120 ++++++++++++++ .../session/tool-output-credentials.test.ts | 71 ++++++++ packages/opencode/test/tool/read.test.ts | 18 +++ 14 files changed, 518 insertions(+), 34 deletions(-) create mode 100644 packages/opencode/src/altimate/tool-output-credentials.ts create mode 100644 packages/opencode/test/session/tool-credential-output.test.ts create mode 100644 packages/opencode/test/session/tool-output-credentials.test.ts diff --git a/packages/opencode/src/altimate/native/connections/credential-store.ts b/packages/opencode/src/altimate/native/connections/credential-store.ts index 5c07df0ffb..9ca2e40216 100644 --- a/packages/opencode/src/altimate/native/connections/credential-store.ts +++ b/packages/opencode/src/altimate/native/connections/credential-store.ts @@ -18,6 +18,14 @@ import { Log } from "@/altimate/util/log" import type { ConnectionConfig } from "@altimateai/drivers" +// altimate_change start — credentials this process has already read or written +const knownCredentials = new Map() + +export function knownStoredCredentials(): Iterable { + return knownCredentials.values() +} +// altimate_change end + const SERVICE_NAME = "altimate-code" const SENSITIVE_FIELDS = new Set([ @@ -104,6 +112,7 @@ async function getBackend(): Promise { /** Swap the credential backend in tests. `undefined` re-probes on next use. */ export function setSecretBackendForTests(next: SecretBackend | null | undefined): void { backend = next + knownCredentials.clear() // altimate_change — reset observations when replacing the backend } /** Store a single credential in the OS credential store (or return false if unavailable). */ @@ -116,6 +125,8 @@ export async function storeCredential( if (!store) return false try { await store.set(`${connectionName}/${field}`, value) + // altimate_change — retain the current credential for exact tool-result redaction + knownCredentials.set(`${connectionName}/${field}`, value) return true } catch (e) { // e.g. no libsecret on Linux, or a locked keychain: report "not stored" so @@ -135,7 +146,13 @@ export async function getCredential( const store = await getBackend() if (!store) return null try { - return await store.get(`${connectionName}/${field}`) + // altimate_change start — remember only the latest value for this account + const account = `${connectionName}/${field}` + const value = await store.get(account) + if (value === null) knownCredentials.delete(account) + else knownCredentials.set(account, value) + return value + // altimate_change end } catch (e) { // Treated as absent so a locked store cannot stop every connect, but logged, so a connection that then fails // for want of its password shows why. @@ -152,7 +169,12 @@ export async function deleteCredential( const store = await getBackend() if (!store) return false try { - return await store.delete(`${connectionName}/${field}`) + // altimate_change start — forget removed credentials + const account = `${connectionName}/${field}` + const deleted = await store.delete(account) + if (deleted) knownCredentials.delete(account) + return deleted + // altimate_change end } catch { return false } @@ -187,7 +209,12 @@ export async function forgetCredentials(name: string, keep?: ConnectionConfig): } catch { present = "unknown" } - if (present === false) continue + // altimate_change start — discard stale observed values + if (present === false) { + knownCredentials.delete(account) + continue + } + // altimate_change end let deleted: boolean try { deleted = await store.delete(account) @@ -196,6 +223,7 @@ export async function forgetCredentials(name: string, keep?: ConnectionConfig): } // A delete that reports "nothing to delete" after an unreadable entry cannot be told from a refusal. if (!deleted) notRemoved.push(field) + else knownCredentials.delete(account) // altimate_change — remove the observation too } return notRemoved } diff --git a/packages/opencode/src/altimate/native/connections/registry.ts b/packages/opencode/src/altimate/native/connections/registry.ts index 98d455d648..96d0fbc9bc 100644 --- a/packages/opencode/src/altimate/native/connections/registry.ts +++ b/packages/opencode/src/altimate/native/connections/registry.ts @@ -1068,6 +1068,14 @@ export async function reload(): Promise { // altimate_change end } +// altimate_change start — use the active registry and its credential classifier for redaction +export function knownCredentialConfigs(): Iterable { + const registry = state() + if (!registry.loaded) loadInto(registry) + return registry.configs.values() +} +// altimate_change end + /** Get the raw config for a connection (for testing). */ export function getConfig(name: string): ConnectionConfig | undefined { // altimate_change start — the current project's registry diff --git a/packages/opencode/src/altimate/tool-output-credentials.ts b/packages/opencode/src/altimate/tool-output-credentials.ts new file mode 100644 index 0000000000..5797d58d1f --- /dev/null +++ b/packages/opencode/src/altimate/tool-output-credentials.ts @@ -0,0 +1,151 @@ +// altimate_change — redact only exact known credential values in tool results. +import { Effect } from "effect" +import { InstanceRef } from "@/effect/instance-ref" +import { attach } from "@/effect/run-service" +import { withAlsInstanceRef } from "@/altimate/tool-zod-compat" +import { isSensitiveField, knownStoredCredentials } from "@/altimate/native/connections/credential-store" + +const replacement = "[REDACTED]" +const minLength = 8 + +export function createCredentialRedactor(values: Iterable = []) { + const literals = new Set() + for (const value of values) { + if (!value || value.length < minLength) continue + literals.add(value) + literals.add(JSON.stringify(value).slice(1, -1)) + } + const ordered = [...literals].sort((a, b) => b.length - a.length) + const pattern = ordered.length ? new RegExp(ordered.map((value) => RegExp.escape(value)).join("|"), "g") : undefined + const maxLiteralLength = ordered[0]?.length ?? 0 + const text = (value: string) => (pattern ? value.replace(pattern, replacement) : value) + const lines = (value: string) => + pattern ? value.replace(pattern, (match) => replacement + "\n".repeat(match.split("\n").length - 1)) : value + + function visit(value: unknown, seen: WeakMap): unknown { + if (typeof value === "string") return text(value) + if (!value || typeof value !== "object") return value + if (seen.has(value)) return seen.get(value) + if (Array.isArray(value)) { + const next: unknown[] = [] + seen.set(value, next) + for (const item of value) next.push(visit(item, seen)) + return next + } + const prototype = Object.getPrototypeOf(value) + if (prototype !== Object.prototype && prototype !== null) return value + const next = Object.create(prototype) + seen.set(value, next) + for (const [key, item] of Object.entries(value)) { + Object.defineProperty(next, text(key), { + value: visit(item, seen), + enumerable: true, + writable: true, + configurable: true, + }) + } + return next + } + + function failure(error: unknown): unknown { + if (!(error instanceof Error)) return pattern ? visit(error, new WeakMap()) : error + const message = text(error.message) + const stack = error.stack === undefined ? undefined : text(error.stack) + if (message === error.message && stack === error.stack) return error + const descriptors = Object.getOwnPropertyDescriptors(error) + return Object.create(Object.getPrototypeOf(error), { + ...descriptors, + message: { value: message, writable: true, configurable: true }, + stack: { value: stack, writable: true, configurable: true }, + }) + } + + return { + failure, + text, + lines, + preview(value: string, limit: number) { + // Check the lookahead so a literal crossing the preview limit is still removed. + const prefix = text(value.slice(0, limit + maxLiteralLength)) + return value.length > limit + maxLiteralLength || prefix.length > limit + ? prefix.slice(0, limit) + "\n\n..." + : prefix + }, + output(value: T): T { + return pattern ? (visit(value, new WeakMap()) as T) : value + }, + } +} + +export type CredentialRedactor = ReturnType + +function addConnectionFields(value: unknown, add: (value: unknown) => void) { + if (!value || typeof value !== "object") return + for (const [key, field] of Object.entries(value)) { + if (isSensitiveField(key)) add(field) + if (field && typeof field === "object") addConnectionFields(field, add) + } +} + +export const loadToolCredentialRedactor = withAlsInstanceRef( + Effect.gen(function* () { + // Message projection is below configuration and the connection registry in the import graph. + const [{ Auth }, { Config }, { Env }, Connections, { Instance }] = yield* Effect.promise(() => + Promise.all([ + import("@/auth"), + import("@/config/config"), + import("@/env"), + import("@/altimate/native/connections/registry"), + import("@/project/instance"), + ]), + ) + const instance = yield* InstanceRef + const values = new Set() + const add = (value: unknown) => { + if (typeof value === "string") values.add(value) + } + const [config, auth] = yield* Effect.promise(() => + Promise.all([instance ? Config.get().catch(() => undefined) : undefined, Auth.all().catch(() => undefined)]), + ) + const environment = [...Object.entries(process.env), ...Object.entries(instance ? Env.all() : {})] + const configuredVariables = new Set() + for (const provider of Object.values(config?.provider ?? {})) { + for (const name of provider.env ?? []) configuredVariables.add(name) + add(provider.options?.apiKey) + for (const model of Object.values(provider.models ?? {})) add(model.options?.apiKey) + } + for (const [name, value] of environment) { + if (name.startsWith(Connections.CONN_ENV_PREFIX)) { + add(value) + if (value) { + try { + addConnectionFields(JSON.parse(value), add) + } catch { + // The complete environment value is still a known literal. + } + } + } else if (/(?:^|_)API_KEY$/i.test(name) || configuredVariables.has(name)) { + add(value) + } + } + for (const entry of Object.values(auth ?? {})) { + if (entry.type === "oauth") { + add(entry.access) + add(entry.refresh) + } else { + add(entry.key) + if (entry.type === "wellknown") add(entry.token) + } + } + if (instance) { + const connections = Instance.restore(instance, () => Connections.knownCredentialConfigs()) + for (const connection of connections) addConnectionFields(connection, add) + } + for (const credential of knownStoredCredentials()) add(credential) + return createCredentialRedactor(values) + }), +) + +export function toolCredentialRedactor() { + return Effect.runPromise(attach(loadToolCredentialRedactor)) +} diff --git a/packages/opencode/src/altimate/tool-zod-compat.ts b/packages/opencode/src/altimate/tool-zod-compat.ts index 15855e8380..0a8c84e3a5 100644 --- a/packages/opencode/src/altimate/tool-zod-compat.ts +++ b/packages/opencode/src/altimate/tool-zod-compat.ts @@ -38,6 +38,8 @@ export interface LegacyContext { abort: AbortSignal callID?: string extra?: { [key: string]: any } + // altimate_change — keep the same credential filter through legacy tool dispatch + redactCredentials?: NewContext["redactCredentials"] messages: SessionV1.WithParts[] permissions?: PermissionV1.Ruleset metadata(input: { title?: string; metadata?: M }): void | Promise @@ -269,6 +271,7 @@ function legacyDefToDef(legacy: LegacyToolDef): DefWithoutID { abort: ctx.abort, callID: ctx.callID, extra: ctx.extra as { [key: string]: any } | undefined, + redactCredentials: ctx.redactCredentials, // altimate_change — shared tool-output filter messages: ctx.messages, permissions: ctx.permissions, metadata: (input) => bridge.promise(ctx.metadata(input)), @@ -300,15 +303,16 @@ export function legacyToInit(legacy: LegacyToolDef): Effect.Effect // CURRENT fiber's InstanceRef and only falls back to the ALS when no fiber is active. Because the // factory runs inside this init Effect's fiber, attach would read an unset InstanceRef and fail // ("InstanceRef not provided"). Provide the ALS instance as InstanceRef so attach finds it. -function withAlsInstanceRef(effect: Effect.Effect): Effect.Effect { - return Effect.suspend(() => { +export function withAlsInstanceRef(effect: Effect.Effect): Effect.Effect { + return Effect.gen(function* () { + if (yield* InstanceRef) return yield* effect let instance: InstanceContext | undefined try { instance = Instance.current } catch { instance = undefined } - return instance ? effect.pipe(Effect.provideService(InstanceRef, instance)) : effect + return yield* (instance ? effect.pipe(Effect.provideService(InstanceRef, instance)) : effect) }) } // altimate_change end diff --git a/packages/opencode/src/session/index.ts b/packages/opencode/src/session/index.ts index aee9ca8898..9031a56d40 100644 --- a/packages/opencode/src/session/index.ts +++ b/packages/opencode/src/session/index.ts @@ -45,8 +45,16 @@ import { AppRuntime } from "@/effect/app-runtime" import { ProjectV2 } from "@opencode-ai/core/project" // altimate_change end +// altimate_change — sanitize result fields before SQLite and subscribers see them +import { toolCredentialRedactor } from "@/altimate/tool-output-credentials" + // altimate_change start — upstream_fix: keep stored, published, and returned parts JSON-safe async function persistPart(part: MessageV2.Part) { + if (part.type === "tool" && (part.state.status === "completed" || part.state.status === "error")) { + const { input, time, status, ...result } = part.state + const redact = await toolCredentialRedactor() + part = { ...part, state: { ...part.state, ...redact.output(result), input, time, status } } as MessageV2.ToolPart + } part = normalizeBigInts(part, "safe-number") const { id, messageID, sessionID, ...data } = part const time = Date.now() diff --git a/packages/opencode/src/session/message-v2.ts b/packages/opencode/src/session/message-v2.ts index e1428f8d99..057df83b59 100644 --- a/packages/opencode/src/session/message-v2.ts +++ b/packages/opencode/src/session/message-v2.ts @@ -18,6 +18,8 @@ import type { SystemError } from "bun" import type { Provider } from "@/provider/provider" import { ModelID, ProviderID } from "@/provider/schema" import { Effect } from "effect" +// altimate_change — redact known credentials in results loaded from older sessions +import { loadToolCredentialRedactor } from "@/altimate/tool-output-credentials" import { createHash } from "node:crypto" // altimate_change start — Claude Agent: stopped-tool flag for its waiting turn import { ToolAborted } from "@/altimate/provider/claude-agent/tools/aborted" @@ -730,6 +732,11 @@ export namespace MessageV2 { options?: { stripMedia?: boolean; toolOutputMaxChars?: number }, // altimate_change end ) { + // altimate_change start — apply at projection before clipping historical tool output + const redact = input.some((message) => message.parts.some((part) => part.type === "tool")) + ? yield* loadToolCredentialRedactor + : undefined + // altimate_change end const result: UIMessage[] = [] const toolNames = new Set() // Track media from tool results that need to be injected as user messages @@ -897,11 +904,12 @@ export namespace MessageV2 { if (part.state.status === "completed") { // altimate_change start — toolOutputMaxChars truncates long tool output for compaction const storedMask = part.state.metadata?.observation_mask - const rawOutputText = part.state.time.compacted + const storedOutputText = part.state.time.compacted ? typeof storedMask === "string" && storedMask.length > 0 ? storedMask : "[Old tool result content cleared]" : part.state.output + const rawOutputText = redact?.text(storedOutputText) ?? storedOutputText const maxChars = options?.toolOutputMaxChars const outputText = !part.state.time.compacted && maxChars !== undefined && rawOutputText.length > maxChars @@ -910,7 +918,10 @@ export namespace MessageV2 { } chars]` : rawOutputText // altimate_change end - const attachments = part.state.time.compacted || options?.stripMedia ? [] : (part.state.attachments ?? []) + // altimate_change start — stored attachment references may also contain credentials + const storedAttachments = part.state.time.compacted || options?.stripMedia ? [] : (part.state.attachments ?? []) + const attachments = redact?.output(storedAttachments) ?? storedAttachments + // altimate_change end // For providers that don't support media in tool results, extract media files // (images, PDFs) to be sent as a separate user message @@ -953,7 +964,7 @@ export namespace MessageV2 { state: "output-available", toolCallId: replayCallID, input: part.state.input, - output, + output: redact?.text(output) ?? output, ...(keepAborted ? { callProviderMetadata: callMetadata } : {}), }) } else { @@ -962,7 +973,7 @@ export namespace MessageV2 { state: "output-error", toolCallId: replayCallID, input: part.state.input, - errorText: part.state.error, + errorText: redact?.text(part.state.error) ?? part.state.error, ...(keepAborted ? { callProviderMetadata: callMetadata } : {}), }) } diff --git a/packages/opencode/src/session/prompt.ts b/packages/opencode/src/session/prompt.ts index 93187d0201..59c15e226d 100644 --- a/packages/opencode/src/session/prompt.ts +++ b/packages/opencode/src/session/prompt.ts @@ -18,6 +18,8 @@ import { stateFileOf } from "../altimate/rules/state-path" import { Log } from "../util/log" import { SessionRevert } from "./revert" import { Session } from "." +// altimate_change — known credential values in session tool results +import { toolCredentialRedactor } from "@/altimate/tool-output-credentials" import { Agent } from "../agent/agent" import { Provider } from "../provider/provider" import { ModelID, ProviderID } from "../provider/schema" @@ -2939,8 +2941,8 @@ export namespace SessionPrompt { await Session.updatePart({ ...match, state: { - title: val.title, - metadata: val.metadata, + title: ctx.redactCredentials?.output(val.title) ?? val.title, // altimate_change — plugin progress + metadata: ctx.redactCredentials?.output(val.metadata) ?? val.metadata, // altimate_change — plugin progress status: "running", input: args, time: { @@ -3012,7 +3014,11 @@ export namespace SessionPrompt { }, ) // altimate_change start — v1.17.9: Tool.Def.execute returns an Effect - const result = await AppRuntime.runPromise(item.execute(args, ctx)) + // altimate_change start — cover custom tools as well as the shared built-in wrapper + const redact = await toolCredentialRedactor() + ctx.redactCredentials = redact + const result = redact.output(await AppRuntime.runPromise(item.execute(args, ctx))) + // altimate_change end // altimate_change end const output = { ...result, @@ -3046,8 +3052,12 @@ export namespace SessionPrompt { await input.lessons?.appendFileLessons(ctx.sessionID, item.id, args, stamped) // altimate_change end // altimate_change start — return the source-stamped output - Tool.assertSuccess(stamped) - return stamped + const safeResult = redact.output(stamped) + Tool.assertSuccess(safeResult) + return safeResult + } catch (error) { + const redact = ctx.redactCredentials ?? (await toolCredentialRedactor()) + throw redact.failure(error) // altimate_change end } finally { input.processor.finishToolExecution(execution) @@ -3106,9 +3116,16 @@ export namespace SessionPrompt { // altimate_change start — a workspace integration call is tracked here, where MCP tools run const startedAt = Date.now() let result + let redact: Awaited> // altimate_change — reuse the MCP call's filter try { - result = await execute(args, opts) + // altimate_change start — sanitize MCP output before hooks, rendering and truncation + const raw = await execute(args, opts) + redact = await toolCredentialRedactor() + result = redact.output(raw) + // altimate_change end } catch (err) { + // altimate_change — protect errors before tracking them or returning them to the SDK + const error = (await toolCredentialRedactor()).failure(err) trackWorkspaceToolCall({ key, clientName, @@ -3118,9 +3135,9 @@ export namespace SessionPrompt { turn: input.turn, startedAt, outcome: "error", - error: err, + error, }) - throw err + throw error } trackWorkspaceToolCall({ key, @@ -3146,6 +3163,8 @@ export namespace SessionPrompt { result, ) + // altimate_change — redact changes from the mutable tool-result hook as well + result = redact.output(result) const textParts: string[] = [] const attachments: Omit[] = [] @@ -3174,7 +3193,7 @@ export namespace SessionPrompt { } } - const truncated = await Truncate.output(textParts.join("\n\n"), {}, input.agent) + const truncated = await Truncate.output(redact.text(textParts.join("\n\n")), {}, input.agent) // altimate_change — match across joined content // altimate_change start — authoritative source + readable title from the original client // name, shared with SessionTools.resolve (session/tools.ts) so the resolvers can't drift. const described = describeMcpTool(key, clientName) @@ -4485,6 +4504,8 @@ NOTE: At any point in time through this workflow you should feel free to ask the { cwd, sessionID: input.sessionID, callID: part.callID }, { env: {} }, ) + // altimate_change — collect credentials before starting the manual shell + const redact = await toolCredentialRedactor() const proc = spawn(shell, args, { cwd, detached: process.platform !== "win32", @@ -4503,7 +4524,7 @@ NOTE: At any point in time through this workflow you should feel free to ask the output += chunk.toString() if (part.state.status === "running") { part.state.metadata = { - output: output, + output: redact.text(output), // altimate_change — persisted shell progress description: "", } Session.updatePart(part) @@ -4514,7 +4535,7 @@ NOTE: At any point in time through this workflow you should feel free to ask the output += chunk.toString() if (part.state.status === "running") { part.state.metadata = { - output: output, + output: redact.text(output), // altimate_change — persisted shell progress description: "", } Session.updatePart(part) @@ -4549,6 +4570,7 @@ NOTE: At any point in time through this workflow you should feel free to ask the if (aborted) { output += "\n\n" + ["", "User aborted the command", ""].join("\n") } + output = redact.text(output) // altimate_change — final manual shell result msg.time.completed = Date.now() await Session.updateMessage(msg) if (part.state.status === "running") { diff --git a/packages/opencode/src/session/tools.ts b/packages/opencode/src/session/tools.ts index aa1ce7fb57..448349e56c 100644 --- a/packages/opencode/src/session/tools.ts +++ b/packages/opencode/src/session/tools.ts @@ -5,6 +5,8 @@ import { ProviderTransform } from "@/provider/transform" import { MCP } from "@/mcp" import { Permission } from "@/permission" import { Tool } from "@/tool/tool" +// altimate_change — exact credential values at the alternate tool resolver +import { loadToolCredentialRedactor, type CredentialRedactor } from "@/altimate/tool-output-credentials" import { ToolJsonSchema } from "@/tool/json-schema" import { ToolRegistry } from "@/tool/registry" import { Truncate } from "@/tool/truncate" @@ -47,7 +49,14 @@ export const resolve = Effect.fn("SessionTools.resolve")(function* (input: { const mcp = yield* MCP.Service const truncate = yield* Truncate.Service - const context = (args: Record, options: ToolExecutionOptions): Tool.Context => ({ + // altimate_change start — use the same exact-value filter for custom tool results and progress + const context = ( + args: Record, + options: ToolExecutionOptions, + redact?: CredentialRedactor, + ): Tool.Context => ({ + redactCredentials: redact, + // altimate_change end sessionID: input.session.id, abort: options.abortSignal!, messageID: input.processor.message.id, @@ -64,8 +73,10 @@ export const resolve = Effect.fn("SessionTools.resolve")(function* (input: { return { ...match, state: { - title: val.title, - metadata: val.metadata, + // altimate_change start — sanitize custom tool progress before persistence + title: redact?.output(val.title) ?? val.title, + metadata: redact?.output(val.metadata) ?? val.metadata, + // altimate_change end status: "running", input: args, time: { start: Date.now() }, @@ -112,13 +123,16 @@ export const resolve = Effect.fn("SessionTools.resolve")(function* (input: { execute(args, options) { return run.promise( Effect.gen(function* () { - const ctx = context(args, options) + const redact = yield* loadToolCredentialRedactor // altimate_change — collect before tool execution + const ctx = context(args, options, redact) // altimate_change — protect progress too yield* plugin.trigger( "tool.execute.before", { tool: item.id, sessionID: ctx.sessionID, callID: ctx.callID }, { args }, ) - const result = yield* item.execute(args, ctx) + // altimate_change start — custom and built-in results use the same filter + const result = redact.output(yield* item.execute(args, ctx)) + // altimate_change end const output = { ...result, attachments: result.attachments?.map((attachment) => ({ @@ -139,11 +153,12 @@ export const resolve = Effect.fn("SessionTools.resolve")(function* (input: { // altimate_change end ) // altimate_change start — propagate the source-stamped output + const safeResult = redact.output(stamped) if (options.abortSignal?.aborted) { - yield* input.processor.completeToolCall(options.toolCallId, stamped) + yield* input.processor.completeToolCall(options.toolCallId, safeResult) } - Tool.assertSuccess(stamped) - return stamped + Tool.assertSuccess(safeResult) + return safeResult // altimate_change end }), ) @@ -174,7 +189,8 @@ export const resolve = Effect.fn("SessionTools.resolve")(function* (input: { { tool: key, sessionID: ctx.sessionID, callID: opts.toolCallId }, { args }, ) - const result: Awaited>> = yield* Effect.gen(function* () { + // altimate_change — sanitize before and after the mutable result hook + let result: Awaited>> = yield* Effect.gen(function* () { yield* ctx.ask({ permission: key, metadata: {}, patterns: ["*"], always: ["*"] }) return yield* Effect.promise(() => execute(args, opts)) }).pipe( @@ -187,12 +203,17 @@ export const resolve = Effect.fn("SessionTools.resolve")(function* (input: { }, }), ) + // altimate_change start — before the hook and before text/attachment conversion + const redact = yield* loadToolCredentialRedactor + result = redact.output(result) + // altimate_change end yield* plugin.trigger( "tool.execute.after", { tool: key, sessionID: ctx.sessionID, callID: opts.toolCallId, args }, result, ) + result = redact.output(result) // altimate_change — cover mutable hook changes const textParts: string[] = [] const attachments: Omit[] = [] for (const contentItem of result.content) { @@ -217,7 +238,7 @@ export const resolve = Effect.fn("SessionTools.resolve")(function* (input: { } } - const truncated = yield* truncate.output(textParts.join("\n\n"), {}, input.agent) + const truncated = yield* truncate.output(redact.text(textParts.join("\n\n")), {}, input.agent) // altimate_change — match across joined content // altimate_change start — authoritative source + readable title from the original client // name, shared with prompt.ts resolveTools so the two resolvers can't drift. const described = describeMcpTool(key, clientName) diff --git a/packages/opencode/src/tool/bash.ts b/packages/opencode/src/tool/bash.ts index 0ef418bdae..d5b49ca839 100644 --- a/packages/opencode/src/tool/bash.ts +++ b/packages/opencode/src/tool/bash.ts @@ -533,7 +533,11 @@ export const BashTool = Tool.define("bash", async () => { ctx.metadata({ metadata: { // truncate the metadata to avoid GIANT blobs of data (has nothing to do w/ what agent can access) - output: output.length > MAX_METADATA_LENGTH ? output.slice(0, MAX_METADATA_LENGTH) + "\n\n..." : output, + // altimate_change start — bound the exact-match scan to the preview and its lookahead + output: + ctx.redactCredentials?.preview(output, MAX_METADATA_LENGTH) ?? + (output.length > MAX_METADATA_LENGTH ? output.slice(0, MAX_METADATA_LENGTH) + "\n\n..." : output), + // altimate_change end description: params.description, }, }) @@ -650,6 +654,8 @@ export const BashTool = Tool.define("bash", async () => { output += "\n\n\n" + resultMetadata.join("\n") + "\n" } + // altimate_change — redact the aggregate stdout/stderr before deriving final metadata + output = ctx.redactCredentials?.text(output) ?? output return { title: params.description, metadata: { diff --git a/packages/opencode/src/tool/read.ts b/packages/opencode/src/tool/read.ts index 54430c8cc0..b0ff427836 100644 --- a/packages/opencode/src/tool/read.ts +++ b/packages/opencode/src/tool/read.ts @@ -400,6 +400,13 @@ export const ReadTool = Tool.define< ) } + // altimate_change start — check the raw window before adding line-number prefixes + if (ctx.redactCredentials) { + const raw = file.raw.join("\n") + const redacted = ctx.redactCredentials.lines(raw) + if (redacted !== raw) file.raw.splice(0, file.raw.length, ...redacted.split("\n")) + } + // altimate_change end let output = [`${filepath}`, `file`, "\n"].join("\n") output += file.raw.map((line, i) => `${i + file.offset}: ${line}`).join("\n") diff --git a/packages/opencode/src/tool/tool.ts b/packages/opencode/src/tool/tool.ts index 4558050b1d..5fd609bf74 100644 --- a/packages/opencode/src/tool/tool.ts +++ b/packages/opencode/src/tool/tool.ts @@ -4,6 +4,8 @@ import { SessionV1 } from "@opencode-ai/core/v1/session" import type { JSONSchema7 } from "@ai-sdk/provider" import type { SessionID, MessageID } from "../session/schema" import * as Truncate from "./truncate" +// altimate_change — exact known credentials at the tool execution boundary +import { loadToolCredentialRedactor, type CredentialRedactor } from "@/altimate/tool-output-credentials" import { Agent } from "@/agent/agent" // altimate_change start — telemetry instrumentation + legacy zod-tool adapter import z from "zod" @@ -67,6 +69,8 @@ export type Context = { abort: AbortSignal callID?: string extra?: { [key: string]: unknown } + // altimate_change — shared with producers that format or clip output + redactCredentials?: CredentialRedactor messages: SessionV1.WithParts[] // altimate_change start — pass effective permission rules to search result filtering permissions?: PermissionV1.Ruleset @@ -348,15 +352,20 @@ function wrap, Result extends Metadat } // Debug mode: trace each call as it starts and ends (no-op unless ALTIMATE_DEBUG). const endTrace = traceToolCall(id, ctx.callID) - const rawResult = yield* execute(decoded as Schema.Schema.Type, ctx).pipe( + const redact = ctx.redactCredentials ?? (yield* loadToolCredentialRedactor) + const metadata = ctx.metadata + ctx = { ...ctx, redactCredentials: redact, metadata: (value) => metadata(redact.output(value)) } + const executed = yield* execute(decoded as Schema.Schema.Type, ctx).pipe( Effect.onError((cause) => Effect.sync(() => { const error = Cause.squash(cause) - endTrace("error", error instanceof Error ? error.message : String(error)) + endTrace("error", redact.text(error instanceof Error ? error.message : String(error))) altimateTrackError(id, decoded, ctx, startTime, error) }), ), ) + // Refresh for tools that read or install a credential during execution. + const rawResult = redact.output((yield* loadToolCredentialRedactor).output(executed)) const failed = isErrorResult(rawResult) const unsuccessful = hasFailureSignals(id, rawResult) // Ended before anything else can throw, so a call that finished is never left "running". diff --git a/packages/opencode/test/session/tool-credential-output.test.ts b/packages/opencode/test/session/tool-credential-output.test.ts new file mode 100644 index 0000000000..bff946aa85 --- /dev/null +++ b/packages/opencode/test/session/tool-credential-output.test.ts @@ -0,0 +1,120 @@ +// altimate_change — model and persistence coverage with synthetic credentials +import { expect, test } from "bun:test" +import path from "node:path" +import { Config } from "@/config/config" +import { BashTool } from "@/tool/bash" +import { Session } from "@/session" +import { MessageV2 } from "@/session/message-v2" +import { ModelID, ProviderID } from "@/provider/schema" +import { SessionID, MessageID, PartID } from "@/session/schema" +import { Instance } from "@/project/instance" +import { Provider } from "@/provider/provider" +import { initTool } from "../altimate/tool-fixture" +import { provideTestInstance, tmpdir } from "../fixture/fixture" + +const configuredKey = "fixture-provider-value" + +test("known credential values are redacted in bash results", async () => { + const marker = "prefix-[REDACTED]-suffix" + await using tmp = await tmpdir({ + git: true, + config: { + provider: { + test: { + name: "Test", + npm: "@ai-sdk/openai-compatible", + options: { apiKey: configuredKey }, + models: { fixture: { name: "Fixture" } }, + }, + }, + }, + init: (dir) => Bun.write(path.join(dir, "fixture.txt"), "prefix-" + configuredKey + "-suffix"), + }) + await provideTestInstance({ + directory: tmp.path, + fn: (instance) => + Instance.restore(instance, async () => { + expect((await Config.get()).provider?.test?.options?.apiKey === configuredKey).toBe(true) + const bash = await initTool(BashTool) + const ctx = { + sessionID: SessionID.make("ses_test"), + messageID: MessageID.make("msg_test"), + agent: "build", + abort: new AbortController().signal, + messages: [], + metadata: () => {}, + ask: async () => {}, + } + const bashResult = await bash.execute({ command: "cat fixture.txt", description: "Print fixture" }, ctx) + const session = await Session.create({}) + const parentID = MessageID.ascending() + const messageID = MessageID.ascending() + const providerID = ProviderID.make("test") + const modelID = ModelID.make("fixture") + await Session.updateMessage({ + id: parentID, + sessionID: session.id, + role: "user", + agent: "build", + model: { providerID, modelID }, + time: { created: 0 }, + }) + await Session.updatePart({ + id: PartID.ascending(), + sessionID: session.id, + messageID: parentID, + type: "text", + text: "Print the fixture", + }) + await Session.updateMessage({ + id: messageID, + sessionID: session.id, + role: "assistant", + agent: "build", + parentID, + providerID, + modelID, + mode: "build", + path: { cwd: tmp.path, root: tmp.path }, + cost: 0, + tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }, + time: { created: 1, completed: 2 }, + }) + await Session.updatePart({ + id: PartID.ascending(), + sessionID: session.id, + messageID, + type: "tool", + tool: "bash", + callID: "call_fixture", + state: { + status: "completed", + input: { command: "cat fixture.txt" }, + title: bashResult.title, + output: bashResult.output, + metadata: bashResult.metadata, + time: { start: 1, end: 2 }, + }, + }) + const persisted = await Session.messages({ sessionID: session.id }) + const stored = JSON.stringify(persisted) + const model = await Provider.getModel(providerID, modelID) + const request = JSON.stringify(await MessageV2.toModelMessages(persisted, model)) + expect({ + bashRaw: bashResult.output.includes(configuredKey), + storedRaw: stored.includes(configuredKey), + requestRaw: request.includes(configuredKey), + bashRedacted: bashResult.output.includes(marker), + storedRedacted: stored.includes(marker), + requestRedacted: request.includes(marker), + }).toEqual({ + bashRaw: false, + storedRaw: false, + requestRaw: false, + bashRedacted: true, + storedRedacted: true, + requestRedacted: true, + }) + }), + }) +}) diff --git a/packages/opencode/test/session/tool-output-credentials.test.ts b/packages/opencode/test/session/tool-output-credentials.test.ts new file mode 100644 index 0000000000..aa99a22e63 --- /dev/null +++ b/packages/opencode/test/session/tool-output-credentials.test.ts @@ -0,0 +1,71 @@ +// altimate_change — exact literal redaction preserves unrelated tool bytes. +import { expect, test } from "bun:test" +import { Auth } from "@/auth" +import * as Store from "@/altimate/native/connections/credential-store" +import { createCredentialRedactor, toolCredentialRedactor } from "@/altimate/tool-output-credentials" + +test("matches literal and JSON-escaped forms inside longer tokens", () => { + const value = 'fixture-["one"]\\two\nthree' + const redact = createCredentialRedactor([value]) + expect(redact.text("prefix-" + value + "-suffix")).toBe("prefix-[REDACTED]-suffix") + expect(redact.text(JSON.stringify({ value }))).toBe('{"value":"[REDACTED]"}') + expect(redact.lines("before\n" + value + "\nafter")).toBe("before\n[REDACTED]\n\nafter") +}) + +test("keeps unrelated output byte-identical with known, empty or missing values", () => { + const original = "SELECT \"fixture\", 'abcd1234', 'AA+/zXY='\r\nshort!\n" + for (const values of [undefined, [], ["", undefined], ["short!", "unrelated-credential"]]) { + expect(createCredentialRedactor(values).text(original)).toBe(original) + } +}) + +test("sanitizes strings throughout a structured result", () => { + const value = "warehouse-fixture-value" + const raw = { + output: "no result", + content: [{ type: "resource", resource: { uri: "local://resource", text: value } }], + metadata: { nested: [{ detail: "before " + value }] }, + } + expect(createCredentialRedactor([value]).output(raw)).toEqual({ + output: "no result", + content: [{ type: "resource", resource: { uri: "local://resource", text: "[REDACTED]" } }], + metadata: { nested: [{ detail: "before [REDACTED]" }] }, + }) +}) + +test("collects environment, provider auth and observed warehouse credentials", async () => { + const provider = "tool-output-test-provider" + const envName = "TOOL_OUTPUT_TEST_API_KEY" + const connectionName = "ALTIMATE_CODE_CONN_TOOL_OUTPUT_TEST" + const envValue = "fixture-environment-value" + const authValue = "fixture-auth-value" + const warehouseValue = "fixture-warehouse-value" + const envPassword = "fixture-json-password" + const connection = JSON.stringify({ type: "postgres", password: envPassword, database: "sample-database" }) + const previousEnv = process.env[envName] + const previousConnection = process.env[connectionName] + process.env[envName] = envValue + process.env[connectionName] = connection + Store.setSecretBackendForTests({ + name: "fixture", + get: async () => warehouseValue, + set: async () => {}, + delete: async () => true, + }) + try { + await Auth.set(provider, { type: "api", key: authValue }) + expect((await Store.getCredential("fixture-connection", "password")) === warehouseValue).toBe(true) + const redact = await toolCredentialRedactor() + for (const value of [envValue, authValue, warehouseValue, envPassword, connection]) { + expect(redact.text(value) === "[REDACTED]").toBe(true) + } + expect(redact.text("sample-database")).toBe("sample-database") + } finally { + await Auth.remove(provider) + Store.setSecretBackendForTests(undefined) + if (previousEnv === undefined) delete process.env[envName] + else process.env[envName] = previousEnv + if (previousConnection === undefined) delete process.env[connectionName] + else process.env[connectionName] = previousConnection + } +}) diff --git a/packages/opencode/test/tool/read.test.ts b/packages/opencode/test/tool/read.test.ts index 57e61b92c3..eeef46b3b7 100644 --- a/packages/opencode/test/tool/read.test.ts +++ b/packages/opencode/test/tool/read.test.ts @@ -811,3 +811,21 @@ describe("tool.read binary detection", () => { }), ) }) + +// altimate_change start — multiline credentials are matched before numbering the read result +it.instance("redacts raw multiline and JSON-escaped credentials in a file", () => + Effect.gen(function* () { + const instance = yield* TestInstance + const file = path.join(instance.directory, "fixture.txt") + const value = 'read-fixture-"value"\nline-two' + const { createCredentialRedactor } = yield* Effect.promise(() => import("@/altimate/tool-output-credentials")) + yield* Effect.promise(() => Bun.write(file, value + "\n" + JSON.stringify({ value }) + "\nunchanged")) + const result = yield* run( + { filePath: file }, + { ...ctx, redactCredentials: createCredentialRedactor([value]) }, + ) + expect(result.output).toContain('1: [REDACTED]\n2: \n3: {"value":"[REDACTED]"}\n4: unchanged') + expect(result.metadata.preview).toBe('[REDACTED]\n\n{"value":"[REDACTED]"}\nunchanged') + }), +) +// altimate_change end From 904662c4f1bbc484369d3793e3d4bbddf8f77660 Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sun, 22 Nov 2026 00:10:38 -0800 Subject: [PATCH 2/9] fix: cover credential output boundaries --- .../native/connections/credential-store.ts | 8 +- .../src/altimate/tool-output-credentials.ts | 281 +++++++++++++++--- packages/opencode/src/mcp/auth.ts | 10 + packages/opencode/src/mcp/index.ts | 22 +- packages/opencode/src/session/index.ts | 5 +- packages/opencode/src/session/prompt.ts | 24 +- packages/opencode/src/session/session.ts | 5 + packages/opencode/src/session/tools.ts | 40 +-- packages/opencode/src/tool/grep.ts | 25 ++ packages/opencode/src/tool/read.ts | 17 +- packages/opencode/src/tool/tool.ts | 7 +- .../session/tool-credential-output.test.ts | 26 +- .../tool-output-credential-sources.test.ts | 141 +++++++++ .../session/tool-output-credentials.test.ts | 22 ++ packages/opencode/test/tool/grep.test.ts | 21 ++ packages/opencode/test/tool/read.test.ts | 16 + 16 files changed, 585 insertions(+), 85 deletions(-) create mode 100644 packages/opencode/test/session/tool-output-credential-sources.test.ts diff --git a/packages/opencode/src/altimate/native/connections/credential-store.ts b/packages/opencode/src/altimate/native/connections/credential-store.ts index 9ca2e40216..7f3c21be44 100644 --- a/packages/opencode/src/altimate/native/connections/credential-store.ts +++ b/packages/opencode/src/altimate/native/connections/credential-store.ts @@ -19,9 +19,9 @@ import { Log } from "@/altimate/util/log" import type { ConnectionConfig } from "@altimateai/drivers" // altimate_change start — credentials this process has already read or written -const knownCredentials = new Map() +const knownCredentials = new Map() -export function knownStoredCredentials(): Iterable { +export function knownStoredCredentials(): Iterable<{ field: string; value: string }> { return knownCredentials.values() } // altimate_change end @@ -126,7 +126,7 @@ export async function storeCredential( try { await store.set(`${connectionName}/${field}`, value) // altimate_change — retain the current credential for exact tool-result redaction - knownCredentials.set(`${connectionName}/${field}`, value) + knownCredentials.set(`${connectionName}/${field}`, { field, value }) return true } catch (e) { // e.g. no libsecret on Linux, or a locked keychain: report "not stored" so @@ -150,7 +150,7 @@ export async function getCredential( const account = `${connectionName}/${field}` const value = await store.get(account) if (value === null) knownCredentials.delete(account) - else knownCredentials.set(account, value) + else knownCredentials.set(account, { field, value }) return value // altimate_change end } catch (e) { diff --git a/packages/opencode/src/altimate/tool-output-credentials.ts b/packages/opencode/src/altimate/tool-output-credentials.ts index 5797d58d1f..39abe009df 100644 --- a/packages/opencode/src/altimate/tool-output-credentials.ts +++ b/packages/opencode/src/altimate/tool-output-credentials.ts @@ -8,6 +8,42 @@ import { isSensitiveField, knownStoredCredentials } from "@/altimate/native/conn const replacement = "[REDACTED]" const minLength = 8 +type Match = { start: number; end: number } +const contentKinds = new Set(["text", "image", "audio", "resource", "resource_link", "file"]) + +// Decode only JSON escapes and keep a map back to the original output bytes. +// The decoded string is used for comparison, never returned as tool output. +function decodeEscapes(value: string) { + const checkpoints: Array<{ index: number; extra: number }> = [] + const parts: string[] = [] + const escapes = /\\(?:["\\/bfnrt]|u[\da-fA-F]{4})/g + let end = 0 + let extra = 0 + for (const match of value.matchAll(escapes)) { + const index = match.index + parts.push(value.slice(end, index), JSON.parse('"' + match[0] + '"')) + const decodedEnd = index - extra + 1 + extra += match[0].length - 1 + checkpoints.push({ index: decodedEnd, extra }) + end = index + match[0].length + } + if (!parts.length) return undefined + parts.push(value.slice(end)) + return { + value: parts.join(""), + sourceOffset(index: number) { + let low = 0 + let high = checkpoints.length + while (low < high) { + const mid = (low + high) >>> 1 + if (checkpoints[mid].index <= index) low = mid + 1 + else high = mid + } + return index + (checkpoints[low - 1]?.extra ?? 0) + }, + } +} + export function createCredentialRedactor(values: Iterable = []) { const literals = new Set() for (const value of values) { @@ -17,10 +53,83 @@ export function createCredentialRedactor(values: Iterable = } const ordered = [...literals].sort((a, b) => b.length - a.length) const pattern = ordered.length ? new RegExp(ordered.map((value) => RegExp.escape(value)).join("|"), "g") : undefined - const maxLiteralLength = ordered[0]?.length ?? 0 - const text = (value: string) => (pattern ? value.replace(pattern, replacement) : value) - const lines = (value: string) => - pattern ? value.replace(pattern, (match) => replacement + "\n".repeat(match.split("\n").length - 1)) : value + const lookahead = (ordered[0]?.length ?? 0) * 6 + + function matches(value: string): Match[] { + if (!pattern) return [] + const found = Array.from(value.matchAll(pattern), (match) => ({ + start: match.index, + end: match.index + match[0].length, + })) + const decoded = value.includes("\\") ? decodeEscapes(value) : undefined + if (!decoded) return found + for (const match of decoded.value.matchAll(pattern)) { + found.push({ + start: decoded.sourceOffset(match.index), + end: decoded.sourceOffset(match.index + match[0].length), + }) + } + found.sort((a, b) => a.start - b.start || b.end - a.end) + const merged: Match[] = [] + for (const match of found) { + const previous = merged.at(-1) + if (previous && match.start < previous.end) previous.end = Math.max(previous.end, match.end) + else merged.push(match) + } + return merged + } + + function replaceMatches(value: string, found: Match[], from = 0, to = value.length, keepLines = false) { + let cursor = from + const parts: string[] = [] + for (const { start, end } of found) { + if (end <= from) continue + if (start >= to) break + parts.push(value.slice(cursor, Math.max(cursor, start)), replacement) + if (keepLines) parts.push("\n".repeat(value.slice(start, end).split("\n").length - 1)) + cursor = end + } + if (!parts.length) return value.slice(from, to) + if (cursor < to) parts.push(value.slice(cursor, to)) + return parts.join("") + } + + const text = (value: string) => { + if (!pattern) return value + if (!value.includes("\\")) return value.replace(pattern, replacement) + return replaceMatches(value, matches(value)) + } + + function unfinishedStart(value: string) { + let start = value.length + for (const literal of ordered) { + const earliest = Math.max(0, value.length - literal.length + 1) + for (let i = value.indexOf(literal[0], earliest); i >= 0 && i < start; i = value.indexOf(literal[0], i + 1)) { + if (literal.startsWith(value.slice(i))) { + start = i + break + } + } + } + return start + } + + function progress(value: string) { + // A preview can be published again after the next chunk. Hold a trailing + // credential prefix until it is either complete or distinguishable. + if (!pattern) return value + const unfinishedEscape = /\\(?:u[\da-fA-F]{0,3})?$/.exec(value) + const completePrefix = unfinishedEscape ? value.slice(0, unfinishedEscape.index) : value + const decoded = completePrefix.includes("\\") ? decodeEscapes(completePrefix) : undefined + const comparison = decoded?.value ?? completePrefix + const unfinished = unfinishedStart(comparison) + const escapedStart = + unfinished < comparison.length ? (decoded?.sourceOffset(unfinished) ?? unfinished) : value.length + const start = Math.min(unfinishedStart(value), escapedStart) + // Match complete credentials against the original string, including a + // credential whose tail is also a prefix of another credential. + return replaceMatches(value, matches(value), 0, start) + } function visit(value: unknown, seen: WeakMap): unknown { if (typeof value === "string") return text(value) @@ -37,8 +146,9 @@ export function createCredentialRedactor(values: Iterable = const next = Object.create(prototype) seen.set(value, next) for (const [key, item] of Object.entries(value)) { - Object.defineProperty(next, text(key), { - value: visit(item, seen), + const discriminator = key === "type" && typeof item === "string" && contentKinds.has(item) + Object.defineProperty(next, key, { + value: discriminator ? item : visit(item, seen), enumerable: true, writable: true, configurable: true, @@ -63,13 +173,19 @@ export function createCredentialRedactor(values: Iterable = return { failure, text, - lines, + lines: (value: string) => replaceMatches(value, matches(value), 0, value.length, true), + progress, preview(value: string, limit: number) { - // Check the lookahead so a literal crossing the preview limit is still removed. - const prefix = text(value.slice(0, limit + maxLiteralLength)) - return value.length > limit + maxLiteralLength || prefix.length > limit - ? prefix.slice(0, limit) + "\n\n..." - : prefix + // Read enough beyond the cap to match any literal crossing it. + const scan = value.slice(0, limit + lookahead) + const prefix = scan.length === value.length ? progress(scan) : text(scan) + return scan.length < value.length || prefix.length > limit ? prefix.slice(0, limit) + "\n\n..." : prefix + }, + window(value: string, from: number, to: number) { + if (!pattern) return value.slice(from, to) + const start = Math.max(0, from - lookahead) + const scan = value.slice(start, to + lookahead) + return replaceMatches(scan, matches(scan), from - start, to - start) }, output(value: T): T { return pattern ? (visit(value, new WeakMap()) as T) : value @@ -79,52 +195,121 @@ export function createCredentialRedactor(values: Iterable = export type CredentialRedactor = ReturnType -function addConnectionFields(value: unknown, add: (value: unknown) => void) { - if (!value || typeof value !== "object") return +export function redactToolResultState( + state: T, + redact: CredentialRedactor, +): T { + const { input, status, time, ...result } = state + return { ...state, ...redact.output(result), input, status, time } +} + +type AddCredential = (value: unknown) => void + +// Provider loaders use these environment names outside their catalog declarations. +const providerCredentialVariables = new Set(["AICORE_SERVICE_KEY", "AWS_BEARER_TOKEN_BEDROCK", "AWS_SECRET_ACCESS_KEY"]) + +function isCredentialName(name: string) { + return /(?:^|_)(?:API_KEY|TOKEN|SECRET|PASSWORD|PASSWD|AUTHORIZATION)$/i.test(name.replaceAll("-", "_")) +} + +function addCredentialHeaders(headers: Record | undefined, add: AddCredential) { + for (const [name, value] of Object.entries(headers ?? {})) { + if (!/(?:^|[-_])(?:authorization|api[-_]key|auth[-_]token|access[-_]token|client[-_]secret)$/i.test(name)) { + continue + } + if (typeof value !== "string") continue + add(value) + if (/(?:^|[-_])authorization$/i.test(name)) { + // Transport values include their scheme, while a tool can return the token alone. + add(/^(?:Bearer|Basic|Token)\s+(\S+)$/i.exec(value)?.[1]) + } + } +} + +function addProviderOptions(options: Record | undefined, add: AddCredential) { + add(options?.apiKey) + add(options?.clientSecret) + add(options?.accessToken) + add(options?.refreshToken) + if (options?.headers && typeof options.headers === "object") { + addCredentialHeaders(options.headers as Record, add) + } +} + +function readJson(value: string): unknown { + try { + return JSON.parse(value) + } catch { + return undefined + } +} + +function addConnectionCredential(name: string, value: unknown, add: AddCredential) { + if (typeof value !== "string" || !value) return + add(value) + // BigQuery accepts service-account credentials both as JSON objects and serialized strings. + if (name === "credentials_json" || name === "keyfile_json") addConnectionFields(readJson(value), add) +} + +function addConnectionFields(value: unknown, add: AddCredential): boolean { + if (!value || typeof value !== "object") return false + let hasCredential = false for (const [key, field] of Object.entries(value)) { - if (isSensitiveField(key)) add(field) - if (field && typeof field === "object") addConnectionFields(field, add) + if (isSensitiveField(key) && typeof field === "string" && field.length >= minLength) { + hasCredential = true + addConnectionCredential(key, field, add) + } else if (addConnectionFields(field, add)) { + hasCredential = true + } } + return hasCredential } export const loadToolCredentialRedactor = withAlsInstanceRef( Effect.gen(function* () { // Message projection is below configuration and the connection registry in the import graph. - const [{ Auth }, { Config }, { Env }, Connections, { Instance }] = yield* Effect.promise(() => - Promise.all([ - import("@/auth"), - import("@/config/config"), - import("@/env"), - import("@/altimate/native/connections/registry"), - import("@/project/instance"), - ]), - ) + const [{ Auth }, { Config }, { Env }, Connections, { Instance }, { McpAuth }, { knownMcpHeaders }] = + yield* Effect.promise(() => + Promise.all([ + import("@/auth"), + import("@/config/config"), + import("@/env"), + import("@/altimate/native/connections/registry"), + import("@/project/instance"), + import("@/mcp/auth"), + import("@/mcp"), + ]), + ) const instance = yield* InstanceRef const values = new Set() const add = (value: unknown) => { if (typeof value === "string") values.add(value) } - const [config, auth] = yield* Effect.promise(() => - Promise.all([instance ? Config.get().catch(() => undefined) : undefined, Auth.all().catch(() => undefined)]), + const [config, auth, mcpAuth] = yield* Effect.promise(() => + Promise.all([ + instance ? Config.get().catch(() => undefined) : undefined, + Auth.all().catch(() => undefined), + McpAuth.all().catch(() => undefined), + ]), ) const environment = [...Object.entries(process.env), ...Object.entries(instance ? Env.all() : {})] const configuredVariables = new Set() for (const provider of Object.values(config?.provider ?? {})) { for (const name of provider.env ?? []) configuredVariables.add(name) - add(provider.options?.apiKey) - for (const model of Object.values(provider.models ?? {})) add(model.options?.apiKey) + addProviderOptions(provider.options, add) + for (const model of Object.values(provider.models ?? {})) { + addProviderOptions(model.options, add) + addCredentialHeaders(model.headers, add) + } } for (const [name, value] of environment) { if (name.startsWith(Connections.CONN_ENV_PREFIX)) { - add(value) - if (value) { - try { - addConnectionFields(JSON.parse(value), add) - } catch { - // The complete environment value is still a known literal. - } - } - } else if (/(?:^|_)API_KEY$/i.test(name) || configuredVariables.has(name)) { + if (!value) continue + const connection = readJson(value) + // Only credential-bearing connection configurations contribute a composite literal. + // Keep opaque values for the credential-prefixed variables supported by headless callers. + if (!connection || typeof connection !== "object" || addConnectionFields(connection, add)) add(value) + } else if (isCredentialName(name) || providerCredentialVariables.has(name) || configuredVariables.has(name)) { add(value) } } @@ -137,11 +322,29 @@ export const loadToolCredentialRedactor = withAlsInstanceRef( if (entry.type === "wellknown") add(entry.token) } } + for (const entry of Object.values(mcpAuth ?? {})) { + add(entry.tokens?.accessToken) + add(entry.tokens?.refreshToken) + add(entry.clientInfo?.clientSecret) + add(entry.codeVerifier) + } + for (const server of Object.values(config?.mcp ?? {})) { + if (!("type" in server)) continue + if (server.type === "remote") { + addCredentialHeaders(server.headers, add) + if (typeof server.oauth === "object") add(server.oauth.clientSecret) + } else { + for (const [name, value] of Object.entries(server.environment ?? {})) { + if (isCredentialName(name)) add(value) + } + } + } if (instance) { + for (const headers of knownMcpHeaders(instance)) addCredentialHeaders(headers, add) const connections = Instance.restore(instance, () => Connections.knownCredentialConfigs()) for (const connection of connections) addConnectionFields(connection, add) } - for (const credential of knownStoredCredentials()) add(credential) + for (const credential of knownStoredCredentials()) addConnectionCredential(credential.field, credential.value, add) return createCredentialRedactor(values) }), ) diff --git a/packages/opencode/src/mcp/auth.ts b/packages/opencode/src/mcp/auth.ts index 6293827b56..423d96e2cb 100644 --- a/packages/opencode/src/mcp/auth.ts +++ b/packages/opencode/src/mcp/auth.ts @@ -5,6 +5,9 @@ import { Global } from "@opencode-ai/core/global" import { Effect, Layer, Context, Option, Schema } from "effect" import { FSUtil } from "@opencode-ai/core/fs-util" import { EffectFlock } from "@opencode-ai/core/util/effect-flock" +// altimate_change start — allow a read without initializing the application runtime +import { makeRuntime } from "@/effect/run-service" +// altimate_change end export const Tokens = Schema.Struct({ accessToken: Schema.mutableKey(Schema.String), @@ -208,4 +211,11 @@ export const defaultLayer = Layer.suspend(() => layer.pipe(Layer.provide(EffectF export const node = LayerNode.make(layer, () => [FSUtil.node, EffectFlock.node]) // altimate_change end +// altimate_change start — expose stored credentials without starting MCP +const run = makeRuntime(Service, defaultLayer) +export function all() { + return run.runPromise((service) => service.all()) +} +// altimate_change end + export * as McpAuth from "./auth" diff --git a/packages/opencode/src/mcp/index.ts b/packages/opencode/src/mcp/index.ts index da8e75a1ce..2e83919415 100644 --- a/packages/opencode/src/mcp/index.ts +++ b/packages/opencode/src/mcp/index.ts @@ -44,6 +44,9 @@ import * as Result from "effect/Result" // altimate_change end import { EffectBridge } from "@/effect/bridge" import { InstanceState } from "@/effect/instance-state" +// altimate_change start — read credentials from live clients without initializing MCP +import type { InstanceContext } from "@/project/instance-context" +// altimate_change end import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process" import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner" import { McpCatalog } from "./catalog" @@ -311,6 +314,18 @@ interface State { // altimate_change end } +// altimate_change start — use the actual request headers, including command-resolved values +const credentialStates = new WeakMap() +const requestHeaders = new WeakMap>() + +export function* knownMcpHeaders(instance: InstanceContext): Iterable> { + for (const client of Object.values(credentialStates.get(instance)?.clients ?? {})) { + const headers = requestHeaders.get(client) + if (headers) yield headers + } +} +// altimate_change end + export interface Interface { readonly status: () => Effect.Effect> readonly clients: () => Effect.Effect> @@ -579,6 +594,8 @@ export const layer = Layer.effect( duration_ms: Date.now() - connectStart, }) // altimate_change end + // altimate_change — retain the headers for this client only while it stays live + requestHeaders.set(result.client, mergedHeaders) return { client: result.client, status: { status: "connected" } as Status, transport: transportLabel } } // If this was an auth error, stop trying other transports @@ -822,7 +839,7 @@ export const layer = Layer.effect( } const state = yield* InstanceState.make( - Effect.fn("MCP.state")(function* () { + Effect.fn("MCP.state")(function* (instance) { const cfg = yield* cfgSvc.get() const bridge = yield* EffectBridge.make() const config = cfg.mcp ?? {} @@ -837,6 +854,9 @@ export const layer = Layer.effect( // altimate_change end } + // altimate_change — lookup is observational and never initiates an MCP connection + credentialStates.set(instance, s) + // altimate_change start — auto-discover MCP servers from external AI tool configs let discoveryResult: { serverNames: string[]; sources: string[] } | null = null try { diff --git a/packages/opencode/src/session/index.ts b/packages/opencode/src/session/index.ts index 9031a56d40..46b8f90238 100644 --- a/packages/opencode/src/session/index.ts +++ b/packages/opencode/src/session/index.ts @@ -46,14 +46,13 @@ import { ProjectV2 } from "@opencode-ai/core/project" // altimate_change end // altimate_change — sanitize result fields before SQLite and subscribers see them -import { toolCredentialRedactor } from "@/altimate/tool-output-credentials" +import { redactToolResultState, toolCredentialRedactor } from "@/altimate/tool-output-credentials" // altimate_change start — upstream_fix: keep stored, published, and returned parts JSON-safe async function persistPart(part: MessageV2.Part) { if (part.type === "tool" && (part.state.status === "completed" || part.state.status === "error")) { - const { input, time, status, ...result } = part.state const redact = await toolCredentialRedactor() - part = { ...part, state: { ...part.state, ...redact.output(result), input, time, status } } as MessageV2.ToolPart + part = { ...part, state: redactToolResultState(part.state, redact) } } part = normalizeBigInts(part, "safe-number") const { id, messageID, sessionID, ...data } = part diff --git a/packages/opencode/src/session/prompt.ts b/packages/opencode/src/session/prompt.ts index 59c15e226d..bcdef92fdf 100644 --- a/packages/opencode/src/session/prompt.ts +++ b/packages/opencode/src/session/prompt.ts @@ -2910,7 +2910,7 @@ export namespace SessionPrompt { const tools: Record = {} // altimate_change start — carry tool identity into repeated-id metadata lookup - const context = (toolName: string, args: any, options: ToolCallOptions) => { + const context = (toolName: string, args: any, options: ToolCallOptions, external = false) => { const execution = input.processor.beginToolExecution(options.toolCallId) const ctx: Tool.Context = { sessionID: input.session.id, @@ -2934,6 +2934,8 @@ export namespace SessionPrompt { metadata: (val: { title?: string; metadata?: any }) => // altimate_change start — Tool.Context.metadata/ask now return Effect (v1.17.9) Effect.promise(async () => { + // altimate_change — an external tool can register a credential before reporting progress + const redact = external ? await toolCredentialRedactor() : ctx.redactCredentials const match = input.processor.partFromToolExecution(execution) ?? input.processor.partFromToolCall(options.toolCallId, { toolName, input: args }) @@ -2941,8 +2943,8 @@ export namespace SessionPrompt { await Session.updatePart({ ...match, state: { - title: ctx.redactCredentials?.output(val.title) ?? val.title, // altimate_change — plugin progress - metadata: ctx.redactCredentials?.output(val.metadata) ?? val.metadata, // altimate_change — plugin progress + title: redact?.output(val.title) ?? val.title, // altimate_change — plugin progress + metadata: redact?.output(val.metadata) ?? val.metadata, // altimate_change — plugin progress status: "running", input: args, time: { @@ -2998,7 +3000,7 @@ export namespace SessionPrompt { inputSchema: jsonSchema(schema as any), async execute(args, options) { // altimate_change start — disambiguate repeated concurrent tool-call ids - const { ctx, execution } = context(item.id, args, options) + const { ctx, execution } = context(item.id, args, options, item.registrySource === "external") // altimate_change // altimate_change end // altimate_change start — release the execution identity on every exit path try { @@ -3017,7 +3019,9 @@ export namespace SessionPrompt { // altimate_change start — cover custom tools as well as the shared built-in wrapper const redact = await toolCredentialRedactor() ctx.redactCredentials = redact - const result = redact.output(await AppRuntime.runPromise(item.execute(args, ctx))) + const raw = await AppRuntime.runPromise(item.execute(args, ctx)) + const resultRedactor = item.registrySource === "external" ? await toolCredentialRedactor() : redact + const result = resultRedactor.output(redact.output(raw)) // altimate_change end // altimate_change end const output = { @@ -3052,12 +3056,12 @@ export namespace SessionPrompt { await input.lessons?.appendFileLessons(ctx.sessionID, item.id, args, stamped) // altimate_change end // altimate_change start — return the source-stamped output - const safeResult = redact.output(stamped) + const safeResult = resultRedactor.output(stamped) Tool.assertSuccess(safeResult) return safeResult } catch (error) { - const redact = ctx.redactCredentials ?? (await toolCredentialRedactor()) - throw redact.failure(error) + const fresh = await toolCredentialRedactor() + throw fresh.failure(ctx.redactCredentials?.failure(error) ?? error) // altimate_change end } finally { input.processor.finishToolExecution(execution) @@ -4524,7 +4528,7 @@ NOTE: At any point in time through this workflow you should feel free to ask the output += chunk.toString() if (part.state.status === "running") { part.state.metadata = { - output: redact.text(output), // altimate_change — persisted shell progress + output: redact.progress(output), // altimate_change — persisted shell progress description: "", } Session.updatePart(part) @@ -4535,7 +4539,7 @@ NOTE: At any point in time through this workflow you should feel free to ask the output += chunk.toString() if (part.state.status === "running") { part.state.metadata = { - output: redact.text(output), // altimate_change — persisted shell progress + output: redact.progress(output), // altimate_change — persisted shell progress description: "", } Session.updatePart(part) diff --git a/packages/opencode/src/session/session.ts b/packages/opencode/src/session/session.ts index d77b027c97..835ac2867f 100644 --- a/packages/opencode/src/session/session.ts +++ b/packages/opencode/src/session/session.ts @@ -1,5 +1,7 @@ // altimate_change start — release session-owned scratch stores on deletion import { closeScratch } from "@/altimate/native/connections/scratch" +// altimate_change — sanitize tool results updated through the Effect session API +import { loadToolCredentialRedactor, redactToolResultState } from "@/altimate/tool-output-credentials" // altimate_change end // altimate_change start — upstream_fix: normalize native integers before part persistence and events import { normalizeBigInts } from "@/altimate/util/json" @@ -694,6 +696,9 @@ export const layer: Layer.Layer< const updatePart = (part: T): Effect.Effect => Effect.gen(function* () { // altimate_change start — upstream_fix: keep stored, published, and returned parts JSON-safe + if (part.type === "tool" && (part.state.status === "completed" || part.state.status === "error")) { + part = { ...part, state: redactToolResultState(part.state, yield* loadToolCredentialRedactor) } + } part = normalizeBigInts(part, "safe-number") // altimate_change end yield* events.publish(SessionV1.Event.PartUpdated, { diff --git a/packages/opencode/src/session/tools.ts b/packages/opencode/src/session/tools.ts index 448349e56c..a640cc6711 100644 --- a/packages/opencode/src/session/tools.ts +++ b/packages/opencode/src/session/tools.ts @@ -54,6 +54,7 @@ export const resolve = Effect.fn("SessionTools.resolve")(function* (input: { args: Record, options: ToolExecutionOptions, redact?: CredentialRedactor, + external = false, ): Tool.Context => ({ redactCredentials: redact, // altimate_change end @@ -68,20 +69,23 @@ export const resolve = Effect.fn("SessionTools.resolve")(function* (input: { permissions: Permission.merge(input.agent.permission, input.session.permission ?? []), // altimate_change end metadata: (val) => - input.processor.updateToolCall(options.toolCallId, (match) => { - if (!["running", "pending"].includes(match.state.status)) return match - return { - ...match, - state: { - // altimate_change start — sanitize custom tool progress before persistence - title: redact?.output(val.title) ?? val.title, - metadata: redact?.output(val.metadata) ?? val.metadata, - // altimate_change end - status: "running", - input: args, - time: { start: Date.now() }, - }, - } + Effect.gen(function* () { + const progressRedactor = external ? yield* loadToolCredentialRedactor : redact + yield* input.processor.updateToolCall(options.toolCallId, (match) => { + if (!["running", "pending"].includes(match.state.status)) return match + return { + ...match, + state: { + // altimate_change start — sanitize custom tool progress before persistence + title: progressRedactor?.output(val.title) ?? val.title, + metadata: progressRedactor?.output(val.metadata) ?? val.metadata, + // altimate_change end + status: "running", + input: args, + time: { start: Date.now() }, + }, + } + }) }), ask: (req) => permission @@ -124,14 +128,16 @@ export const resolve = Effect.fn("SessionTools.resolve")(function* (input: { return run.promise( Effect.gen(function* () { const redact = yield* loadToolCredentialRedactor // altimate_change — collect before tool execution - const ctx = context(args, options, redact) // altimate_change — protect progress too + const ctx = context(args, options, redact, item.registrySource === "external") // altimate_change yield* plugin.trigger( "tool.execute.before", { tool: item.id, sessionID: ctx.sessionID, callID: ctx.callID }, { args }, ) // altimate_change start — custom and built-in results use the same filter - const result = redact.output(yield* item.execute(args, ctx)) + const raw = yield* item.execute(args, ctx) + const resultRedactor = item.registrySource === "external" ? yield* loadToolCredentialRedactor : redact + const result = resultRedactor.output(redact.output(raw)) // altimate_change end const output = { ...result, @@ -153,7 +159,7 @@ export const resolve = Effect.fn("SessionTools.resolve")(function* (input: { // altimate_change end ) // altimate_change start — propagate the source-stamped output - const safeResult = redact.output(stamped) + const safeResult = resultRedactor.output(stamped) if (options.abortSignal?.aborted) { yield* input.processor.completeToolCall(options.toolCallId, safeResult) } diff --git a/packages/opencode/src/tool/grep.ts b/packages/opencode/src/tool/grep.ts index cb1ea6a3ee..909895bffc 100644 --- a/packages/opencode/src/tool/grep.ts +++ b/packages/opencode/src/tool/grep.ts @@ -162,6 +162,31 @@ export const GrepTool = Tool.define( line: item.line, text: item.text, })) + // Restore adjacency before adding line-number labels. A gap or a + // pre-clipped ripgrep record cannot supply an actual line boundary. + const redact = ctx.redactCredentials + if (redact) { + for (let start = 0; start < final.length; ) { + let end = start + 1 + while (end < final.length) { + const previous = final[end - 1] + const next = final[end] + if (next.path !== previous.path || next.line !== previous.line + 1 || !previous.text.endsWith("\n")) { + break + } + end++ + } + if (end - start > 1) { + const joined = final.slice(start, end).map((match) => match.text).join("") + const redacted = redact.lines(joined) + if (redacted !== joined) { + const lines = redacted.split(/(?<=\n)/) + for (let i = start; i < end; i++) final[i].text = lines[i - start] ?? "" + } + } + start = end + } + } const total = final.length const hasMore = permitted > limit // altimate_change end diff --git a/packages/opencode/src/tool/read.ts b/packages/opencode/src/tool/read.ts index b0ff427836..340a040eae 100644 --- a/packages/opencode/src/tool/read.ts +++ b/packages/opencode/src/tool/read.ts @@ -150,7 +150,13 @@ export const ReadTool = Tool.define< // altimate_change start — character windows preserve the existing line and byte limits const lines = Effect.fn("ReadTool.lines")(function* ( filepath: string, - opts: { limit: number; offset: number; char_offset?: number; max_chars?: number }, + opts: { + limit: number + offset: number + char_offset?: number + max_chars?: number + redactCredentials?: Tool.Context["redactCredentials"] + }, ) { const start = opts.offset - 1 const raw: string[] = [] @@ -201,9 +207,13 @@ export const ReadTool = Tool.define< } const end = windowStart + chunk.length const clipped = end < text.length + // Match against the source before displaying its character window. Keep the + // continuation offsets in source coordinates, even when the placeholder is shorter. + const visible = + clipped || windowStart > 0 ? (opts.redactCredentials?.window(text, windowStart, end) ?? chunk) : chunk const line = clipped - ? `${chunk}... (line truncated to ${chunk.length} chars) (Use offset=${flags.count}, limit=1, char_offset=${end}, max_chars=${Math.min(maxChars, chunk.length)} to continue this line.)` - : chunk + ? `${visible}... (line truncated to ${chunk.length} chars) (Use offset=${flags.count}, limit=1, char_offset=${end}, max_chars=${Math.min(maxChars, chunk.length)} to continue this line.)` + : visible flags.lineCut ||= clipped // altimate_change end const size = Buffer.byteLength(line, "utf-8") + (raw.length > 0 ? 1 : 0) @@ -392,6 +402,7 @@ export const ReadTool = Tool.define< offset: params.offset || 1, char_offset: params.char_offset, max_chars: params.max_chars, + redactCredentials: ctx.redactCredentials, }) // altimate_change end if (file.count < file.offset && !(file.count === 0 && file.offset === 1)) { diff --git a/packages/opencode/src/tool/tool.ts b/packages/opencode/src/tool/tool.ts index 5fd609bf74..d7fc48276f 100644 --- a/packages/opencode/src/tool/tool.ts +++ b/packages/opencode/src/tool/tool.ts @@ -357,9 +357,10 @@ function wrap, Result extends Metadat ctx = { ...ctx, redactCredentials: redact, metadata: (value) => metadata(redact.output(value)) } const executed = yield* execute(decoded as Schema.Schema.Type, ctx).pipe( Effect.onError((cause) => - Effect.sync(() => { - const error = Cause.squash(cause) - endTrace("error", redact.text(error instanceof Error ? error.message : String(error))) + Effect.gen(function* () { + const fresh = yield* loadToolCredentialRedactor + const error = fresh.failure(redact.failure(Cause.squash(cause))) + endTrace("error", error instanceof Error ? error.message : String(error)) altimateTrackError(id, decoded, ctx, startTime, error) }), ), diff --git a/packages/opencode/test/session/tool-credential-output.test.ts b/packages/opencode/test/session/tool-credential-output.test.ts index bff946aa85..ac81885e4d 100644 --- a/packages/opencode/test/session/tool-credential-output.test.ts +++ b/packages/opencode/test/session/tool-credential-output.test.ts @@ -36,13 +36,16 @@ test("known credential values are redacted in bash results", async () => { Instance.restore(instance, async () => { expect((await Config.get()).provider?.test?.options?.apiKey === configuredKey).toBe(true) const bash = await initTool(BashTool) + const previews: string[] = [] const ctx = { sessionID: SessionID.make("ses_test"), messageID: MessageID.make("msg_test"), agent: "build", abort: new AbortController().signal, messages: [], - metadata: () => {}, + metadata: (update: { metadata?: { output?: string } }) => { + if (update.metadata?.output) previews.push(update.metadata.output) + }, ask: async () => {}, } const bashResult = await bash.execute({ command: "cat fixture.txt", description: "Print fixture" }, ctx) @@ -80,7 +83,7 @@ test("known credential values are redacted in bash results", async () => { tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }, time: { created: 1, completed: 2 }, }) - await Session.updatePart({ + const toolPart = { id: PartID.ascending(), sessionID: session.id, messageID, @@ -91,29 +94,42 @@ test("known credential values are redacted in bash results", async () => { status: "completed", input: { command: "cat fixture.txt" }, title: bashResult.title, - output: bashResult.output, - metadata: bashResult.metadata, + output: "prefix-" + configuredKey + "-suffix", + metadata: { output: configuredKey }, time: { start: 1, end: 2 }, }, - }) + } satisfies MessageV2.ToolPart + await Session.updatePart(toolPart) const persisted = await Session.messages({ sessionID: session.id }) const stored = JSON.stringify(persisted) const model = await Provider.getModel(providerID, modelID) const request = JSON.stringify(await MessageV2.toModelMessages(persisted, model)) + const historical = persisted.map((message) => + message.info.id === messageID ? { ...message, parts: [toolPart] } : message, + ) + const replay = JSON.stringify(await MessageV2.toModelMessages(historical, model)) expect({ bashRaw: bashResult.output.includes(configuredKey), storedRaw: stored.includes(configuredKey), requestRaw: request.includes(configuredKey), + replayRaw: replay.includes(configuredKey), + previewRaw: previews.some((preview) => preview.includes(configuredKey)), bashRedacted: bashResult.output.includes(marker), storedRedacted: stored.includes(marker), requestRedacted: request.includes(marker), + replayRedacted: replay.includes(marker), + previewRedacted: previews.some((preview) => preview.includes("[REDACTED]")), }).toEqual({ bashRaw: false, storedRaw: false, requestRaw: false, + replayRaw: false, + previewRaw: false, bashRedacted: true, storedRedacted: true, requestRedacted: true, + replayRedacted: true, + previewRedacted: true, }) }), }) diff --git a/packages/opencode/test/session/tool-output-credential-sources.test.ts b/packages/opencode/test/session/tool-output-credential-sources.test.ts new file mode 100644 index 0000000000..cb5d09daef --- /dev/null +++ b/packages/opencode/test/session/tool-output-credential-sources.test.ts @@ -0,0 +1,141 @@ +// altimate_change — synthetic fixtures for known credential sources. +import { expect, test } from "bun:test" +import { toolCredentialRedactor } from "@/altimate/tool-output-credentials" +import * as Store from "@/altimate/native/connections/credential-store" +import { AppRuntime } from "@/effect/app-runtime" +import { MCP } from "@/mcp" +import { McpAuth } from "@/mcp/auth" +import { Instance } from "@/project/instance" +import { provideTestInstance, tmpdir } from "../fixture/fixture" + +test("collects provider token environment names and MCP OAuth credentials", async () => { + const providerToken = "fixture-gateway-token" + const accessToken = "fixture-mcp-access-token" + const refreshToken = "fixture-mcp-refresh-token" + const clientSecret = "fixture-mcp-client-secret" + const clientId = "fixture-mcp-client-id" + const entry = "fixture-output-redaction" + const previous = process.env.CF_AIG_TOKEN + process.env.CF_AIG_TOKEN = providerToken + try { + await AppRuntime.runPromise( + McpAuth.use.set(entry, { tokens: { accessToken, refreshToken }, clientInfo: { clientId, clientSecret } }), + ) + const redact = await toolCredentialRedactor() + for (const value of [providerToken, accessToken, refreshToken, clientSecret]) { + expect(redact.text(value)).toBe("[REDACTED]") + } + expect(redact.text(clientId)).toBe(clientId) + } finally { + if (previous === undefined) delete process.env.CF_AIG_TOKEN + else process.env.CF_AIG_TOKEN = previous + await AppRuntime.runPromise(McpAuth.use.remove(entry)) + } +}) + +test("collects warehouse credential leaves without changing a password-free connection value", async () => { + const envName = "ALTIMATE_CODE_CONN_REDACTION_SOURCE_TEST" + const cleanEnvName = "ALTIMATE_CODE_CONN_REDACTION_CLEAN_SOURCE_TEST" + const privateKey = "fixture-environment-private-key\nsecond-line" + const storedKey = "fixture-stored-private-key\nsecond-line" + const accountId = "fixture-warehouse-account" + const stored = JSON.stringify({ private_key: storedKey, client_email: accountId }) + const config = JSON.stringify({ type: "bigquery", credentials_json: JSON.stringify({ private_key: privateKey }) }) + const clean = JSON.stringify({ type: "duckdb", path: "fixture-local.duckdb" }) + const previous = process.env[envName] + const previousClean = process.env[cleanEnvName] + process.env[envName] = config + process.env[cleanEnvName] = clean + Store.setSecretBackendForTests({ + name: "fixture", + get: async () => stored, + set: async () => {}, + delete: async () => true, + }) + try { + await Store.getCredential("fixture-output-redaction", "credentials_json") + const redact = await toolCredentialRedactor() + for (const value of [privateKey, storedKey, config, stored]) { + expect(redact.text(value)).toBe("[REDACTED]") + } + expect(redact.text(JSON.stringify({ key: storedKey }))).toBe('{"key":"[REDACTED]"}') + expect(redact.text(clean)).toBe(clean) + expect(redact.text(accountId)).toBe(accountId) + } finally { + Store.setSecretBackendForTests(undefined) + if (previous === undefined) delete process.env[envName] + else process.env[envName] = previous + if (previousClean === undefined) delete process.env[cleanEnvName] + else process.env[cleanEnvName] = previousClean + } +}) + +test("collects configured and command-resolved MCP authorization without changing ordinary headers", async () => { + const configuredToken = "fixture-configured-token" + const resolvedToken = "fixture-command-resolved-token" + const traceHeader = "fixture-not-a-credential" + const serverName = "fixture-output-redaction" + using server = Bun.serve({ + hostname: "127.0.0.1", + port: 0, + async fetch(request) { + if (request.method !== "POST") return new Response(null, { status: 405 }) + const message = (await request.json()) as { + id?: number + method: string + params?: { protocolVersion: string } + } + if (message.id === undefined) return new Response(null, { status: 202 }) + return Response.json({ + jsonrpc: "2.0", + id: message.id, + result: { + protocolVersion: message.params?.protocolVersion, + capabilities: {}, + serverInfo: { name: serverName, version: "1" }, + }, + }) + }, + }) + await using tmp = await tmpdir({ + config: { + mcp: { + configured: { + type: "remote", + enabled: false, + url: String(server.url), + headers: { Authorization: "Bearer " + configuredToken, "X-Trace-Id": traceHeader }, + }, + }, + }, + }) + await provideTestInstance({ + directory: tmp.path, + fn: (instance) => + Instance.restore(instance, async () => { + await MCP.add(serverName, { + type: "remote", + url: String(server.url), + timeout: 1_000, + headersCommand: { + Authorization: [ + process.execPath, + "-e", + `process.stdout.write("Bearer " + ${JSON.stringify(resolvedToken)})`, + ], + }, + }) + try { + expect((await MCP.status())[serverName]?.status).toBe("connected") + const redact = await toolCredentialRedactor() + for (const value of [configuredToken, resolvedToken, "Bearer " + resolvedToken]) { + expect(redact.text(value)).toBe("[REDACTED]") + } + expect(redact.text(traceHeader)).toBe(traceHeader) + } finally { + await MCP.remove(serverName) + } + expect((await toolCredentialRedactor()).text(resolvedToken)).toBe(resolvedToken) + }), + }) +}, 25_000) diff --git a/packages/opencode/test/session/tool-output-credentials.test.ts b/packages/opencode/test/session/tool-output-credentials.test.ts index aa99a22e63..174160d730 100644 --- a/packages/opencode/test/session/tool-output-credentials.test.ts +++ b/packages/opencode/test/session/tool-output-credentials.test.ts @@ -69,3 +69,25 @@ test("collects environment, provider auth and observed warehouse credentials", a else process.env[connectionName] = previousConnection } }) + +test("matches alternate JSON escapes without reserializing clean text", () => { + const redact = createCredentialRedactor(["fixture/key"]) + const escaped = String.raw`{"one":"fi\u0078ture\/key","two":"fi\u0078ture\/other"}` + expect(redact.text(escaped)).toBe(String.raw`{"one":"[REDACTED]","two":"fi\u0078ture\/other"}`) +}) + +test("holds credential fragments out of streamed Bash previews", () => { + const redact = createCredentialRedactor(["fixture-key-value"]) + expect(redact.preview("out: fixture-key", 80)).toBe("out: ") + expect(redact.preview("out: fixture-key-value", 80)).toBe("out: [REDACTED]") + expect(redact.preview("out: fixture-results", 80)).toBe("out: fixture-results") + expect(redact.preview("line with \\", 80)).toBe("line with \\") + expect(redact.preview("prefix fixture-key-value", 18)).toBe("prefix [REDACTED]") +}) + +test("preserves tool result envelope keys and MCP discriminators", () => { + const redact = createCredentialRedactor(["metadata", "resource"]) + expect( + redact.output({ metadata: { detail: "metadata" }, content: [{ type: "resource", text: "resource" }] }), + ).toEqual({ metadata: { detail: "[REDACTED]" }, content: [{ type: "resource", text: "[REDACTED]" }] }) +}) diff --git a/packages/opencode/test/tool/grep.test.ts b/packages/opencode/test/tool/grep.test.ts index 352bfe6674..7a39b559fb 100644 --- a/packages/opencode/test/tool/grep.test.ts +++ b/packages/opencode/test/tool/grep.test.ts @@ -136,6 +136,27 @@ describe("tool.grep", () => { }), ) + // altimate_change start — grep matches a multiline credential before inserting line labels + it.instance("redacts a known credential across adjacent grep matches", () => + Effect.gen(function* () { + const test = yield* TestInstance + const value = "fixture-first\nfixture-second" + const file = path.join(test.directory, "match.txt") + const { createCredentialRedactor } = yield* Effect.promise(() => import("@/altimate/tool-output-credentials")) + yield* Effect.promise(() => Bun.write(file, "start " + value + " finish\n")) + const info = yield* GrepTool + const grep = yield* info.init() + const args = { pattern: "fixture", path: file } + const ordinary = yield* grep.execute(args, { ...ctx, redactCredentials: createCredentialRedactor([]) }) + const redacted = yield* grep.execute(args, { ...ctx, redactCredentials: createCredentialRedactor([value]) }) + expect(redacted.output).toBe( + ordinary.output.replace("start fixture-first", "start [REDACTED]").replace("fixture-second finish", " finish"), + ) + expect(redacted.metadata.matches).toBe(2) + }), + ) + // altimate_change end + it.instance("does not report an unknown total when results are truncated", () => Effect.gen(function* () { const test = yield* TestInstance diff --git a/packages/opencode/test/tool/read.test.ts b/packages/opencode/test/tool/read.test.ts index eeef46b3b7..304c2bb631 100644 --- a/packages/opencode/test/tool/read.test.ts +++ b/packages/opencode/test/tool/read.test.ts @@ -828,4 +828,20 @@ it.instance("redacts raw multiline and JSON-escaped credentials in a file", () = expect(result.metadata.preview).toBe('[REDACTED]\n\n{"value":"[REDACTED]"}\nunchanged') }), ) + +it.instance("redacts a credential even when each character window contains only a fragment", () => + Effect.gen(function* () { + const instance = yield* TestInstance + const file = path.join(instance.directory, "credential-window.txt") + const value = "fixture-window-value" + const { createCredentialRedactor } = yield* Effect.promise(() => import("@/altimate/tool-output-credentials")) + yield* Effect.promise(() => Bun.write(file, "start " + value + " finish")) + const next = { ...ctx, redactCredentials: createCredentialRedactor([value]) } + const first = yield* run({ filePath: file, max_chars: 15 }, next) + expect(first.output).toContain("1: start [REDACTED]... (line truncated to 15 chars)") + expect(first.output).toContain("char_offset=15") + const last = yield* run({ filePath: file, char_offset: 15 }, next) + expect(last.output).toContain("1: [REDACTED] finish") + }), +) // altimate_change end From b2f21f5a09cab05660da8c527a44a71185f84a71 Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sun, 22 Nov 2026 00:18:04 -0800 Subject: [PATCH 3/9] fix: redact credentials before tool output rendering --- packages/core/src/ripgrep.ts | 20 ++- .../altimate/native/connections/registry.ts | 44 ++++- .../src/altimate/tool-output-credentials.ts | 81 ++++++--- packages/opencode/src/session/prompt.ts | 2 +- packages/opencode/src/session/tools.ts | 2 +- packages/opencode/src/tool/apply_patch.ts | 10 +- packages/opencode/src/tool/edit.ts | 32 ++-- packages/opencode/src/tool/grep.ts | 15 +- packages/opencode/src/tool/read.ts | 155 ++++++++++++------ packages/opencode/src/tool/write.ts | 5 +- .../altimate/registry-per-project.test.ts | 22 +++ .../session/tool-output-credentials.test.ts | 20 +++ packages/opencode/test/tool/edit.test.ts | 20 +++ packages/opencode/test/tool/grep.test.ts | 19 +++ packages/opencode/test/tool/read.test.ts | 42 ++++- 15 files changed, 369 insertions(+), 120 deletions(-) diff --git a/packages/core/src/ripgrep.ts b/packages/core/src/ripgrep.ts index 5e111e86f4..a7d6918f10 100644 --- a/packages/core/src/ripgrep.ts +++ b/packages/core/src/ripgrep.ts @@ -136,7 +136,7 @@ const decodeField = (value: unknown): { text: string; raw?: Buffer } | undefined * than silently switching these records to a different unit: without it, a line beginning with one * bad byte reports `needle` at [3,9) of a string where [3,9) reads "edle t". */ -const normalizeMatch = (json: object): unknown => { +const normalizeMatch = (json: object, renderClippedLine?: GrepInput["renderClippedLine"]): unknown => { const data = readProp(json, "data") if (!data || typeof data !== "object") return json const lines = decodeField(readProp(data, "lines")) @@ -190,7 +190,12 @@ const normalizeMatch = (json: object): unknown => { ...data, // Capped here rather than after decoding: the full line is retained by `Stream.runCollect` // until the search ends, and nothing downstream ever shows more than this. See LINE_TEXT_CAP. - lines: { text: capLineText(lines.text) }, + lines: { + text: + renderClippedLine && lines.text.length > LINE_TEXT_CAP + ? renderClippedLine(lines.text, LINE_TEXT_CAP) + "..." + : capLineText(lines.text), + }, // Sliced BEFORE decoding so a pathological submatch count is not decoded only to be dropped. submatches: Array.isArray(submatches) ? submatches.slice(0, MAX_SUBMATCHES).flatMap((submatch) => { @@ -256,6 +261,9 @@ export interface GrepInput { readonly filterPath?: (relativePath: string) => boolean readonly scanLimit?: number // altimate_change end + // altimate_change start — let the tool render the visible window from the full source line + readonly renderClippedLine?: (text: string, end: number) => string + // altimate_change end } export interface Interface { @@ -486,7 +494,7 @@ export const layer = Layer.effect( // which `Effect.catch` deliberately does not catch — so it would abort the // stream rather than skip one record, defeating the point of this change. const normalized = yield* Effect.try({ - try: () => normalizeMatch(json), + try: () => normalizeMatch(json, input.renderClippedLine), catch: (cause) => failure("record could not be normalized", cause), }) const match = yield* Schema.decodeUnknownEffect(RawMatch)(normalized).pipe( @@ -537,9 +545,9 @@ export const layer = Layer.effect( }), line: match.line_number, offset: match.absolute_offset, - // altimate_change start — upstream_fix: capped at parse time, see LINE_TEXT_CAP. - // Re-applied here so the cap still holds if the parser ever stops trimming. - text: capLineText(match.lines.text), + // altimate_change start — the parser caps the source window before collection. + // Keep the rendered window intact when a replacement lengthens that slice. + text: match.lines.text, // altimate_change end submatches: match.submatches.map((submatch) => ({ text: submatch.match.text, diff --git a/packages/opencode/src/altimate/native/connections/registry.ts b/packages/opencode/src/altimate/native/connections/registry.ts index 96d0fbc9bc..8c446e8a76 100644 --- a/packages/opencode/src/altimate/native/connections/registry.ts +++ b/packages/opencode/src/altimate/native/connections/registry.ts @@ -258,7 +258,8 @@ function resolveStorePaths( // Loading // --------------------------------------------------------------------------- -function loadFromFile(filePath: string): Record { +// altimate_change — observational reads leave registry diagnostics to warehouse loading +function loadFromFile(filePath: string, quiet = false): Record { try { if (!fs.existsSync(filePath)) return {} const raw = fs.readFileSync(filePath, "utf-8") @@ -266,7 +267,8 @@ function loadFromFile(filePath: string): Record { if (typeof parsed !== "object" || parsed === null) return {} return parsed as Record } catch (e) { - Log.Default.warn(`Failed to load connections from ${filePath}: ${e}`) + // altimate_change — leave diagnostics to an explicit warehouse load + if (!quiet) Log.Default.warn(`Failed to load connections from ${filePath}: ${e}`) return {} } } @@ -1068,11 +1070,38 @@ export async function reload(): Promise { // altimate_change end } -// altimate_change start — use the active registry and its credential classifier for redaction -export function knownCredentialConfigs(): Iterable { - const registry = state() - if (!registry.loaded) loadInto(registry) - return registry.configs.values() +// altimate_change start — observe current sources without initializing or updating the registry +type FileSource = "global" | "local" +const observedFiles = new Map< + FileSource, + { file: string; version: string; configs: Record } +>() + +function observeFile(file: string, source: FileSource): Record { + try { + const stat = fs.statSync(file, { bigint: true, throwIfNoEntry: false }) + if (stat?.isFile()) { + const version = [stat.dev, stat.ino, stat.size, stat.mtimeNs, stat.ctimeNs].join(":") + const previous = observedFiles.get(source) + if (previous?.file === file && previous.version === version) return previous.configs + const configs = loadFromFile(file, true) + observedFiles.set(source, { file, version, configs }) + return configs + } + } catch { + // Connections files are optional, and a concurrent edit can remove one. + } + observedFiles.delete(source) + return {} +} + +export function* knownCredentialConfigs(): Iterable { + const dir = path.resolve(projectRoot()) + const active = injectionActive ? injectedState : states.get(dir) + if (active?.loaded) yield* active.configs.values() + // A file can be read by tools even when another source shadows its connection name. + yield* Object.values(observeFile(globalConfigPath(), "global")) + yield* Object.values(observeFile(localConfigPathFor(dir), "local")) } // altimate_change end @@ -1092,6 +1121,7 @@ export function reset(): void { states.clear() injectedState = undefined injectionActive = false + observedFiles.clear() // altimate_change end censusSent = false } diff --git a/packages/opencode/src/altimate/tool-output-credentials.ts b/packages/opencode/src/altimate/tool-output-credentials.ts index 39abe009df..978f8bca50 100644 --- a/packages/opencode/src/altimate/tool-output-credentials.ts +++ b/packages/opencode/src/altimate/tool-output-credentials.ts @@ -49,6 +49,8 @@ export function createCredentialRedactor(values: Iterable = for (const value of values) { if (!value || value.length < minLength) continue literals.add(value) + const normalized = value.replace(/\r\n?/g, "\n") + if (normalized.length >= minLength) literals.add(normalized) literals.add(JSON.stringify(value).slice(1, -1)) } const ordered = [...literals].sort((a, b) => b.length - a.length) @@ -57,19 +59,21 @@ export function createCredentialRedactor(values: Iterable = function matches(value: string): Match[] { if (!pattern) return [] - const found = Array.from(value.matchAll(pattern), (match) => ({ - start: match.index, - end: match.index + match[0].length, - })) + const found: Match[] = [] + const collect = (source: string, offset: (index: number) => number) => { + let match: RegExpExecArray | null + while ((match = pattern.exec(source))) { + found.push({ start: offset(match.index), end: offset(match.index + match[0].length) }) + // Continue at the next start, so a second credential can extend past this match. + pattern.lastIndex = match.index + 1 + } + } + collect(value, (index) => index) const decoded = value.includes("\\") ? decodeEscapes(value) : undefined - if (!decoded) return found - for (const match of decoded.value.matchAll(pattern)) { - found.push({ - start: decoded.sourceOffset(match.index), - end: decoded.sourceOffset(match.index + match[0].length), - }) + if (decoded) { + collect(decoded.value, decoded.sourceOffset) + found.sort((a, b) => a.start - b.start || b.end - a.end) } - found.sort((a, b) => a.start - b.start || b.end - a.end) const merged: Match[] = [] for (const match of found) { const previous = merged.at(-1) @@ -94,11 +98,7 @@ export function createCredentialRedactor(values: Iterable = return parts.join("") } - const text = (value: string) => { - if (!pattern) return value - if (!value.includes("\\")) return value.replace(pattern, replacement) - return replaceMatches(value, matches(value)) - } + const text = (value: string) => (pattern ? replaceMatches(value, matches(value)) : value) function unfinishedStart(value: string) { let start = value.length @@ -131,14 +131,28 @@ export function createCredentialRedactor(values: Iterable = return replaceMatches(value, matches(value), 0, start) } - function visit(value: unknown, seen: WeakMap): unknown { + type Frame = "envelope" | "content" | "metadata" | "data" + // These metadata keys are consumed by the framework after redaction. Other + // keys can be arbitrary tool output, including credentials used as keys. + const metadataFields = new Set([ + "error", + "success", + "truncated", + "outputPath", + "observation_mask", + "interrupted", + "rejected", + "source", + ]) + + function visit(value: unknown, seen: WeakMap, frame: Frame): unknown { if (typeof value === "string") return text(value) if (!value || typeof value !== "object") return value if (seen.has(value)) return seen.get(value) if (Array.isArray(value)) { const next: unknown[] = [] seen.set(value, next) - for (const item of value) next.push(visit(item, seen)) + for (const item of value) next.push(visit(item, seen, frame)) return next } const prototype = Object.getPrototypeOf(value) @@ -146,9 +160,25 @@ export function createCredentialRedactor(values: Iterable = const next = Object.create(prototype) seen.set(value, next) for (const [key, item] of Object.entries(value)) { - const discriminator = key === "type" && typeof item === "string" && contentKinds.has(item) - Object.defineProperty(next, key, { - value: discriminator ? item : visit(item, seen), + const toolField = frame === "envelope" || frame === "content" || (frame === "metadata" && metadataFields.has(key)) + const childFrame: Frame = + frame === "envelope" && key === "metadata" + ? "metadata" + : (frame === "envelope" && (key === "content" || key === "attachments")) || + (frame === "content" && key === "resource") + ? "content" + : "data" + const discriminator = frame === "content" && key === "type" && typeof item === "string" && contentKinds.has(item) + let safeKey = toolField ? key : text(key) + if (safeKey !== key) { + // Keep distinct entries when two arbitrary keys collapse to the same placeholder. + const baseKey = safeKey + for (let n = 2; Object.hasOwn(next, safeKey) || Object.hasOwn(value, safeKey); n++) { + safeKey = baseKey + " (" + n + ")" + } + } + Object.defineProperty(next, safeKey, { + value: discriminator ? item : visit(item, seen, childFrame), enumerable: true, writable: true, configurable: true, @@ -158,7 +188,7 @@ export function createCredentialRedactor(values: Iterable = } function failure(error: unknown): unknown { - if (!(error instanceof Error)) return pattern ? visit(error, new WeakMap()) : error + if (!(error instanceof Error)) return pattern ? visit(error, new WeakMap(), "data") : error const message = text(error.message) const stack = error.stack === undefined ? undefined : text(error.stack) if (message === error.message && stack === error.stack) return error @@ -173,6 +203,8 @@ export function createCredentialRedactor(values: Iterable = return { failure, text, + multilineLookaround: ordered.find((value) => value.includes("\n"))?.length ?? 0, + windowLookaround: lookahead, lines: (value: string) => replaceMatches(value, matches(value), 0, value.length, true), progress, preview(value: string, limit: number) { @@ -188,7 +220,10 @@ export function createCredentialRedactor(values: Iterable = return replaceMatches(scan, matches(scan), from - start, to - start) }, output(value: T): T { - return pattern ? (visit(value, new WeakMap()) as T) : value + return pattern ? (visit(value, new WeakMap(), Array.isArray(value) ? "content" : "envelope") as T) : value + }, + metadata(value: T): T { + return pattern ? (visit(value, new WeakMap(), "metadata") as T) : value }, } } diff --git a/packages/opencode/src/session/prompt.ts b/packages/opencode/src/session/prompt.ts index bcdef92fdf..225fcc4702 100644 --- a/packages/opencode/src/session/prompt.ts +++ b/packages/opencode/src/session/prompt.ts @@ -2944,7 +2944,7 @@ export namespace SessionPrompt { ...match, state: { title: redact?.output(val.title) ?? val.title, // altimate_change — plugin progress - metadata: redact?.output(val.metadata) ?? val.metadata, // altimate_change — plugin progress + metadata: redact?.metadata(val.metadata) ?? val.metadata, // altimate_change — plugin progress status: "running", input: args, time: { diff --git a/packages/opencode/src/session/tools.ts b/packages/opencode/src/session/tools.ts index a640cc6711..c76962fb71 100644 --- a/packages/opencode/src/session/tools.ts +++ b/packages/opencode/src/session/tools.ts @@ -78,7 +78,7 @@ export const resolve = Effect.fn("SessionTools.resolve")(function* (input: { state: { // altimate_change start — sanitize custom tool progress before persistence title: progressRedactor?.output(val.title) ?? val.title, - metadata: progressRedactor?.output(val.metadata) ?? val.metadata, + metadata: progressRedactor?.metadata(val.metadata) ?? val.metadata, // altimate_change end status: "running", input: args, diff --git a/packages/opencode/src/tool/apply_patch.ts b/packages/opencode/src/tool/apply_patch.ts index e89fe319eb..4645bbcbb4 100644 --- a/packages/opencode/src/tool/apply_patch.ts +++ b/packages/opencode/src/tool/apply_patch.ts @@ -5,9 +5,9 @@ import { EventV2Bridge } from "@/event-v2-bridge" import { Watcher } from "@opencode-ai/core/filesystem/watcher" import { InstanceState } from "@/effect/instance-state" import { Patch } from "../patch" -import { createTwoFilesPatch, diffLines } from "diff" +import { diffLines } from "diff" import { assertExternalDirectoryEffect, assertSensitiveWriteEffect } from "./external-directory" -import { trimDiff } from "./edit" +import { toolFileDiff } from "./edit" // altimate_change — redact diff source before rendering import { LSP } from "@/lsp/lsp" import { FSUtil } from "@opencode-ai/core/fs-util" import DESCRIPTION from "./apply_patch.txt" @@ -82,7 +82,7 @@ export const ApplyPatchTool = Tool.define( const newContent = hunk.contents.length === 0 || hunk.contents.endsWith("\n") ? hunk.contents : `${hunk.contents}\n` const next = Bom.split(newContent) - const diff = trimDiff(createTwoFilesPatch(filePath, filePath, oldContent, next.text)) + const diff = toolFileDiff(filePath, oldContent, next.text, ctx.redactCredentials) let additions = 0 let deletions = 0 @@ -133,7 +133,7 @@ export const ApplyPatchTool = Tool.define( return yield* Effect.fail(new Error(`apply_patch verification failed: ${error}`)) } - const diff = trimDiff(createTwoFilesPatch(filePath, filePath, oldContent, newContent)) + const diff = toolFileDiff(filePath, oldContent, newContent, ctx.redactCredentials) let additions = 0 let deletions = 0 @@ -175,7 +175,7 @@ export const ApplyPatchTool = Tool.define( ), ) const contentToDelete = source.text - const deleteDiff = trimDiff(createTwoFilesPatch(filePath, filePath, contentToDelete, "")) + const deleteDiff = toolFileDiff(filePath, contentToDelete, "", ctx.redactCredentials) const deletions = contentToDelete.split("\n").length diff --git a/packages/opencode/src/tool/edit.ts b/packages/opencode/src/tool/edit.ts index a3720b2e6a..5c6e931fba 100644 --- a/packages/opencode/src/tool/edit.ts +++ b/packages/opencode/src/tool/edit.ts @@ -125,7 +125,7 @@ export const EditTool = Tool.define( const desiredBom = next.bom contentOld = "" contentNew = next.text - diff = trimDiff(createTwoFilesPatch(filePath, filePath, contentOld, contentNew)) + diff = toolFileDiff(filePath, contentOld, contentNew, ctx.redactCredentials) yield* ctx.ask({ permission: "edit", patterns: [path.relative(instance.worktree, filePath)], @@ -167,13 +167,11 @@ export const EditTool = Tool.define( const desiredBom = source.bom || next.bom contentNew = next.text - diff = trimDiff( - createTwoFilesPatch( - filePath, - filePath, - normalizeLineEndings(contentOld), - normalizeLineEndings(contentNew), - ), + diff = toolFileDiff( + filePath, + normalizeLineEndings(contentOld), + normalizeLineEndings(contentNew), + ctx.redactCredentials, ) yield* ctx.ask({ permission: "edit", @@ -194,13 +192,11 @@ export const EditTool = Tool.define( file: filePath, event: "change", }) - diff = trimDiff( - createTwoFilesPatch( - filePath, - filePath, - normalizeLineEndings(contentOld), - normalizeLineEndings(contentNew), - ), + diff = toolFileDiff( + filePath, + normalizeLineEndings(contentOld), + normalizeLineEndings(contentNew), + ctx.redactCredentials, ) // altimate_change start — upstream_fix: restore stale-file guard (dropped in v1.17.9 merge) Instance.restore(instance, () => FileTime.read(ctx.sessionID, filePath)) @@ -679,6 +675,12 @@ export const ContextAwareReplacer: Replacer = function* (content, find) { } } +// altimate_change start — diff prefixes must not split known multiline credentials +export function toolFileDiff(path: string, before: string, after: string, redact?: Tool.Context["redactCredentials"]) { + return trimDiff(createTwoFilesPatch(path, path, redact?.lines(before) ?? before, redact?.lines(after) ?? after)) +} +// altimate_change end + export function trimDiff(diff: string): string { const lines = diff.split("\n") const contentLines = lines.filter( diff --git a/packages/opencode/src/tool/grep.ts b/packages/opencode/src/tool/grep.ts index 909895bffc..3c6e27c143 100644 --- a/packages/opencode/src/tool/grep.ts +++ b/packages/opencode/src/tool/grep.ts @@ -113,11 +113,22 @@ export const GrepTool = Tool.define( permitted++ return true } + const redact = ctx.redactCredentials + const renderClippedLine = redact ? (text: string, end: number) => redact.window(text, 0, end) : undefined const result: Match[] = [] if (!listedToLimit && approved.size === candidates.length) { // Every candidate was listed and approved: one content search, as before. A listing cut at // the cap may hide unlisted files, so it always takes the restricted path below. - result.push(...(yield* ripgrep.grep({ cwd, pattern: params.pattern, file, include, limit, scanLimit, filterPath }))) + result.push(...(yield* ripgrep.grep({ + cwd, + pattern: params.pattern, + file, + include, + limit, + scanLimit, + filterPath, + renderClippedLine, + }))) } else { // Some candidates are denied or unapproved: restrict ripgrep to the approved files so a denied // file with many matches neither gets read nor consumes the scan ceiling. Literal, rooted globs @@ -144,6 +155,7 @@ export const GrepTool = Tool.define( limit: limit - result.length, scanLimit: scanLimit - scanned, filterPath, + renderClippedLine, }))) } } @@ -164,7 +176,6 @@ export const GrepTool = Tool.define( })) // Restore adjacency before adding line-number labels. A gap or a // pre-clipped ripgrep record cannot supply an actual line boundary. - const redact = ctx.redactCredentials if (redact) { for (let start = 0; start < final.length; ) { let end = start + 1 diff --git a/packages/opencode/src/tool/read.ts b/packages/opencode/src/tool/read.ts index 340a040eae..166aed2c4c 100644 --- a/packages/opencode/src/tool/read.ts +++ b/packages/opencode/src/tool/read.ts @@ -171,66 +171,120 @@ export const ReadTool = Tool.define< // line of the upstream splitLines pipeline) and use a tagged error to stop the // upstream file stream as soon as the byte cap is reached. const decoder = new TextDecoder("utf-8") + // altimate_change start — bounded source context crosses read page boundaries + const redact = opts.redactCredentials + const lookaround = redact?.multilineLookaround ?? 0 + const pending: { text: string; end: number }[] = [] + let position = 0 + let before = "" + + const render = (text: string, after: string) => { + flags.count += 1 + if (flags.count <= start) return + if (raw.length >= opts.limit) { + flags.more = true + return + } + + const splitsPair = (index: number) => + index > 0 && /[\uD800-\uDBFF]/.test(text[index - 1]) && /[\uDC00-\uDFFF]/.test(text[index] ?? "") + const windowStart = splitsPair(charOffset) ? charOffset - 1 : charOffset + const byteLimit = MAX_BYTES - 512 + let stop = Math.min(text.length, windowStart + maxChars, windowStart + byteLimit) + if (splitsPair(stop)) stop += stop === windowStart + 1 ? 1 : -1 + let chunk = text.slice(windowStart, stop) + // Reserve room for the continuation message within the same byte cap. + if (Buffer.byteLength(chunk, "utf-8") > byteLimit) { + let bytes = 0 + let chars = 0 + for (const char of chunk) { + const size = Buffer.byteLength(char, "utf-8") + if (bytes + size > byteLimit) break + bytes += size + chars += char.length + } + chunk = chunk.slice(0, chars) + } + + const end = windowStart + chunk.length + const clipped = end < text.length + let visible = chunk + if (redact && chunk.length > 0) { + if (lookaround && (windowStart < lookaround || text.length - end < lookaround)) { + // Join neighboring fragments only when the selected characters could + // overlap a multiline match. A huge source line is never copied here. + const left = Math.max(0, windowStart - redact.windowLookaround) + const right = Math.min(text.length, end + redact.windowLookaround) + const prefix = left === 0 ? before : "" + const suffix = right === text.length ? after : "" + const context = prefix + text.slice(left, right) + suffix + const from = prefix.length + windowStart - left + visible = redact.window(context, from, from + chunk.length) + } else { + visible = redact.window(text, windowStart, end) + } + } + const line = clipped + ? `${visible}... (line truncated to ${chunk.length} chars) (Use offset=${flags.count}, limit=1, char_offset=${end}, max_chars=${Math.min(maxChars, chunk.length)} to continue this line.)` + : visible + flags.lineCut ||= clipped + + const size = Buffer.byteLength(line, "utf-8") + (raw.length > 0 ? 1 : 0) + if (flags.bytes + size > MAX_BYTES) { + flags.cut = true + flags.more = true + flags.done = true + return + } + raw.push(line) + flags.bytes += size + } + + const remember = (text: string) => { + if (!lookaround) return + const suffixStart = Math.max(0, text.length - lookaround + 1) + before = (before + text.slice(suffixStart) + "\n").slice(-lookaround) + } + + const flush = (eof = false) => { + let index = 0 + while (!flags.done && index < pending.length) { + const current = pending[index] + if (!eof && position - current.end < lookaround) break + let after = "" + for (let next = index + 1; next < pending.length && after.length < lookaround; next++) { + after += "\n" + pending[next].text.slice(0, lookaround - after.length - 1) + } + render(current.text, after) + remember(current.text) + index++ + } + pending.splice(0, index) + } + yield* fs.stream(filepath).pipe( Stream.map((bytes) => decoder.decode(bytes, { stream: true })), Stream.splitLines, Stream.runForEach((text) => Effect.gen(function* () { if (flags.done) return yield* new ReadStop() - flags.count += 1 - if (flags.count <= start) return - - if (raw.length >= opts.limit) { - flags.more = true + if (!lookaround || (pending.length === 0 && (flags.count < start || raw.length >= opts.limit))) { + render(text, "") + if (flags.done) return yield* new ReadStop() + if (raw.length < opts.limit) remember(text) return } - // altimate_change start — expose a continuation for every clipped line - const splitsPair = (index: number) => - index > 0 && /[\uD800-\uDBFF]/.test(text[index - 1]) && /[\uDC00-\uDFFF]/.test(text[index] ?? "") - const windowStart = splitsPair(charOffset) ? charOffset - 1 : charOffset - const byteLimit = MAX_BYTES - 512 - let stop = Math.min(text.length, windowStart + maxChars, windowStart + byteLimit) - if (splitsPair(stop)) stop += stop === windowStart + 1 ? 1 : -1 - let chunk = text.slice(windowStart, stop) - // Reserve room for the continuation message within the same byte cap. - if (Buffer.byteLength(chunk, "utf-8") > byteLimit) { - let bytes = 0 - let chars = 0 - for (const char of chunk) { - const size = Buffer.byteLength(char, "utf-8") - if (bytes + size > byteLimit) break - bytes += size - chars += char.length - } - chunk = chunk.slice(0, chars) - } - const end = windowStart + chunk.length - const clipped = end < text.length - // Match against the source before displaying its character window. Keep the - // continuation offsets in source coordinates, even when the placeholder is shorter. - const visible = - clipped || windowStart > 0 ? (opts.redactCredentials?.window(text, windowStart, end) ?? chunk) : chunk - const line = clipped - ? `${visible}... (line truncated to ${chunk.length} chars) (Use offset=${flags.count}, limit=1, char_offset=${end}, max_chars=${Math.min(maxChars, chunk.length)} to continue this line.)` - : visible - flags.lineCut ||= clipped - // altimate_change end - const size = Buffer.byteLength(line, "utf-8") + (raw.length > 0 ? 1 : 0) - if (flags.bytes + size <= MAX_BYTES) { - raw.push(line) - flags.bytes += size - return - } - - flags.cut = true - flags.more = true - flags.done = true - return yield* new ReadStop() + position += text.length + 1 + pending.push({ text, end: position }) + flush() + if (flags.done) return yield* new ReadStop() }), ), Effect.catchTag("ReadStop", () => Effect.void), ) + if (!flags.done) flush(true) + // altimate_change end // altimate_change start — distinguish clipped text from remaining lines return { raw, count: flags.count, cut: flags.cut, more: flags.more, lineCut: flags.lineCut, offset: opts.offset } @@ -411,13 +465,6 @@ export const ReadTool = Tool.define< ) } - // altimate_change start — check the raw window before adding line-number prefixes - if (ctx.redactCredentials) { - const raw = file.raw.join("\n") - const redacted = ctx.redactCredentials.lines(raw) - if (redacted !== raw) file.raw.splice(0, file.raw.length, ...redacted.split("\n")) - } - // altimate_change end let output = [`${filepath}`, `file`, "\n"].join("\n") output += file.raw.map((line, i) => `${i + file.offset}: ${line}`).join("\n") diff --git a/packages/opencode/src/tool/write.ts b/packages/opencode/src/tool/write.ts index a3290b7731..2631113037 100644 --- a/packages/opencode/src/tool/write.ts +++ b/packages/opencode/src/tool/write.ts @@ -3,7 +3,6 @@ import * as path from "path" import { Effect } from "effect" import * as Tool from "./tool" import { LSP } from "@/lsp/lsp" -import { createTwoFilesPatch } from "diff" import DESCRIPTION from "./write.txt" import { EventV2Bridge } from "@/event-v2-bridge" import { FileSystem } from "@opencode-ai/core/filesystem" @@ -11,7 +10,7 @@ import { Watcher } from "@opencode-ai/core/filesystem/watcher" import { Format } from "../format" import { FSUtil } from "@opencode-ai/core/fs-util" import { InstanceState } from "@/effect/instance-state" -import { trimDiff } from "./edit" +import { toolFileDiff } from "./edit" // altimate_change — redact diff source before rendering import { assertExternalDirectoryEffect, assertSensitiveWriteEffect } from "./external-directory" import * as Bom from "@/util/bom" // altimate_change start — upstream_fix: restore stale-file guard (dropped in v1.17.9 merge) @@ -80,7 +79,7 @@ export const WriteTool = Tool.define( const contentOld = source.text const contentNew = next.text - const diff = trimDiff(createTwoFilesPatch(filepath, filepath, contentOld, contentNew)) + const diff = toolFileDiff(filepath, contentOld, contentNew, ctx.redactCredentials) // altimate_change yield* ctx.ask({ permission: "edit", patterns: [path.relative(instance.worktree, filepath)], diff --git a/packages/opencode/test/altimate/registry-per-project.test.ts b/packages/opencode/test/altimate/registry-per-project.test.ts index f192d63a71..a5aee41b9f 100644 --- a/packages/opencode/test/altimate/registry-per-project.test.ts +++ b/packages/opencode/test/altimate/registry-per-project.test.ts @@ -188,6 +188,28 @@ describe("project-local connections are per project", () => { expect(inProject(projectB, () => Registry.names())).toEqual(["b1"]) }) + // altimate_change start — tool-output filtering reads current files without loading the registry + test("credential observation leaves the first warehouse load fresh", () => { + writeGlobal({ shared: { type: "postgres", host: "global" } }) + writeLocal(projectA, { shared: { type: "postgres", host: "initial-local" } }) + const observedHosts = () => [...Registry.knownCredentialConfigs()].map((config) => config.host) + + inProject(projectA, () => { + expect(observedHosts()).toEqual(["global", "initial-local"]) + writeLocal(projectA, { shared: { type: "postgres", host: "first-warehouse-use" } }) + expect(Registry.getConfig("shared")?.host).toBe("first-warehouse-use") + + writeLocal(projectA, { shared: { type: "postgres", host: "updated-local" } }) + expect(observedHosts()).toEqual(["first-warehouse-use", "global", "updated-local"]) + expect(Registry.getConfig("shared")?.host).toBe("first-warehouse-use") + + Registry.setConfigs({ shared: { type: "postgres", host: "injected" } }) + expect(observedHosts()).toEqual(["injected", "global", "updated-local"]) + expect(Registry.getConfig("shared")?.host).toBe("injected") + }) + }) + // altimate_change end + test("global and env entries are visible in both projects", () => { writeGlobal({ shared_global: { type: "postgres", host: "g" } }) process.env.ALTIMATE_CODE_CONN_SHARED_ENV = JSON.stringify({ type: "postgres", host: "e" }) diff --git a/packages/opencode/test/session/tool-output-credentials.test.ts b/packages/opencode/test/session/tool-output-credentials.test.ts index 174160d730..839681e65f 100644 --- a/packages/opencode/test/session/tool-output-credentials.test.ts +++ b/packages/opencode/test/session/tool-output-credentials.test.ts @@ -91,3 +91,23 @@ test("preserves tool result envelope keys and MCP discriminators", () => { redact.output({ metadata: { detail: "metadata" }, content: [{ type: "resource", text: "resource" }] }), ).toEqual({ metadata: { detail: "[REDACTED]" }, content: [{ type: "resource", text: "[REDACTED]" }] }) }) + +test("redacts the full union of overlapping exact credential values", () => { + const redact = createCredentialRedactor(["first-fixture", "fixture-tail"]) + expect(redact.text("before first-fixture-tail after")).toBe("before [REDACTED] after") + expect(redact.window("first-fixture-tail/visible", 14, 26)).toBe("[REDACTED]/visible") +}) + +test("filters arbitrary structured keys without renaming result protocol fields", () => { + const credential = "fixture-header-value" + const redact = createCredentialRedactor([credential, "structuredContent", "truncated"]) + expect( + redact.output({ + structuredContent: { [credential]: "value", "[REDACTED]": "existing" }, + metadata: { truncated: true, [credential]: "detail" }, + }), + ).toEqual({ + structuredContent: { "[REDACTED] (2)": "value", "[REDACTED]": "existing" }, + metadata: { truncated: true, "[REDACTED]": "detail" }, + }) +}) diff --git a/packages/opencode/test/tool/edit.test.ts b/packages/opencode/test/tool/edit.test.ts index ce6a73675f..6a8226817f 100644 --- a/packages/opencode/test/tool/edit.test.ts +++ b/packages/opencode/test/tool/edit.test.ts @@ -172,6 +172,26 @@ describe("tool.edit", () => { }), ) + // altimate_change start — the diff is redacted independently of the written file + it.instance("redacts multiline credentials in neighboring diff context", () => + Effect.gen(function* () { + const test = yield* TestInstance + const filepath = path.join(test.directory, "diff-fixture.txt") + const value = "fixture-beginning\nfixture-ending" + yield* put(filepath, value + "\nchange this\n") + const { createCredentialRedactor } = yield* Effect.promise(() => import("@/altimate/tool-output-credentials")) + const result = yield* run( + { filePath: filepath, oldString: "change this", newString: "changed" }, + { ...ctx, redactCredentials: createCredentialRedactor([value]) }, + ) + expect(result.metadata.diff).toContain("[REDACTED]") + expect(result.metadata.diff.includes("fixture-beginning")).toBe(false) + expect(result.metadata.diff.includes("fixture-ending")).toBe(false) + expect((yield* load(filepath)) === value + "\nchanged\n").toBe(true) + }), + ) + // altimate_change end + it.instance("rejects editing an existing file modified after it was read", () => Effect.gen(function* () { const test = yield* TestInstance diff --git a/packages/opencode/test/tool/grep.test.ts b/packages/opencode/test/tool/grep.test.ts index 7a39b559fb..20d7d95ce4 100644 --- a/packages/opencode/test/tool/grep.test.ts +++ b/packages/opencode/test/tool/grep.test.ts @@ -155,6 +155,25 @@ describe("tool.grep", () => { expect(redacted.metadata.matches).toBe(2) }), ) + + it.instance("redacts a credential crossing the retained grep line window", () => + Effect.gen(function* () { + const test = yield* TestInstance + const value = "fixture-overlapping-credential" + const file = path.join(test.directory, "match.txt") + const prefix = "needle".padEnd(1_996, "x") + const { createCredentialRedactor } = yield* Effect.promise(() => import("@/altimate/tool-output-credentials")) + yield* Effect.promise(() => Bun.write(file, prefix + value + "\n")) + const info = yield* GrepTool + const grep = yield* info.init() + const args = { pattern: "needle", path: file } + const ordinary = yield* grep.execute(args, { ...ctx, redactCredentials: createCredentialRedactor([]) }) + const redacted = yield* grep.execute(args, { ...ctx, redactCredentials: createCredentialRedactor([value]) }) + expect(ordinary.output).toBe(`Found 1 matches\n${file}:\n Line 1: ${prefix}fixt...`) + expect(redacted.output).toBe(`Found 1 matches\n${file}:\n Line 1: ${prefix}[REDACTED]...`) + expect(redacted.metadata).toEqual(ordinary.metadata) + }), + ) // altimate_change end it.instance("does not report an unknown total when results are truncated", () => diff --git a/packages/opencode/test/tool/read.test.ts b/packages/opencode/test/tool/read.test.ts index 304c2bb631..1484ef2f22 100644 --- a/packages/opencode/test/tool/read.test.ts +++ b/packages/opencode/test/tool/read.test.ts @@ -824,8 +824,8 @@ it.instance("redacts raw multiline and JSON-escaped credentials in a file", () = { filePath: file }, { ...ctx, redactCredentials: createCredentialRedactor([value]) }, ) - expect(result.output).toContain('1: [REDACTED]\n2: \n3: {"value":"[REDACTED]"}\n4: unchanged') - expect(result.metadata.preview).toBe('[REDACTED]\n\n{"value":"[REDACTED]"}\nunchanged') + expect(result.output).toContain('1: [REDACTED]\n2: [REDACTED]\n3: {"value":"[REDACTED]"}\n4: unchanged') + expect(result.metadata.preview).toBe('[REDACTED]\n[REDACTED]\n{"value":"[REDACTED]"}\nunchanged') }), ) @@ -836,7 +836,7 @@ it.instance("redacts a credential even when each character window contains only const value = "fixture-window-value" const { createCredentialRedactor } = yield* Effect.promise(() => import("@/altimate/tool-output-credentials")) yield* Effect.promise(() => Bun.write(file, "start " + value + " finish")) - const next = { ...ctx, redactCredentials: createCredentialRedactor([value]) } + const next = { ...ctx, redactCredentials: createCredentialRedactor([value, "short\nkey"]) } const first = yield* run({ filePath: file, max_chars: 15 }, next) expect(first.output).toContain("1: start [REDACTED]... (line truncated to 15 chars)") expect(first.output).toContain("char_offset=15") @@ -844,4 +844,40 @@ it.instance("redacts a credential even when each character window contains only expect(last.output).toContain("1: [REDACTED] finish") }), ) + +it.instance("redacts a literal multiline credential across read pages", () => + Effect.gen(function* () { + const instance = yield* TestInstance + const file = path.join(instance.directory, "credential-pages.txt") + const value = "first-fragment\n\nlast-fragment" + const { createCredentialRedactor } = yield* Effect.promise(() => import("@/altimate/tool-output-credentials")) + yield* Effect.promise(() => Bun.write(file, "start " + value + " finish")) + const next = { ...ctx, redactCredentials: createCredentialRedactor([value]) } + const first = yield* run({ filePath: file, limit: 1 }, next) + expect(first.output).toContain("1: start [REDACTED]") + expect(first.output).toContain("Showing lines 1-1 of 3") + const second = yield* run({ filePath: file, offset: 2, limit: 1 }, next) + expect(second.metadata.preview).toBe("") + const last = yield* run({ filePath: file, offset: 3, limit: 1 }, next) + expect(last.output).toContain("3: [REDACTED] finish") + expect(last.output).toContain("(End of file - total 3 lines)") + }), +) + +it.instance("redacts a literal multiline credential when its last line exceeds the current byte budget", () => + Effect.gen(function* () { + const instance = yield* TestInstance + const file = path.join(instance.directory, "credential-at-cap.txt") + const value = "first-fragment\nlast-fragment" + const { createCredentialRedactor } = yield* Effect.promise(() => import("@/altimate/tool-output-credentials")) + const padding = ("x".repeat(2000) + "\n").repeat(25) + yield* Effect.promise(() => Bun.write(file, padding + value + " " + "y".repeat(1800))) + const next = { ...ctx, redactCredentials: createCredentialRedactor([value]) } + const first = yield* run({ filePath: file }, next) + expect(first.output).toContain("26: [REDACTED]") + expect(first.output).toContain("Use offset=27 to continue.") + const rest = yield* run({ filePath: file, offset: 27 }, next) + expect(rest.output).toContain("27: [REDACTED] " + "y".repeat(1800)) + }), +) // altimate_change end From de51808cb20c6e2e8629012749a0946bf758eb0a Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sun, 22 Nov 2026 00:23:24 -0800 Subject: [PATCH 4/9] fix: preserve credential matching across clipped search lines --- packages/core/src/ripgrep.ts | 63 +++++++--- .../src/altimate/tool-output-credentials.ts | 29 +++-- packages/opencode/src/tool/grep.ts | 111 +++++++++++++----- .../session/tool-output-credentials.test.ts | 19 +++ packages/opencode/test/tool/grep.test.ts | 26 ++++ 5 files changed, 197 insertions(+), 51 deletions(-) diff --git a/packages/core/src/ripgrep.ts b/packages/core/src/ripgrep.ts index a7d6918f10..d5fed99dbe 100644 --- a/packages/core/src/ripgrep.ts +++ b/packages/core/src/ripgrep.ts @@ -136,11 +136,12 @@ const decodeField = (value: unknown): { text: string; raw?: Buffer } | undefined * than silently switching these records to a different unit: without it, a line beginning with one * bad byte reports `needle` at [3,9) of a string where [3,9) reads "edle t". */ -const normalizeMatch = (json: object, renderClippedLine?: GrepInput["renderClippedLine"]): unknown => { +const normalizeMatch = (json: object, onSourceLine?: (text: string) => void): unknown => { const data = readProp(json, "data") if (!data || typeof data !== "object") return json const lines = decodeField(readProp(data, "lines")) if (!lines) return json + onSourceLine?.(lines.text) const raw = lines.raw // Submatch `start`/`end` are BYTE offsets into the RAW line, so a lossy decode invalidates them: // every undecodable byte widens to a 3-byte U+FFFD. They are rebased onto the decoded text's own @@ -190,12 +191,7 @@ const normalizeMatch = (json: object, renderClippedLine?: GrepInput["renderClipp ...data, // Capped here rather than after decoding: the full line is retained by `Stream.runCollect` // until the search ends, and nothing downstream ever shows more than this. See LINE_TEXT_CAP. - lines: { - text: - renderClippedLine && lines.text.length > LINE_TEXT_CAP - ? renderClippedLine(lines.text, LINE_TEXT_CAP) + "..." - : capLineText(lines.text), - }, + lines: { text: capLineText(lines.text) }, // Sliced BEFORE decoding so a pathological submatch count is not decoded only to be dropped. submatches: Array.isArray(submatches) ? submatches.slice(0, MAX_SUBMATCHES).flatMap((submatch) => { @@ -261,11 +257,32 @@ export interface GrepInput { readonly filterPath?: (relativePath: string) => boolean readonly scanLimit?: number // altimate_change end - // altimate_change start — let the tool render the visible window from the full source line - readonly renderClippedLine?: (text: string, end: number) => string - // altimate_change end + // altimate_change start — supply source context separately from the capped public match + readonly sourceContext?: { + readonly lookaround: number + readonly onMatch: (line: GrepSourceLine) => void + } +} +// altimate_change end + +// altimate_change start — retain only enough source to render the visible window and adjacent lines +export interface GrepSourceLine { + readonly head: string + readonly tail?: string + readonly visibleEnd: number } +function retainSourceLine(text: string, lookaround: number): GrepSourceLine { + const visibleEnd = Math.min(text.length, LINE_TEXT_CAP) + if (text.length <= LINE_TEXT_CAP + 2 * lookaround) return { head: text, visibleEnd } + return { + head: text.slice(0, LINE_TEXT_CAP + lookaround), + tail: text.slice(-lookaround), + visibleEnd, + } +} +// altimate_change end + export interface Interface { readonly find: (input: FindInput) => Effect.Effect readonly glob: (input: GlobInput) => Effect.Effect @@ -440,7 +457,7 @@ export const layer = Layer.effect( grep: (input) => Effect.suspend(() => { const skipped: { count: number; samples: string[] } = { count: 0, samples: [] } - return run({ + return run({ ...input, filter: input.filterPath ? (match) => input.filterPath!(match.path.text) : undefined, args: [ @@ -493,15 +510,28 @@ export const layer = Layer.effect( // `normalizeMatch` is plain synchronous code. A throw there would be a DEFECT, // which `Effect.catch` deliberately does not catch — so it would abort the // stream rather than skip one record, defeating the point of this change. + const context = input.sourceContext + let sourceLine: GrepSourceLine | undefined const normalized = yield* Effect.try({ - try: () => normalizeMatch(json, input.renderClippedLine), + try: () => + normalizeMatch( + json, + context && + ((text) => { + sourceLine = retainSourceLine(text, context.lookaround) + }), + ), catch: (cause) => failure("record could not be normalized", cause), }) const match = yield* Schema.decodeUnknownEffect(RawMatch)(normalized).pipe( Effect.mapError((cause) => failure("unexpected match shape", cause)), ) // `normalizeMatch` already caps submatches and line text, so nothing is re-trimmed. - return { ...match.data, path: { text: match.data.path.text.replace(/^\.[\\/]/, "") } } + return { + ...match.data, + path: { text: match.data.path.text.replace(/^\.[\\/]/, "") }, + sourceLine, + } }).pipe( Effect.catch((cause) => Effect.sync(() => { @@ -532,6 +562,7 @@ export const layer = Layer.effect( // altimate_change end Effect.map((result) => result.items.map((match) => { + if (match.sourceLine) input.sourceContext?.onMatch(match.sourceLine) const relative = match.path.text .replace(/^(?:\.[\\/])+/u, "") .replace(/^[\\/]+/u, "") @@ -545,9 +576,9 @@ export const layer = Layer.effect( }), line: match.line_number, offset: match.absolute_offset, - // altimate_change start — the parser caps the source window before collection. - // Keep the rendered window intact when a replacement lengthens that slice. - text: match.lines.text, + // altimate_change start — upstream_fix: capped at parse time, see LINE_TEXT_CAP. + // Re-applied here so the cap still holds if the parser ever stops trimming. + text: capLineText(match.lines.text), // altimate_change end submatches: match.submatches.map((submatch) => ({ text: submatch.match.text, diff --git a/packages/opencode/src/altimate/tool-output-credentials.ts b/packages/opencode/src/altimate/tool-output-credentials.ts index 978f8bca50..6f414dffb3 100644 --- a/packages/opencode/src/altimate/tool-output-credentials.ts +++ b/packages/opencode/src/altimate/tool-output-credentials.ts @@ -49,8 +49,7 @@ export function createCredentialRedactor(values: Iterable = for (const value of values) { if (!value || value.length < minLength) continue literals.add(value) - const normalized = value.replace(/\r\n?/g, "\n") - if (normalized.length >= minLength) literals.add(normalized) + literals.add(value.replace(/\r\n?/g, "\n")) literals.add(JSON.stringify(value).slice(1, -1)) } const ordered = [...literals].sort((a, b) => b.length - a.length) @@ -152,13 +151,23 @@ export function createCredentialRedactor(values: Iterable = if (Array.isArray(value)) { const next: unknown[] = [] seen.set(value, next) - for (const item of value) next.push(visit(item, seen, frame)) - return next + let changed = false + for (const item of value) { + const safeItem = visit(item, seen, frame) + changed ||= safeItem !== item + next.push(safeItem) + } + if (changed) return next + seen.set(value, value) + return value } const prototype = Object.getPrototypeOf(value) - if (prototype !== Object.prototype && prototype !== null) return value - const next = Object.create(prototype) + const plain = prototype === Object.prototype || prototype === null + // SDK media and native values such as dates and typed arrays retain their representation. + if (!plain && Object.prototype.toString.call(value) !== "[object Object]") return value + const next = Object.create(plain ? prototype : Object.prototype) seen.set(value, next) + let changed = false for (const [key, item] of Object.entries(value)) { const toolField = frame === "envelope" || frame === "content" || (frame === "metadata" && metadataFields.has(key)) const childFrame: Frame = @@ -177,14 +186,18 @@ export function createCredentialRedactor(values: Iterable = safeKey = baseKey + " (" + n + ")" } } + const safeItem = discriminator ? item : visit(item, seen, childFrame) + changed ||= safeKey !== key || safeItem !== item Object.defineProperty(next, safeKey, { - value: discriminator ? item : visit(item, seen, childFrame), + value: safeItem, enumerable: true, writable: true, configurable: true, }) } - return next + if (changed) return next + seen.set(value, value) + return value } function failure(error: unknown): unknown { diff --git a/packages/opencode/src/tool/grep.ts b/packages/opencode/src/tool/grep.ts index 3c6e27c143..2695ed889b 100644 --- a/packages/opencode/src/tool/grep.ts +++ b/packages/opencode/src/tool/grep.ts @@ -9,6 +9,78 @@ import * as Tool from "./tool" // altimate_change start — enforce read permissions on bounded search results import type { Match } from "@opencode-ai/core/filesystem/schema" import { readableSearchResults, searchReadApproval } from "@/altimate/search-read-permissions" +import type { CredentialRedactor } from "@/altimate/tool-output-credentials" +// altimate_change end + +// altimate_change start — redact adjacent source lines before adding labels or clipping +type RenderedLine = { path: string; line: number; text: string } + +function redactSourceLines( + rows: RenderedLine[], + sourceLines: readonly Ripgrep.GrepSourceLine[], + redact: CredentialRedactor, +): void { + const pieces: string[] = [] + const ranges: { row: RenderedLine; from: number; to: number; clipped: boolean }[] = [] + let length = 0 + + function flush() { + if (ranges.length) { + const source = pieces.join("") + if ( + ranges[0].from === 0 && + ranges[ranges.length - 1].to === source.length && + ranges.every((range) => !range.clipped) + ) { + const redacted = redact.lines(source) + if (redacted !== source) { + const lines = redacted.split(/(?<=\n)/) + for (const [index, range] of ranges.entries()) range.row.text = lines[index] ?? "" + } + } else { + for (const { row, from, to, clipped } of ranges) { + const visible = source.slice(from, to) + const rendered = redact.window(source, from, to) + if (visible === rendered) continue + const ending = visible.endsWith("\r\n") ? "\r\n" : visible.endsWith("\n") ? "\n" : "" + row.text = rendered + (rendered.endsWith(ending) ? "" : ending) + (clipped ? "..." : "") + } + } + } + pieces.length = ranges.length = length = 0 + } + + for (const [index, row] of rows.entries()) { + const source = sourceLines[index] + const previous = rows[index - 1] + const previousSource = sourceLines[index - 1] + if ( + previous && + (previous.path !== row.path || + previous.line + 1 !== row.line || + !(previousSource.tail ?? previousSource.head).endsWith("\n")) + ) { + flush() + } + + ranges.push({ + row, + from: length, + to: length + source.visibleEnd, + clipped: source.tail !== undefined || source.visibleEnd < source.head.length, + }) + pieces.push(source.head) + length += source.head.length + if (source.tail !== undefined) { + // The omitted middle is a real gap. Keep the tail for matches that cross + // the next newline, without inventing adjacency between head and tail. + flush() + pieces.push(source.tail) + length = source.tail.length + } + } + flush() +} // altimate_change end export const Parameters = Schema.Struct({ @@ -114,7 +186,15 @@ export const GrepTool = Tool.define( return true } const redact = ctx.redactCredentials - const renderClippedLine = redact ? (text: string, end: number) => redact.window(text, 0, end) : undefined + const sourceLines: Ripgrep.GrepSourceLine[] = [] + const sourceContext = redact?.windowLookaround + ? { + lookaround: redact.windowLookaround, + onMatch: (line: Ripgrep.GrepSourceLine) => { + sourceLines.push(line) + }, + } + : undefined const result: Match[] = [] if (!listedToLimit && approved.size === candidates.length) { // Every candidate was listed and approved: one content search, as before. A listing cut at @@ -127,7 +207,7 @@ export const GrepTool = Tool.define( limit, scanLimit, filterPath, - renderClippedLine, + sourceContext, }))) } else { // Some candidates are denied or unapproved: restrict ripgrep to the approved files so a denied @@ -155,7 +235,7 @@ export const GrepTool = Tool.define( limit: limit - result.length, scanLimit: scanLimit - scanned, filterPath, - renderClippedLine, + sourceContext, }))) } } @@ -174,30 +254,7 @@ export const GrepTool = Tool.define( line: item.line, text: item.text, })) - // Restore adjacency before adding line-number labels. A gap or a - // pre-clipped ripgrep record cannot supply an actual line boundary. - if (redact) { - for (let start = 0; start < final.length; ) { - let end = start + 1 - while (end < final.length) { - const previous = final[end - 1] - const next = final[end] - if (next.path !== previous.path || next.line !== previous.line + 1 || !previous.text.endsWith("\n")) { - break - } - end++ - } - if (end - start > 1) { - const joined = final.slice(start, end).map((match) => match.text).join("") - const redacted = redact.lines(joined) - if (redacted !== joined) { - const lines = redacted.split(/(?<=\n)/) - for (let i = start; i < end; i++) final[i].text = lines[i - start] ?? "" - } - } - start = end - } - } + if (redact && sourceLines.length === final.length) redactSourceLines(final, sourceLines, redact) const total = final.length const hasMore = permitted > limit // altimate_change end diff --git a/packages/opencode/test/session/tool-output-credentials.test.ts b/packages/opencode/test/session/tool-output-credentials.test.ts index 839681e65f..da2c507c9b 100644 --- a/packages/opencode/test/session/tool-output-credentials.test.ts +++ b/packages/opencode/test/session/tool-output-credentials.test.ts @@ -111,3 +111,22 @@ test("filters arbitrary structured keys without renaming result protocol fields" metadata: { truncated: true, "[REDACTED]": "detail" }, }) }) + +test("filters serializable plugin class fields and retains clean instances", () => { + class Details { + constructor(readonly token: string) {} + } + + const value = "fixture-class-value" + const redact = createCredentialRedactor([value]) + expect(JSON.stringify(redact.output({ metadata: { details: new Details(value) } }))).toBe( + '{"metadata":{"details":{"token":"[REDACTED]"}}}', + ) + const clean = { metadata: { details: new Details("ordinary detail") } } + expect(redact.output(clean)).toBe(clean) +}) + +test("redacts normalized CRLF variants based on the source credential length", () => { + const redact = createCredentialRedactor(["abc\r\ndef"]) + expect(redact.lines("abc\ndef")).toBe("[REDACTED]\n") +}) diff --git a/packages/opencode/test/tool/grep.test.ts b/packages/opencode/test/tool/grep.test.ts index 20d7d95ce4..a8b7dcfde3 100644 --- a/packages/opencode/test/tool/grep.test.ts +++ b/packages/opencode/test/tool/grep.test.ts @@ -174,6 +174,32 @@ describe("tool.grep", () => { expect(redacted.metadata).toEqual(ordinary.metadata) }), ) + + it.instance("redacts multiline credentials across clipped adjacent grep records", () => + Effect.gen(function* () { + const test = yield* TestInstance + const value = "fixture-first-half\nfixture-second-half" + const file = path.join(test.directory, "match.txt") + const { createCredentialRedactor } = yield* Effect.promise(() => import("@/altimate/tool-output-credentials")) + const info = yield* GrepTool + const grep = yield* info.init() + const args = { pattern: "needle|fixture", path: file } + const redactCredentials = createCredentialRedactor([value]) + + for (const [length, visibleSuffix] of [[1_996, "fixt"], [4_000, ""]] as const) { + const prefix = "needle".padEnd(length, "x") + yield* Effect.promise(() => Bun.write(file, prefix + value + " ordinary\n")) + const ordinary = yield* grep.execute(args, { ...ctx, redactCredentials: createCredentialRedactor([]) }) + const redacted = yield* grep.execute(args, { ...ctx, redactCredentials }) + const head = prefix.slice(0, 2_000) + const heading = `Found 2 matches\n${file}:\n Line 1: ${head}` + expect(ordinary.output).toBe(`${heading}${visibleSuffix}...\n Line 2: fixture-second-half ordinary\n`) + const placeholder = visibleSuffix ? "[REDACTED]" : "" + expect(redacted.output).toBe(`${heading}${placeholder}...\n Line 2: [REDACTED] ordinary\n`) + expect(redacted.metadata).toEqual(ordinary.metadata) + } + }), + ) // altimate_change end it.instance("does not report an unknown total when results are truncated", () => From 0b2db65999c77e9ae5fb1b4ac1505f479581c4c1 Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sun, 22 Nov 2026 00:28:31 -0800 Subject: [PATCH 5/9] fix: redact known values in additional tool output formats --- .../src/altimate/tool-output-credentials.ts | 57 ++++++++++++++++++- .../src/altimate/tools/project-scan.ts | 15 +++-- packages/opencode/src/tool/edit.ts | 33 ++++++++++- packages/opencode/src/tool/read.ts | 9 ++- .../session/tool-output-credentials.test.ts | 17 ++++++ packages/opencode/test/tool/edit.test.ts | 16 ++++++ .../opencode/test/tool/project-scan.test.ts | 23 +++++++- packages/opencode/test/tool/read.test.ts | 20 +++++++ 8 files changed, 178 insertions(+), 12 deletions(-) diff --git a/packages/opencode/src/altimate/tool-output-credentials.ts b/packages/opencode/src/altimate/tool-output-credentials.ts index 6f414dffb3..01a80fc4b2 100644 --- a/packages/opencode/src/altimate/tool-output-credentials.ts +++ b/packages/opencode/src/altimate/tool-output-credentials.ts @@ -130,6 +130,41 @@ export function createCredentialRedactor(values: Iterable = return replaceMatches(value, matches(value), 0, start) } + // Binary attachments must stay valid. When their transport or decoded bytes + // contain an exact credential, omit the bytes and leave a text placeholder. + let binaryLiterals: Buffer[] | undefined + function containsCredentialBytes(encoded: string) { + if (!encoded || !pattern) return false + if (text(encoded) !== encoded) return true + binaryLiterals ??= ordered.map((literal) => Buffer.from(literal, "utf8")) + const bytes = Buffer.from(encoded, "base64") + return binaryLiterals.some((literal) => bytes.includes(literal)) + } + + const omitAttachment = Symbol("omitAttachment") + function filterMedia(value: unknown): unknown { + if (!value || typeof value !== "object") return value + const item = value as Record + if (item.type === "file" && typeof item.url === "string") { + const data = /^data:[^,]*;base64,(.*)$/s.exec(item.url)?.[1] + if (data && containsCredentialBytes(data)) return omitAttachment + } + if ((item.type === "image" || item.type === "audio") && typeof item.data === "string") { + if (containsCredentialBytes(item.data)) return { type: "text", text: replacement } + } + if (item.type === "resource" && item.resource && typeof item.resource === "object") { + const resource = item.resource as Record + if (typeof resource.blob === "string" && containsCredentialBytes(resource.blob)) { + const { blob, ...rest } = resource + return { + ...item, + resource: { ...rest, text: resource.text ? text(String(resource.text)) + "\n" + replacement : replacement }, + } + } + } + return value + } + type Frame = "envelope" | "content" | "metadata" | "data" // These metadata keys are consumed by the framework after redaction. Other // keys can be arbitrary tool output, including credentials used as keys. @@ -153,7 +188,12 @@ export function createCredentialRedactor(values: Iterable = seen.set(value, next) let changed = false for (const item of value) { - const safeItem = visit(item, seen, frame) + const filtered = frame === "content" ? filterMedia(item) : item + if (filtered === omitAttachment) { + changed = true + continue + } + const safeItem = visit(filtered, seen, frame) changed ||= safeItem !== item next.push(safeItem) } @@ -195,7 +235,20 @@ export function createCredentialRedactor(values: Iterable = configurable: true, }) } - if (changed) return next + if (changed) { + if (frame === "envelope") { + const attachments = Object.getOwnPropertyDescriptor(value, "attachments")?.value + if ( + Array.isArray(attachments) && + Array.isArray(next.attachments) && + next.attachments.length < attachments.length && + typeof next.output === "string" + ) { + next.output += "\n" + replacement + } + } + return next + } seen.set(value, value) return value } diff --git a/packages/opencode/src/altimate/tools/project-scan.ts b/packages/opencode/src/altimate/tools/project-scan.ts index ca0cb1551a..0f896a91c5 100644 --- a/packages/opencode/src/altimate/tools/project-scan.ts +++ b/packages/opencode/src/altimate/tools/project-scan.ts @@ -4,6 +4,8 @@ import { Dispatcher } from "../native" import { existsSync, readFileSync } from "fs" import path from "path" import { Telemetry } from "@/telemetry" +// altimate_change — use exact credentials before clipping Git output +import type { CredentialRedactor } from "@/altimate/tool-output-credentials" // altimate_change — onboarding funnel: environment_scan_completed import * as OnboardingTelemetry from "@/altimate/telemetry/onboarding" import { Config } from "@/config/config" @@ -115,7 +117,8 @@ export function safeSpawnSync( } } -export async function detectGit(): Promise { +// altimate_change — redact full Git stderr before bounding its tool result +export async function detectGit(redact?: CredentialRedactor): Promise { const isRepoResult = safeSpawnSync(["git", "rev-parse", "--is-inside-work-tree"]) // Distinguish three cases that all produce isRepo=false: // 1. Git binary missing (null result) — record "git" in degraded. @@ -141,9 +144,10 @@ export async function detectGit(): Promise { // session transcripts, or App Insights via `metadata.git.gitError`. // Cap at 240 chars after masking (mask replacements can shorten or // lengthen the input minimally). - const maskedStderr = isRepoResult.stderr - ? Telemetry.maskString(isRepoResult.stderr.trim()).slice(0, 240) || undefined - : undefined + // altimate_change start — match against the full source before either masker or display clips it. + const stderr = redact?.text(isRepoResult.stderr) ?? isRepoResult.stderr + const maskedStderr = stderr ? Telemetry.maskString(stderr.trim()).slice(0, 240) || undefined : undefined + // altimate_change end return { isRepo: false, gitAvailable: true, @@ -608,7 +612,8 @@ export const ProjectScanTool = Tool.define("project_scan", { // expected to fail-safe (return a "not found" or empty result) rather // than throw — see detectGit + detectDataTools. const [git, dbtProject, envVars, dataTools, configFiles] = await Promise.all([ - detectGit().catch(() => { + // altimate_change — retain exact matching across the Git stderr display limits + detectGit(ctx.redactCredentials).catch(() => { degraded.add("git") return { isRepo: false } as GitInfo }), diff --git a/packages/opencode/src/tool/edit.ts b/packages/opencode/src/tool/edit.ts index 5c6e931fba..e5ff616890 100644 --- a/packages/opencode/src/tool/edit.ts +++ b/packages/opencode/src/tool/edit.ts @@ -7,7 +7,7 @@ import * as path from "path" import { Effect, Schema, Semaphore } from "effect" import * as Tool from "./tool" import { LSP } from "@/lsp/lsp" -import { createTwoFilesPatch, diffLines } from "diff" +import { createTwoFilesPatch, diffLines, formatPatch, structuredPatch } from "diff" import DESCRIPTION from "./edit.txt" import { FileSystem } from "@opencode-ai/core/filesystem" import { Watcher } from "@opencode-ai/core/filesystem/watcher" @@ -675,9 +675,36 @@ export const ContextAwareReplacer: Replacer = function* (content, find) { } } -// altimate_change start — diff prefixes must not split known multiline credentials +// altimate_change start — preserve diff structure while masking lines from the original source export function toolFileDiff(path: string, before: string, after: string, redact?: Tool.Context["redactCredentials"]) { - return trimDiff(createTwoFilesPatch(path, path, redact?.lines(before) ?? before, redact?.lines(after) ?? after)) + const safeBefore = redact?.lines(before) ?? before + const safeAfter = redact?.lines(after) ?? after + if (safeBefore === before && safeAfter === after) { + return trimDiff(createTwoFilesPatch(path, path, before, after)) + } + + const patch = structuredPatch(path, path, before, after) + const oldLines = safeBefore.split("\n") + const newLines = safeAfter.split("\n") + for (const hunk of patch.hunks) { + let oldIndex = hunk.oldStart - 1 + let newIndex = hunk.newStart - 1 + hunk.lines = hunk.lines.map((line) => { + const prefix = line[0] + if (prefix === "-") return prefix + oldLines[oldIndex++] + if (prefix === "+") return prefix + newLines[newIndex++] + if (prefix !== " ") return line + const original = line.slice(1) + const oldLine = oldLines[oldIndex++] + const newLine = newLines[newIndex++] + if (oldLine === newLine) return prefix + oldLine + if (oldLine === original) return prefix + newLine + if (newLine === original) return prefix + oldLine + // A context line can intersect different credentials on the two sides. + return prefix + "[REDACTED]" + }) + } + return trimDiff(formatPatch(patch)) } // altimate_change end diff --git a/packages/opencode/src/tool/read.ts b/packages/opencode/src/tool/read.ts index 166aed2c4c..acf10e8532 100644 --- a/packages/opencode/src/tool/read.ts +++ b/packages/opencode/src/tool/read.ts @@ -177,6 +177,7 @@ export const ReadTool = Tool.define< const pending: { text: string; end: number }[] = [] let position = 0 let before = "" + let terminalNewline = false const render = (text: string, after: string) => { flags.count += 1 @@ -255,6 +256,8 @@ export const ReadTool = Tool.define< for (let next = index + 1; next < pending.length && after.length < lookaround; next++) { after += "\n" + pending[next].text.slice(0, lookaround - after.length - 1) } + // splitLines drops the final delimiter. Restore it for matching only. + if (eof && terminalNewline && after.length < lookaround) after += "\n" render(current.text, after) remember(current.text) index++ @@ -263,7 +266,11 @@ export const ReadTool = Tool.define< } yield* fs.stream(filepath).pipe( - Stream.map((bytes) => decoder.decode(bytes, { stream: true })), + Stream.map((bytes) => { + const text = decoder.decode(bytes, { stream: true }) + if (lookaround && text.length > 0) terminalNewline = text.endsWith("\n") || text.endsWith("\r") + return text + }), Stream.splitLines, Stream.runForEach((text) => Effect.gen(function* () { diff --git a/packages/opencode/test/session/tool-output-credentials.test.ts b/packages/opencode/test/session/tool-output-credentials.test.ts index da2c507c9b..ddae70db60 100644 --- a/packages/opencode/test/session/tool-output-credentials.test.ts +++ b/packages/opencode/test/session/tool-output-credentials.test.ts @@ -130,3 +130,20 @@ test("redacts normalized CRLF variants based on the source credential length", ( const redact = createCredentialRedactor(["abc\r\ndef"]) expect(redact.lines("abc\ndef")).toBe("[REDACTED]\n") }) + +test("omits credential-bearing attachment bytes while preserving clean media", () => { + const value = "fixture-media-value" + const redact = createCredentialRedactor([value]) + const encoded = Buffer.from("PDF " + value).toString("base64") + const unsafe = { type: "file", mime: "application/pdf", url: "data:application/pdf;base64," + encoded } + const clean = { type: "file", mime: "application/pdf", url: "data:application/pdf;base64,Y2xlYW4=" } + expect(redact.output({ output: "Attached files", attachments: [unsafe, clean] })).toEqual({ + output: "Attached files\n[REDACTED]", + attachments: [clean], + }) + expect( + redact.output({ + content: [{ type: "resource", resource: { uri: "test://file", blob: encoded, text: "context" } }], + }), + ).toEqual({ content: [{ type: "resource", resource: { uri: "test://file", text: "context\n[REDACTED]" } }] }) +}) diff --git a/packages/opencode/test/tool/edit.test.ts b/packages/opencode/test/tool/edit.test.ts index 6a8226817f..67ba08ed6b 100644 --- a/packages/opencode/test/tool/edit.test.ts +++ b/packages/opencode/test/tool/edit.test.ts @@ -190,6 +190,22 @@ describe("tool.edit", () => { expect((yield* load(filepath)) === value + "\nchanged\n").toBe(true) }), ) + it.instance("keeps an approval diff hunk when only a credential changes", () => + Effect.gen(function* () { + const test = yield* TestInstance + const filepath = path.join(test.directory, "credential-diff-fixture.txt") + const before = "fixture-before-value" + const after = "fixture-after-value" + yield* put(filepath, before + "\n") + const { createCredentialRedactor } = yield* Effect.promise(() => import("@/altimate/tool-output-credentials")) + const result = yield* run( + { filePath: filepath, oldString: before, newString: after }, + { ...ctx, redactCredentials: createCredentialRedactor([before, after]) }, + ) + expect(result.metadata.diff).toContain("-[REDACTED]\n+[REDACTED]") + expect((yield* load(filepath)) === after + "\n").toBe(true) + }), + ) // altimate_change end it.instance("rejects editing an existing file modified after it was read", () => diff --git a/packages/opencode/test/tool/project-scan.test.ts b/packages/opencode/test/tool/project-scan.test.ts index fa79b98778..81549d77b8 100644 --- a/packages/opencode/test/tool/project-scan.test.ts +++ b/packages/opencode/test/tool/project-scan.test.ts @@ -1,5 +1,6 @@ // @ts-nocheck -import { describe, expect, test, beforeAll, beforeEach, afterEach, afterAll } from "bun:test" +// altimate_change — scoped Git stderr fixture +import { describe, expect, test, beforeAll, beforeEach, afterEach, afterAll, spyOn } from "bun:test" import path from "path" import os from "os" import fsp from "fs/promises" @@ -161,6 +162,26 @@ describe("detectGit", () => { } }) + // altimate_change start — use synthetic credentials that span the Git output caps + test("redacts known Git stderr before clipping it for metadata and display", async () => { + const { createCredentialRedactor } = await import("@/altimate/tool-output-credentials") + const value = "fixture-git-credential" + const redact = createCredentialRedactor([value]) + const spawn = spyOn(Bun, "spawnSync") + try { + for (const cap of [120, 240]) { + const prefix = "x".repeat(cap - 11) + " " + const fixture = { exitCode: 2, stdout: Buffer.alloc(0), stderr: Buffer.from(prefix + value) } + spawn.mockReturnValueOnce(fixture).mockReturnValueOnce(fixture) + expect((await detectGit()).gitError?.stderr?.slice(0, cap)).toBe((prefix + value).slice(0, cap)) + expect((await detectGit(redact)).gitError?.stderr?.slice(0, cap)).toBe(prefix + "[REDACTED]") + } + } finally { + spawn.mockRestore() + } + }) + // altimate_change end + afterAll(async () => { await fsp.rm(tmpRoot, { recursive: true, force: true }).catch(() => {}) }) diff --git a/packages/opencode/test/tool/read.test.ts b/packages/opencode/test/tool/read.test.ts index 1484ef2f22..e3fc414389 100644 --- a/packages/opencode/test/tool/read.test.ts +++ b/packages/opencode/test/tool/read.test.ts @@ -880,4 +880,24 @@ it.instance("redacts a literal multiline credential when its last line exceeds t expect(rest.output).toContain("27: [REDACTED] " + "y".repeat(1800)) }), ) + +it.instance("matches a multiline credential that includes the file's final newline", () => + Effect.gen(function* () { + const instance = yield* TestInstance + const file = path.join(instance.directory, "credential-final-newline.txt") + const value = "first-fragment\nlast-fragment\n" + const { createCredentialRedactor } = yield* Effect.promise(() => import("@/altimate/tool-output-credentials")) + const next = { ...ctx, redactCredentials: createCredentialRedactor([value]) } + yield* Effect.promise(() => Bun.write(file, "start " + value)) + const first = yield* run({ filePath: file, limit: 1 }, next) + expect(first.metadata.preview).toBe("start [REDACTED]") + const last = yield* run({ filePath: file, offset: 2 }, next) + expect(last.output).toContain("2: [REDACTED]\n\n(End of file - total 2 lines)") + expect(last.metadata.preview).toBe("[REDACTED]") + + yield* Effect.promise(() => Bun.write(file, "start " + value.slice(0, -1))) + const unterminated = yield* run({ filePath: file }, next) + expect(unterminated.metadata.preview).toBe("start first-fragment\nlast-fragment") + }), +) // altimate_change end From c3266ccc27f1275c863b22b923d54324ebb5a882 Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sun, 22 Nov 2026 00:30:57 -0800 Subject: [PATCH 6/9] fix: handle case-insensitive attachment encoding labels --- packages/opencode/src/altimate/tool-output-credentials.ts | 2 +- packages/opencode/test/session/tool-output-credentials.test.ts | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/opencode/src/altimate/tool-output-credentials.ts b/packages/opencode/src/altimate/tool-output-credentials.ts index 01a80fc4b2..a2639400f3 100644 --- a/packages/opencode/src/altimate/tool-output-credentials.ts +++ b/packages/opencode/src/altimate/tool-output-credentials.ts @@ -146,7 +146,7 @@ export function createCredentialRedactor(values: Iterable = if (!value || typeof value !== "object") return value const item = value as Record if (item.type === "file" && typeof item.url === "string") { - const data = /^data:[^,]*;base64,(.*)$/s.exec(item.url)?.[1] + const data = /^data:[^,]*;base64,(.*)$/is.exec(item.url)?.[1] if (data && containsCredentialBytes(data)) return omitAttachment } if ((item.type === "image" || item.type === "audio") && typeof item.data === "string") { diff --git a/packages/opencode/test/session/tool-output-credentials.test.ts b/packages/opencode/test/session/tool-output-credentials.test.ts index ddae70db60..d772422f1c 100644 --- a/packages/opencode/test/session/tool-output-credentials.test.ts +++ b/packages/opencode/test/session/tool-output-credentials.test.ts @@ -137,7 +137,8 @@ test("omits credential-bearing attachment bytes while preserving clean media", ( const encoded = Buffer.from("PDF " + value).toString("base64") const unsafe = { type: "file", mime: "application/pdf", url: "data:application/pdf;base64," + encoded } const clean = { type: "file", mime: "application/pdf", url: "data:application/pdf;base64,Y2xlYW4=" } - expect(redact.output({ output: "Attached files", attachments: [unsafe, clean] })).toEqual({ + const uppercase = { ...unsafe, url: "DATA:application/pdf;BASE64," + encoded } + expect(redact.output({ output: "Attached files", attachments: [unsafe, uppercase, clean] })).toEqual({ output: "Attached files\n[REDACTED]", attachments: [clean], }) From a30defc220b1524ae618f21d15ce8a4e9821424d Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sun, 22 Nov 2026 00:48:10 -0800 Subject: [PATCH 7/9] fix: preserve tool result structure during credential filtering --- .../altimate/native/connections/registry.ts | 4 +- .../src/altimate/tool-output-credentials.ts | 155 ++++++------ packages/opencode/src/session/message-v2.ts | 2 +- packages/opencode/src/session/prompt.ts | 15 +- packages/opencode/src/session/tools.ts | 16 +- packages/opencode/src/tool/tool.ts | 27 +- .../test/altimate/tools/answer-commit.test.ts | 4 +- .../session/custom-tool-credentials.test.ts | 170 +++++++++++++ packages/opencode/test/session/prompt.test.ts | 15 +- .../session/tool-credential-output.test.ts | 234 ++++++++++-------- .../tool-output-credential-sources.test.ts | 125 +++++----- .../session/tool-output-credentials.test.ts | 19 +- .../tool-output-mcp-credentials.fixture.ts | 76 ++++++ .../skill/release-v0.7.3-adversarial.test.ts | 10 +- .../test/tool/credential-errors.test.ts | 58 +++++ .../opencode/test/tool/project-scan.test.ts | 6 + 16 files changed, 647 insertions(+), 289 deletions(-) create mode 100644 packages/opencode/test/session/custom-tool-credentials.test.ts create mode 100644 packages/opencode/test/session/tool-output-mcp-credentials.fixture.ts create mode 100644 packages/opencode/test/tool/credential-errors.test.ts diff --git a/packages/opencode/src/altimate/native/connections/registry.ts b/packages/opencode/src/altimate/native/connections/registry.ts index 8c446e8a76..8776c68856 100644 --- a/packages/opencode/src/altimate/native/connections/registry.ts +++ b/packages/opencode/src/altimate/native/connections/registry.ts @@ -1095,8 +1095,8 @@ function observeFile(file: string, source: FileSource): Record { - const dir = path.resolve(projectRoot()) +export function* knownCredentialConfigs(projectDir = projectRoot()): Iterable { + const dir = path.resolve(projectDir) const active = injectionActive ? injectedState : states.get(dir) if (active?.loaded) yield* active.configs.values() // A file can be read by tools even when another source shadows its connection name. diff --git a/packages/opencode/src/altimate/tool-output-credentials.ts b/packages/opencode/src/altimate/tool-output-credentials.ts index a2639400f3..94054c477b 100644 --- a/packages/opencode/src/altimate/tool-output-credentials.ts +++ b/packages/opencode/src/altimate/tool-output-credentials.ts @@ -54,7 +54,8 @@ export function createCredentialRedactor(values: Iterable = } const ordered = [...literals].sort((a, b) => b.length - a.length) const pattern = ordered.length ? new RegExp(ordered.map((value) => RegExp.escape(value)).join("|"), "g") : undefined - const lookahead = (ordered[0]?.length ?? 0) * 6 + const longest = ordered[0]?.length ?? 0 + const lookahead = longest * 6 function matches(value: string): Match[] { if (!pattern) return [] @@ -114,8 +115,8 @@ export function createCredentialRedactor(values: Iterable = } function progress(value: string) { - // A preview can be published again after the next chunk. Hold a trailing - // credential prefix until it is either complete or distinguishable. + // Keep the tail private until more output arrives or the stream ends. + // Alternate JSON escape spellings can require a longer holdback. if (!pattern) return value const unfinishedEscape = /\\(?:u[\da-fA-F]{0,3})?$/.exec(value) const completePrefix = unfinishedEscape ? value.slice(0, unfinishedEscape.index) : value @@ -124,7 +125,7 @@ export function createCredentialRedactor(values: Iterable = const unfinished = unfinishedStart(comparison) const escapedStart = unfinished < comparison.length ? (decoded?.sourceOffset(unfinished) ?? unfinished) : value.length - const start = Math.min(unfinishedStart(value), escapedStart) + const start = Math.min(Math.max(0, value.length - longest + 1), escapedStart) // Match complete credentials against the original string, including a // credential whose tail is also a prefix of another credential. return replaceMatches(value, matches(value), 0, start) @@ -165,19 +166,7 @@ export function createCredentialRedactor(values: Iterable = return value } - type Frame = "envelope" | "content" | "metadata" | "data" - // These metadata keys are consumed by the framework after redaction. Other - // keys can be arbitrary tool output, including credentials used as keys. - const metadataFields = new Set([ - "error", - "success", - "truncated", - "outputPath", - "observation_mask", - "interrupted", - "rejected", - "source", - ]) + type Frame = "envelope" | "content" | "data" function visit(value: unknown, seen: WeakMap, frame: Frame): unknown { if (typeof value === "string") return text(value) @@ -201,6 +190,21 @@ export function createCredentialRedactor(values: Iterable = seen.set(value, value) return value } + if (value instanceof Error) { + const descriptors = Object.getOwnPropertyDescriptors(value) + const next = Object.create(Object.getPrototypeOf(value)) + seen.set(value, next) + let changed = false + for (const descriptor of Object.values(descriptors)) { + if (!("value" in descriptor)) continue + const safe = visit(descriptor.value, seen, "data") + changed ||= safe !== descriptor.value + descriptor.value = safe + } + if (changed) return Object.defineProperties(next, descriptors) + seen.set(value, value) + return value + } const prototype = Object.getPrototypeOf(value) const plain = prototype === Object.prototype || prototype === null // SDK media and native values such as dates and typed arrays retain their representation. @@ -209,26 +213,15 @@ export function createCredentialRedactor(values: Iterable = seen.set(value, next) let changed = false for (const [key, item] of Object.entries(value)) { - const toolField = frame === "envelope" || frame === "content" || (frame === "metadata" && metadataFields.has(key)) const childFrame: Frame = - frame === "envelope" && key === "metadata" - ? "metadata" - : (frame === "envelope" && (key === "content" || key === "attachments")) || - (frame === "content" && key === "resource") - ? "content" - : "data" + (frame === "envelope" && (key === "content" || key === "attachments")) || + (frame === "content" && key === "resource") + ? "content" + : "data" const discriminator = frame === "content" && key === "type" && typeof item === "string" && contentKinds.has(item) - let safeKey = toolField ? key : text(key) - if (safeKey !== key) { - // Keep distinct entries when two arbitrary keys collapse to the same placeholder. - const baseKey = safeKey - for (let n = 2; Object.hasOwn(next, safeKey) || Object.hasOwn(value, safeKey); n++) { - safeKey = baseKey + " (" + n + ")" - } - } const safeItem = discriminator ? item : visit(item, seen, childFrame) - changed ||= safeKey !== key || safeItem !== item - Object.defineProperty(next, safeKey, { + changed ||= safeItem !== item + Object.defineProperty(next, key, { value: safeItem, enumerable: true, writable: true, @@ -253,21 +246,10 @@ export function createCredentialRedactor(values: Iterable = return value } - function failure(error: unknown): unknown { - if (!(error instanceof Error)) return pattern ? visit(error, new WeakMap(), "data") : error - const message = text(error.message) - const stack = error.stack === undefined ? undefined : text(error.stack) - if (message === error.message && stack === error.stack) return error - const descriptors = Object.getOwnPropertyDescriptors(error) - return Object.create(Object.getPrototypeOf(error), { - ...descriptors, - message: { value: message, writable: true, configurable: true }, - stack: { value: stack, writable: true, configurable: true }, - }) - } - return { - failure, + failure(error: unknown): unknown { + return pattern ? visit(error, new WeakMap(), "data") : error + }, text, multilineLookaround: ordered.find((value) => value.includes("\n"))?.length ?? 0, windowLookaround: lookahead, @@ -289,7 +271,7 @@ export function createCredentialRedactor(values: Iterable = return pattern ? (visit(value, new WeakMap(), Array.isArray(value) ? "content" : "envelope") as T) : value }, metadata(value: T): T { - return pattern ? (visit(value, new WeakMap(), "metadata") as T) : value + return pattern ? (visit(value, new WeakMap(), "data") as T) : value }, } } @@ -347,40 +329,59 @@ function readJson(value: string): unknown { function addConnectionCredential(name: string, value: unknown, add: AddCredential) { if (typeof value !== "string" || !value) return + if (name === "credentials_json" || name === "keyfile_json") { + const fields = readJson(value) + if (fields && typeof fields === "object") { + addConnectionFields(fields, add) + return + } + } + if (name === "connection_string" && value.includes("://")) { + const url = URL.parse(value) + if (url) { + if (url.password) { + add(url.password) + try { + add(decodeURIComponent(url.password)) + } catch { + // The encoded password still matches if a percent escape is invalid. + } + } + for (const query of url.search.slice(1).split("&")) { + for (const [key, field] of new URLSearchParams(query)) { + const normalized = key.toLowerCase() + if (!isSensitiveField(normalized) && !isCredentialName(normalized)) continue + add(field) + add(query.slice(query.indexOf("=") + 1)) + } + } + return + } + } add(value) - // BigQuery accepts service-account credentials both as JSON objects and serialized strings. - if (name === "credentials_json" || name === "keyfile_json") addConnectionFields(readJson(value), add) } -function addConnectionFields(value: unknown, add: AddCredential): boolean { - if (!value || typeof value !== "object") return false - let hasCredential = false +function addConnectionFields(value: unknown, add: AddCredential) { + if (!value || typeof value !== "object") return for (const [key, field] of Object.entries(value)) { - if (isSensitiveField(key) && typeof field === "string" && field.length >= minLength) { - hasCredential = true - addConnectionCredential(key, field, add) - } else if (addConnectionFields(field, add)) { - hasCredential = true - } + if (isSensitiveField(key)) addConnectionCredential(key, field, add) + addConnectionFields(field, add) } - return hasCredential } export const loadToolCredentialRedactor = withAlsInstanceRef( Effect.gen(function* () { // Message projection is below configuration and the connection registry in the import graph. - const [{ Auth }, { Config }, { Env }, Connections, { Instance }, { McpAuth }, { knownMcpHeaders }] = - yield* Effect.promise(() => - Promise.all([ - import("@/auth"), - import("@/config/config"), - import("@/env"), - import("@/altimate/native/connections/registry"), - import("@/project/instance"), - import("@/mcp/auth"), - import("@/mcp"), - ]), - ) + const [{ Auth }, { Config }, { Env }, Connections, { McpAuth }, { knownMcpHeaders }] = yield* Effect.promise(() => + Promise.all([ + import("@/auth"), + import("@/config/config"), + import("@/env"), + import("@/altimate/native/connections/registry"), + import("@/mcp/auth"), + import("@/mcp"), + ]), + ) const instance = yield* InstanceRef const values = new Set() const add = (value: unknown) => { @@ -405,11 +406,7 @@ export const loadToolCredentialRedactor = withAlsInstanceRef( } for (const [name, value] of environment) { if (name.startsWith(Connections.CONN_ENV_PREFIX)) { - if (!value) continue - const connection = readJson(value) - // Only credential-bearing connection configurations contribute a composite literal. - // Keep opaque values for the credential-prefixed variables supported by headless callers. - if (!connection || typeof connection !== "object" || addConnectionFields(connection, add)) add(value) + if (value) addConnectionFields(readJson(value), add) } else if (isCredentialName(name) || providerCredentialVariables.has(name) || configuredVariables.has(name)) { add(value) } @@ -442,7 +439,7 @@ export const loadToolCredentialRedactor = withAlsInstanceRef( } if (instance) { for (const headers of knownMcpHeaders(instance)) addCredentialHeaders(headers, add) - const connections = Instance.restore(instance, () => Connections.knownCredentialConfigs()) + const connections = Connections.knownCredentialConfigs(instance.directory) for (const connection of connections) addConnectionFields(connection, add) } for (const credential of knownStoredCredentials()) addConnectionCredential(credential.field, credential.value, add) diff --git a/packages/opencode/src/session/message-v2.ts b/packages/opencode/src/session/message-v2.ts index 057df83b59..af29b6d6f9 100644 --- a/packages/opencode/src/session/message-v2.ts +++ b/packages/opencode/src/session/message-v2.ts @@ -909,7 +909,7 @@ export namespace MessageV2 { ? storedMask : "[Old tool result content cleared]" : part.state.output - const rawOutputText = redact?.text(storedOutputText) ?? storedOutputText + const rawOutputText = redact?.output(storedOutputText) ?? storedOutputText const maxChars = options?.toolOutputMaxChars const outputText = !part.state.time.compacted && maxChars !== undefined && rawOutputText.length > maxChars diff --git a/packages/opencode/src/session/prompt.ts b/packages/opencode/src/session/prompt.ts index 225fcc4702..05897ff239 100644 --- a/packages/opencode/src/session/prompt.ts +++ b/packages/opencode/src/session/prompt.ts @@ -2935,7 +2935,9 @@ export namespace SessionPrompt { // altimate_change start — Tool.Context.metadata/ask now return Effect (v1.17.9) Effect.promise(async () => { // altimate_change — an external tool can register a credential before reporting progress - const redact = external ? await toolCredentialRedactor() : ctx.redactCredentials + const redact = external ? await toolCredentialRedactor() : undefined + const title = ctx.redactCredentials?.output(val.title) ?? val.title // altimate_change + const metadata = ctx.redactCredentials?.metadata(val.metadata) ?? val.metadata // altimate_change const match = input.processor.partFromToolExecution(execution) ?? input.processor.partFromToolCall(options.toolCallId, { toolName, input: args }) @@ -2943,8 +2945,8 @@ export namespace SessionPrompt { await Session.updatePart({ ...match, state: { - title: redact?.output(val.title) ?? val.title, // altimate_change — plugin progress - metadata: redact?.metadata(val.metadata) ?? val.metadata, // altimate_change — plugin progress + title: redact?.output(title) ?? title, // altimate_change — plugin progress + metadata: redact?.metadata(metadata) ?? metadata, // altimate_change — plugin progress status: "running", input: args, time: { @@ -3004,6 +3006,8 @@ export namespace SessionPrompt { // altimate_change end // altimate_change start — release the execution identity on every exit path try { + const redact = await toolCredentialRedactor() // altimate_change — capture credentials before execution and hooks + ctx.redactCredentials = redact // altimate_change await Plugin.trigger( "tool.execute.before", { @@ -3017,8 +3021,6 @@ export namespace SessionPrompt { ) // altimate_change start — v1.17.9: Tool.Def.execute returns an Effect // altimate_change start — cover custom tools as well as the shared built-in wrapper - const redact = await toolCredentialRedactor() - ctx.redactCredentials = redact const raw = await AppRuntime.runPromise(item.execute(args, ctx)) const resultRedactor = item.registrySource === "external" ? await toolCredentialRedactor() : redact const result = resultRedactor.output(redact.output(raw)) @@ -3056,7 +3058,8 @@ export namespace SessionPrompt { await input.lessons?.appendFileLessons(ctx.sessionID, item.id, args, stamped) // altimate_change end // altimate_change start — return the source-stamped output - const safeResult = resultRedactor.output(stamped) + const fresh = await toolCredentialRedactor() + const safeResult = fresh.output(resultRedactor.output(redact.output(stamped))) Tool.assertSuccess(safeResult) return safeResult } catch (error) { diff --git a/packages/opencode/src/session/tools.ts b/packages/opencode/src/session/tools.ts index c76962fb71..74734f3091 100644 --- a/packages/opencode/src/session/tools.ts +++ b/packages/opencode/src/session/tools.ts @@ -77,8 +77,8 @@ export const resolve = Effect.fn("SessionTools.resolve")(function* (input: { ...match, state: { // altimate_change start — sanitize custom tool progress before persistence - title: progressRedactor?.output(val.title) ?? val.title, - metadata: progressRedactor?.metadata(val.metadata) ?? val.metadata, + title: progressRedactor?.output(redact?.output(val.title) ?? val.title) ?? val.title, + metadata: progressRedactor?.metadata(redact?.metadata(val.metadata) ?? val.metadata) ?? val.metadata, // altimate_change end status: "running", input: args, @@ -135,7 +135,14 @@ export const resolve = Effect.fn("SessionTools.resolve")(function* (input: { { args }, ) // altimate_change start — custom and built-in results use the same filter - const raw = yield* item.execute(args, ctx) + const raw = yield* item.execute(args, ctx).pipe( + Effect.catchCause((cause) => + Effect.gen(function* () { + const fresh = yield* loadToolCredentialRedactor + return yield* Effect.failCause(Tool.redactFailureCause(Tool.redactFailureCause(cause, redact), fresh)) + }), + ), + ) const resultRedactor = item.registrySource === "external" ? yield* loadToolCredentialRedactor : redact const result = resultRedactor.output(redact.output(raw)) // altimate_change end @@ -159,7 +166,8 @@ export const resolve = Effect.fn("SessionTools.resolve")(function* (input: { // altimate_change end ) // altimate_change start — propagate the source-stamped output - const safeResult = resultRedactor.output(stamped) + const fresh = yield* loadToolCredentialRedactor + const safeResult = fresh.output(resultRedactor.output(redact.output(stamped))) if (options.abortSignal?.aborted) { yield* input.processor.completeToolCall(options.toolCallId, safeResult) } diff --git a/packages/opencode/src/tool/tool.ts b/packages/opencode/src/tool/tool.ts index d7fc48276f..6ab6ce50d7 100644 --- a/packages/opencode/src/tool/tool.ts +++ b/packages/opencode/src/tool/tool.ts @@ -1,5 +1,5 @@ import { PermissionV1 } from "@opencode-ai/core/v1/permission" -import { Cause, Effect, Schema } from "effect" +import { Cause, Context as EffectContext, Effect, Schema } from "effect" import { SessionV1 } from "@opencode-ai/core/v1/session" import type { JSONSchema7 } from "@ai-sdk/provider" import type { SessionID, MessageID } from "../session/schema" @@ -169,6 +169,25 @@ export type InferDef = ? Def : never +// altimate_change start — preserve failures, defects, and cancellation while redacting their values. +export function redactFailureCause(cause: Cause.Cause, redact: CredentialRedactor): Cause.Cause { + return Cause.fromReasons( + cause.reasons.map((reason) => { + if (Cause.isFailReason(reason)) + return Cause.makeFailReason(redact.failure(reason.error) as E).annotate( + EffectContext.makeUnsafe(new Map(reason.annotations)), + ) + if (Cause.isDieReason(reason)) + return Cause.makeDieReason(redact.failure(reason.defect)).annotate( + EffectContext.makeUnsafe(new Map(reason.annotations)), + ) + return reason + }), + ) +} + +// altimate_change end + // altimate_change start — telemetry helpers (ported from the fork's pre-1.17 Tool.define wrapper) function altimateTrackError(id: string, args: unknown, ctx: Context, startTime: number, error: unknown) { try { @@ -356,12 +375,14 @@ function wrap, Result extends Metadat const metadata = ctx.metadata ctx = { ...ctx, redactCredentials: redact, metadata: (value) => metadata(redact.output(value)) } const executed = yield* execute(decoded as Schema.Schema.Type, ctx).pipe( - Effect.onError((cause) => + Effect.catchCause((cause) => Effect.gen(function* () { const fresh = yield* loadToolCredentialRedactor - const error = fresh.failure(redact.failure(Cause.squash(cause))) + const safeCause = redactFailureCause(redactFailureCause(cause, redact), fresh) + const error = Cause.squash(safeCause) endTrace("error", error instanceof Error ? error.message : String(error)) altimateTrackError(id, decoded, ctx, startTime, error) + return yield* Effect.failCause(safeCause) }), ), ) diff --git a/packages/opencode/test/altimate/tools/answer-commit.test.ts b/packages/opencode/test/altimate/tools/answer-commit.test.ts index 6bcffbeaa8..d020e4b14d 100644 --- a/packages/opencode/test/altimate/tools/answer-commit.test.ts +++ b/packages/opencode/test/altimate/tools/answer-commit.test.ts @@ -154,11 +154,11 @@ describe("answer_commit validation repair", () => { ) }) -test("answer_commit executes synchronously with native Effect permissions", async () => { +test("answer_commit executes with native Effect permissions and credential filtering", async () => { const info = await toolInfo(AnswerCommit.AnswerCommitTool) const tool = await Effect.runPromise(info.init()) const asks: unknown[] = [] - const result = Effect.runSync( + const result = await Effect.runPromise( tool.execute( { fields: [{ name: "Count", value: 2 }], basis: "Counted records at record grain using records.id." }, { diff --git a/packages/opencode/test/session/custom-tool-credentials.test.ts b/packages/opencode/test/session/custom-tool-credentials.test.ts new file mode 100644 index 0000000000..c5a310ed04 --- /dev/null +++ b/packages/opencode/test/session/custom-tool-credentials.test.ts @@ -0,0 +1,170 @@ +// altimate_change — synthetic credential collection across custom tool execution and hooks +import { expect, spyOn } from "bun:test" +import { Effect, Layer, Schema } from "effect" +import { Agent } from "@/agent/agent" +import { MCP } from "@/mcp" +import { Permission } from "@/permission" +import { Plugin } from "@/plugin" +import { ProjectID } from "@/project/schema" +import type { Provider } from "@/provider/provider" +import { ModelID, ProviderID } from "@/provider/schema" +import { MessageID, SessionID, PartID } from "@/session/schema" +import { Session } from "@/session" +import { SessionPrompt } from "@/session/prompt" +import * as Precedence from "@/altimate/workspace/precedence" +import { ToolRegistry } from "@/tool/registry" +import { testEffect } from "../lib/effect" + +const model: Provider.Model = { + id: ModelID.make("test-model"), + providerID: ProviderID.make("test"), + api: { id: "test-model", url: "https://example.com", npm: "@ai-sdk/openai" }, + name: "Test model", + capabilities: { + temperature: true, + reasoning: false, + attachment: false, + toolcall: true, + interleaved: false, + input: { text: true, audio: false, image: false, video: false, pdf: false }, + output: { text: true, audio: false, image: false, video: false, pdf: false }, + }, + cost: { input: 0, output: 0, cache: { read: 0, write: 0 } }, + limit: { context: 128000, output: 4096 }, + status: "active", + options: {}, + headers: {}, + release_date: "2026-01-01", +} +const agent: Agent.Info = { + name: "build", + mode: "primary", + options: {}, + permission: Permission.fromConfig({ "*": "allow" }), +} +const it = testEffect(Layer.empty) +for (const outcome of ["result", "error"] as const) + it.live(`shipped resolver refreshes custom tool ${outcome} and progress credentials`, () => + Effect.gen(function* () { + const sessionID = SessionID.make("ses_prompt_tools_errors") + const finished: unknown[] = [] + const secret = "synthetic-external-runtime-credential" + const variable = "ALTIMATE_EXTERNAL_FIXTURE_TOKEN" + const previous = process.env[variable] + delete process.env[variable] + const updates: unknown[] = [] + const update = spyOn(Session, "updatePart").mockImplementation( + Object.assign( + async (part: Parameters[0]) => { + updates.push(part) + return part + }, + { force: Session.updatePart.force, schema: Session.updatePart.schema }, + ), + ) + const registry = spyOn(ToolRegistry, "tools").mockResolvedValue([ + { + id: "backend_failure", + description: "Return a backend failure", + parameters: Schema.Struct({}), + registrySource: "external", + execute: (_args, ctx) => + Effect.gen(function* () { + process.env[variable] = secret + yield* ctx.metadata({ title: secret, metadata: { detail: secret } }) + if (outcome === "error") return yield* Effect.die(new Error(`Fixture failed: ${secret}`)) + return { title: secret, output: secret, metadata: { detail: secret } } + }), + }, + ]) + const mcp = spyOn(MCP, "tools").mockResolvedValue({}) + const plugins = spyOn(Plugin, "trigger").mockImplementation(async (name, _input, output) => { + if (name === "tool.execute.after") { + process.env[variable] = "synthetic-hook-runtime-credential" + if (output && typeof output === "object") Object.assign(output, { output: process.env[variable] }) + } + return output + }) + const precedence = spyOn(Precedence, "refresh").mockResolvedValue({ + enabled: false, + workspaceName: "", + shadowed: new Map(), + }) + try { + const tools = yield* Effect.promise(() => + SessionPrompt.resolveTools({ + agent, + model, + bypassAgentCheck: false, + messages: [], + session: { + id: sessionID, + slug: "prompt-tools-errors", + projectID: ProjectID.make("project"), + directory: "/tmp", + title: "Prompt tool errors", + version: "1", + time: { created: 0, updated: 0 }, + }, + processor: { + message: { + id: MessageID.make("msg_prompt_tools_errors"), + sessionID, + role: "assistant", + time: { created: 0 }, + parentID: MessageID.make("msg_user"), + modelID: model.id, + providerID: model.providerID, + mode: "build", + agent: "build", + path: { cwd: "/tmp", root: "/tmp" }, + cost: 0, + tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }, + }, + beginToolExecution: (raw) => ({ raw, occurrence: 0 }), + finishToolExecution: (execution) => { + finished.push(execution) + }, + partFromToolExecution: () => ({ + id: PartID.make("prt_fixture"), + sessionID, + messageID: MessageID.make("msg_prompt_tools_errors"), + type: "tool", + tool: "backend_failure", + callID: "call_error", + state: { status: "running", input: {}, time: { start: 0 } }, + }), + partFromToolCall: () => undefined, + process: async () => { + throw new Error("unexpected processor invocation") + }, + }, + }), + ) + const result = yield* Effect.promise(async () => { + try { + return await tools.backend_failure.execute!( + {}, + { toolCallId: "call_error", messages: [], abortSignal: new AbortController().signal }, + ) + } catch (error) { + return String(error) + } + }) + expect(JSON.stringify(result)).not.toContain(secret) + expect(JSON.stringify(result)).not.toContain("synthetic-hook-runtime-credential") + expect(JSON.stringify(result)).toContain("[REDACTED]") + expect(JSON.stringify(updates)).not.toContain(secret) + expect(JSON.stringify(updates)).toContain("[REDACTED]") + expect(finished).toEqual([{ raw: "call_error", occurrence: 0 }]) + } finally { + update.mockRestore() + if (previous === undefined) delete process.env[variable] + else process.env[variable] = previous + precedence.mockRestore() + plugins.mockRestore() + mcp.mockRestore() + registry.mockRestore() + } + }), + ) diff --git a/packages/opencode/test/session/prompt.test.ts b/packages/opencode/test/session/prompt.test.ts index eef6c80017..e1461859d4 100644 --- a/packages/opencode/test/session/prompt.test.ts +++ b/packages/opencode/test/session/prompt.test.ts @@ -2712,15 +2712,21 @@ unixNoLLMServer( { config: cfg }, ) +// altimate_change — running output may hold a trailing chunk until shell completion unixNoLLMServer( "shell updates running metadata before process exit", () => withSh(() => Effect.gen(function* () { const { prompt, chat } = yield* boot() + const instance = yield* TestInstance const fiber = yield* prompt - .shell({ sessionID: chat.id, agent: "build", command: "printf first && sleep 0.2 && printf second" }) + .shell({ + sessionID: chat.id, + agent: "build", + command: 'printf first; while [ ! -f shell-progress-release ]; do sleep 0.02; done; printf second', + }) .pipe(Effect.forkChild) yield* pollWithTimeout( @@ -2728,13 +2734,14 @@ unixNoLLMServer( const msgs = yield* MessageV2.filterCompactedEffect(chat.id) const taskMsg = msgs.find((item) => item.info.role === "assistant") const tool = taskMsg ? toolPart(taskMsg.parts) : undefined - if (tool?.state.status === "running" && tool.state.metadata?.output.includes("first")) return true + if (tool?.state.status === "running" && typeof tool.state.metadata?.output === "string") return true }), "timed out waiting for running shell metadata", ) - const exit = yield* Fiber.await(fiber) - expect(Exit.isSuccess(exit)).toBe(true) + yield* Effect.promise(() => Bun.write(path.join(instance.directory, "shell-progress-release"), "ready")) + const result = yield* Fiber.join(fiber) + expect(completedTool(result.parts)?.state.output).toBe("firstsecond") }), ), { config: cfg }, diff --git a/packages/opencode/test/session/tool-credential-output.test.ts b/packages/opencode/test/session/tool-credential-output.test.ts index ac81885e4d..f19e14410b 100644 --- a/packages/opencode/test/session/tool-credential-output.test.ts +++ b/packages/opencode/test/session/tool-credential-output.test.ts @@ -1,4 +1,4 @@ -// altimate_change — model and persistence coverage with synthetic credentials +// altimate_change — independent boundary coverage with raw synthetic credentials import { expect, test } from "bun:test" import path from "node:path" import { Config } from "@/config/config" @@ -9,83 +9,74 @@ import { ModelID, ProviderID } from "@/provider/schema" import { SessionID, MessageID, PartID } from "@/session/schema" import { Instance } from "@/project/instance" import { Provider } from "@/provider/provider" +import { Database, eq } from "@/storage/db" +import { PartTable } from "@opencode-ai/core/session/sql" import { initTool } from "../altimate/tool-fixture" import { provideTestInstance, tmpdir } from "../fixture/fixture" const configuredKey = "fixture-provider-value" - -test("known credential values are redacted in bash results", async () => { - const marker = "prefix-[REDACTED]-suffix" - await using tmp = await tmpdir({ - git: true, - config: { - provider: { - test: { - name: "Test", - npm: "@ai-sdk/openai-compatible", - options: { apiKey: configuredKey }, - models: { fixture: { name: "Fixture" } }, - }, - }, +const rawOutput = "prefix-" + configuredKey + "-suffix" +const safeOutput = "prefix-[REDACTED]-suffix" +const providerID = ProviderID.make("test") +const modelID = ModelID.make("fixture") +const config = { + provider: { + test: { + name: "Test", + npm: "@ai-sdk/openai-compatible", + options: { apiKey: configuredKey }, + models: { fixture: { name: "Fixture" } }, }, - init: (dir) => Bun.write(path.join(dir, "fixture.txt"), "prefix-" + configuredKey + "-suffix"), - }) + }, +} + +async function inConfiguredInstance(fn: (directory: string) => Promise) { + await using tmp = await tmpdir({ git: true, config }) await provideTestInstance({ directory: tmp.path, fn: (instance) => Instance.restore(instance, async () => { - expect((await Config.get()).provider?.test?.options?.apiKey === configuredKey).toBe(true) - const bash = await initTool(BashTool) - const previews: string[] = [] - const ctx = { - sessionID: SessionID.make("ses_test"), - messageID: MessageID.make("msg_test"), - agent: "build", - abort: new AbortController().signal, - messages: [], - metadata: (update: { metadata?: { output?: string } }) => { - if (update.metadata?.output) previews.push(update.metadata.output) - }, - ask: async () => {}, - } - const bashResult = await bash.execute({ command: "cat fixture.txt", description: "Print fixture" }, ctx) - const session = await Session.create({}) - const parentID = MessageID.ascending() - const messageID = MessageID.ascending() - const providerID = ProviderID.make("test") - const modelID = ModelID.make("fixture") - await Session.updateMessage({ - id: parentID, - sessionID: session.id, - role: "user", - agent: "build", - model: { providerID, modelID }, - time: { created: 0 }, - }) - await Session.updatePart({ - id: PartID.ascending(), - sessionID: session.id, - messageID: parentID, - type: "text", - text: "Print the fixture", - }) - await Session.updateMessage({ - id: messageID, - sessionID: session.id, - role: "assistant", - agent: "build", - parentID, - providerID, - modelID, - mode: "build", - path: { cwd: tmp.path, root: tmp.path }, - cost: 0, - tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }, - time: { created: 1, completed: 2 }, - }) - const toolPart = { + expect((await Config.get()).provider?.test?.options?.apiKey).toBe(configuredKey) + await fn(tmp.path) + }), + }) +} + +// Construct raw historical input without calling any output or persistence sanitizer. +function rawHistory(directory: string, sessionID = SessionID.make("ses_fixture")): MessageV2.WithParts[] { + const parentID = MessageID.ascending() + const messageID = MessageID.ascending() + return [ + { + info: { + id: parentID, + sessionID, + role: "user", + agent: "build", + model: { providerID, modelID }, + time: { created: 0 }, + }, + parts: [{ id: PartID.ascending(), sessionID, messageID: parentID, type: "text", text: "Print the fixture" }], + }, + { + info: { + id: messageID, + sessionID, + role: "assistant", + agent: "build", + parentID, + providerID, + modelID, + mode: "build", + path: { cwd: directory, root: directory }, + cost: 0, + tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }, + time: { created: 1, completed: 2 }, + }, + parts: [ + { id: PartID.ascending(), - sessionID: session.id, + sessionID, messageID, type: "tool", tool: "bash", @@ -93,44 +84,79 @@ test("known credential values are redacted in bash results", async () => { state: { status: "completed", input: { command: "cat fixture.txt" }, - title: bashResult.title, - output: "prefix-" + configuredKey + "-suffix", + title: "Print fixture", + output: rawOutput, metadata: { output: configuredKey }, time: { start: 1, end: 2 }, }, - } satisfies MessageV2.ToolPart - await Session.updatePart(toolPart) - const persisted = await Session.messages({ sessionID: session.id }) - const stored = JSON.stringify(persisted) - const model = await Provider.getModel(providerID, modelID) - const request = JSON.stringify(await MessageV2.toModelMessages(persisted, model)) - const historical = persisted.map((message) => - message.info.id === messageID ? { ...message, parts: [toolPart] } : message, - ) - const replay = JSON.stringify(await MessageV2.toModelMessages(historical, model)) - expect({ - bashRaw: bashResult.output.includes(configuredKey), - storedRaw: stored.includes(configuredKey), - requestRaw: request.includes(configuredKey), - replayRaw: replay.includes(configuredKey), - previewRaw: previews.some((preview) => preview.includes(configuredKey)), - bashRedacted: bashResult.output.includes(marker), - storedRedacted: stored.includes(marker), - requestRedacted: request.includes(marker), - replayRedacted: replay.includes(marker), - previewRedacted: previews.some((preview) => preview.includes("[REDACTED]")), - }).toEqual({ - bashRaw: false, - storedRaw: false, - requestRaw: false, - replayRaw: false, - previewRaw: false, - bashRedacted: true, - storedRedacted: true, - requestRedacted: true, - replayRedacted: true, - previewRedacted: true, - }) - }), + }, + ], + }, + ] +} + +test("persistence redacts raw tool results before writing SQLite", async () => { + await inConfiguredInstance(async (directory) => { + const session = await Session.create({}) + const history = rawHistory(directory, session.id) + for (const message of history) { + await Session.updateMessage(message.info) + for (const part of message.parts) await Session.updatePart(part) + } + const raw = history[1].parts[0] as MessageV2.ToolPart + const stored = Database.use((db) => db.select().from(PartTable).where(eq(PartTable.id, raw.id)).get())! + expect(JSON.stringify(stored)).not.toContain(configuredKey) + expect(stored.data).toMatchObject({ state: { output: safeOutput, metadata: { output: "[REDACTED]" } } }) + expect(raw.state).toMatchObject({ output: rawOutput, metadata: { output: configuredKey } }) + }) +}) + +test("model replay redacts raw historical tool output independently of persistence", async () => { + await inConfiguredInstance(async (directory) => { + const history = rawHistory(directory) + const model = await Provider.getModel(providerID, modelID) + const request = JSON.stringify(await MessageV2.toModelMessages(history, model)) + expect(request).not.toContain(configuredKey) + expect(request).toContain(safeOutput) + expect(history[1].parts[0]).toMatchObject({ state: { output: rawOutput } }) + }) +}) + +test("bash progress holds a split credential until the next chunk and redacts final output", async () => { + await inConfiguredInstance(async (directory) => { + const split = Math.floor(configuredKey.length / 2) + const head = configuredKey.slice(0, split) + const tail = configuredKey.slice(split) + await Bun.write(path.join(directory, "head.txt"), "prefix-" + head) + await Bun.write(path.join(directory, "tail.txt"), tail + "-suffix") + // The child waits for the first metadata callback, proving two distinct data + // events without relying on a sleep to split stdout/stderr delivery. + const bash = await initTool(BashTool) + const previews: string[] = [] + const result = await bash.execute( + { + command: "cat head.txt; while [ ! -e release ]; do sleep 0.01; done; cat tail.txt >&2", + description: "Print split fixture", + timeout: 5000, + }, + { + sessionID: SessionID.make("ses_test"), + messageID: MessageID.make("msg_test"), + agent: "build", + abort: new AbortController().signal, + messages: [], + ask: async () => {}, + metadata: (update: { metadata?: { output?: string } }) => { + if (update.metadata?.output === undefined) return + previews.push(update.metadata.output) + if (previews.length === 2) return Bun.write(path.join(directory, "release"), "ready").then(() => {}) + }, + }, + ) + expect(previews.length).toBeGreaterThanOrEqual(3) + expect(previews[1]).not.toContain(head) + for (const preview of previews) expect(preview).not.toContain(configuredKey) + expect(result.output).toBe(safeOutput) + expect(result.metadata.output).toBe(safeOutput) }) }) diff --git a/packages/opencode/test/session/tool-output-credential-sources.test.ts b/packages/opencode/test/session/tool-output-credential-sources.test.ts index cb5d09daef..621769db7c 100644 --- a/packages/opencode/test/session/tool-output-credential-sources.test.ts +++ b/packages/opencode/test/session/tool-output-credential-sources.test.ts @@ -1,11 +1,13 @@ // altimate_change — synthetic fixtures for known credential sources. +import path from "node:path" +import { mkdir } from "node:fs/promises" +import { Effect } from "effect" +import { InstanceRef } from "@/effect/instance-ref" import { expect, test } from "bun:test" -import { toolCredentialRedactor } from "@/altimate/tool-output-credentials" +import { loadToolCredentialRedactor, toolCredentialRedactor } from "@/altimate/tool-output-credentials" import * as Store from "@/altimate/native/connections/credential-store" import { AppRuntime } from "@/effect/app-runtime" -import { MCP } from "@/mcp" import { McpAuth } from "@/mcp/auth" -import { Instance } from "@/project/instance" import { provideTestInstance, tmpdir } from "../fixture/fixture" test("collects provider token environment names and MCP OAuth credentials", async () => { @@ -55,10 +57,14 @@ test("collects warehouse credential leaves without changing a password-free conn try { await Store.getCredential("fixture-output-redaction", "credentials_json") const redact = await toolCredentialRedactor() - for (const value of [privateKey, storedKey, config, stored]) { + for (const value of [privateKey, storedKey]) { expect(redact.text(value)).toBe("[REDACTED]") } expect(redact.text(JSON.stringify({ key: storedKey }))).toBe('{"key":"[REDACTED]"}') + expect(redact.text(config)).toBe( + JSON.stringify({ type: "bigquery", credentials_json: JSON.stringify({ private_key: "[REDACTED]" }) }), + ) + expect(redact.text(stored)).toBe(JSON.stringify({ private_key: "[REDACTED]", client_email: accountId })) expect(redact.text(clean)).toBe(clean) expect(redact.text(accountId)).toBe(accountId) } finally { @@ -70,72 +76,57 @@ test("collects warehouse credential leaves without changing a password-free conn } }) -test("collects configured and command-resolved MCP authorization without changing ordinary headers", async () => { - const configuredToken = "fixture-configured-token" - const resolvedToken = "fixture-command-resolved-token" - const traceHeader = "fixture-not-a-credential" - const serverName = "fixture-output-redaction" - using server = Bun.serve({ - hostname: "127.0.0.1", - port: 0, - async fetch(request) { - if (request.method !== "POST") return new Response(null, { status: 405 }) - const message = (await request.json()) as { - id?: number - method: string - params?: { protocolVersion: string } - } - if (message.id === undefined) return new Response(null, { status: 202 }) - return Response.json({ - jsonrpc: "2.0", - id: message.id, - result: { - protocolVersion: message.params?.protocolVersion, - capabilities: {}, - serverInfo: { name: serverName, version: "1" }, - }, - }) - }, - }) +test("collects configured and command-resolved MCP authorization in an isolated SDK process", async () => { + // Other MCP suites replace the SDK transports in the parent process. + const child = Bun.spawn( + [process.execPath, "test", path.join(import.meta.dir, "tool-output-mcp-credentials.fixture.ts")], + { cwd: path.resolve(import.meta.dir, "../.."), stdout: "ignore", stderr: "pipe" }, + ) + const [code, stderr] = await Promise.all([child.exited, new Response(child.stderr).text()]) + expect({ code, error: code === 0 ? "" : stderr }).toEqual({ code: 0, error: "" }) +}, 40_000) + +test("collects serialized connection URL credentials without masking ordinary URL fields", async () => { + const envName = "ALTIMATE_CODE_CONN_REDACTION_URL_TEST" + const previous = process.env[envName] + const password = "fixture/url-password" + const encodedPassword = encodeURIComponent(password) + const token = "fixture-url-token" + const connection = `postgres://reader:${encodedPassword}@warehouse.example/db?token=${token}®ion=west` + const clean = "postgres://reader@warehouse.example/db?region=west" + process.env[envName] = JSON.stringify({ type: "postgres", connection_string: connection, host: "warehouse.example" }) + try { + const redact = await toolCredentialRedactor() + expect(redact.text(password)).toBe("[REDACTED]") + expect(redact.text(connection)).toBe( + `postgres://reader:[REDACTED]@warehouse.example/db?token=[REDACTED]®ion=west`, + ) + expect(redact.text(clean)).toBe(clean) + } finally { + if (previous === undefined) delete process.env[envName] + else process.env[envName] = previous + } +}) + +test("collects local warehouse credentials with only InstanceRef context", async () => { + const password = "fixture-instance-ref-password" await using tmp = await tmpdir({ - config: { - mcp: { - configured: { - type: "remote", - enabled: false, - url: String(server.url), - headers: { Authorization: "Bearer " + configuredToken, "X-Trace-Id": traceHeader }, - }, - }, + init: async (directory) => { + const configDir = path.join(directory, ".altimate-code") + await mkdir(configDir) + await Bun.write( + path.join(configDir, "connections.json"), + JSON.stringify({ fixture: { type: "postgres", password } }), + ) }, }) await provideTestInstance({ directory: tmp.path, - fn: (instance) => - Instance.restore(instance, async () => { - await MCP.add(serverName, { - type: "remote", - url: String(server.url), - timeout: 1_000, - headersCommand: { - Authorization: [ - process.execPath, - "-e", - `process.stdout.write("Bearer " + ${JSON.stringify(resolvedToken)})`, - ], - }, - }) - try { - expect((await MCP.status())[serverName]?.status).toBe("connected") - const redact = await toolCredentialRedactor() - for (const value of [configuredToken, resolvedToken, "Bearer " + resolvedToken]) { - expect(redact.text(value)).toBe("[REDACTED]") - } - expect(redact.text(traceHeader)).toBe(traceHeader) - } finally { - await MCP.remove(serverName) - } - expect((await toolCredentialRedactor()).text(resolvedToken)).toBe(resolvedToken) - }), + fn: async (instance) => { + const redact = await Effect.runPromise( + loadToolCredentialRedactor.pipe(Effect.provideService(InstanceRef, instance)), + ) + expect(redact.text(password)).toBe("[REDACTED]") + }, }) -}, 25_000) +}) diff --git a/packages/opencode/test/session/tool-output-credentials.test.ts b/packages/opencode/test/session/tool-output-credentials.test.ts index d772422f1c..2c1877bcbd 100644 --- a/packages/opencode/test/session/tool-output-credentials.test.ts +++ b/packages/opencode/test/session/tool-output-credentials.test.ts @@ -56,10 +56,10 @@ test("collects environment, provider auth and observed warehouse credentials", a await Auth.set(provider, { type: "api", key: authValue }) expect((await Store.getCredential("fixture-connection", "password")) === warehouseValue).toBe(true) const redact = await toolCredentialRedactor() - for (const value of [envValue, authValue, warehouseValue, envPassword, connection]) { + for (const value of [envValue, authValue, warehouseValue, envPassword]) { expect(redact.text(value) === "[REDACTED]").toBe(true) } - expect(redact.text("sample-database")).toBe("sample-database") + expect(redact.text(connection)).toBe(connection.replace(envPassword, "[REDACTED]")) } finally { await Auth.remove(provider) Store.setSecretBackendForTests(undefined) @@ -78,10 +78,11 @@ test("matches alternate JSON escapes without reserializing clean text", () => { test("holds credential fragments out of streamed Bash previews", () => { const redact = createCredentialRedactor(["fixture-key-value"]) - expect(redact.preview("out: fixture-key", 80)).toBe("out: ") + expect(redact.preview("ordinary output: fixture-key", 80)).toBe("ordinary out") expect(redact.preview("out: fixture-key-value", 80)).toBe("out: [REDACTED]") - expect(redact.preview("out: fixture-results", 80)).toBe("out: fixture-results") - expect(redact.preview("line with \\", 80)).toBe("line with \\") + expect(redact.preview("out: fixture-results", 80)).toBe("out:") + expect(redact.text("out: fixture-results")).toBe("out: fixture-results") + expect(redact.preview("line with \\", 80)).toBe("") expect(redact.preview("prefix fixture-key-value", 18)).toBe("prefix [REDACTED]") }) @@ -98,17 +99,17 @@ test("redacts the full union of overlapping exact credential values", () => { expect(redact.window("first-fixture-tail/visible", 14, 26)).toBe("[REDACTED]/visible") }) -test("filters arbitrary structured keys without renaming result protocol fields", () => { +test("preserves names throughout tool result and metadata schemas", () => { const credential = "fixture-header-value" const redact = createCredentialRedactor([credential, "structuredContent", "truncated"]) expect( redact.output({ - structuredContent: { [credential]: "value", "[REDACTED]": "existing" }, + structuredContent: { [credential]: credential, "[REDACTED]": "existing" }, metadata: { truncated: true, [credential]: "detail" }, }), ).toEqual({ - structuredContent: { "[REDACTED] (2)": "value", "[REDACTED]": "existing" }, - metadata: { truncated: true, "[REDACTED]": "detail" }, + structuredContent: { [credential]: "[REDACTED]", "[REDACTED]": "existing" }, + metadata: { truncated: true, [credential]: "detail" }, }) }) diff --git a/packages/opencode/test/session/tool-output-mcp-credentials.fixture.ts b/packages/opencode/test/session/tool-output-mcp-credentials.fixture.ts new file mode 100644 index 0000000000..97df40a30b --- /dev/null +++ b/packages/opencode/test/session/tool-output-mcp-credentials.fixture.ts @@ -0,0 +1,76 @@ +// altimate_change — exercise the real MCP SDK with synthetic credentials. +import { expect, test } from "bun:test" +import { toolCredentialRedactor } from "@/altimate/tool-output-credentials" +import { MCP } from "@/mcp" +import { Instance } from "@/project/instance" +import { provideTestInstance, tmpdir } from "../fixture/fixture" + +test("collects configured and command-resolved MCP authorization without changing ordinary headers", async () => { + const configuredToken = "fixture-configured-token" + const resolvedToken = "fixture-command-resolved-token" + const traceHeader = "fixture-not-a-credential" + const serverName = "fixture-output-redaction" + using server = Bun.serve({ + hostname: "127.0.0.1", + port: 0, + async fetch(request) { + if (request.method !== "POST") return new Response(null, { status: 405 }) + const message = (await request.json()) as { + id?: number + method: string + params?: { protocolVersion: string } + } + if (message.id === undefined) return new Response(null, { status: 202 }) + return Response.json({ + jsonrpc: "2.0", + id: message.id, + result: { + protocolVersion: message.params?.protocolVersion, + capabilities: {}, + serverInfo: { name: serverName, version: "1" }, + }, + }) + }, + }) + await using tmp = await tmpdir({ + config: { + mcp: { + configured: { + type: "remote", + enabled: false, + url: String(server.url), + headers: { Authorization: "Bearer " + configuredToken, "X-Trace-Id": traceHeader }, + }, + }, + }, + }) + await provideTestInstance({ + directory: tmp.path, + fn: (instance) => + Instance.restore(instance, async () => { + await MCP.add(serverName, { + type: "remote", + url: String(server.url), + timeout: 1_000, + headersCommand: { + Authorization: [ + process.execPath, + "-e", + `process.stdout.write("Bearer " + ${JSON.stringify(resolvedToken)})`, + ], + }, + }) + try { + expect((await MCP.status())[serverName]?.status).toBe("connected") + const redact = await toolCredentialRedactor() + for (const value of [configuredToken, resolvedToken, "Bearer " + resolvedToken]) { + expect(redact.text(value)).toBe("[REDACTED]") + } + expect(redact.text(traceHeader)).toBe(traceHeader) + } finally { + await MCP.remove(serverName) + } + expect((await toolCredentialRedactor()).text(resolvedToken)).toBe(resolvedToken) + }), + }) +}, 25_000) diff --git a/packages/opencode/test/skill/release-v0.7.3-adversarial.test.ts b/packages/opencode/test/skill/release-v0.7.3-adversarial.test.ts index ecb06232eb..63ecdd5a7e 100644 --- a/packages/opencode/test/skill/release-v0.7.3-adversarial.test.ts +++ b/packages/opencode/test/skill/release-v0.7.3-adversarial.test.ts @@ -13,7 +13,7 @@ * missing" Bun throws; GitInfo.gitAvailable + gitError surface the * three states (missing / corrupted / not-a-repo) distinctly. remoteUrl * now strips embedded HTTPS userinfo (creds-leak hardening, pre-release). - * gitError.stderr is masked via Telemetry.maskString before persisting + * gitError.stderr is capped after masking * (PII hardening, pre-release). * * 3. build agent name normalization (#833). normalizeAgentName helper at @@ -172,13 +172,7 @@ describe("project_scan privacy hardening", () => { expect(PROJECT_SCAN_SRC).toMatch(/parsed\.password\s*=\s*""/) }) - test("gitError.stderr is masked via Telemetry.maskString before persisting", () => { - // Raw git stderr contains paths-with-usernames, commit emails on bad-object - // errors, and unmasked HTTPS remote URLs on auth failures. Pre-release - // fix routes it through the existing masker. - expect(PROJECT_SCAN_SRC).toMatch(/Telemetry\.maskString\(\s*isRepoResult\.stderr/) - }) - + // altimate_change — tool/project-scan.test.ts checks actual stderr masking. test("gitError.stderr is length-capped after masking (not before)", () => { // Mask first, then slice — otherwise we could slice mid-mask-replacement // and surface a half-redacted email. The mask replacements can shift diff --git a/packages/opencode/test/tool/credential-errors.test.ts b/packages/opencode/test/tool/credential-errors.test.ts new file mode 100644 index 0000000000..bb54770d20 --- /dev/null +++ b/packages/opencode/test/tool/credential-errors.test.ts @@ -0,0 +1,58 @@ +// altimate_change — synthetic coverage for thrown tool errors. +import { expect, spyOn, test } from "bun:test" +import { Effect, Schema } from "effect" +import { Tool } from "@/tool/tool" +import { Telemetry } from "@/altimate/telemetry" +import { MessageID, SessionID } from "@/session/schema" +import { initTool } from "../altimate/tool-fixture" + +for (const kind of ["failure", "defect"] as const) { + test(`Tool.define redacts newly learned credentials in ${kind} and telemetry`, async () => { + const key = "ALTIMATE_FIXTURE_THROWN_TOKEN" + const prior = process.env[key] + const secret = "synthetic-learned-error-credential" + const events: Parameters[0][] = [] + const track = spyOn(Telemetry, "track").mockImplementation((event) => { + events.push(event) + }) + try { + delete process.env[key] + const tool = await initTool( + Tool.define( + "credential_error_fixture", + Effect.succeed({ + description: "Synthetic error fixture", + parameters: Schema.Struct({}), + execute: () => + Effect.gen(function* () { + process.env[key] = secret + const error = new Error(`Fixture failed: ${secret}`, { cause: new Error(secret) }) + return yield* kind === "failure" ? (Effect.fail(error) as Effect.Effect) : Effect.die(error) + }), + }), + ), + ) + const error = await tool + .execute( + {}, + { + sessionID: SessionID.make("ses_fixture"), + messageID: MessageID.make("msg_fixture"), + agent: "build", + abort: new AbortController().signal, + messages: [], + }, + ) + .catch((error) => error) + expect(String(error)).toContain("Fixture failed: [REDACTED]") + expect(String(error)).not.toContain(secret) + expect((error as Error).cause).toMatchObject({ message: "[REDACTED]" }) + expect(JSON.stringify(events)).not.toContain(secret) + expect(events.some((event) => event.type === "tool_call" && event.error?.includes("[REDACTED]"))).toBe(true) + } finally { + track.mockRestore() + if (prior === undefined) delete process.env[key] + else process.env[key] = prior + } + }) +} diff --git a/packages/opencode/test/tool/project-scan.test.ts b/packages/opencode/test/tool/project-scan.test.ts index 81549d77b8..26c19b21d8 100644 --- a/packages/opencode/test/tool/project-scan.test.ts +++ b/packages/opencode/test/tool/project-scan.test.ts @@ -169,6 +169,12 @@ describe("detectGit", () => { const redact = createCredentialRedactor([value]) const spawn = spyOn(Bun, "spawnSync") try { + spawn.mockReturnValueOnce({ + exitCode: 2, + stdout: Buffer.alloc(0), + stderr: Buffer.from("failed for reader@example.test"), + }) + expect((await detectGit()).gitError?.stderr).not.toContain("reader@example.test") for (const cap of [120, 240]) { const prefix = "x".repeat(cap - 11) + " " const fixture = { exitCode: 2, stdout: Buffer.alloc(0), stderr: Buffer.from(prefix + value) } From 31078cca44599608712b708f9b91f7c61041dd31 Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sun, 22 Nov 2026 01:00:27 -0800 Subject: [PATCH 8/9] fix: handle credential redaction edge cases --- packages/core/test/ripgrep.test.ts | 48 +++++++- .../src/altimate/tool-output-credentials.ts | 111 ++++++++++++++--- packages/opencode/src/mcp/auth.ts | 2 + packages/opencode/src/session/message-v2.ts | 13 +- packages/opencode/src/tool/apply_patch.ts | 5 +- packages/opencode/src/tool/edit.ts | 24 ++++ packages/opencode/src/tool/tool.ts | 10 +- packages/opencode/src/tool/write.ts | 3 +- packages/opencode/test/session/prompt.test.ts | 9 +- .../session/tool-credential-output.test.ts | 27 +++++ .../tool-output-credential-sources.test.ts | 106 +++++++++++++++- .../session/tool-output-credentials.test.ts | 10 +- .../tool-output-mcp-credentials.fixture.ts | 18 +-- .../opencode/test/tool/apply_patch.test.ts | 31 +++++ .../test/tool/credential-errors.test.ts | 114 +++++++++++++++++- .../opencode/test/tool/project-scan.test.ts | 2 +- packages/opencode/test/tool/write.test.ts | 60 +++++++++ 17 files changed, 547 insertions(+), 46 deletions(-) diff --git a/packages/core/test/ripgrep.test.ts b/packages/core/test/ripgrep.test.ts index f2953973cb..b657a3a54c 100644 --- a/packages/core/test/ripgrep.test.ts +++ b/packages/core/test/ripgrep.test.ts @@ -131,7 +131,14 @@ describe("Ripgrep", () => { ) yield* Effect.promise(() => fs.writeFile(path.join(tmp.path, "c-plain.txt"), "needle here\n")) - const matches = yield* (yield* Ripgrep.Service).grep({ cwd: tmp.path, pattern: "needle", limit: 10 }) + const source: Ripgrep.GrepSourceLine[] = [] + const matches = yield* (yield* Ripgrep.Service).grep({ + cwd: tmp.path, + pattern: "needle", + limit: 10, + sourceContext: { lookaround: 64, onMatch: (line) => source.push(line) }, + }) + expect(source).toEqual(matches.map((match) => ({ head: match.text, visibleEnd: match.text.length }))) // The non-UTF8 file is reported like any other rather than dropped or fatal. expect(matches.map((item) => item.entry.path).sort()).toEqual([ @@ -196,7 +203,7 @@ describe("Ripgrep", () => { // risk is a Windows-only spawn/quoting regression going unnoticed rather than a parsing one. const stubTest = bunTest.skipIf(process.platform === "win32") - const grepWithStubbedRecords = (records: string[]) => + const grepWithStubbedRecords = (records: string[], options: Partial = {}) => Effect.acquireUseRelease( Effect.promise(() => tmpdir()), (tmp) => @@ -209,7 +216,7 @@ describe("Ripgrep", () => { return yield* Effect.gen(function* () { const rg = yield* Ripgrep.Service - return yield* rg.grep({ cwd: tmp.path, pattern: "needle", limit: 100 }) + return yield* rg.grep({ cwd: tmp.path, pattern: "needle", limit: 100, ...options }) }).pipe( Effect.provide( Ripgrep.layer.pipe( @@ -232,6 +239,41 @@ describe("Ripgrep", () => { /** The single aggregate warning for the last search, or undefined when nothing was skipped. */ const lastSkip = () => skipWarnings.at(-1) + stubTest("keeps source context aligned with retained matches after rejected and filtered records", async () => { + const source: Ripgrep.GrepSourceLine[] = [] + const longLine = "needle".padEnd(1_996, "x") + "fixture-overlapping-credential\n" + const decodedLine = "needle \ufffd tail\n" + const matches = await Effect.runPromise( + grepWithStubbedRecords( + [ + matchRecord("a.txt", { lines: { text: longLine } }), + // No source callback is possible, but these cannot pass RawMatch either. + matchRecord("bad-lines.txt", { lines: { text: 42 } }), + matchRecord("bad-bytes.txt", { lines: { bytes: "!!!" } }), + // Normalization captures source, but schema rejection must not publish it. + matchRecord("bad-line-number.txt", { line_number: 0 }), + matchRecord("filtered.txt"), + matchRecord("b.txt", { lines: { bytes: Buffer.from("needle \xff tail\n", "binary").toString("base64") } }), + matchRecord("past-limit.txt"), + ], + { + limit: 2, + filterPath: (relative) => !relative.endsWith("filtered.txt"), + sourceContext: { lookaround: 64, onMatch: (line) => source.push(line) }, + }, + ), + ) + + expect(matches.map((item) => item.entry.path)).toEqual([RelativePath.make("a.txt"), RelativePath.make("b.txt")]) + expect(source).toEqual([ + { head: longLine, visibleEnd: 2_000 }, + { head: decodedLine, visibleEnd: decodedLine.length }, + ]) + expect(matches[0].text).toBe(longLine.slice(0, 2_000) + "...") + expect(matches[1].text).toBe(decodedLine) + expect(lastSkip()?.skipped).toBe(3) + }) + stubTest("skips an unparseable record without failing the search", async () => { const matches = await Effect.runPromise( grepWithStubbedRecords([ diff --git a/packages/opencode/src/altimate/tool-output-credentials.ts b/packages/opencode/src/altimate/tool-output-credentials.ts index 94054c477b..a72e3c7bd2 100644 --- a/packages/opencode/src/altimate/tool-output-credentials.ts +++ b/packages/opencode/src/altimate/tool-output-credentials.ts @@ -1,5 +1,7 @@ // altimate_change — redact only exact known credential values in tool results. import { Effect } from "effect" +import { stat } from "node:fs/promises" +import { createHash } from "node:crypto" import { InstanceRef } from "@/effect/instance-ref" import { attach } from "@/effect/run-service" import { withAlsInstanceRef } from "@/altimate/tool-zod-compat" @@ -134,12 +136,25 @@ export function createCredentialRedactor(values: Iterable = // Binary attachments must stay valid. When their transport or decoded bytes // contain an exact credential, omit the bytes and leave a text placeholder. let binaryLiterals: Buffer[] | undefined + const binaryMatches = new Map() function containsCredentialBytes(encoded: string) { if (!encoded || !pattern) return false - if (text(encoded) !== encoded) return true + // Replay may allocate new attachment objects on every turn. Cache only a + // digest and a match bit, without retaining the attachment itself. + const digest = encoded.length >= 4096 ? createHash("sha256").update(encoded).digest("hex") : undefined + const cached = digest ? binaryMatches.get(digest) : undefined + if (cached !== undefined) return cached binaryLiterals ??= ordered.map((literal) => Buffer.from(literal, "utf8")) - const bytes = Buffer.from(encoded, "base64") - return binaryLiterals.some((literal) => bytes.includes(literal)) + let contains = text(encoded) !== encoded + if (!contains) { + const bytes = Buffer.from(encoded, "base64") + contains = binaryLiterals.some((literal) => bytes.includes(literal)) + } + if (digest) { + if (binaryMatches.size >= 64) binaryMatches.clear() + binaryMatches.set(digest, contains) + } + return contains } const omitAttachment = Symbol("omitAttachment") @@ -192,9 +207,11 @@ export function createCredentialRedactor(values: Iterable = } if (value instanceof Error) { const descriptors = Object.getOwnPropertyDescriptors(value) - const next = Object.create(Object.getPrototypeOf(value)) + // DOMException's name and message live in native slots, not own descriptors. + const dom = value instanceof DOMException ? new DOMException(text(value.message), text(value.name)) : undefined + const next = dom ?? Object.create(Object.getPrototypeOf(value)) seen.set(value, next) - let changed = false + let changed = !!dom && (dom.message !== value.message || dom.name !== value.name) for (const descriptor of Object.values(descriptors)) { if (!("value" in descriptor)) continue const safe = visit(descriptor.value, seen, "data") @@ -209,7 +226,7 @@ export function createCredentialRedactor(values: Iterable = const plain = prototype === Object.prototype || prototype === null // SDK media and native values such as dates and typed arrays retain their representation. if (!plain && Object.prototype.toString.call(value) !== "[object Object]") return value - const next = Object.create(plain ? prototype : Object.prototype) + const next = Object.create(prototype) seen.set(value, next) let changed = false for (const [key, item] of Object.entries(value)) { @@ -290,14 +307,20 @@ type AddCredential = (value: unknown) => void // Provider loaders use these environment names outside their catalog declarations. const providerCredentialVariables = new Set(["AICORE_SERVICE_KEY", "AWS_BEARER_TOKEN_BEDROCK", "AWS_SECRET_ACCESS_KEY"]) +const databaseCredentialVariables = new Set(["PGPASSWORD", "MYSQL_PWD"]) +const providerLocationVariables = /(?:^|_)(?:REGION|LOCATION|PROJECT|PROJECT_ID|ENDPOINT|BASE_URL|HOST)$/i +const publicCertificateFields = new Set(["ssl_cert", "ssl_ca", "tls_cert", "tls_ca_cert"]) function isCredentialName(name: string) { - return /(?:^|_)(?:API_KEY|TOKEN|SECRET|PASSWORD|PASSWD|AUTHORIZATION)$/i.test(name.replaceAll("-", "_")) + return ( + databaseCredentialVariables.has(name) || + /(?:^|_)(?:API_KEY|TOKEN|SECRET|PASSWORD|PASSWD|PRIVATE_KEY|AUTHORIZATION)$/i.test(name.replaceAll("-", "_")) + ) } function addCredentialHeaders(headers: Record | undefined, add: AddCredential) { for (const [name, value] of Object.entries(headers ?? {})) { - if (!/(?:^|[-_])(?:authorization|api[-_]key|auth[-_]token|access[-_]token|client[-_]secret)$/i.test(name)) { + if (!/(?:^|[-_])(?:authorization|api[-_]?key|(?:auth|access|api|private)[-_]token|client[-_]secret)$/i.test(name)) { continue } if (typeof value !== "string") continue @@ -328,7 +351,7 @@ function readJson(value: string): unknown { } function addConnectionCredential(name: string, value: unknown, add: AddCredential) { - if (typeof value !== "string" || !value) return + if (typeof value !== "string" || !value || publicCertificateFields.has(name)) return if (name === "credentials_json" || name === "keyfile_json") { const fields = readJson(value) if (fields && typeof fields === "object") { @@ -336,8 +359,8 @@ function addConnectionCredential(name: string, value: unknown, add: AddCredentia return } } - if (name === "connection_string" && value.includes("://")) { - const url = URL.parse(value) + if (name === "connection_string") { + const url = value.includes("://") ? URL.parse(value) : null if (url) { if (url.password) { add(url.password) @@ -350,13 +373,34 @@ function addConnectionCredential(name: string, value: unknown, add: AddCredentia for (const query of url.search.slice(1).split("&")) { for (const [key, field] of new URLSearchParams(query)) { const normalized = key.toLowerCase() - if (!isSensitiveField(normalized) && !isCredentialName(normalized)) continue + if (normalized !== "pwd" && !isSensitiveField(normalized) && !isCredentialName(normalized)) continue add(field) add(query.slice(query.indexOf("=") + 1)) } } return } + // Connection attributes can be unquoted, quoted or ODBC-braced. Parse + // only when the entire value is an attribute list. + const attributes = [ + ...value.matchAll(/\s*([\w -]{1,80}?)\s*=\s*(\{(?:[^}]|}})*}|"(?:[^"]|"")*"|'(?:[^']|'')*'|[^;]*?)\s*(?:;|$)/gy), + ] + if (attributes.length && attributes.map((match) => match[0]).join("") === value) { + for (const [, key, field] of attributes) { + const normalized = key.trim().toLowerCase() + if (normalized !== "pwd" && !isSensitiveField(normalized) && !isCredentialName(normalized)) continue + const delimiter = field[0] + const closing = delimiter === "{" ? "}" : delimiter + if (["{", '"', "'"].includes(delimiter) && field.endsWith(closing)) { + const contents = field.slice(1, -1) + add(contents) + add(contents.replaceAll(closing + closing, closing)) + } else { + add(field) + } + } + return + } } add(value) } @@ -369,6 +413,31 @@ function addConnectionFields(value: unknown, add: AddCredential) { } } +// The two auth stores are shared across instances. Reuse their last stable +// snapshots, and refresh when a writer or another process changes the file. +function authFileSnapshot() { + let cached: { version: string; data: Promise } | undefined + return async (file: string, load: () => Promise) => { + const info = await stat(file, { bigint: true }).catch(() => undefined) + if (!info) { + cached = undefined + return undefined + } + const version = `${file}:${info.ino}:${info.size}:${info.mtimeNs}:${info.ctimeNs}` + if (cached?.version === version) return cached.data + const data = load().catch(() => { + if (cached?.data === data) cached = undefined + return undefined + }) + cached = { version, data } + return data + } +} + +const readProviderAuth = authFileSnapshot>>() +const readMcpAuth = authFileSnapshot>>() +let lastSnapshot: { values: Set; redactor: CredentialRedactor } | undefined + export const loadToolCredentialRedactor = withAlsInstanceRef( Effect.gen(function* () { // Message projection is below configuration and the connection registry in the import graph. @@ -390,14 +459,18 @@ export const loadToolCredentialRedactor = withAlsInstanceRef( const [config, auth, mcpAuth] = yield* Effect.promise(() => Promise.all([ instance ? Config.get().catch(() => undefined) : undefined, - Auth.all().catch(() => undefined), - McpAuth.all().catch(() => undefined), + process.env.OPENCODE_AUTH_CONTENT + ? Auth.all().catch(() => undefined) + : readProviderAuth(Auth.filepath(), Auth.all), + readMcpAuth(McpAuth.credentialFile, McpAuth.all), ]), ) const environment = [...Object.entries(process.env), ...Object.entries(instance ? Env.all() : {})] const configuredVariables = new Set() for (const provider of Object.values(config?.provider ?? {})) { - for (const name of provider.env ?? []) configuredVariables.add(name) + for (const name of provider.env ?? []) { + if (!providerLocationVariables.test(name)) configuredVariables.add(name) + } addProviderOptions(provider.options, add) for (const model of Object.values(provider.models ?? {})) { addProviderOptions(model.options, add) @@ -443,7 +516,13 @@ export const loadToolCredentialRedactor = withAlsInstanceRef( for (const connection of connections) addConnectionFields(connection, add) } for (const credential of knownStoredCredentials()) addConnectionCredential(credential.field, credential.value, add) - return createCredentialRedactor(values) + const previous = lastSnapshot + if (previous && previous.values.size === values.size && [...values].every((value) => previous.values.has(value))) { + return previous.redactor + } + const redactor = createCredentialRedactor(values) + lastSnapshot = { values, redactor } + return redactor }), ) diff --git a/packages/opencode/src/mcp/auth.ts b/packages/opencode/src/mcp/auth.ts index 423d96e2cb..ddb6f81460 100644 --- a/packages/opencode/src/mcp/auth.ts +++ b/packages/opencode/src/mcp/auth.ts @@ -213,6 +213,8 @@ export const node = LayerNode.make(layer, () => [FSUtil.node, EffectFlock.node]) // altimate_change start — expose stored credentials without starting MCP const run = makeRuntime(Service, defaultLayer) +export const credentialFile = filepath + export function all() { return run.runPromise((service) => service.all()) } diff --git a/packages/opencode/src/session/message-v2.ts b/packages/opencode/src/session/message-v2.ts index af29b6d6f9..12a87771ba 100644 --- a/packages/opencode/src/session/message-v2.ts +++ b/packages/opencode/src/session/message-v2.ts @@ -909,7 +909,15 @@ export namespace MessageV2 { ? storedMask : "[Old tool result content cleared]" : part.state.output - const rawOutputText = redact?.output(storedOutputText) ?? storedOutputText + // Redact as one result envelope so omitted credential-bearing media + // leaves the same visible note as newly produced tool results. + const storedAttachments = + part.state.time.compacted || options?.stripMedia ? [] : (part.state.attachments ?? []) + const safeResult = redact?.output({ output: storedOutputText, attachments: storedAttachments }) ?? { + output: storedOutputText, + attachments: storedAttachments, + } + const rawOutputText = safeResult.output const maxChars = options?.toolOutputMaxChars const outputText = !part.state.time.compacted && maxChars !== undefined && rawOutputText.length > maxChars @@ -919,8 +927,7 @@ export namespace MessageV2 { : rawOutputText // altimate_change end // altimate_change start — stored attachment references may also contain credentials - const storedAttachments = part.state.time.compacted || options?.stripMedia ? [] : (part.state.attachments ?? []) - const attachments = redact?.output(storedAttachments) ?? storedAttachments + const attachments = safeResult.attachments // altimate_change end // For providers that don't support media in tool results, extract media files diff --git a/packages/opencode/src/tool/apply_patch.ts b/packages/opencode/src/tool/apply_patch.ts index 4645bbcbb4..9a7171f1df 100644 --- a/packages/opencode/src/tool/apply_patch.ts +++ b/packages/opencode/src/tool/apply_patch.ts @@ -7,7 +7,7 @@ import { InstanceState } from "@/effect/instance-state" import { Patch } from "../patch" import { diffLines } from "diff" import { assertExternalDirectoryEffect, assertSensitiveWriteEffect } from "./external-directory" -import { toolFileDiff } from "./edit" // altimate_change — redact diff source before rendering +import { assertNoCredentialPlaceholderOverwrite, toolFileDiff } from "./edit" // altimate_change — redact diff source before rendering import { LSP } from "@/lsp/lsp" import { FSUtil } from "@opencode-ai/core/fs-util" import DESCRIPTION from "./apply_patch.txt" @@ -78,10 +78,11 @@ export const ApplyPatchTool = Tool.define( switch (hunk.type) { case "add": { - const oldContent = "" + const oldContent = (yield* afs.existsSafe(filePath)) ? (yield* Bom.readFile(afs, filePath)).text : "" // altimate_change const newContent = hunk.contents.length === 0 || hunk.contents.endsWith("\n") ? hunk.contents : `${hunk.contents}\n` const next = Bom.split(newContent) + assertNoCredentialPlaceholderOverwrite(oldContent, next.text, ctx.redactCredentials) // altimate_change const diff = toolFileDiff(filePath, oldContent, next.text, ctx.redactCredentials) let additions = 0 diff --git a/packages/opencode/src/tool/edit.ts b/packages/opencode/src/tool/edit.ts index e5ff616890..78b6c41ed3 100644 --- a/packages/opencode/src/tool/edit.ts +++ b/packages/opencode/src/tool/edit.ts @@ -675,6 +675,30 @@ export const ContextAwareReplacer: Replacer = function* (content, find) { } } +// altimate_change start — refuse whole-file rewrites with credential placeholders. +// Targeted edits can +// still change unrelated lines without copying credential placeholders to disk. +export function assertNoCredentialPlaceholderOverwrite( + before: string, + after: string, + redact?: Tool.Context["redactCredentials"], +) { + if (!redact || !after.includes("[REDACTED]")) return + const changes = diffLines(before, after) + if (!changes.some((change) => change.added && change.value.includes("[REDACTED]"))) return + let offset = 0 + for (const change of changes) { + if (change.added) continue + const start = offset + offset += change.value.length + if (!change.removed || redact.window(before, start, offset) === change.value) continue + throw new Error( + "Refusing to overwrite a known credential with a [REDACTED] placeholder. Use a targeted edit that preserves credential lines.", + ) + } +} +// altimate_change end + // altimate_change start — preserve diff structure while masking lines from the original source export function toolFileDiff(path: string, before: string, after: string, redact?: Tool.Context["redactCredentials"]) { const safeBefore = redact?.lines(before) ?? before diff --git a/packages/opencode/src/tool/tool.ts b/packages/opencode/src/tool/tool.ts index 6ab6ce50d7..5e16bb05eb 100644 --- a/packages/opencode/src/tool/tool.ts +++ b/packages/opencode/src/tool/tool.ts @@ -379,10 +379,16 @@ function wrap, Result extends Metadat Effect.gen(function* () { const fresh = yield* loadToolCredentialRedactor const safeCause = redactFailureCause(redactFailureCause(cause, redact), fresh) - const error = Cause.squash(safeCause) + return yield* Effect.failCause(safeCause) + }), + ), + // onError is an uninterruptible finalizer: cancellation must close the + // trace even when the asynchronous failure-redaction recovery is skipped. + Effect.onError((cause) => + Effect.sync(() => { + const error = Cause.squash(redactFailureCause(cause, redact)) endTrace("error", error instanceof Error ? error.message : String(error)) altimateTrackError(id, decoded, ctx, startTime, error) - return yield* Effect.failCause(safeCause) }), ), ) diff --git a/packages/opencode/src/tool/write.ts b/packages/opencode/src/tool/write.ts index 2631113037..dbd812cd72 100644 --- a/packages/opencode/src/tool/write.ts +++ b/packages/opencode/src/tool/write.ts @@ -10,7 +10,7 @@ import { Watcher } from "@opencode-ai/core/filesystem/watcher" import { Format } from "../format" import { FSUtil } from "@opencode-ai/core/fs-util" import { InstanceState } from "@/effect/instance-state" -import { toolFileDiff } from "./edit" // altimate_change — redact diff source before rendering +import { assertNoCredentialPlaceholderOverwrite, toolFileDiff } from "./edit" // altimate_change — redact diff source before rendering import { assertExternalDirectoryEffect, assertSensitiveWriteEffect } from "./external-directory" import * as Bom from "@/util/bom" // altimate_change start — upstream_fix: restore stale-file guard (dropped in v1.17.9 merge) @@ -79,6 +79,7 @@ export const WriteTool = Tool.define( const contentOld = source.text const contentNew = next.text + assertNoCredentialPlaceholderOverwrite(contentOld, contentNew, ctx.redactCredentials) // altimate_change const diff = toolFileDiff(filepath, contentOld, contentNew, ctx.redactCredentials) // altimate_change yield* ctx.ask({ permission: "edit", diff --git a/packages/opencode/test/session/prompt.test.ts b/packages/opencode/test/session/prompt.test.ts index e1461859d4..6e3440d78d 100644 --- a/packages/opencode/test/session/prompt.test.ts +++ b/packages/opencode/test/session/prompt.test.ts @@ -43,6 +43,7 @@ import { SessionSummary } from "../../src/session/summary" import { Instruction } from "../../src/session/instruction" import { SessionProcessor } from "../../src/session/processor" import { SessionPrompt } from "../../src/session/prompt" +import { loadToolCredentialRedactor } from "@/altimate/tool-output-credentials" // altimate_change import { SessionRevert } from "../../src/session/revert" import { SessionRunState } from "../../src/session/run-state" import { MessageID, PartID, SessionID } from "../../src/session/schema" @@ -2720,12 +2721,14 @@ unixNoLLMServer( Effect.gen(function* () { const { prompt, chat } = yield* boot() const instance = yield* TestInstance + const redact = yield* loadToolCredentialRedactor + const padding = Math.max(1, redact.windowLookaround / 6) const fiber = yield* prompt .shell({ sessionID: chat.id, agent: "build", - command: 'printf first; while [ ! -f shell-progress-release ]; do sleep 0.02; done; printf second', + command: `printf 'first%${padding}s' ''; while [ ! -f shell-progress-release ]; do sleep 0.02; done; printf second`, }) .pipe(Effect.forkChild) @@ -2734,14 +2737,14 @@ unixNoLLMServer( const msgs = yield* MessageV2.filterCompactedEffect(chat.id) const taskMsg = msgs.find((item) => item.info.role === "assistant") const tool = taskMsg ? toolPart(taskMsg.parts) : undefined - if (tool?.state.status === "running" && typeof tool.state.metadata?.output === "string") return true + if (tool?.state.status === "running" && tool.state.metadata?.output?.startsWith("first")) return true }), "timed out waiting for running shell metadata", ) yield* Effect.promise(() => Bun.write(path.join(instance.directory, "shell-progress-release"), "ready")) const result = yield* Fiber.join(fiber) - expect(completedTool(result.parts)?.state.output).toBe("firstsecond") + expect(completedTool(result.parts)?.state.output).toBe("first" + " ".repeat(padding) + "second") }), ), { config: cfg }, diff --git a/packages/opencode/test/session/tool-credential-output.test.ts b/packages/opencode/test/session/tool-credential-output.test.ts index f19e14410b..ba7654db56 100644 --- a/packages/opencode/test/session/tool-credential-output.test.ts +++ b/packages/opencode/test/session/tool-credential-output.test.ts @@ -160,3 +160,30 @@ test("bash progress holds a split credential until the next chunk and redacts fi expect(result.metadata.output).toBe(safeOutput) }) }) + +test("model replay marks an omitted credential-bearing attachment", async () => { + await inConfiguredInstance(async (directory) => { + const history = rawHistory(directory) + const part = history[1].parts[0] as MessageV2.ToolPart + if (part.state.status !== "completed") throw new Error("expected completed fixture") + const encoded = Buffer.from(configuredKey).toString("base64") + part.state.output = "Attachment follows" + part.state.attachments = [ + { + id: PartID.ascending(), + sessionID: part.sessionID, + messageID: part.messageID, + type: "file", + mime: "image/png", + filename: "fixture.png", + url: "data:image/png;base64," + encoded, + }, + ] + const model = await Provider.getModel(providerID, modelID) + const request = JSON.stringify(await MessageV2.toModelMessages(history, model)) + expect(request).toContain("Attachment follows\\n[REDACTED]") + expect(request).not.toContain(encoded) + expect(request).not.toContain(configuredKey) + expect(part.state.attachments).toHaveLength(1) + }) +}) diff --git a/packages/opencode/test/session/tool-output-credential-sources.test.ts b/packages/opencode/test/session/tool-output-credential-sources.test.ts index 621769db7c..9727576f05 100644 --- a/packages/opencode/test/session/tool-output-credential-sources.test.ts +++ b/packages/opencode/test/session/tool-output-credential-sources.test.ts @@ -3,10 +3,11 @@ import path from "node:path" import { mkdir } from "node:fs/promises" import { Effect } from "effect" import { InstanceRef } from "@/effect/instance-ref" -import { expect, test } from "bun:test" +import { expect, spyOn, test } from "bun:test" import { loadToolCredentialRedactor, toolCredentialRedactor } from "@/altimate/tool-output-credentials" import * as Store from "@/altimate/native/connections/credential-store" import { AppRuntime } from "@/effect/app-runtime" +import { Instance } from "@/project/instance" import { McpAuth } from "@/mcp/auth" import { provideTestInstance, tmpdir } from "../fixture/fixture" @@ -18,6 +19,7 @@ test("collects provider token environment names and MCP OAuth credentials", asyn const clientId = "fixture-mcp-client-id" const entry = "fixture-output-redaction" const previous = process.env.CF_AIG_TOKEN + const reads = spyOn(McpAuth, "all") process.env.CF_AIG_TOKEN = providerToken try { await AppRuntime.runPromise( @@ -28,7 +30,16 @@ test("collects provider token environment names and MCP OAuth credentials", asyn expect(redact.text(value)).toBe("[REDACTED]") } expect(redact.text(clientId)).toBe(clientId) + expect(await toolCredentialRedactor()).toBe(redact) + expect(reads).toHaveBeenCalledTimes(1) + const rotated = "fixture-mcp-rotated-token" + await AppRuntime.runPromise(McpAuth.use.set(entry, { tokens: { accessToken: rotated } })) + const updated = await toolCredentialRedactor() + expect(updated.text(rotated)).toBe("[REDACTED]") + expect(updated.text(accessToken)).toBe(accessToken) + expect(reads).toHaveBeenCalledTimes(2) } finally { + reads.mockRestore() if (previous === undefined) delete process.env.CF_AIG_TOKEN else process.env.CF_AIG_TOKEN = previous await AppRuntime.runPromise(McpAuth.use.remove(entry)) @@ -80,11 +91,16 @@ test("collects configured and command-resolved MCP authorization in an isolated // Other MCP suites replace the SDK transports in the parent process. const child = Bun.spawn( [process.execPath, "test", path.join(import.meta.dir, "tool-output-mcp-credentials.fixture.ts")], - { cwd: path.resolve(import.meta.dir, "../.."), stdout: "ignore", stderr: "pipe" }, + { + cwd: path.resolve(import.meta.dir, "../.."), + stdout: "ignore", + stderr: "pipe", + signal: AbortSignal.timeout(65_000), + }, ) const [code, stderr] = await Promise.all([child.exited, new Response(child.stderr).text()]) expect({ code, error: code === 0 ? "" : stderr }).toEqual({ code: 0, error: "" }) -}, 40_000) +}, 70_000) test("collects serialized connection URL credentials without masking ordinary URL fields", async () => { const envName = "ALTIMATE_CODE_CONN_REDACTION_URL_TEST" @@ -130,3 +146,87 @@ test("collects local warehouse credentials with only InstanceRef context", async }, }) }) + +test("collects warehouse credentials but preserves public certificate settings", async () => { + const fixtures = { + PGPASSWORD: "fixture-postgres-credential", + MYSQL_PWD: "fixture-mysql-credential", + FIXTURE_PRIVATE_KEY: "fixture-environment-private-key", + ALTIMATE_CODE_CONN_FIXTURE_DSN: JSON.stringify({ + type: "fixture", + connection_string: 'Server=warehouse.example;Pwd={fixture;odbc}}password};Token="fixture-token";', + ssl_cert: "/fixture/client-cert.pem", + ssl_ca: "/fixture/root-ca.pem", + tls_ca_cert: "/fixture/tls-ca.pem", + }), + } + const previous = Object.keys(fixtures).map((key) => [key, process.env[key]] as const) + Object.assign(process.env, fixtures) + try { + const redact = await toolCredentialRedactor() + for (const value of [ + fixtures.PGPASSWORD, + fixtures.MYSQL_PWD, + fixtures.FIXTURE_PRIVATE_KEY, + "fixture;odbc}password", + "fixture-token", + ]) { + expect(redact.text(value)).toBe("[REDACTED]") + } + const config = JSON.parse(fixtures.ALTIMATE_CODE_CONN_FIXTURE_DSN) + expect(redact.text(config.connection_string)).toBe('Server=warehouse.example;Pwd={[REDACTED]};Token="[REDACTED]";') + for (const field of [config.ssl_cert, config.ssl_ca, config.tls_ca_cert]) { + expect(redact.text(field)).toBe(field) + } + } finally { + for (const [key, value] of previous) { + if (value === undefined) delete process.env[key] + else process.env[key] = value + } + } +}) + +test("preserves provider locations and collects explicit auth aliases and MCP header credentials", async () => { + const variables = { FIXTURE_AUTH_ALIAS: "fixture-provider-alias", GOOGLE_VERTEX_LOCATION: "fixture-central1" } + const previous = Object.keys(variables).map((key) => [key, process.env[key]] as const) + Object.assign(process.env, variables) + const headers = { + apikey: "fixture-header-api", + "Private-Token": "fixture-header-private", + "X-Api-Token": "fixture-header-token", + "X-Trace-Id": "fixture-header-trace", + } + try { + await using tmp = await tmpdir({ + config: { + provider: { + fixture: { env: ["FIXTURE_AUTH_ALIAS", "GOOGLE_VERTEX_LOCATION"] }, + }, + mcp: { + fixture: { type: "remote", enabled: false, url: "https://fixture.invalid", headers }, + }, + }, + }) + await Instance.provide({ + directory: tmp.path, + fn: async () => { + try { + const redact = await toolCredentialRedactor() + expect(redact.text(variables.FIXTURE_AUTH_ALIAS)).toBe("[REDACTED]") + expect(redact.text(variables.GOOGLE_VERTEX_LOCATION)).toBe(variables.GOOGLE_VERTEX_LOCATION) + expect(redact.text(headers.apikey)).toBe("[REDACTED]") + expect(redact.text(headers["Private-Token"])).toBe("[REDACTED]") + expect(redact.text(headers["X-Api-Token"])).toBe("[REDACTED]") + expect(redact.text(headers["X-Trace-Id"])).toBe(headers["X-Trace-Id"]) + } finally { + await Instance.dispose() + } + }, + }) + } finally { + for (const [key, value] of previous) { + if (value === undefined) delete process.env[key] + else process.env[key] = value + } + } +}) diff --git a/packages/opencode/test/session/tool-output-credentials.test.ts b/packages/opencode/test/session/tool-output-credentials.test.ts index 2c1877bcbd..3b77981c12 100644 --- a/packages/opencode/test/session/tool-output-credentials.test.ts +++ b/packages/opencode/test/session/tool-output-credentials.test.ts @@ -116,13 +116,17 @@ test("preserves names throughout tool result and metadata schemas", () => { test("filters serializable plugin class fields and retains clean instances", () => { class Details { constructor(readonly token: string) {} + detail() { + return this.token + } } const value = "fixture-class-value" const redact = createCredentialRedactor([value]) - expect(JSON.stringify(redact.output({ metadata: { details: new Details(value) } }))).toBe( - '{"metadata":{"details":{"token":"[REDACTED]"}}}', - ) + const result = redact.output({ metadata: { details: new Details(value) } }) + expect(result.metadata.details).toBeInstanceOf(Details) + expect(result.metadata.details.detail()).toBe("[REDACTED]") + expect(JSON.stringify(result)).toBe('{"metadata":{"details":{"token":"[REDACTED]"}}}') const clean = { metadata: { details: new Details("ordinary detail") } } expect(redact.output(clean)).toBe(clean) }) diff --git a/packages/opencode/test/session/tool-output-mcp-credentials.fixture.ts b/packages/opencode/test/session/tool-output-mcp-credentials.fixture.ts index 97df40a30b..652aeba5e3 100644 --- a/packages/opencode/test/session/tool-output-mcp-credentials.fixture.ts +++ b/packages/opencode/test/session/tool-output-mcp-credentials.fixture.ts @@ -3,7 +3,7 @@ import { expect, test } from "bun:test" import { toolCredentialRedactor } from "@/altimate/tool-output-credentials" import { MCP } from "@/mcp" import { Instance } from "@/project/instance" -import { provideTestInstance, tmpdir } from "../fixture/fixture" +import { tmpdir } from "../fixture/fixture" test("collects configured and command-resolved MCP authorization without changing ordinary headers", async () => { const configuredToken = "fixture-configured-token" @@ -44,14 +44,15 @@ test("collects configured and command-resolved MCP authorization without changin }, }, }) - await provideTestInstance({ + // Only MCP and configuration are needed. Avoid full project bootstrap in this subprocess. + await Instance.provide({ directory: tmp.path, - fn: (instance) => - Instance.restore(instance, async () => { + fn: async () => { + try { await MCP.add(serverName, { type: "remote", url: String(server.url), - timeout: 1_000, + timeout: 5_000, headersCommand: { Authorization: [ process.execPath, @@ -71,6 +72,9 @@ test("collects configured and command-resolved MCP authorization without changin await MCP.remove(serverName) } expect((await toolCredentialRedactor()).text(resolvedToken)).toBe(resolvedToken) - }), + } finally { + await Instance.dispose() + } + }, }) -}, 25_000) +}, 60_000) diff --git a/packages/opencode/test/tool/apply_patch.test.ts b/packages/opencode/test/tool/apply_patch.test.ts index 01a09add87..70222c7e19 100644 --- a/packages/opencode/test/tool/apply_patch.test.ts +++ b/packages/opencode/test/tool/apply_patch.test.ts @@ -531,3 +531,34 @@ EOF` }), ) }) + +it.instance("refuses an add-file patch that overwrites a credential with a read placeholder", () => + Effect.gen(function* () { + const test = yield* TestInstance + const target = path.join(test.directory, "profiles.yml") + const secret = "synthetic-add-file-password" + const original = `password: ${secret}\nport: 5432\n` + yield* Effect.promise(() => fs.writeFile(target, original)) + const { createCredentialRedactor } = yield* Effect.promise(() => import("@/altimate/tool-output-credentials")) + let approvals = 0 + const context = { + ...baseCtx, + redactCredentials: createCredentialRedactor([secret]), + ask: () => + Effect.sync(() => { + approvals++ + }), + } + const exit = yield* execute( + { + patchText: "*** Begin Patch\n*** Add File: profiles.yml\n+password: [REDACTED]\n+port: 5433\n*** End Patch", + }, + context, + ).pipe(Effect.exit) + expect(Exit.isFailure(exit)).toBe(true) + if (Exit.isFailure(exit)) + expect(String(Cause.squash(exit.cause))).toContain("Refusing to overwrite a known credential") + expect(approvals).toBe(0) + expect(yield* Effect.promise(() => fs.readFile(target, "utf-8"))).toBe(original) + }), +) diff --git a/packages/opencode/test/tool/credential-errors.test.ts b/packages/opencode/test/tool/credential-errors.test.ts index bb54770d20..5a747e5c3c 100644 --- a/packages/opencode/test/tool/credential-errors.test.ts +++ b/packages/opencode/test/tool/credential-errors.test.ts @@ -1,10 +1,10 @@ // altimate_change — synthetic coverage for thrown tool errors. import { expect, spyOn, test } from "bun:test" -import { Effect, Schema } from "effect" +import { Cause, Effect, Exit, Fiber, Schema } from "effect" import { Tool } from "@/tool/tool" import { Telemetry } from "@/altimate/telemetry" import { MessageID, SessionID } from "@/session/schema" -import { initTool } from "../altimate/tool-fixture" +import { initTool, toolInfo } from "../altimate/tool-fixture" for (const kind of ["failure", "defect"] as const) { test(`Tool.define redacts newly learned credentials in ${kind} and telemetry`, async () => { @@ -56,3 +56,113 @@ for (const kind of ["failure", "defect"] as const) { } }) } + +// External cancellation must run the completion observer even when recovery cannot run. +for (const mode of ["fiber", "signal"] as const) { + test(`Tool.define completes trace and telemetry on external ${mode} interruption`, async () => { + const Debug = await import("@/altimate/debug/mode") + const completed: unknown[] = [] + const events: Parameters[0][] = [] + const trace = spyOn(Debug, "traceToolCall").mockImplementation(() => (...args) => { + completed.push(args) + }) + const track = spyOn(Telemetry, "track").mockImplementation((event) => { + events.push(event) + }) + const ready = Promise.withResolvers() + try { + const info = await toolInfo( + Tool.define( + "interrupt_fixture", + Effect.succeed({ + description: "Synthetic interruption fixture", + parameters: Schema.Struct({}), + execute: () => + Effect.gen(function* () { + ready.resolve() + return yield* Effect.never + }), + }), + ), + ) + const tool = await Effect.runPromise(info.init()) + const controller = new AbortController() + const execute = tool.execute( + {}, + { + sessionID: SessionID.make("ses_fixture"), + messageID: MessageID.make("msg_fixture"), + agent: "build", + abort: controller.signal, + messages: [], + metadata: () => Effect.void, + ask: () => Effect.void, + }, + ) + if (mode === "fiber") { + const fiber = Effect.runFork(execute) + await ready.promise + await Effect.runPromise(Fiber.interrupt(fiber)) + const exit = await Effect.runPromise(Fiber.await(fiber)) + expect(Exit.isFailure(exit) && Cause.hasInterruptsOnly(exit.cause)).toBe(true) + } else { + const result = Effect.runPromiseExit(execute, { signal: controller.signal }) + await ready.promise + controller.abort() + const exit = await result + expect(Exit.isFailure(exit) && Cause.hasInterruptsOnly(exit.cause)).toBe(true) + } + expect(completed).toEqual([["error", expect.any(String)]]) + expect(events.filter((event) => event.type === "tool_call" && event.status === "error")).toHaveLength(1) + } finally { + trace.mockRestore() + track.mockRestore() + } + }) +} + +for (const nested of [false, true]) { + test(`Tool.define preserves safe DOMException behavior (nested cause: ${nested})`, async () => { + const key = "ALTIMATE_FIXTURE_DOM_TOKEN" + const prior = process.env[key] + const secret = "synthetic-host-error-credential" + try { + process.env[key] = secret + const tool = await initTool( + Tool.define( + "host_error_fixture", + Effect.succeed({ + description: "Synthetic host error fixture", + parameters: Schema.Struct({}), + execute: () => + Effect.sync(() => { + const error = new DOMException(`Fixture failed: ${secret}`, "AbortError") + if (nested) Object.defineProperty(error, "cause", { value: new Error(secret), configurable: true }) + throw error + }), + }), + ), + ) + const error = await tool + .execute( + {}, + { + sessionID: SessionID.make("ses_fixture"), + messageID: MessageID.make("msg_fixture"), + agent: "build", + abort: new AbortController().signal, + messages: [], + }, + ) + .catch((error) => error) + expect(error).toBeInstanceOf(DOMException) + expect(error.name).toBe("AbortError") + expect(error.code).toBe(DOMException.ABORT_ERR) + expect(String(error)).toBe("AbortError: Fixture failed: [REDACTED]") + if (nested) expect(error.cause.message).toBe("[REDACTED]") + } finally { + if (prior === undefined) delete process.env[key] + else process.env[key] = prior + } + }) +} diff --git a/packages/opencode/test/tool/project-scan.test.ts b/packages/opencode/test/tool/project-scan.test.ts index 26c19b21d8..8fdcd9fd28 100644 --- a/packages/opencode/test/tool/project-scan.test.ts +++ b/packages/opencode/test/tool/project-scan.test.ts @@ -174,7 +174,7 @@ describe("detectGit", () => { stdout: Buffer.alloc(0), stderr: Buffer.from("failed for reader@example.test"), }) - expect((await detectGit()).gitError?.stderr).not.toContain("reader@example.test") + expect((await detectGit()).gitError?.stderr).toMatch(/^failed for (?!reader@example\.test).+/) for (const cap of [120, 240]) { const prefix = "x".repeat(cap - 11) + " " const fixture = { exitCode: 2, stdout: Buffer.alloc(0), stderr: Buffer.from(prefix + value) } diff --git a/packages/opencode/test/tool/write.test.ts b/packages/opencode/test/tool/write.test.ts index 47f5741d05..28a9e2388c 100644 --- a/packages/opencode/test/tool/write.test.ts +++ b/packages/opencode/test/tool/write.test.ts @@ -320,3 +320,63 @@ describe("tool.write", () => { ) }) }) + +it.instance("refuses to replace a known credential with a read placeholder before approval", () => + Effect.gen(function* () { + const test = yield* TestInstance + const filepath = path.join(test.directory, "profiles.yml") + const secret = "synthetic-write-password" + const original = `password: ${secret}\nport: 5432\n` + yield* putRead(filepath, original) + const { createCredentialRedactor } = yield* Effect.promise(() => import("@/altimate/tool-output-credentials")) + let approvals = 0 + const exit = yield* run( + { filePath: filepath, content: "password: [REDACTED]\nport: 5433\n" }, + { + ...ctx, + redactCredentials: createCredentialRedactor([secret]), + ask: () => + Effect.sync(() => { + approvals++ + }), + }, + ).pipe(Effect.exit) + expect(failureMessage(exit)).toContain("Refusing to overwrite a known credential") + expect(failureMessage(exit)).not.toContain(secret) + expect(approvals).toBe(0) + expect(yield* Effect.promise(() => fs.readFile(filepath, "utf-8"))).toBe(original) + }), +) + +it.instance("refuses a placeholder in the middle of a multiline credential", () => + Effect.gen(function* () { + const test = yield* TestInstance + const filepath = path.join(test.directory, "credential.txt") + const secret = "fixture first\nfixture second\nfixture third" + yield* putRead(filepath, secret) + const { createCredentialRedactor } = yield* Effect.promise(() => import("@/altimate/tool-output-credentials")) + const exit = yield* run( + { filePath: filepath, content: secret.replace("fixture second", "[REDACTED]") }, + { ...ctx, redactCredentials: createCredentialRedactor([secret]) }, + ).pipe(Effect.exit) + expect(failureMessage(exit)).toContain("Refusing to overwrite a known credential") + expect(yield* Effect.promise(() => fs.readFile(filepath, "utf-8"))).toBe(secret) + }), +) + +it.instance("allows unrelated changes with existing literal placeholders and preserved credentials", () => + Effect.gen(function* () { + const test = yield* TestInstance + const filepath = path.join(test.directory, "profiles.yml") + const secret = "synthetic-write-password" + const original = `password: ${secret}\nexample: [REDACTED]\nport: 5432\n` + const updated = original.replace("5432", "5433") + yield* putRead(filepath, original) + const { createCredentialRedactor } = yield* Effect.promise(() => import("@/altimate/tool-output-credentials")) + yield* run( + { filePath: filepath, content: updated }, + { ...ctx, redactCredentials: createCredentialRedactor([secret]) }, + ) + expect(yield* Effect.promise(() => fs.readFile(filepath, "utf-8"))).toBe(updated) + }), +) From b055428b6b8477d11498e18da730a7be64926c05 Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sun, 22 Nov 2026 01:09:22 -0800 Subject: [PATCH 9/9] fix: guard credential placeholders in file edits --- .../src/altimate/tool-output-credentials.ts | 232 ++++++++++-------- .../opencode/src/altimate/tool-zod-compat.ts | 5 +- packages/opencode/src/session/processor.ts | 10 +- packages/opencode/src/tool/apply_patch.ts | 1 + packages/opencode/src/tool/bash.ts | 4 +- packages/opencode/src/tool/edit.ts | 1 + .../session/tool-credential-output.test.ts | 36 ++- .../tool-output-credential-sources.test.ts | 8 +- .../session/tool-output-credentials.test.ts | 21 ++ .../opencode/test/tool/apply_patch.test.ts | 31 +++ packages/opencode/test/tool/edit.test.ts | 34 +++ 11 files changed, 278 insertions(+), 105 deletions(-) diff --git a/packages/opencode/src/altimate/tool-output-credentials.ts b/packages/opencode/src/altimate/tool-output-credentials.ts index a72e3c7bd2..af65fdd2b7 100644 --- a/packages/opencode/src/altimate/tool-output-credentials.ts +++ b/packages/opencode/src/altimate/tool-output-credentials.ts @@ -116,10 +116,10 @@ export function createCredentialRedactor(values: Iterable = return start } - function progress(value: string) { + function progressStatus(value: string) { // Keep the tail private until more output arrives or the stream ends. // Alternate JSON escape spellings can require a longer holdback. - if (!pattern) return value + if (!pattern) return { output: value, withheld: false } const unfinishedEscape = /\\(?:u[\da-fA-F]{0,3})?$/.exec(value) const completePrefix = unfinishedEscape ? value.slice(0, unfinishedEscape.index) : value const decoded = completePrefix.includes("\\") ? decodeEscapes(completePrefix) : undefined @@ -130,24 +130,40 @@ export function createCredentialRedactor(values: Iterable = const start = Math.min(Math.max(0, value.length - longest + 1), escapedStart) // Match complete credentials against the original string, including a // credential whose tail is also a prefix of another credential. - return replaceMatches(value, matches(value), 0, start) + const found = matches(value) + const lastVisible = found.find(({ start: matchStart, end }) => matchStart < start && end > start) + return { + output: replaceMatches(value, found, 0, start), + withheld: (lastVisible?.end ?? start) < value.length, + } + } + + function previewStatus(value: string, limit: number) { + // Read enough beyond the cap to match any literal crossing it. + const scan = value.slice(0, limit + lookahead) + const progress = scan.length === value.length ? progressStatus(scan) : { output: text(scan), withheld: false } + const truncated = scan.length < value.length || progress.output.length > limit + return { ...progress, output: truncated ? progress.output.slice(0, limit) + "\n\n..." : progress.output } } // Binary attachments must stay valid. When their transport or decoded bytes // contain an exact credential, omit the bytes and leave a text placeholder. let binaryLiterals: Buffer[] | undefined const binaryMatches = new Map() - function containsCredentialBytes(encoded: string) { + function containsCredentialBytes(encoded: string, encoding: "base64" | "percent" = "base64") { if (!encoded || !pattern) return false // Replay may allocate new attachment objects on every turn. Cache only a // digest and a match bit, without retaining the attachment itself. - const digest = encoded.length >= 4096 ? createHash("sha256").update(encoded).digest("hex") : undefined + const digest = + encoded.length >= 4096 + ? createHash("sha256").update(encoding).update(encoded, "utf16le").digest("hex") + : undefined const cached = digest ? binaryMatches.get(digest) : undefined if (cached !== undefined) return cached binaryLiterals ??= ordered.map((literal) => Buffer.from(literal, "utf8")) let contains = text(encoded) !== encoded if (!contains) { - const bytes = Buffer.from(encoded, "base64") + const bytes = encoding === "base64" ? Buffer.from(encoded, "base64") : decodePercentBytes(encoded) contains = binaryLiterals.some((literal) => bytes.includes(literal)) } if (digest) { @@ -157,13 +173,30 @@ export function createCredentialRedactor(values: Iterable = return contains } + function decodePercentBytes(encoded: string) { + const bytes = Buffer.allocUnsafe(Buffer.byteLength(encoded)) + let offset = 0 + let written = 0 + for (const match of encoded.matchAll(/(?:%[\da-f]{2})+/gi)) { + written += bytes.write(encoded.slice(offset, match.index), written, "utf8") + for (let index = 0; index < match[0].length; index += 3) { + bytes[written++] = Number.parseInt(match[0].slice(index + 1, index + 3), 16) + } + offset = match.index + match[0].length + } + written += bytes.write(encoded.slice(offset), written, "utf8") + return bytes.subarray(0, written) + } + const omitAttachment = Symbol("omitAttachment") function filterMedia(value: unknown): unknown { if (!value || typeof value !== "object") return value const item = value as Record if (item.type === "file" && typeof item.url === "string") { - const data = /^data:[^,]*;base64,(.*)$/is.exec(item.url)?.[1] - if (data && containsCredentialBytes(data)) return omitAttachment + const data = /^data:([^,]*),(.*)$/is.exec(item.url) + if (data && containsCredentialBytes(data[2], /;base64$/i.test(data[1]) ? "base64" : "percent")) { + return omitAttachment + } } if ((item.type === "image" || item.type === "audio") && typeof item.data === "string") { if (containsCredentialBytes(item.data)) return { type: "text", text: replacement } @@ -212,7 +245,8 @@ export function createCredentialRedactor(values: Iterable = const next = dom ?? Object.create(Object.getPrototypeOf(value)) seen.set(value, next) let changed = !!dom && (dom.message !== value.message || dom.name !== value.name) - for (const descriptor of Object.values(descriptors)) { + for (const key of Reflect.ownKeys(descriptors)) { + const descriptor: PropertyDescriptor = Reflect.get(descriptors, key) if (!("value" in descriptor)) continue const safe = visit(descriptor.value, seen, "data") changed ||= safe !== descriptor.value @@ -271,13 +305,9 @@ export function createCredentialRedactor(values: Iterable = multilineLookaround: ordered.find((value) => value.includes("\n"))?.length ?? 0, windowLookaround: lookahead, lines: (value: string) => replaceMatches(value, matches(value), 0, value.length, true), - progress, - preview(value: string, limit: number) { - // Read enough beyond the cap to match any literal crossing it. - const scan = value.slice(0, limit + lookahead) - const prefix = scan.length === value.length ? progress(scan) : text(scan) - return scan.length < value.length || prefix.length > limit ? prefix.slice(0, limit) + "\n\n..." : prefix - }, + progress: (value: string) => progressStatus(value).output, + previewStatus, + preview: (value: string, limit: number) => previewStatus(value, limit).output, window(value: string, from: number, to: number) { if (!pattern) return value.slice(from, to) const start = Math.max(0, from - lookahead) @@ -423,6 +453,11 @@ function authFileSnapshot() { cached = undefined return undefined } + // Whole-second timestamps cannot distinguish same-size rewrites within a second. + if (info.mtimeNs % 1_000_000_000n === 0n) { + cached = undefined + return load().catch(() => undefined) + } const version = `${file}:${info.ino}:${info.size}:${info.mtimeNs}:${info.ctimeNs}` if (cached?.version === version) return cached.data const data = load().catch(() => { @@ -438,93 +473,96 @@ const readProviderAuth = authFileSnapshot>>() let lastSnapshot: { values: Set; redactor: CredentialRedactor } | undefined -export const loadToolCredentialRedactor = withAlsInstanceRef( - Effect.gen(function* () { - // Message projection is below configuration and the connection registry in the import graph. - const [{ Auth }, { Config }, { Env }, Connections, { McpAuth }, { knownMcpHeaders }] = yield* Effect.promise(() => - Promise.all([ - import("@/auth"), - import("@/config/config"), - import("@/env"), - import("@/altimate/native/connections/registry"), - import("@/mcp/auth"), - import("@/mcp"), - ]), - ) - const instance = yield* InstanceRef - const values = new Set() - const add = (value: unknown) => { - if (typeof value === "string") values.add(value) - } - const [config, auth, mcpAuth] = yield* Effect.promise(() => - Promise.all([ - instance ? Config.get().catch(() => undefined) : undefined, - process.env.OPENCODE_AUTH_CONTENT - ? Auth.all().catch(() => undefined) - : readProviderAuth(Auth.filepath(), Auth.all), - readMcpAuth(McpAuth.credentialFile, McpAuth.all), - ]), - ) - const environment = [...Object.entries(process.env), ...Object.entries(instance ? Env.all() : {})] - const configuredVariables = new Set() - for (const provider of Object.values(config?.provider ?? {})) { - for (const name of provider.env ?? []) { - if (!providerLocationVariables.test(name)) configuredVariables.add(name) - } - addProviderOptions(provider.options, add) - for (const model of Object.values(provider.models ?? {})) { - addProviderOptions(model.options, add) - addCredentialHeaders(model.headers, add) - } +const collectToolCredentialRedactor = Effect.gen(function* () { + // Message projection is below configuration and the connection registry in the import graph. + const [{ Auth }, { Config }, { Env }, Connections, { McpAuth }, { knownMcpHeaders }] = yield* Effect.promise(() => + Promise.all([ + import("@/auth"), + import("@/config/config"), + import("@/env"), + import("@/altimate/native/connections/registry"), + import("@/mcp/auth"), + import("@/mcp"), + ]), + ) + const instance = yield* InstanceRef + const values = new Set() + const add = (value: unknown) => { + if (typeof value === "string") values.add(value) + } + const [config, auth, mcpAuth] = yield* Effect.promise(() => + Promise.all([ + instance ? Config.get().catch(() => undefined) : undefined, + process.env.OPENCODE_AUTH_CONTENT + ? Auth.all().catch(() => undefined) + : readProviderAuth(Auth.filepath(), Auth.all), + readMcpAuth(McpAuth.credentialFile, McpAuth.all), + ]), + ) + const environment = [...Object.entries(process.env), ...Object.entries(instance ? Env.all() : {})] + const configuredVariables = new Set() + for (const provider of Object.values(config?.provider ?? {})) { + for (const name of provider.env ?? []) { + if (!providerLocationVariables.test(name)) configuredVariables.add(name) } - for (const [name, value] of environment) { - if (name.startsWith(Connections.CONN_ENV_PREFIX)) { - if (value) addConnectionFields(readJson(value), add) - } else if (isCredentialName(name) || providerCredentialVariables.has(name) || configuredVariables.has(name)) { - add(value) - } + addProviderOptions(provider.options, add) + for (const model of Object.values(provider.models ?? {})) { + addProviderOptions(model.options, add) + addCredentialHeaders(model.headers, add) } - for (const entry of Object.values(auth ?? {})) { - if (entry.type === "oauth") { - add(entry.access) - add(entry.refresh) - } else { - add(entry.key) - if (entry.type === "wellknown") add(entry.token) - } + } + for (const [name, value] of environment) { + if (name.startsWith(Connections.CONN_ENV_PREFIX)) { + if (value) addConnectionFields(readJson(value), add) + } else if (isCredentialName(name) || providerCredentialVariables.has(name) || configuredVariables.has(name)) { + add(value) } - for (const entry of Object.values(mcpAuth ?? {})) { - add(entry.tokens?.accessToken) - add(entry.tokens?.refreshToken) - add(entry.clientInfo?.clientSecret) - add(entry.codeVerifier) + } + for (const entry of Object.values(auth ?? {})) { + if (entry.type === "oauth") { + add(entry.access) + add(entry.refresh) + } else { + add(entry.key) + if (entry.type === "wellknown") add(entry.token) } - for (const server of Object.values(config?.mcp ?? {})) { - if (!("type" in server)) continue - if (server.type === "remote") { - addCredentialHeaders(server.headers, add) - if (typeof server.oauth === "object") add(server.oauth.clientSecret) - } else { - for (const [name, value] of Object.entries(server.environment ?? {})) { - if (isCredentialName(name)) add(value) - } + } + for (const entry of Object.values(mcpAuth ?? {})) { + add(entry.tokens?.accessToken) + add(entry.tokens?.refreshToken) + add(entry.clientInfo?.clientSecret) + add(entry.codeVerifier) + } + for (const server of Object.values(config?.mcp ?? {})) { + if (!("type" in server)) continue + if (server.type === "remote") { + addCredentialHeaders(server.headers, add) + if (typeof server.oauth === "object") add(server.oauth.clientSecret) + } else { + for (const [name, value] of Object.entries(server.environment ?? {})) { + if (isCredentialName(name)) add(value) } } - if (instance) { - for (const headers of knownMcpHeaders(instance)) addCredentialHeaders(headers, add) - const connections = Connections.knownCredentialConfigs(instance.directory) - for (const connection of connections) addConnectionFields(connection, add) - } - for (const credential of knownStoredCredentials()) addConnectionCredential(credential.field, credential.value, add) - const previous = lastSnapshot - if (previous && previous.values.size === values.size && [...values].every((value) => previous.values.has(value))) { - return previous.redactor - } - const redactor = createCredentialRedactor(values) - lastSnapshot = { values, redactor } - return redactor - }), -) + } + if (instance) { + for (const headers of knownMcpHeaders(instance)) addCredentialHeaders(headers, add) + const connections = Connections.knownCredentialConfigs(instance.directory) + for (const connection of connections) addConnectionFields(connection, add) + } + for (const credential of knownStoredCredentials()) addConnectionCredential(credential.field, credential.value, add) + const previous = lastSnapshot + if (previous && previous.values.size === values.size && [...values].every((value) => previous.values.has(value))) { + return previous.redactor + } + const redactor = createCredentialRedactor(values) + lastSnapshot = { values, redactor } + return redactor +}) + +export const loadToolCredentialRedactor = Effect.gen(function* () { + if (yield* InstanceRef) return yield* collectToolCredentialRedactor + return yield* withAlsInstanceRef(collectToolCredentialRedactor) +}) export function toolCredentialRedactor() { return Effect.runPromise(attach(loadToolCredentialRedactor)) diff --git a/packages/opencode/src/altimate/tool-zod-compat.ts b/packages/opencode/src/altimate/tool-zod-compat.ts index 0a8c84e3a5..68ddffdf24 100644 --- a/packages/opencode/src/altimate/tool-zod-compat.ts +++ b/packages/opencode/src/altimate/tool-zod-compat.ts @@ -304,15 +304,14 @@ export function legacyToInit(legacy: LegacyToolDef): Effect.Effect // factory runs inside this init Effect's fiber, attach would read an unset InstanceRef and fail // ("InstanceRef not provided"). Provide the ALS instance as InstanceRef so attach finds it. export function withAlsInstanceRef(effect: Effect.Effect): Effect.Effect { - return Effect.gen(function* () { - if (yield* InstanceRef) return yield* effect + return Effect.suspend(() => { let instance: InstanceContext | undefined try { instance = Instance.current } catch { instance = undefined } - return yield* (instance ? effect.pipe(Effect.provideService(InstanceRef, instance)) : effect) + return instance ? effect.pipe(Effect.provideService(InstanceRef, instance)) : effect }) } // altimate_change end diff --git a/packages/opencode/src/session/processor.ts b/packages/opencode/src/session/processor.ts index 02687babb4..8d24c9083f 100644 --- a/packages/opencode/src/session/processor.ts +++ b/packages/opencode/src/session/processor.ts @@ -53,12 +53,20 @@ import { abortableStream } from "./abortable-stream" // altimate_change start — upstream_fix: preserve child findings even when parent cancellation wins the tool-result race import { interruptedTaskOutput } from "./task-output" -async function interruptedToolPart(part: MessageV2.ToolPart, capTokens: number): Promise { +export async function interruptedToolPart(part: MessageV2.ToolPart, capTokens: number): Promise { const state = part.state if (state.status !== "running" && state.status !== "pending") return part const metadata = state.status === "running" ? ToolResultCap.capInterruptedMetadata(state.metadata, capTokens) : { interrupted: true } + if ( + part.tool === "bash" && + state.status === "running" && + state.metadata?.credentialPreviewWithheld === true && + typeof metadata.output === "string" + ) { + metadata.output += "\n[Interrupted shell preview may omit the last part of the output.]" + } const time = { start: state.status === "running" ? state.time.start : Date.now(), end: Date.now() } if ( part.tool === "task" && state.status === "running" && diff --git a/packages/opencode/src/tool/apply_patch.ts b/packages/opencode/src/tool/apply_patch.ts index 9a7171f1df..29a6cd07ca 100644 --- a/packages/opencode/src/tool/apply_patch.ts +++ b/packages/opencode/src/tool/apply_patch.ts @@ -134,6 +134,7 @@ export const ApplyPatchTool = Tool.define( return yield* Effect.fail(new Error(`apply_patch verification failed: ${error}`)) } + assertNoCredentialPlaceholderOverwrite(oldContent, newContent, ctx.redactCredentials) const diff = toolFileDiff(filePath, oldContent, newContent, ctx.redactCredentials) let additions = 0 diff --git a/packages/opencode/src/tool/bash.ts b/packages/opencode/src/tool/bash.ts index d5b49ca839..e30b2053c2 100644 --- a/packages/opencode/src/tool/bash.ts +++ b/packages/opencode/src/tool/bash.ts @@ -530,13 +530,15 @@ export const BashTool = Tool.define("bash", async () => { const append = (chunk: Buffer) => { output += chunk.toString() + const preview = ctx.redactCredentials?.previewStatus(output, MAX_METADATA_LENGTH) // altimate_change ctx.metadata({ metadata: { // truncate the metadata to avoid GIANT blobs of data (has nothing to do w/ what agent can access) // altimate_change start — bound the exact-match scan to the preview and its lookahead output: - ctx.redactCredentials?.preview(output, MAX_METADATA_LENGTH) ?? + preview?.output ?? (output.length > MAX_METADATA_LENGTH ? output.slice(0, MAX_METADATA_LENGTH) + "\n\n..." : output), + ...(preview?.withheld && { credentialPreviewWithheld: true }), // altimate_change end description: params.description, }, diff --git a/packages/opencode/src/tool/edit.ts b/packages/opencode/src/tool/edit.ts index 78b6c41ed3..fed903a80a 100644 --- a/packages/opencode/src/tool/edit.ts +++ b/packages/opencode/src/tool/edit.ts @@ -166,6 +166,7 @@ export const EditTool = Tool.define( const next = Bom.split(replace(contentOld, old, replacement, params.replaceAll)) const desiredBom = source.bom || next.bom contentNew = next.text + assertNoCredentialPlaceholderOverwrite(contentOld, contentNew, ctx.redactCredentials) diff = toolFileDiff( filePath, diff --git a/packages/opencode/test/session/tool-credential-output.test.ts b/packages/opencode/test/session/tool-credential-output.test.ts index ba7654db56..e959e47f8e 100644 --- a/packages/opencode/test/session/tool-credential-output.test.ts +++ b/packages/opencode/test/session/tool-credential-output.test.ts @@ -146,10 +146,13 @@ test("bash progress holds a split credential until the next chunk and redacts fi abort: new AbortController().signal, messages: [], ask: async () => {}, - metadata: (update: { metadata?: { output?: string } }) => { + metadata: (update: { metadata?: { output?: string; credentialPreviewWithheld?: boolean } }) => { if (update.metadata?.output === undefined) return previews.push(update.metadata.output) - if (previews.length === 2) return Bun.write(path.join(directory, "release"), "ready").then(() => {}) + if (previews.length === 2) { + expect(update.metadata.credentialPreviewWithheld).toBe(true) + return Bun.write(path.join(directory, "release"), "ready").then(() => {}) + } }, }, ) @@ -187,3 +190,32 @@ test("model replay marks an omitted credential-bearing attachment", async () => expect(part.state.attachments).toHaveLength(1) }) }) + +test("interrupted bash replay identifies a potentially withheld preview tail", async () => { + await inConfiguredInstance(async (directory) => { + const { interruptedToolPart } = await import("@/session/processor") + const history = rawHistory(directory) + const part = history[1].parts[0] as MessageV2.ToolPart + part.state = { + status: "running", + input: { command: "fixture" }, + time: { start: 1 }, + metadata: { output: "Partial safe output" }, + } + const completePreview = await interruptedToolPart(part, 1000) + expect(completePreview.state).toMatchObject({ metadata: { output: "Partial safe output" } }) + part.state.metadata = { ...part.state.metadata, credentialPreviewWithheld: true } + const interrupted = await interruptedToolPart(part, 1000) + expect(interrupted.state).toMatchObject({ + status: "error", + metadata: { + interrupted: true, + output: "Partial safe output\n[Interrupted shell preview may omit the last part of the output.]", + }, + }) + history[1].parts = [interrupted] + const model = await Provider.getModel(providerID, modelID) + const request = JSON.stringify(await MessageV2.toModelMessages(history, model)) + expect(request).toContain("preview may omit the last part") + }) +}) diff --git a/packages/opencode/test/session/tool-output-credential-sources.test.ts b/packages/opencode/test/session/tool-output-credential-sources.test.ts index 9727576f05..315a25b8ba 100644 --- a/packages/opencode/test/session/tool-output-credential-sources.test.ts +++ b/packages/opencode/test/session/tool-output-credential-sources.test.ts @@ -1,6 +1,6 @@ // altimate_change — synthetic fixtures for known credential sources. import path from "node:path" -import { mkdir } from "node:fs/promises" +import { mkdir, utimes } from "node:fs/promises" import { Effect } from "effect" import { InstanceRef } from "@/effect/instance-ref" import { expect, spyOn, test } from "bun:test" @@ -38,6 +38,12 @@ test("collects provider token environment names and MCP OAuth credentials", asyn expect(updated.text(rotated)).toBe("[REDACTED]") expect(updated.text(accessToken)).toBe(accessToken) expect(reads).toHaveBeenCalledTimes(2) + + // Whole-second timestamps cannot safely cache rewrites of equal size. + await utimes(McpAuth.credentialFile, 1_700_000_000, 1_700_000_000) + await toolCredentialRedactor() + await toolCredentialRedactor() + expect(reads).toHaveBeenCalledTimes(4) } finally { reads.mockRestore() if (previous === undefined) delete process.env.CF_AIG_TOKEN diff --git a/packages/opencode/test/session/tool-output-credentials.test.ts b/packages/opencode/test/session/tool-output-credentials.test.ts index 3b77981c12..b4afd6f132 100644 --- a/packages/opencode/test/session/tool-output-credentials.test.ts +++ b/packages/opencode/test/session/tool-output-credentials.test.ts @@ -153,3 +153,24 @@ test("omits credential-bearing attachment bytes while preserving clean media", ( }), ).toEqual({ content: [{ type: "resource", resource: { uri: "test://file", text: "context\n[REDACTED]" } }] }) }) + +test("redacts credential values in symbol-keyed error properties", () => { + const detail = Symbol("detail") + const credential = "fixture-symbol-credential" + const error = Object.assign(new Error("Could not load fixture"), { [detail]: credential }) + const safe = createCredentialRedactor([credential]).failure(error) as typeof error + expect(safe).toBeInstanceOf(Error) + expect(safe[detail]).toBe("[REDACTED]") + expect(error[detail]).toBe(credential) +}) + +test("omits attachments with a credential in percent-encoded data bytes", () => { + const credential = "fixture/percent+credential" + const redact = createCredentialRedactor([credential]) + const unsafe = { type: "file", mime: "text/plain", url: "data:text/plain,%FF" + encodeURIComponent(credential) } + const clean = { ...unsafe, url: "data:text/plain,ordinary%2Fcontent" } + expect(redact.output({ output: "Attached files", attachments: [unsafe, clean] })).toEqual({ + output: "Attached files\n[REDACTED]", + attachments: [clean], + }) +}) diff --git a/packages/opencode/test/tool/apply_patch.test.ts b/packages/opencode/test/tool/apply_patch.test.ts index 70222c7e19..c3a1ee90fb 100644 --- a/packages/opencode/test/tool/apply_patch.test.ts +++ b/packages/opencode/test/tool/apply_patch.test.ts @@ -562,3 +562,34 @@ it.instance("refuses an add-file patch that overwrites a credential with a read expect(yield* Effect.promise(() => fs.readFile(target, "utf-8"))).toBe(original) }), ) + +it.instance("refuses an update patch that replaces a credential with a placeholder", () => + Effect.gen(function* () { + const test = yield* TestInstance + const target = path.join(test.directory, "profiles.yml") + const secret = "synthetic-update-password" + const original = `password: ${secret}\nport: 5432\n` + yield* Effect.promise(() => fs.writeFile(target, original)) + const { createCredentialRedactor } = yield* Effect.promise(() => import("@/altimate/tool-output-credentials")) + let approvals = 0 + const context = { + ...baseCtx, + redactCredentials: createCredentialRedactor([secret]), + ask: () => + Effect.sync(() => { + approvals++ + }), + } + const exit = yield* execute( + { + patchText: `*** Begin Patch\n*** Update File: profiles.yml\n@@\n-password: ${secret}\n+password: [REDACTED]\n*** End Patch`, + }, + context, + ).pipe(Effect.exit) + expect(Exit.isFailure(exit)).toBe(true) + if (Exit.isFailure(exit)) + expect(String(Cause.squash(exit.cause))).toContain("Refusing to overwrite a known credential") + expect(approvals).toBe(0) + expect(yield* Effect.promise(() => fs.readFile(target, "utf-8"))).toBe(original) + }), +) diff --git a/packages/opencode/test/tool/edit.test.ts b/packages/opencode/test/tool/edit.test.ts index 67ba08ed6b..0213549c24 100644 --- a/packages/opencode/test/tool/edit.test.ts +++ b/packages/opencode/test/tool/edit.test.ts @@ -643,3 +643,37 @@ describe("tool.edit", () => { ) }) }) + +it.instance("refuses a fuzzy block replacement that copies a redacted credential placeholder", () => + Effect.gen(function* () { + const test = yield* TestInstance + const filepath = path.join(test.directory, "profiles.yml") + const secret = "hunter2-prod-pw" + const original = `outputs:\n user: dev\n password: ${secret}\n port: 5432\n` + const oldString = original.replace(secret, "[REDACTED]") + const newString = oldString.replace("5432", "5433") + const { replace } = yield* Effect.promise(() => import("@/tool/edit")) + // Confirm that the real fuzzy matcher accepts this redacted block. The + // regression must exercise the guard, rather than a no-match failure. + expect(replace(original, oldString, newString)).toContain(newString) + yield* put(filepath, original) + const { createCredentialRedactor } = yield* Effect.promise(() => import("@/altimate/tool-output-credentials")) + let approvals = 0 + const exit = yield* run( + { filePath: filepath, oldString, newString }, + { + ...ctx, + redactCredentials: createCredentialRedactor([secret]), + ask: () => + Effect.sync(() => { + approvals++ + }), + }, + ).pipe(Effect.exit) + expect(Exit.isFailure(exit)).toBe(true) + if (Exit.isFailure(exit)) + expect(String(Cause.squash(exit.cause))).toContain("Refusing to overwrite a known credential") + expect(approvals).toBe(0) + expect(yield* load(filepath)).toBe(original) + }), +)