Skip to content

Refuse plain http to the name localhost - #5

Merged
levidurfee merged 1 commit into
developmentfrom
refuse-http-localhost
Nov 1, 2026
Merged

levidurfee merged 1 commit into
developmentfrom
refuse-http-localhost

Conversation

@levidurfee

Copy link
Copy Markdown
Contributor

WithBaseURL allowed plain http for localhost, 127.0.0.1 and ::1. This PR drops the name localhost, so plain http is allowed only when the host is exactly 127.0.0.1 or ::1.

Why

localhost is a name, so it is looked up. The lookup can reach DNS when the hosts file does not list localhost, or when nsswitch.conf puts DNS before files. The resolver (Go's own, and glibc's) then asks for localhost.<search domain> first. Whoever controls that zone can answer with another machine, and that machine receives the API token in clear text.

A review reproduced this with the old code. The setup was an unprivileged network namespace, a fake DNS server, and a CGO_ENABLED=0 build:

  • With no localhost line in the hosts file, http://localhost:8080 sent Authorization: Bearer … to 192.0.2.10.
  • With hosts: dns files in nsswitch.conf, the token reached 192.0.2.10 even though the hosts file listed localhost.

The new code refuses both URLs. IP literals are never looked up, and net/http never sends them through a proxy.

Changes

  • parseBaseURL accepts plain http only for 127.0.0.1 and ::1. The host is no longer lowercased. That step existed only so that net/http's proxy bypass matched LOCALHOST.
  • The WithBaseURL and package docs, the example comment, the security guidance and the CHANGELOG (marked Breaking) are updated.
  • New TestBaseURL rows cover:
    • http://localhost in several spellings, which is refused;
    • https://localhost, which is still accepted;
    • other spellings of loopback (127.1, 2130706433, [::ffff:127.0.0.1], [0:0:0:0:0:0:0:1], [::1%25lo]), which are refused;
    • an uppercase scheme, which is accepted and normalized.
  • A new check asserts the exact error message.

Review

A three-lens review ran on this change, and each finding was checked by a separate verifier:

  • Bypasses: about 130 candidate URLs were tried. Every accepted http URL dialed 127.0.0.1 or ::1, with no proxy, under both the default client and a cloned default transport.
  • The stated reason: checked against Go 1.27's resolver source, and reproduced as described above.
  • Downstream impact: see below.

Follow-up when consumers upgrade

The consumers all pin v0.1.0 today, so nothing breaks until they upgrade. These places rely on http://localhost:

  • rypt-cli:
    • cmd/rypt/hardening_test.go:130, the LOCALHOST, LocalHost and localhost cases;
    • internal/cli/roundtrip_test.go:338;
    • README.md:98;
    • scripts/probe.go:15-16 and its lowercasing block at lines 77-80.
  • rypt-store: internal/keys/provider/provider_test.go:130 and :196.
  • age-plugin-rypt: the comment at api.go:103. The plugin already enforces the same rule.

This is a breaking change, so per CONTRIBUTING.md the next release would be a minor bump (v0.2.0).

🤖 Generated with Claude Code

https://claude.ai/code/session_01NYjhaGCRVp9GXLUuUZv8g1

WithBaseURL allowed plain http for localhost, 127.0.0.1 and ::1. A name
is looked up, and the lookup can reach DNS: when the hosts file does not
list localhost, or when DNS comes first in nsswitch. The resolver then
asks for localhost.<search domain> first, so whoever controls that zone
can answer with another machine, which receives the token in clear text.
A review reproduced this with the old code in a network namespace.

Plain http is now allowed only when the host is exactly 127.0.0.1 or
::1. IP literals are never looked up, and net/http never proxies them.
The host no longer needs lowercasing, which existed only so that
net/http's proxy bypass matched "LOCALHOST".

This breaks callers that pass http://localhost; they can use
http://127.0.0.1, http://[::1] or https://localhost.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NYjhaGCRVp9GXLUuUZv8g1
@levidurfee
levidurfee merged commit 362d7db into development Nov 1, 2026
3 checks passed
@levidurfee
levidurfee deleted the refuse-http-localhost branch November 1, 2026 23:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant