Files
orca/src/main/devin/hook-service.ts
T
4797cf81a4 fix(agent-hooks): stream hook payload to a temp file instead of inlining it on the curl command line (#4475)
The POSIX agent-hook script for every curl-based agent inlined the full
event payload via `curl --data-urlencode "payload=${payload}"`. Tool
output can be tens of KB, so the resulting process command line could be
multi-KB — which endpoint security tools (e.g. Microsoft Defender for
Endpoint) flag as an oversized/suspicious command line. That produced a
false-positive detection on Orca's own loopback (127.0.0.1) telemetry POST.

Stream the payload to an mktemp file and post it with
`--data-urlencode "payload@$payload_file"` instead. The urlencoded body on
the wire is byte-identical, so the agent-hook receiver is unchanged; the
payload simply never appears on a process command line. `trap ... EXIT`
removes the temp file on every exit path. Small bounded metadata fields
(paneKey/tabId/worktreeId/env/version) stay inline.

Applied to all curl-based agents: claude, codex, command-code, copilot,
cursor, droid, gemini, grok, antigravity. (amp/hermes/opencode post via
the HTTP request body and were never affected.) The Windows post-command
shares the same latent pattern and is flagged as follow-up.

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-authored-by: Neil <4138956+nwparker@users.noreply.github.com>
Co-authored-by: Orca <help@stably.ai>
2026-07-03 16:26:53 -07:00

273 lines
11 KiB
TypeScript

import type { SFTPWrapper } from 'ssh2'
import type { AgentHookInstallState, AgentHookInstallStatus } from '../../shared/agent-hook-types'
import {
buildWindowsAgentHookPostCommand,
writeHooksJson,
writeManagedScript
} from '../agent-hooks/installer-utils'
import {
readTextFileRemote,
writeHooksJsonRemote,
writeManagedScriptRemote
} from '../agent-hooks/installer-utils-remote'
import {
applyDevinManagedHooks,
DEVIN_EVENTS,
getDevinConfigPath,
getDevinManagedCommand,
getDevinManagedScriptFileName,
getDevinManagedScriptPath,
getDevinPosixManagedScriptFileName,
getDevinRemoteConfigPath,
getDevinRemoteManagedCommand,
removeDevinManagedHooks
} from './hook-settings'
import {
mergeHookInstallDetail,
parseDevinHooksConfigText,
readConfigFromOrcaOverlapDetail,
readDevinHooksConfig
} from './hook-config-json'
function getManagedScript(target: 'local' | 'posix' = 'local'): string {
if (target === 'local' && process.platform === 'win32') {
return [
'@echo off',
'setlocal',
// Why: the endpoint file holds the *live* port/token for this Orca
// install. A PTY that survived an Orca restart has stale PORT/TOKEN
// baked into its env from the old instance — loading `endpoint.cmd`
// (`set KEY=VALUE` lines) via `call` refreshes them so the hook
// reaches the current server. Falls through to PTY env if the file
// is missing (first run / pre-endpoint-file / running outside Orca).
'if defined ORCA_AGENT_HOOK_ENDPOINT if exist "%ORCA_AGENT_HOOK_ENDPOINT%" call "%ORCA_AGENT_HOOK_ENDPOINT%" 2>nul',
'if "%ORCA_AGENT_HOOK_PORT%"=="" exit /b 0',
'if "%ORCA_AGENT_HOOK_TOKEN%"=="" exit /b 0',
'if "%ORCA_PANE_KEY%"=="" exit /b 0',
buildWindowsAgentHookPostCommand('devin'),
'exit /b 0',
''
].join('\r\n')
}
return [
'#!/bin/sh',
// Why: the endpoint file holds the *live* port/token for this Orca
// install. PTYs that survive an Orca restart have stale PORT/TOKEN
// baked into their env from the old instance — sourcing the file here
// lets us reach the new server. Falls back to PTY env if the file is
// missing (first-run / pre-endpoint-file scripts / running outside Orca).
// Why: suppress stderr on the `.` builtin. A TOCTOU race (endpoint unlinked
// between the `[ -r ]` test and the source) or a malformed line (e.g. CRLF
// bled in from a cross-platform userData copy) would otherwise print a
// parse error that agent transcripts could surface. Stale coords → dead
// port → silent-fail is the documented fail-open path anyway — the env-var
// guards below handle the empty PORT/TOKEN case — so swallowing the noise
// here is strictly better than leaking shell errors into the hook output.
// `|| :` defends against an eventual `set -e` in an outer script context
// (not present today) aborting the hook on a parse error.
'if [ -n "$ORCA_AGENT_HOOK_ENDPOINT" ] && [ -r "$ORCA_AGENT_HOOK_ENDPOINT" ]; then',
' . "$ORCA_AGENT_HOOK_ENDPOINT" 2>/dev/null || :',
'fi',
'if [ -z "$ORCA_AGENT_HOOK_PORT" ] || [ -z "$ORCA_AGENT_HOOK_TOKEN" ] || [ -z "$ORCA_PANE_KEY" ]; then',
' exit 0',
'fi',
'payload=$(cat)',
'if [ -z "$payload" ]; then',
' exit 0',
'fi',
// Why: worktreeId embeds a filesystem path, so hand-building JSON in POSIX
// shell is not safe once a path contains quotes or newlines. Post the raw
// hook payload plus metadata as form fields and let the receiver parse it.
// Timeout caps best-effort hook posts if the local listener stalls.
// Why: pipe payload to curl's stdin (`payload@-`) instead of an inline
// `payload=$VALUE` arg, so tens-of-KB tool output stays off the curl
// command line (EDR command-line false positives). Wire body is identical.
'printf \'%s\' "$payload" | curl -sS -X POST "http://127.0.0.1:${ORCA_AGENT_HOOK_PORT}/hook/devin" \\',
' --connect-timeout 0.5 --max-time 1.5 \\',
' -H "Content-Type: application/x-www-form-urlencoded" \\',
' -H "X-Orca-Agent-Hook-Token: ${ORCA_AGENT_HOOK_TOKEN}" \\',
' --data-urlencode "paneKey=${ORCA_PANE_KEY}" \\',
' --data-urlencode "tabId=${ORCA_TAB_ID}" \\',
' --data-urlencode "launchToken=${ORCA_AGENT_LAUNCH_TOKEN}" \\',
' --data-urlencode "worktreeId=${ORCA_WORKTREE_ID}" \\',
' --data-urlencode "env=${ORCA_AGENT_HOOK_ENV}" \\',
' --data-urlencode "version=${ORCA_AGENT_HOOK_VERSION}" \\',
' --data-urlencode "payload@-" >/dev/null 2>&1 || true',
'exit 0',
''
].join('\n')
}
export class DevinHookService {
getStatus(): AgentHookInstallStatus {
const configPath = getDevinConfigPath()
const scriptPath = getDevinManagedScriptPath()
const config = readDevinHooksConfig(configPath)
if (!config) {
return {
agent: 'devin',
state: 'error',
configPath,
managedHooksPresent: false,
detail: 'Could not parse Devin config.json'
}
}
// Why: Report `partial` when only some managed events are registered so the
// sidebar surfaces a degraded install rather than a false-positive
// `installed`. Each DEVIN_EVENTS entry must contain the managed command for
// the integration to function end-to-end.
const command = getDevinManagedCommand(scriptPath)
const missing: string[] = []
let presentCount = 0
for (const event of DEVIN_EVENTS) {
const definitions = Array.isArray(config.hooks?.[event.eventName])
? config.hooks![event.eventName]!
: []
const hasCommand = definitions.some((definition) =>
(definition.hooks ?? []).some((hook) => hook.command === command)
)
if (hasCommand) {
presentCount += 1
} else {
missing.push(event.eventName)
}
}
const managedHooksPresent = presentCount > 0
let state: AgentHookInstallState
let detail: string | null
if (missing.length === 0) {
state = 'installed'
detail = null
} else if (presentCount === 0) {
state = 'not_installed'
detail = null
} else {
state = 'partial'
detail = `Managed hook missing for events: ${missing.join(', ')}`
}
return {
agent: 'devin',
state,
configPath,
managedHooksPresent,
detail: mergeHookInstallDetail(detail, readConfigFromOrcaOverlapDetail(config))
}
}
install(): AgentHookInstallStatus {
const configPath = getDevinConfigPath()
const scriptPath = getDevinManagedScriptPath()
const config = readDevinHooksConfig(configPath)
if (!config) {
return {
agent: 'devin',
state: 'error',
configPath,
managedHooksPresent: false,
detail: 'Could not parse Devin config.json'
}
}
const command = getDevinManagedCommand(scriptPath)
const nextConfig = applyDevinManagedHooks(config, command, getDevinManagedScriptFileName())
writeManagedScript(scriptPath, getManagedScript())
writeHooksJson(configPath, nextConfig)
return this.getStatus()
}
// Why: install Orca's Devin hook settings on the remote box rather than the
// local machine. Caller passes the user's SFTP handle plus the resolved
// remote `$HOME`; POSIX-only by design (Windows-remote deferred).
async installRemote(sftp: SFTPWrapper, remoteHome: string): Promise<AgentHookInstallStatus> {
// Why: remote-Windows is out of scope for v1 — we ship POSIX-shaped paths
// and a `.sh` managed script body. The remote platform is gated by the
// relay's capability RPC at a higher layer; we cannot detect it from
// `process.platform` here (that's the local box).
const remoteConfigPath = getDevinRemoteConfigPath(remoteHome)
const remoteScriptFileName = getDevinPosixManagedScriptFileName()
const remoteScriptPath = `${remoteHome.replace(/\/$/, '')}/.orca/agent-hooks/${remoteScriptFileName}`
// Why: SFTP reads/writes fail far more often than local fs (network drops,
// EACCES on remote dirs, disk full, channel closed). Wrap the entire
// install flow in try/catch so a transient I/O failure surfaces as a
// structured `state: 'error'` result for the UI, not an unstructured
// rejection the caller has to remember to handle. A `null` config
// specifically means "file present but unparseable" — keep that branch
// distinct so the user sees an actionable message.
try {
// Why: Devin config.json is JSONC (comments); stock
// JSON.parse rejects them. Read the raw text via SFTP and parse with
// jsonc-parser, mirroring the local readDevinHooksConfig path.
const body = await readTextFileRemote(sftp, remoteConfigPath)
const config =
body === null ? {} : parseDevinHooksConfigText(body, 'remote Devin config.json')
if (!config) {
return {
agent: 'devin',
state: 'error',
configPath: remoteConfigPath,
managedHooksPresent: false,
detail: 'Could not parse remote Devin config.json'
}
}
// Why: the POSIX wrapper is identical regardless of where the script
// lands; only the path differs. Reuse the same wrapper helper.
const command = getDevinRemoteManagedCommand(remoteScriptPath)
const nextConfig = applyDevinManagedHooks(config, command, remoteScriptFileName)
// Why: write the script first, then the settings — settings.json
// referencing a missing script body would fire `command not found` on
// every tool call until the user re-runs install. Doing it in this
// order means a partial-failure mid-install at worst leaves the user
// with a working script no settings.json points at (a no-op), instead
// of broken settings.json.
// Why: SSH remotes use POSIX `.sh` hook paths even when Orca itself is
// running on Windows; never derive remote script syntax from local OS.
await writeManagedScriptRemote(sftp, remoteScriptPath, getManagedScript('posix'))
await writeHooksJsonRemote(sftp, remoteConfigPath, nextConfig)
return {
agent: 'devin',
state: 'installed',
configPath: remoteConfigPath,
managedHooksPresent: true,
detail: null
}
} catch (err) {
return {
agent: 'devin',
state: 'error',
configPath: remoteConfigPath,
managedHooksPresent: false,
detail: err instanceof Error ? err.message : String(err)
}
}
}
remove(): AgentHookInstallStatus {
const configPath = getDevinConfigPath()
const config = readDevinHooksConfig(configPath)
if (!config) {
return {
agent: 'devin',
state: 'error',
configPath,
managedHooksPresent: false,
detail: 'Could not parse Devin config.json'
}
}
const { config: nextConfig, changed } = removeDevinManagedHooks(
config,
getDevinManagedScriptFileName()
)
if (changed) {
writeHooksJson(configPath, nextConfig)
}
return this.getStatus()
}
}
export const devinHookService = new DevinHookService()