diff --git a/README.md b/README.md index 5d5081bb..6abac978 100644 --- a/README.md +++ b/README.md @@ -167,7 +167,7 @@ It connects to `localhost:8080` by default. Each setting comes from the first of | `--output`, `-o` | `GFSCTL_OUTPUT` | `text` (or `json`) | | `--timeout` | `GFSCTL_TIMEOUT` | `30s` | -Use `GFSCTL_TOKEN` or the config file for the token: other local users can read `--token` with `ps`. Statistics answer 503 until mount feeds them to the API, and `gfsctl` prints that error. +Use `GFSCTL_TOKEN` or the config file for the token: other local users can read `--token` with `ps`. gfsctl and the API speak plain HTTP, so for a remote mount don't send the token across the network: tunnel over SSH (`ssh -L 8080:127.0.0.1:8080 node`, then `gfsctl status`) or put a TLS proxy in front of the API. Statistics answer 503 until mount feeds them to the API, and `gfsctl` prints that error. ## Multi-Node Setup diff --git a/cmd/gfsctl/client/client.go b/cmd/gfsctl/client/client.go index c3042244..14b434d3 100644 --- a/cmd/gfsctl/client/client.go +++ b/cmd/gfsctl/client/client.go @@ -13,6 +13,9 @@ import ( "time" ) +// MaxResponseSize bounds how much of one response the client reads. +const MaxResponseSize = 16 << 20 + // Client is an HTTP client for the GoClusterFS API. type Client struct { host string @@ -94,11 +97,15 @@ func (c *Client) doRequest(ctx context.Context, method, path string) ([]byte, er } defer resp.Body.Close() - body, err := io.ReadAll(resp.Body) + body, err := io.ReadAll(io.LimitReader(resp.Body, MaxResponseSize+1)) if err != nil { return nil, fmt.Errorf("read response: %w", err) } + if len(body) > MaxResponseSize { + return nil, fmt.Errorf("response from %s is larger than %d bytes", path, MaxResponseSize) + } + if resp.StatusCode >= 400 { var errResp ErrorResponse if json.Unmarshal(body, &errResp) == nil && errResp.Error != "" { diff --git a/cmd/gfsctl/client/client_test.go b/cmd/gfsctl/client/client_test.go index 75463bac..2c7f0560 100644 --- a/cmd/gfsctl/client/client_test.go +++ b/cmd/gfsctl/client/client_test.go @@ -5,6 +5,7 @@ import ( "errors" "net/http" "net/http/httptest" + "strings" "testing" ) @@ -50,3 +51,19 @@ func TestAPIErrorCarriesStatusAndDetails(t *testing.T) { t.Fatalf("non-JSON 503: %v", err) } } + +func TestOversizedResponseIsRefused(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + chunk := make([]byte, 1<<20) + for written := 0; written <= MaxResponseSize; written += len(chunk) { + if _, err := w.Write(chunk); err != nil { + return + } + } + })) + defer srv.Close() + c := &Client{baseURL: srv.URL, httpClient: srv.Client()} + if _, err := c.GetRaw(context.Background(), "/metrics"); err == nil || !strings.Contains(err.Error(), "larger than") { + t.Fatalf("oversized response: %v", err) + } +} diff --git a/cmd/gfsctl/commands/commands_test.go b/cmd/gfsctl/commands/commands_test.go index 1e590174..2998eccf 100644 --- a/cmd/gfsctl/commands/commands_test.go +++ b/cmd/gfsctl/commands/commands_test.go @@ -284,3 +284,19 @@ func TestVersionNeedsNoServer(t *testing.T) { t.Fatalf("err=%v output=%q", err, out) } } + +func TestConfigShowHidesToken(t *testing.T) { + const token = "abcd1234wxyz" + out, _, err := run(t, "--token", token, "config", "show") + if err != nil { + t.Fatal(err) + } + for _, part := range []string{"abcd", "wxyz", token} { + if strings.Contains(out, part) { + t.Errorf("config show reveals %q:\n%s", part, out) + } + } + if !strings.Contains(out, "set (12 chars)") { + t.Errorf("config show does not say the token is set:\n%s", out) + } +} diff --git a/cmd/gfsctl/commands/config_cmd.go b/cmd/gfsctl/commands/config_cmd.go index 2024d6c2..d8020706 100644 --- a/cmd/gfsctl/commands/config_cmd.go +++ b/cmd/gfsctl/commands/config_cmd.go @@ -135,8 +135,5 @@ func maskToken(token string) string { if token == "" { return "(not set)" } - if len(token) <= 8 { - return "****" - } - return token[:4] + "..." + token[len(token)-4:] + return fmt.Sprintf("set (%d chars)", len(token)) }