Skip to content

CLI machine pairing incorrectly classifies trailing-dot localhost as remote #67419

Done

Description

@AdemGhalleb

Description

The CLI's isLoopback() helper does not correctly recognize localhost hostnames ending with a trailing DNS dot.

For example, localhost. is a valid absolute DNS name referring to localhost, but the current implementation classifies it as a non-loopback hostname.

This can affect machine address selection and pairing, potentially causing a local address to be treated as remotely reachable.

Expected behavior

The hostname should be recognized as loopback, just like localhost.

Actual behavior

The hostname is classified as non-loopback because the trailing DNS dot is not normalized.

Additional context

The same helper should distinguish valid IPv4 loopback addresses from arbitrary hostnames beginning with 127..

For example:

  • 127.0.0.1 should be loopback.
  • 127.example.com should not be classified as loopback.

I have a proposed fix that normalizes trailing DNS dots, validates IPv4 addresses, and adds regression tests.

Plugins

none

OpenCode version

lts

Steps to reproduce

Call isLoopback() with:

isLoopback("localhost.")

Screenshot

No response

Operating System

Windows11

Terminal

Windows terminal

Activity

  1. added
    area/cli
    type/bug
    version/v2
    on Nov 30, 2026
  2. opencodejr commented on Nov 30, 2026

    @opencodejr
    Contributor

    Thanks for the report. I reproduced both cases on the released CLI (v2.4.3, the latest release; the issue didn't give an exact version) using opencode pair --url, which prints a QR code only for addresses it treats as reachable from other devices:

    • http://localhost:4096 and http://127.0.0.1:4096 print no QR code, which is correct.
    • http://localhost.:4096 prints a QR code, so it is treated as remote.
    • http://127.example.com:4096 prints no QR code, so it is treated as loopback.

    It still happens on the latest dev (c5341fe). The script below runs these four cases. With opencode machine add, the misclassification only changes the order of the addresses, because the iroh entry makes it say "Works from any network." either way.

    Reproduction script (repro.sh)
    #!/bin/sh
    # Reproduces #67419: `opencode pair --url` decides whether to print a QR code
    # by checking whether the URL's host is loopback (isLoopback). Loopback URLs get
    # no QR code. `localhost.` is wrongly treated as remote (a QR code is printed),
    # and `127.example.com` is wrongly treated as loopback (no QR code).
    # Usage: sh repro.sh            (uses `opencode` from PATH)
    #        OC_DEV=/path/to/checkout sh repro.sh   (runs the CLI from source)
    export HOME="${REPRO_HOME:-/tmp/opencode/tmp/h1}"
    mkdir -p "$HOME"
    if [ -n "$OC_DEV" ]; then
      # Run from packages/cli so the background service spawned from source resolves its JSX runtime.
      cd "$OC_DEV/packages/cli"
      OC="bun --conditions=browser --preload=$OC_DEV/packages/tui/node_modules/@opentui/solid/scripts/preload.js $OC_DEV/packages/cli/src/index.ts"
    else
      OC=opencode
    fi
    $OC --version
    for u in http://localhost:4096 http://localhost.:4096 http://127.0.0.1:4096 http://127.example.com:4096; do
      out=$(timeout 60 $OC pair --url "$u" 2>&1)
      if ! echo "$out" | grep -q '/auth/connect/'; then qr="ERROR: $(echo "$out" | head -2)"
      elif echo "$out" | grep -q '██'; then qr="QR code printed (treated as remote)"
      else qr="no QR code (treated as loopback)"; fi
      echo "$u -> $qr"
    done

    Run it: sh repro.sh

  3. four1er commented on Dec 1, 2026

    @four1er

    I'm picking this up — reproducing the issue now and will open a PR with a fix.

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

    area/cliplatform/windowsreproducedtype/bugversion/v2

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions