diff --git a/README.md b/README.md index 4717b2c..84c0712 100644 --- a/README.md +++ b/README.md @@ -976,6 +976,7 @@ automation use. | `ZENNOTES_VAULT` | Default local vault root | | `ZENNOTES_SERVER` | Default server selector; precedes `ZENNOTES_VAULT` | | `ZENNOTES_REMOTE_TOKEN` | Remote API token for scripts; explicit `--token` wins | +| `ZENNOTES_REMOTE_HEADERS` | Extra headers for a server behind an access gateway such as Cloudflare Access, one `Name: value` per line | | `ZENNOTES_WORKSPACE_SOURCE` | CLI source: `app` or `terminal` | | `ZENNOTES_CONFIG_DIR` | Override configuration, workspace, and credential locations; also isolates managed instances | | `XDG_CONFIG_HOME` | Base directory for portable configuration when no explicit override is set | @@ -989,6 +990,37 @@ environment, then the applicable saved credentials. `zn connect` stores them separately from workspace profiles. MCP clients can therefore use a saved connection without embedding tokens in their own configuration. +### A server behind Cloudflare Access or another gateway + +An access gateway in front of the server turns away every request without its +credentials. `zn`, `zn tui` and `zn mcp` send them as extra headers on every +request, the live-update WebSocket included, from `ZENNOTES_REMOTE_HEADERS`: +one `Name: value` per line, blank lines and `#` lines skipped. For a +Cloudflare Access service token: + +```sh +export ZENNOTES_REMOTE_HEADERS=$'CF-Access-Client-Id: .access\nCF-Access-Client-Secret: ' +zn list --server https://notes.example.com +``` + +An MCP client's JSON config can only write the line break as `\n`, which +counts as one: + +```json +"env": { + "ZENNOTES_REMOTE_HEADERS": "CF-Access-Client-Id: .access\nCF-Access-Client-Secret: " +} +``` + +Keep the server's own token in `--token`, `ZENNOTES_REMOTE_TOKEN` or +`zn connect`: the gateway checks the headers, then the server checks the +token. Authorization and the headers the connection sets itself (Host, +Content-Type, Upgrade and the like) are refused, and a value that does not +parse stops the command before anything is sent. Headers are never written to +`credentials.toml`. The desktop app takes the same lines in its connect +dialog and keeps them in the system keychain, which zn cannot read, so give +zn the variable too. + ## Troubleshooting ### Copy a TUI version report diff --git a/internal/cli/help.go b/internal/cli/help.go index 3d5eacf..bdc4d80 100644 --- a/internal/cli/help.go +++ b/internal/cli/help.go @@ -149,6 +149,7 @@ var globalFlags = []helpRow{ var environmentRows = []helpRow{ {"ZENNOTES_VAULT", "Default vault root when --vault is not given", ""}, {"ZENNOTES_REMOTE_TOKEN", "Server token for CI and scripts; `zn connect` stores one for you otherwise", ""}, + {"ZENNOTES_REMOTE_HEADERS", "Extra headers for a server behind an access gateway such as Cloudflare Access, one \"Name: value\" per line", ""}, {"ZENNOTES_SERVER", "Default server when neither --vault nor --server is given (otherwise zn follows the vault the app has open)", ""}, {"ZENNOTES_CONFIG_DIR", "Override the ZenNotes config directory", ""}, {"ZENNOTES_WORKSPACE_SOURCE", "Default workspace source: app or terminal. `zn tui` uses terminal unless its flag overrides it", ""}, diff --git a/internal/remote/assets.go b/internal/remote/assets.go index 43a182f..4595850 100644 --- a/internal/remote/assets.go +++ b/internal/remote/assets.go @@ -10,7 +10,6 @@ import ( "mime/multipart" "net/http" "path/filepath" - "strings" ) func (c *Client) ImportAsset(ctx context.Context, note, name string, body io.Reader) (vault.ImportedAsset, error) { @@ -38,18 +37,11 @@ func (c *Client) ImportAsset(ctx context.Context, note, name string, body io.Rea return vault.ImportedAsset{}, err } req.Header.Set("Content-Type", writer.FormDataContentType()) - if c.AuthToken != "" { - req.Header.Set("Authorization", "Bearer "+c.AuthToken) - } - resp, err := c.http.Do(req) + resp, err := c.send(req, "/api/assets/upload") if err != nil { return vault.ImportedAsset{}, err } defer resp.Body.Close() - if resp.StatusCode < 200 || resp.StatusCode > 299 { - raw, _ := io.ReadAll(io.LimitReader(resp.Body, 64<<10)) - return vault.ImportedAsset{}, &RequestError{Status: resp.StatusCode, Message: requestErrorMessage(c.BaseURL, "/api/assets/upload", resp.StatusCode, http.StatusText(resp.StatusCode), strings.TrimSpace(string(raw)))} - } var out vault.ImportedAsset err = json.NewDecoder(io.LimitReader(resp.Body, 1<<20)).Decode(&out) return out, err diff --git a/internal/remote/client.go b/internal/remote/client.go index 5effa8c..cb5a186 100644 --- a/internal/remote/client.go +++ b/internal/remote/client.go @@ -65,10 +65,16 @@ func ConnectionErrorMessage(baseURL string, err error) string { return fmt.Sprintf("Could not connect to the ZenNotes server at %s. Make sure the server is running and the URL is correct.%s%s", baseURL, detail, hint) } -func requestErrorMessage(baseURL, path string, status int, statusText, text string) string { +func requestErrorMessage(baseURL, path string, status int, statusText, contentType, text string) string { if status == 401 { return fmt.Sprintf("The ZenNotes server rejected the connection. Check the auth token for %s and try again.", baseURL) } + // The ZenNotes server answers in plain text. An HTML 403 came from + // something in front of it, most often an access gateway turning away a + // request without its headers; its page would only be noise here. + if status == http.StatusForbidden && strings.Contains(strings.ToLower(contentType), "text/html") { + return GatewayRefusedMessage(baseURL) + } msg := fmt.Sprintf("Remote server request failed (%d %s) for %s", status, statusText, path) if text != "" { msg += ": " + text @@ -80,16 +86,64 @@ func requestErrorMessage(baseURL, path string, status int, statusText, text stri type Client struct { BaseURL string AuthToken string - http *http.Client + // Headers go with every request and the change feed's handshake, for an + // access gateway in front of the server (see headers.go). + Headers []Header + headersErr error + http *http.Client } -// NewClient binds a client to a server and an optional token. +// NewClient binds a client to a server and an optional token. It sends the +// extra headers from ZENNOTES_REMOTE_HEADERS; a value that does not parse +// fails every request with the reason, before anything is sent. func NewClient(baseURL, authToken string) *Client { + headers, err := EnvHeaders() return &Client{ - BaseURL: NormalizeBaseURL(baseURL), - AuthToken: authToken, - http: &http.Client{Timeout: 60 * time.Second}, + BaseURL: NormalizeBaseURL(baseURL), + AuthToken: authToken, + Headers: headers, + headersErr: err, + http: &http.Client{Timeout: 60 * time.Second}, + } +} + +// requestHeaders is what every request to the server carries: the token, +// then the extra headers, which never replace one already set. +func (c *Client) requestHeaders(h http.Header) error { + if c.headersErr != nil { + return c.headersErr + } + if c.AuthToken != "" && h.Get("Authorization") == "" { + h.Set("Authorization", "Bearer "+c.AuthToken) + } + ApplyHeaders(h, c.Headers) + return nil +} + +// send performs a prepared request and turns every failure into the error a +// caller shows: the network, a gateway's sign-in redirect, a non-2xx answer. +// A returned response is 2xx and the caller closes it. +func (c *Client) send(req *http.Request, path string) (*http.Response, error) { + if err := c.requestHeaders(req.Header); err != nil { + return nil, err + } + resp, err := c.http.Do(req) + if err != nil { + return nil, errors.New(ConnectionErrorMessage(c.BaseURL, err)) + } + if msg := gatewayRedirectMessage(c.BaseURL, resp.Request.URL); msg != "" { + resp.Body.Close() + return nil, &RequestError{Status: http.StatusForbidden, Message: msg} + } + if resp.StatusCode < 200 || resp.StatusCode > 299 { + defer resp.Body.Close() + text, _ := io.ReadAll(io.LimitReader(resp.Body, 64<<10)) + return nil, &RequestError{ + Status: resp.StatusCode, + Message: requestErrorMessage(c.BaseURL, path, resp.StatusCode, http.StatusText(resp.StatusCode), resp.Header.Get("Content-Type"), strings.TrimSpace(string(text))), + } } + return resp, nil } // NewClientWithTransport is a client whose requests go through transport instead @@ -118,24 +172,14 @@ func (c *Client) do(ctx context.Context, method, path string, body any, out any) if err != nil { return err } - if c.AuthToken != "" { - req.Header.Set("Authorization", "Bearer "+c.AuthToken) - } if body != nil { req.Header.Set("Content-Type", "application/json") } - resp, err := c.http.Do(req) + resp, err := c.send(req, path) if err != nil { - return errors.New(ConnectionErrorMessage(c.BaseURL, err)) + return err } defer resp.Body.Close() - if resp.StatusCode < 200 || resp.StatusCode > 299 { - text, _ := io.ReadAll(io.LimitReader(resp.Body, 64<<10)) - return &RequestError{ - Status: resp.StatusCode, - Message: requestErrorMessage(c.BaseURL, path, resp.StatusCode, http.StatusText(resp.StatusCode), strings.TrimSpace(string(text))), - } - } if out == nil || resp.StatusCode == http.StatusNoContent { return nil } @@ -155,18 +199,11 @@ func (c *Client) GetRaw(ctx context.Context, path string) ([]byte, error) { if err != nil { return nil, err } - if c.AuthToken != "" { - req.Header.Set("Authorization", "Bearer "+c.AuthToken) - } - resp, err := c.http.Do(req) + resp, err := c.send(req, path) if err != nil { - return nil, errors.New(ConnectionErrorMessage(c.BaseURL, err)) + return nil, err } defer resp.Body.Close() - if resp.StatusCode < 200 || resp.StatusCode > 299 { - text, _ := io.ReadAll(io.LimitReader(resp.Body, 64<<10)) - return nil, &RequestError{Status: resp.StatusCode, Message: requestErrorMessage(c.BaseURL, path, resp.StatusCode, http.StatusText(resp.StatusCode), strings.TrimSpace(string(text)))} - } return io.ReadAll(io.LimitReader(resp.Body, 64<<20)) } diff --git a/internal/remote/headers.go b/internal/remote/headers.go new file mode 100644 index 0000000..3a924af --- /dev/null +++ b/internal/remote/headers.go @@ -0,0 +1,188 @@ +package remote + +import ( + "fmt" + "net/http" + "net/url" + "os" + "regexp" + "strings" + "sync" +) + +// Extra HTTP headers sent with every request to a server (ZenNotes/zennotes +// #1091). They exist for an access gateway in front of the server, such as a +// Cloudflare Access service token (CF-Access-Client-Id and +// CF-Access-Client-Secret), which turns away any request without them before +// the server sees it. +// +// The format and every rule here mirror packages/shared-domain/src/ +// remote-headers.ts in the desktop app, which keeps the same lines in the OS +// keychain and hands its bundled zn and MCP server the same variable: one +// `Name: value` per line, the way curl and every proxy's documentation write +// a header. zn reads them only from the environment. credentials.toml holds +// tokens and nothing else, and the values are as secret as a token. + +// HeadersEnv is the variable zn, zn tui and zn mcp read the headers from. +const HeadersEnv = "ZENNOTES_REMOTE_HEADERS" + +// MaxHeaders is more than any gateway needs; a pasted file is likelier. +const MaxHeaders = 20 + +// Header is one extra request header. +type Header struct { + Name string + Value string +} + +// RFC 9110 field-name token characters. +var headerNameRe = regexp.MustCompile("^[!#$%&'*+.^_`|~0-9A-Za-z-]+$") + +// Names the connection itself owns: Go's transport and the WebSocket +// handshake set these, and a second copy breaks the request instead of +// reaching the gateway. Authorization carries the server's own token. +var reservedHeaderNames = map[string]bool{ + "authorization": true, + "connection": true, + "content-length": true, + "content-type": true, + "host": true, + "keep-alive": true, + "proxy-connection": true, + "te": true, + "trailer": true, + "transfer-encoding": true, + "upgrade": true, +} + +func reservedReason(name string) string { + lower := strings.ToLower(name) + if lower == "authorization" { + return "use the auth token for Authorization" + } + if reservedHeaderNames[lower] || strings.HasPrefix(lower, "sec-websocket-") { + return name + " is set by the connection itself" + } + return "" +} + +// hasControl reports a control character other than tab: a CR or LF would +// split the request. +func hasControl(value string) bool { + for _, r := range value { + if (r < 0x20 && r != '\t') || r == 0x7f { + return true + } + } + return false +} + +// ParseHeaders reads `Name: value` lines. Blank lines and lines starting with +// `#` are skipped. The first problem wins and names its line; nothing is +// half-accepted. +func ParseHeaders(text string) ([]Header, error) { + text = strings.ReplaceAll(text, "\r\n", "\n") + text = strings.ReplaceAll(text, "\r", "\n") + headers := []Header{} + seen := map[string]bool{} + for index, raw := range strings.Split(text, "\n") { + line := strings.TrimSpace(raw) + if line == "" || strings.HasPrefix(line, "#") { + continue + } + where := fmt.Sprintf("Line %d", index+1) + colon := strings.Index(line, ":") + if colon <= 0 { + return nil, fmt.Errorf("%s: write each header as Name: value.", where) + } + name := strings.TrimSpace(line[:colon]) + value := strings.TrimSpace(line[colon+1:]) + if !headerNameRe.MatchString(name) { + return nil, fmt.Errorf("%s: %q is not a valid header name.", where, name) + } + if reason := reservedReason(name); reason != "" { + return nil, fmt.Errorf("%s: %s.", where, reason) + } + if value == "" { + return nil, fmt.Errorf("%s: %s has no value.", where, name) + } + if hasControl(value) { + return nil, fmt.Errorf("%s: %s has a control character in its value.", where, name) + } + key := strings.ToLower(name) + if seen[key] { + return nil, fmt.Errorf("%s: %s is listed twice.", where, name) + } + seen[key] = true + headers = append(headers, Header{Name: name, Value: value}) + if len(headers) > MaxHeaders { + return nil, fmt.Errorf("At most %d headers are allowed.", MaxHeaders) + } + } + return headers, nil +} + +// HeadersFromEnv reads HeadersEnv through getenv. Unset or blank means none. +// A JSON env block (an MCP client's config) can only carry a line break as +// the two characters `\n`, so those count as one. +func HeadersFromEnv(getenv func(string) string) ([]Header, error) { + raw := getenv(HeadersEnv) + if strings.TrimSpace(raw) == "" { + return nil, nil + } + headers, err := ParseHeaders(strings.ReplaceAll(raw, `\n`, "\n")) + if err != nil { + return nil, fmt.Errorf("%s: %w", HeadersEnv, err) + } + return headers, nil +} + +var ( + envHeadersOnce sync.Once + envHeaders []Header + envHeadersErr error +) + +// EnvHeaders is HeadersFromEnv for this process, read once. A value that does +// not parse is an error every request returns before it is sent: running on +// without the headers would only trade this message for the gateway's 403. +func EnvHeaders() ([]Header, error) { + envHeadersOnce.Do(func() { + envHeaders, envHeadersErr = HeadersFromEnv(os.Getenv) + }) + return envHeaders, envHeadersErr +} + +// ApplyHeaders adds the headers to h. They never replace one already set. +func ApplyHeaders(h http.Header, headers []Header) { + for _, header := range headers { + if h.Get(header.Name) == "" { + h.Set(header.Name, header.Value) + } + } +} + +// GatewayRefusedMessage names a gateway in front of the server that turned a +// request away. The ZenNotes server answers in plain text, so an HTML 403 +// came from something in front of it. +func GatewayRefusedMessage(baseURL string) string { + return fmt.Sprintf("Something in front of the ZenNotes server at %s refused the request. If it sits behind an access gateway such as Cloudflare Access, set its headers in %s.", baseURL, HeadersEnv) +} + +// gatewayRedirectMessage is non-empty when a request ended on another host: +// a gateway such as Cloudflare Access sends a request without its headers to +// a sign-in page, and the HTML there would otherwise fail as JSON. A redirect +// that stays on the server's host (http to https) is fine. +func gatewayRedirectMessage(baseURL string, final *url.URL) string { + if final == nil { + return "" + } + base, err := url.Parse(baseURL) + if err != nil { + return "" + } + if strings.EqualFold(base.Hostname(), final.Hostname()) { + return "" + } + return fmt.Sprintf("The ZenNotes server at %s sent the request to a sign-in page at %s. If the server sits behind an access gateway such as Cloudflare Access, set its headers in %s.", baseURL, final.Hostname(), HeadersEnv) +} diff --git a/internal/remote/headers_test.go b/internal/remote/headers_test.go new file mode 100644 index 0000000..969d821 --- /dev/null +++ b/internal/remote/headers_test.go @@ -0,0 +1,253 @@ +package remote + +import ( + "context" + "errors" + "net/http" + "net/http/httptest" + "net/url" + "reflect" + "strings" + "sync" + "testing" + "time" + + "github.com/coder/websocket" +) + +// The cases mirror packages/shared-domain/src/remote-headers.test.ts in the +// desktop app (ZenNotes/zennotes #1091): both read the same lines. + +const cloudflare = "CF-Access-Client-Id: abc123.access\nCF-Access-Client-Secret: s3cr3t" + +var cloudflareHeaders = []Header{ + {Name: "CF-Access-Client-Id", Value: "abc123.access"}, + {Name: "CF-Access-Client-Secret", Value: "s3cr3t"}, +} + +func TestParseHeadersReadsAServiceToken(t *testing.T) { + got, err := ParseHeaders(cloudflare) + if err != nil || !reflect.DeepEqual(got, cloudflareHeaders) { + t.Fatalf("got %v, %v", got, err) + } +} + +func TestParseHeadersSkipsBlanksAndCommentsAndKeepsColonsInTheValue(t *testing.T) { + got, err := ParseHeaders("\n# gateway\n X-Trace : a:b:c \r\n\n") + if err != nil || !reflect.DeepEqual(got, []Header{{Name: "X-Trace", Value: "a:b:c"}}) { + t.Fatalf("got %v, %v", got, err) + } + if got, err := ParseHeaders(" \n "); err != nil || len(got) != 0 { + t.Fatalf("empty box: %v, %v", got, err) + } +} + +func TestParseHeadersNamesTheLineThatIsWrong(t *testing.T) { + cases := map[string]string{ + "X-A: 1\nno colon here": "Line 2: write each header as Name: value.", + "X-Empty:": "Line 1: X-Empty has no value.", + "Authorization: Bearer x": "Line 1: use the auth token for Authorization.", + "X-A: 1\nx-a: 2": "Line 2: x-a is listed twice.", + } + for input, want := range cases { + if _, err := ParseHeaders(input); err == nil || err.Error() != want { + t.Errorf("%q: got %v, want %q", input, err, want) + } + } + for _, input := range []string{"Bad Name: 1", "Host: x", "content-length: 1", "Upgrade: x", "Sec-WebSocket-Key: x", "Connection: x", "X-A: one\x00two"} { + if _, err := ParseHeaders(input); err == nil { + t.Errorf("%q: expected a refusal", input) + } + } +} + +func TestParseHeadersCapsTheList(t *testing.T) { + var lines []string + for i := 0; i <= MaxHeaders; i++ { + lines = append(lines, "X-H"+strings.Repeat("a", i+1)+": v") + } + if _, err := ParseHeaders(strings.Join(lines, "\n")); err == nil { + t.Fatal("expected the cap to refuse 21 headers") + } +} + +func TestHeadersFromEnv(t *testing.T) { + env := func(value string) func(string) string { + return func(key string) string { + if key == HeadersEnv { + return value + } + return "" + } + } + if got, err := HeadersFromEnv(env("")); err != nil || got != nil { + t.Fatalf("unset: %v, %v", got, err) + } + for _, raw := range []string{cloudflare, strings.Replace(cloudflare, "\n", `\n`, 1)} { + got, err := HeadersFromEnv(env(raw)) + if err != nil || !reflect.DeepEqual(got, cloudflareHeaders) { + t.Fatalf("%q: got %v, %v", raw, got, err) + } + } + _, err := HeadersFromEnv(env("nonsense")) + if err == nil || err.Error() != "ZENNOTES_REMOTE_HEADERS: Line 1: write each header as Name: value." { + t.Fatalf("got %v", err) + } +} + +func TestApplyHeadersNeverReplacesOneAlreadySet(t *testing.T) { + h := http.Header{} + h.Set("X-A", "kept") + ApplyHeaders(h, []Header{{Name: "X-A", Value: "ignored"}, {Name: "CF-Access-Client-Id", Value: "id"}}) + if h.Get("X-A") != "kept" || h.Get("CF-Access-Client-Id") != "id" { + t.Fatal(h) + } +} + +// gatedServer stands in for ZenNotes behind a gateway: without both headers it +// answers 403 with an HTML page, the way Cloudflare Access does. +func gatedServer(t *testing.T) (*httptest.Server, func() []string) { + t.Helper() + var mu sync.Mutex + var seen []string + passes := func(r *http.Request) bool { + return r.Header.Get("CF-Access-Client-Id") == "abc123.access" && r.Header.Get("CF-Access-Client-Secret") == "s3cr3t" + } + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + ok := passes(r) + mu.Lock() + seen = append(seen, r.Method+" "+r.URL.Path+map[bool]string{true: " pass", false: " blocked"}[ok]) + mu.Unlock() + if !ok { + w.Header().Set("Content-Type", "text/html") + w.WriteHeader(http.StatusForbidden) + _, _ = w.Write([]byte("Forbidden")) + return + } + switch r.URL.Path { + case "/api/vault": + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"root":"/v","name":"v"}`)) + case "/api/assets/raw": + _, _ = w.Write([]byte("bytes")) + case "/api/assets/upload": + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"path":"assets/a.png"}`)) + case "/api/watch": + conn, err := websocket.Accept(w, r, nil) + if err != nil { + return + } + defer conn.CloseNow() + ctx, cancel := context.WithTimeout(r.Context(), 3*time.Second) + defer cancel() + _ = conn.Write(ctx, websocket.MessageText, []byte(`{"kind":"add","path":"gated.md"}`)) + _, _, _ = conn.Read(ctx) + default: + http.NotFound(w, r) + } + })) + t.Cleanup(server.Close) + return server, func() []string { + mu.Lock() + defer mu.Unlock() + return append([]string(nil), seen...) + } +} + +func TestClientSendsTheHeadersOnEveryKindOfRequest(t *testing.T) { + server, seen := gatedServer(t) + client := NewClient(server.URL, "tok") + client.Headers, client.headersErr = cloudflareHeaders, nil + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + var info map[string]any + if err := client.Get(ctx, "/api/vault", &info); err != nil || info["name"] != "v" { + t.Fatalf("json: %v %v", info, err) + } + if raw, err := client.ReadAsset(ctx, "a.png"); err != nil || string(raw) != "bytes" { + t.Fatalf("raw: %q %v", raw, err) + } + if out, err := client.ImportAsset(ctx, "inbox/n.md", "a.png", strings.NewReader("x")); err != nil || out.Path != "assets/a.png" { + t.Fatalf("upload: %v %v", out, err) + } + events, err := client.WatchChanges(ctx) + if err != nil { + t.Fatalf("watch: %v", err) + } + if ev := <-events; ev.Path != "gated.md" || ev.Err != nil { + t.Fatalf("watch event: %+v", ev) + } + for _, line := range seen() { + if !strings.HasSuffix(line, " pass") { + t.Fatalf("a request missed the headers: %v", seen()) + } + } + if len(seen()) != 4 { + t.Fatalf("expected 4 requests, got %v", seen()) + } +} + +func TestWithoutTheHeadersTheRefusalNamesTheGateway(t *testing.T) { + server, _ := gatedServer(t) + client := NewClient(server.URL, "tok") + client.Headers, client.headersErr = nil, nil + err := client.Get(context.Background(), "/api/vault", nil) + if StatusOf(err) != http.StatusForbidden || !strings.Contains(err.Error(), "access gateway such as Cloudflare Access") || strings.Contains(err.Error(), "") { + t.Fatalf("got %v", err) + } + if _, err := client.WatchChanges(context.Background()); err == nil { + t.Fatal("the handshake should be refused without the headers") + } +} + +func TestABadVariableFailsBeforeAnyRequest(t *testing.T) { + server, seen := gatedServer(t) + client := NewClient(server.URL, "tok") + client.headersErr = errors.New("ZENNOTES_REMOTE_HEADERS: Line 1: write each header as Name: value.") + if err := client.Get(context.Background(), "/api/vault", nil); err == nil || !strings.HasPrefix(err.Error(), "ZENNOTES_REMOTE_HEADERS: ") { + t.Fatalf("got %v", err) + } + if _, err := client.WatchChanges(context.Background()); err == nil { + t.Fatal("watch should fail too") + } + if len(seen()) != 0 { + t.Fatalf("nothing should reach the server: %v", seen()) + } +} + +func TestARedirectToASignInPageNamesTheGateway(t *testing.T) { + signIn := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/html") + _, _ = w.Write([]byte("sign in")) + })) + defer signIn.Close() + // localhost and 127.0.0.1 are different host names, as a gateway's + // sign-in host is to the server's. + signInURL := strings.Replace(signIn.URL, "127.0.0.1", "localhost", 1) + gate := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + http.Redirect(w, r, signInURL+"/cdn-cgi/access/login", http.StatusFound) + })) + defer gate.Close() + client := NewClient(gate.URL, "tok") + client.Headers, client.headersErr = nil, nil + err := client.Get(context.Background(), "/api/vault", &map[string]any{}) + if StatusOf(err) != http.StatusForbidden || !strings.Contains(err.Error(), "sign-in page at localhost") { + t.Fatalf("got %v", err) + } +} + +func TestGatewayRedirectMessageLetsTheSameHostThrough(t *testing.T) { + final, _ := url.Parse("https://notes.example.com/api/vault") + if msg := gatewayRedirectMessage("http://notes.example.com", final); msg != "" { + t.Fatalf("http to https on the same host is not a gateway: %q", msg) + } +} + +func TestAPlainText403KeepsTheServersOwnWords(t *testing.T) { + msg := requestErrorMessage("https://notes.example.com", "/api/fs/browse", 403, "Forbidden", "text/plain; charset=utf-8", "path is outside the allowed browse roots") + if !strings.Contains(msg, "path is outside the allowed browse roots") { + t.Fatal(msg) + } +} diff --git a/internal/remote/watch.go b/internal/remote/watch.go index 6c50a01..64e19c3 100644 --- a/internal/remote/watch.go +++ b/internal/remote/watch.go @@ -17,9 +17,11 @@ type ChangeEvent struct { } func (c *Client) WatchChanges(ctx context.Context) (<-chan ChangeEvent, error) { + // The handshake is a plain HTTP request, and a gateway checks it like any + // other: without the extra headers the socket never opens. headers := http.Header{} - if c.AuthToken != "" { - headers.Set("Authorization", "Bearer "+c.AuthToken) + if err := c.requestHeaders(headers); err != nil { + return nil, err } dialCtx, cancel := context.WithTimeout(ctx, 10*time.Second) defer cancel() diff --git a/internal/tui/helpdata.go b/internal/tui/helpdata.go index e76f2e6..1703e91 100644 --- a/internal/tui/helpdata.go +++ b/internal/tui/helpdata.go @@ -245,6 +245,7 @@ func helpLines(a *App, filter string) []string { "~/.config/zennotes/config.toml\tShared with the desktop app: [vim], [editor], [appearance], [view], [keymaps], [text_replacements]", ".zennotes/vault.json\tVault settings: system folders, periodic notes, favorites, task exclusions", "ZENNOTES_VAULT / ZENNOTES_SERVER / ZENNOTES_REMOTE_TOKEN\tPick a vault or server without flags", + "ZENNOTES_REMOTE_HEADERS\tExtra headers for a server behind an access gateway, one \"Name: value\" per line", "zn tui --vault \tOpen a specific vault; --server opens a self-hosted server", "", "## Not in the terminal", diff --git a/internal/tui/version.go b/internal/tui/version.go index dddc77c..432c65c 100644 --- a/internal/tui/version.go +++ b/internal/tui/version.go @@ -20,6 +20,7 @@ import ( tea "github.com/charmbracelet/bubbletea" "github.com/ZenNotes/tui/internal/backend" + "github.com/ZenNotes/tui/internal/remote" "github.com/ZenNotes/tui/internal/selfupdate" "github.com/ZenNotes/tui/internal/vim" ) @@ -156,6 +157,11 @@ func connectedServerVersion(ctx context.Context, address, token string) string { if token != "" { req.Header.Set("Authorization", "Bearer "+token) } + // A server behind an access gateway answers only with its headers. A + // value that does not parse sends none; every other request names why. + if headers, err := remote.EnvHeaders(); err == nil { + remote.ApplyHeaders(req.Header, headers) + } client := &http.Client{Timeout: 2 * time.Second, CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }} response, err := client.Do(req) if err != nil {