Skip to content

Injected <env> block reports the gateway's own working directory, misleading client agents #173

Closed

Description

@Swcmb

Summary

The system prompt injected into free-tier requests carries an <env> block whose Working directory and Workspace root are the gateway process's own cwd, resolved once via os.Getwd() in opencodeEnvironment() (internal/gateway/opencode_prompt.go:51). Every client receives the gateway's directory, never its own, so agents that trust this block operate on the wrong path.

Actual behavior

A Claude Code client launched with cwd D:\Projects connected through a gateway running from D:\ai-configs\opencode2api receives, at the top of its context:

<env>
  Working directory: D:\ai-configs\opencode2api
  Workspace root folder: D:\ai-configs\opencode2api
  Is directory a git repo: yes
  Platform: win32
  Today's date: ...
</env>

The client's own (correct) environment section appears later in the prompt, so the two sources conflict. In practice an agent followed the injected block and ran a repo-wide command against the gateway's directory instead of the client's project.

Expected behavior

The injected env block carries only facts the gateway can state truthfully. Fields it cannot know (client cwd, git-repo status) are omitted, or the values are operator-configurable.

Steps to reproduce

  1. Start the gateway from any directory, e.g. cd /somewhere && ./opencode2api -config config.json.
  2. Send a free-model request from a client running in a different directory.
  3. Observe the upstream request body (or the model's view): <env> reports /somewhere for every client regardless of their actual cwd.

Environment

  • Version: v1.4.7 (confirmed identical at HEAD b296a61)
  • Platform: Windows 11, gateway started via scheduled task; reproduces from any launch directory
  • Configuration: anonymous: true, free model, v2 lane

Root cause

internal/gateway/opencode_prompt.go:51-61:

opencodeEnvOnce.Do(func() {
    directory := ""
    if current, err := os.Getwd(); err == nil {
        directory = filepath.Clean(strings.TrimSpace(current))
    }
    if directory == "" || directory == "." {
        directory = "/workspace"
    }
    opencodeWorkDir = directory
})

os.Getwd() returns the gateway process's directory. The same block hardcodes Is directory a git repo: yes regardless of what the client has open. For a multi-user or remote deployment the value also leaks the operator's local paths.

Proposed fix

Either of:

  1. Omit client-unknowable fields from the injected env: keep model, Platform, Today's date; drop Working directory, Workspace root folder, and the hardcoded git-repo line. This preserves the abuse-check shape of the prompt without asserting false facts.
  2. Configurable override: an optional config field (e.g. under models or upstream) letting operators set a neutral directory string for the injected block.

Option 1 is sufficient and needs no configuration.

Verification

  • Reproduced the wrong-directory injection against a local v1.4.7 gateway (client cwd and injected value compared in the transcript).
  • Confirmed via source read that sync.Once pins the value for the process lifetime, so restarting the gateway from a different directory is the only way to change it.

Activity

  1. Swcmb commented on Nov 8, 2026

    @Swcmb
    Author

    Refining the proposed fix after a closer look at the shape question: omitting fields changes the block's structure, and a safer variant is to keep every field and pin the directory to /workspace — the code's own fallback when os.Getwd() fails ("the official container path", opencode_prompt.go:57-59).

    Why this preserves the abuse-check shape:

    • The upstream checks cannot match the value of Working directory — every legitimate CLI session reports a different cwd, and an official CLI running in a container reports exactly /workspace. Sessions with that value have necessarily been accepted before. What the checks can anchor on is the base prompt text, the field labels and structure, the official tool set, and streaming — all preserved when only the value is pinned.
    • Filling /workspace keeps the block field-for-field identical to a containerized official CLI session: same labels, same field count, same order.
    • It also fixes the operator-path leak for remote and multi-user deployments, which option 2 (configurable override) only solves when the operator bothers to configure it.

    Minimal diff:

    opencodeEnvOnce.Do(func() {
        opencodeWorkDir = "/workspace"
    })

    (Is directory a git repo: yes can stay: real sessions report both values depending on what they have open, so the upstream cannot require one.)

  2. jasonxu114514 commented on Nov 16, 2026

    @jasonxu114514
    Owner

    Implemented the configurable alternative on main in 14faec7: 14faec7 . Set upstream.prompt_environment with working_directory, workspace_root, platform and is_git_repo; README documents defaults and usage. Official prompt/env structure is preserved and shell descriptions follow the explicit platform. Defaults remain unchanged. This is deployment-wide manual configuration, not automatic client detection; existing official client prompts are untouched. All shaping paths share it and WebUI saves preserve it. Offline tests, vet and Windows/Linux builds passed; zz_ tests were removed afterward as requested. Arbitrary overrides have not been live-validated upstream. Closing as addressed through the optional configuration approach; operators must opt in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions