Skip to content

webfetch skips HTML conversion for mixed-case media types #49322

In review

Description

@Xuxyyy

Description

webfetch skips HTML conversion when the server uses different casing in its media type.

For example, a response with:

Content-Type: Text/HTML; Charset=UTF-8

passes the textual-content check, but format: "markdown" and format: "text" return the raw HTML. The conversion path checks the original header with a case-sensitive includes("text/html").

HTTP media types are case-insensitive. Mixed-case text/html values should behave like lowercase text/html.

Plugins

Built-in webfetch only.

OpenCode version

V2 source at ba753b383f678896eb8df8f243faecc9f4e27c66.

Steps to reproduce

  1. Return <h1>Hello</h1> with Content-Type: Text/HTML; Charset=UTF-8.
  2. Call webfetch with format: "markdown".
  3. Observe raw <h1>Hello</h1> instead of # Hello.

Operating System

macOS arm64.

Activity

  1. opencodejr commented on Oct 28, 2026

    @opencodejr
    Contributor

    Thanks for the clear report. I've reproduced this on the latest dev (950111ff).

    I used the existing webfetch tool tests with a faked response that returns <h1>Hello</h1>:

    • With Content-Type: text/html; charset=utf-8, format: "markdown" returns # Hello and format: "text" returns Hello.
    • With Content-Type: Text/HTML; Charset=UTF-8, both formats return the raw <h1>Hello</h1>.

    It happens as you describe. The textual-content check uses the lowercased media type, but convert() in packages/core/src/tool/plugin/webfetch.ts checks the original header with a case-sensitive includes("text/html"). The script below reproduces it against a source checkout.

    Reproduction script (repro.sh)
    #!/usr/bin/env bash
    # Reproduces #49322: webfetch skips HTML conversion for mixed-case Content-Type.
    # Usage: REPO=/path/to/opencode/checkout bash repro.sh   (after `bun install` in the repo)
    set -euo pipefail
    REPO="${REPO:-/workspace/repo}"
    T="$REPO/packages/core/test/tool-webfetch-mixedcase.repro.test.ts"
    # Reuse the existing webfetch test harness (mock HTTP layer) and add one case.
    head -n "$(grep -n '^describe("WebFetchTool helpers"' "$REPO/packages/core/test/tool-webfetch.test.ts" | cut -d: -f1)" \
      "$REPO/packages/core/test/tool-webfetch.test.ts" | sed '$d' > "$T"
    cat >> "$T" <<'EOF'
    describe("repro #49322", () => {
      for (const ct of ["text/html; charset=utf-8", "Text/HTML; Charset=UTF-8"]) {
        it.effect(`converts HTML with Content-Type: ${ct}`, () =>
          Effect.gen(function* () {
            reset()
            respond = () => Effect.succeed(new Response("<h1>Hello</h1>", { headers: { "content-type": ct } }))
            const registry = yield* Tool.Service
            const md: any = yield* executeTool(registry, call({ url: "https://1.1.1.1", format: "markdown" }))
            const txt: any = yield* executeTool(registry, call({ url: "https://1.1.1.1", format: "text" }))
            console.log(`[${ct}] markdown=${JSON.stringify(md.content?.[0]?.text)} text=${JSON.stringify(txt.content?.[0]?.text)}`)
            expect(md.content?.[0]?.text).toBe("# Hello")
            expect(txt.content?.[0]?.text).toBe("Hello")
          }),
        )
      }
    })
    EOF
    cd "$REPO/packages/core"
    bun test "$T" || status=$?
    rm -f "$T"
    exit "${status:-0}"

    Run it: bash repro.sh

  2. Guembri01 commented on Nov 27, 2026

    @Guembri01

    Same root cause as #63888, fixed together in #66871.

    convert() compared the header with a case-sensitive includes("text/html"), so Text/HTML; Charset=UTF-8 fell through while the textual-content check above it had already accepted the response. It now matches through mimeFrom, which lowercases the type and drops the charset parameter.

    The PR carries your repro as its own test: <h1>Hello</h1> served with Text/HTML; Charset=UTF-8 returns # Hello for markdown and Hello for text; on dev both come back as the raw markup.

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions