Security: Arbitrary code execution via Function constructor in debug agent #8267

Open
opened 2026-02-16 18:09:33 -05:00 by yindo · 1 comment
Owner

Originally created by @riftzen-bit on GitHub (Feb 1, 2026).

Originally assigned to: @thdxr on GitHub.

Summary

While reviewing the codebase, I found a security vulnerability in the debug agent command that allows arbitrary JavaScript code execution.

Location

packages/opencode/src/cli/cmd/debug/agent.ts:99

Issue

The parseParams function attempts to parse user input as JSON first, but if that fails, it falls back to using the Function constructor to evaluate the input as JavaScript:

const parsed = iife(() => {
  try {
    return JSON.parse(trimmed)
  } catch (jsonError) {
    try {
      return new Function(`return (${trimmed})`)()  // <-- Arbitrary code execution
    } catch (evalError) {
      throw new Error(...)
    }
  }
})

This allows an attacker to execute arbitrary JavaScript code with the full privileges of the opencode process by passing malicious input via the --params flag.

Exploitation Example

opencode agent build --params "process.exit(1); return {}"

Impact

  • Severity: High (requires local CLI access)
  • Impact: Code execution with opencode process privileges

Suggested Fix

Remove the Function constructor fallback entirely and only accept valid JSON input:

const parsed = iife(() => {
  try {
    return JSON.parse(trimmed)
  } catch (jsonError) {
    throw new Error(`Failed to parse --params. Must be valid JSON. Error: ${jsonError}`)
  }
})
Originally created by @riftzen-bit on GitHub (Feb 1, 2026). Originally assigned to: @thdxr on GitHub. ## Summary While reviewing the codebase, I found a security vulnerability in the debug agent command that allows arbitrary JavaScript code execution. ## Location `packages/opencode/src/cli/cmd/debug/agent.ts:99` ## Issue The `parseParams` function attempts to parse user input as JSON first, but if that fails, it falls back to using the `Function` constructor to evaluate the input as JavaScript: ```typescript const parsed = iife(() => { try { return JSON.parse(trimmed) } catch (jsonError) { try { return new Function(`return (${trimmed})`)() // <-- Arbitrary code execution } catch (evalError) { throw new Error(...) } } }) ``` This allows an attacker to execute arbitrary JavaScript code with the full privileges of the opencode process by passing malicious input via the `--params` flag. ## Exploitation Example ```bash opencode agent build --params "process.exit(1); return {}" ``` ## Impact - **Severity**: High (requires local CLI access) - **Impact**: Code execution with opencode process privileges ## Suggested Fix Remove the `Function` constructor fallback entirely and only accept valid JSON input: ```typescript const parsed = iife(() => { try { return JSON.parse(trimmed) } catch (jsonError) { throw new Error(`Failed to parse --params. Must be valid JSON. Error: ${jsonError}`) } }) ```
Author
Owner

@github-actions[bot] commented on GitHub (Feb 1, 2026):

This issue might be a duplicate of existing issues. Please check:

  • #6355: RCE and file read vulnerability (different vulnerability but related arbitrary code execution issue)
  • #6361: No trusted workspace functionality leads to arbitrary commands execution on startup (related arbitrary command execution issue)

Feel free to ignore if these don't address your specific case.

@github-actions[bot] commented on GitHub (Feb 1, 2026): This issue might be a duplicate of existing issues. Please check: - #6355: RCE and file read vulnerability (different vulnerability but related arbitrary code execution issue) - #6361: No trusted workspace functionality leads to arbitrary commands execution on startup (related arbitrary command execution issue) Feel free to ignore if these don't address your specific case.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#8267