diff --git a/README.md b/README.md index eb467b8..46007ff 100644 --- a/README.md +++ b/README.md @@ -95,7 +95,7 @@ rypt decrypt < photos.tgz.rypt | tar -xzf - | --- | --- | | `RYPT_TOKEN` | Required. The rypt API token. There is no flag for it, so it never appears in a process list. | | `RYPT_KEY_ID` | The default key for `encrypt`. | -| `RYPT_API_URL` | The API base URL, default `https://api.rypt.dev`. Plain `http` is accepted only for `localhost`, `127.0.0.1`, and `::1`, and is never sent through a proxy. This variable exists for tests. | +| `RYPT_API_URL` | The API base URL, default `https://api.rypt.dev`. Plain `http` is accepted only for `127.0.0.1` and `[::1]`, written exactly so, and is never sent through a proxy; the name `localhost` needs `https`. This variable exists for tests. | Input and output: diff --git a/cmd/rypt/hardening_test.go b/cmd/rypt/hardening_test.go index 8a0e39c..a51d323 100644 --- a/cmd/rypt/hardening_test.go +++ b/cmd/rypt/hardening_test.go @@ -4,16 +4,20 @@ package main import ( "bytes" + "net" "net/http" "net/http/httptest" "net/url" "os" "os/exec" "path/filepath" + "strings" "sync/atomic" "syscall" "testing" "time" + + "github.com/x6c-co/rypt-cli/internal/cli" ) // TestSecondSignal: a second Ctrl-C during the grace period ends the wait @@ -114,10 +118,12 @@ func TestHangupIgnored(t *testing.T) { } } -// TestPlainHTTPNeverProxied: with HTTP_PROXY set, a plain-http API URL on -// loopback is reached directly, however "localhost" is capitalized. A proxy -// would see the token in clear text. (net/http reads the proxy variables -// once per process, hence a child process per case.) +// TestPlainHTTPNeverProxied: with HTTP_PROXY set, a plain-http API URL is +// reached directly. A proxy would see the token in clear text. Plain http +// is allowed only to the addresses 127.0.0.1 and ::1, never to a name such +// as localhost, which is looked up and could resolve to another machine, so +// http://localhost is a usage error and sends nothing. (net/http reads the +// proxy variables once per process, hence a child process per case.) func TestPlainHTTPNeverProxied(t *testing.T) { t.Parallel() var proxied atomic.Int32 @@ -127,21 +133,27 @@ func TestPlainHTTPNeverProxied(t *testing.T) { })) t.Cleanup(proxy.Close) - for _, host := range []string{"LOCALHOST", "LocalHost", "localhost", "127.0.0.1"} { - api := newE2EAPI(t) - u, err := url.Parse(api.srv.URL) - if err != nil { - t.Fatal(err) - } - u.Host = host + ":" + u.Port() - dir := t.TempDir() - cmd, _, stderr := command(t, dir, api, "encrypt", "-o", "out.rypt") - cmd.Env = append(cmd.Env, "RYPT_API_URL="+u.String(), "HTTP_PROXY="+proxy.URL, "http_proxy="+proxy.URL) + // encrypt runs "rypt encrypt -o out.rypt" in a new directory, with + // RYPT_API_URL set to apiURL and HTTP_PROXY set to the proxy. + encrypt := func(t *testing.T, api *e2eAPI, apiURL string) (code int, dir, stderr string) { + t.Helper() + dir = t.TempDir() + cmd, _, errBuf := command(t, dir, api, "encrypt", "-o", "out.rypt") + cmd.Env = append(cmd.Env, "RYPT_API_URL="+apiURL, "HTTP_PROXY="+proxy.URL, "http_proxy="+proxy.URL) cmd.Stdin = bytes.NewReader([]byte("proxied?")) - if code := exitCode(t, cmd.Run()); code != 0 { + code = exitCode(t, cmd.Run()) + checkStderr(t, errBuf.String()) + return code, dir, errBuf.String() + } + // reached checks that encrypt with the API's own plain-http URL sends + // one wrap request to the API and writes out.rypt. + reached := func(t *testing.T, api *e2eAPI) { + t.Helper() + u := api.srv.URL + code, dir, stderr := encrypt(t, api, u) + if code != 0 { t.Errorf("RYPT_API_URL=%s with HTTP_PROXY set: exit %d\n%s", u, code, stderr) } - checkStderr(t, stderr.String()) if w, _ := api.counts(); w != 1 { t.Errorf("RYPT_API_URL=%s: the API got %d wrap requests, want 1", u, w) } @@ -149,6 +161,57 @@ func TestPlainHTTPNeverProxied(t *testing.T) { t.Errorf("RYPT_API_URL=%s: %v", u, err) } } + + // The subtests run one after another, so the proxy check at the end + // covers all of them. + t.Run("127.0.0.1", func(t *testing.T) { + api := newE2EAPI(t) + if !strings.HasPrefix(api.srv.URL, "http://127.0.0.1:") { + t.Fatalf("unexpected API server URL %s", api.srv.URL) + } + reached(t, api) + }) + t.Run("[::1]", func(t *testing.T) { + ln, err := net.Listen("tcp6", "[::1]:0") + if err != nil { + t.Skipf("no IPv6 loopback: %v", err) + } + api := newE2EAPIOn(t, ln) + if !strings.HasPrefix(api.srv.URL, "http://[::1]:") { + t.Fatalf("unexpected API server URL %s", api.srv.URL) + } + reached(t, api) + }) + // The port is a live API's, so a request that rypt sent would show in + // its counts. Where localhost resolves only to ::1, the request would + // fail to connect instead, and the exit code check catches that. + t.Run("localhost refused", func(t *testing.T) { + api := newE2EAPI(t) + u, err := url.Parse(api.srv.URL) + if err != nil { + t.Fatal(err) + } + for _, host := range []string{"localhost", "LOCALHOST"} { + apiURL := "http://" + host + ":" + u.Port() + code, dir, stderr := encrypt(t, api, apiURL) + if code != cli.ExitUsage { + t.Errorf("RYPT_API_URL=%s: exit %d, want %d\n%s", apiURL, code, cli.ExitUsage, stderr) + } + if !strings.Contains(stderr, "RYPT_API_URL") || !strings.Contains(stderr, "https") { + t.Errorf("RYPT_API_URL=%s: stderr does not say that the URL needs https:\n%s", apiURL, stderr) + } + if _, err := os.Stat(filepath.Join(dir, "out.rypt")); err == nil { + t.Errorf("RYPT_API_URL=%s: out.rypt was written", apiURL) + } + if l := leftovers(t, dir); len(l) != 0 { + t.Errorf("RYPT_API_URL=%s: temporary files left: %q", apiURL, l) + } + } + if w, _ := api.counts(); w != 0 { + t.Errorf("the API got %d wrap requests, want 0", w) + } + }) + if n := proxied.Load(); n != 0 { t.Errorf("the proxy received %d requests, want 0", n) } diff --git a/cmd/rypt/main_test.go b/cmd/rypt/main_test.go index 6de17a9..c209535 100644 --- a/cmd/rypt/main_test.go +++ b/cmd/rypt/main_test.go @@ -9,6 +9,7 @@ import ( "encoding/json" "errors" "io" + "net" "net/http" "net/http/httptest" "os" @@ -102,6 +103,17 @@ func newE2EAPI(t *testing.T) *e2eAPI { return a } +// newE2EAPIOn is newE2EAPI with the server on ln instead of 127.0.0.1. +func newE2EAPIOn(t *testing.T, ln net.Listener) *e2eAPI { + a := &e2eAPI{deks: map[string][]byte{}, entered: make(chan string, 16)} + a.srv = httptest.NewUnstartedServer(http.HandlerFunc(a.serve)) + a.srv.Listener.Close() + a.srv.Listener = ln + a.srv.Start() + t.Cleanup(a.srv.Close) + return a +} + // blockRequests makes every request wait until the test ends or the // client goes away. func (a *e2eAPI) blockRequests(t *testing.T) { diff --git a/go.mod b/go.mod index 4b89f14..3c90956 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/x6c-co/rypt-cli go 1.27.2 require ( - github.com/x6c-co/rypt v0.1.0 + github.com/x6c-co/rypt v0.2.0 golang.org/x/crypto v0.58.0 golang.org/x/sys v0.49.0 golang.org/x/term v0.47.0 diff --git a/go.sum b/go.sum index 3478441..65a97f6 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/x6c-co/rypt v0.1.0 h1:Te7nRwj4bHQHjLQKjqm3LBP0MlEgHzBe0V3dkBkc6Yk= -github.com/x6c-co/rypt v0.1.0/go.mod h1:CdxA8wPiJuR0J/k1h+H5UTdoaoLYJOWmm7HhVtyWFk0= +github.com/x6c-co/rypt v0.2.0 h1:OKL5vbHsjG7sOpTZ1H81JNoyPsz48zmdVUBzcGD5EDA= +github.com/x6c-co/rypt v0.2.0/go.mod h1:CdxA8wPiJuR0J/k1h+H5UTdoaoLYJOWmm7HhVtyWFk0= golang.org/x/crypto v0.58.0 h1:COkYLr4k7nDI4r6QtMML2AfzeIswWHAJthLS6K/M/54= golang.org/x/crypto v0.58.0/go.mod h1:Xh+kl5A+M0gMiWDytH2Hhr5Z7WbNCpv/eB/EFgfSk+w= golang.org/x/sys v0.49.0 h1:XbzkgYJHdqh/8m2Uu0W/dQv8nktxx4BFHp1M0gROTXA= diff --git a/internal/cli/roundtrip_test.go b/internal/cli/roundtrip_test.go index 4b435cd..05b2f74 100644 --- a/internal/cli/roundtrip_test.go +++ b/internal/cli/roundtrip_test.go @@ -328,14 +328,15 @@ func TestKeyIDSources(t *testing.T) { }) } -// TestAPIURL: RYPT_API_URL with a trailing slash, a path prefix, and each -// loopback name that may use plain http. +// TestAPIURL: RYPT_API_URL with and without a trailing slash, with a path +// prefix, and with each address that may use plain http (127.0.0.1 and +// ::1). The name localhost is refused; see TestUsageErrors. func TestAPIURL(t *testing.T) { t.Parallel() h := newHarness(t) h.env["RYPT_API_URL"] = h.api.URL() + "/" ct := h.encryptFixture("a.rypt", []byte("x")) - h.env["RYPT_API_URL"] = strings.Replace(h.api.URL(), "127.0.0.1", "localhost", 1) + h.env["RYPT_API_URL"] = h.api.URL() expect(t, h.runIn(ct, "decrypt"), ExitOK) h.wantCalls(1, 1) diff --git a/internal/cli/usage_test.go b/internal/cli/usage_test.go index 0621a57..ecbb46f 100644 --- a/internal/cli/usage_test.go +++ b/internal/cli/usage_test.go @@ -103,6 +103,7 @@ func TestUsageErrors(t *testing.T) { {"api url http remote", map[string]string{"RYPT_API_URL": "http://api.rypt.dev"}, []string{"encrypt", "IN"}}, {"api url http private ip", map[string]string{"RYPT_API_URL": "http://10.0.0.1:8080"}, []string{"encrypt", "IN"}}, {"api url http 127.0.0.2", map[string]string{"RYPT_API_URL": "http://127.0.0.2:8080"}, []string{"encrypt", "IN"}}, + {"api url http localhost", map[string]string{"RYPT_API_URL": "http://localhost:1"}, []string{"encrypt", "IN"}}, {"api url http localhost lookalike", map[string]string{"RYPT_API_URL": "http://localhost.example.com"}, []string{"encrypt", "IN"}}, {"api url http decrypt", map[string]string{"RYPT_API_URL": "http://example.com"}, []string{"decrypt", "CT"}}, {"api url user info", map[string]string{"RYPT_API_URL": "https://user:hunter2pw@127.0.0.1:1"}, []string{"encrypt", "IN"}}, diff --git a/scripts/probe.go b/scripts/probe.go index df3f9d0..a7858f0 100644 --- a/scripts/probe.go +++ b/scripts/probe.go @@ -12,9 +12,10 @@ // instead searches for the largest aad that wrap accepts on the key, which // takes about twenty requests. // -// RYPT_API_URL is checked with the same rules as rypt's: https, or plain -// http to localhost only. Redirects are not followed, and text from the -// server is printed quoted, so control characters show as escapes. +// RYPT_API_URL is checked by rypt.NewClient: it must use https, or plain +// http only to 127.0.0.1 or [::1], never to the name localhost. Redirects +// are not followed, and text from the server is printed quoted, so control +// characters show as escapes. // // go run scripts/probe.go [-limits] package main @@ -74,10 +75,6 @@ func main() { base = rypt.DefaultBaseURL } baseURL, _ := url.Parse(base) // valid: NewClient accepted it - if baseURL.Scheme == "http" { - // net/http bypasses a proxy only for the exact name "localhost". - baseURL.Host = strings.ToLower(baseURL.Host) - } base = strings.TrimRight(baseURL.String(), "/") fmt.Printf("base URL: %s\n\n", base)