diff --git a/cmd/syncthing/main.go b/cmd/syncthing/main.go index 14b850e64..8f5c0916d 100644 --- a/cmd/syncthing/main.go +++ b/cmd/syncthing/main.go @@ -11,6 +11,7 @@ import ( "cmp" "context" "crypto/tls" + "encoding/json" "errors" "fmt" "io" @@ -360,19 +361,7 @@ func checkUpgrade() (upgrade.Release, error) { return release, nil } -func upgradeViaRest() error { - cfg, err := loadOrDefaultConfig() - if err != nil { - return err - } - - u, err := url.Parse(cfg.GUI().URL()) - if err != nil { - return err - } - u.Path = path.Join(u.Path, "rest/system/upgrade") - target := u.String() - r, _ := http.NewRequest(http.MethodPost, target, nil) +func makeRestCall(cfg config.Wrapper, r *http.Request) (*http.Response, error) { r.Header.Set("X-Api-Key", cfg.GUI().APIKey) r.Header.Set("User-Agent", build.UserAgent()) @@ -386,20 +375,40 @@ func upgradeViaRest() error { Timeout: 60 * time.Second, } resp, err := client.Do(r) + if err != nil { + return nil, err + } + + if resp.StatusCode != http.StatusOK { + defer resp.Body.Close() + bs, err := io.ReadAll(resp.Body) + if err != nil { + return nil, err + } + return nil, errors.New(string(bs)) + } + + return resp, nil +} + +func upgradeViaRest() error { + cfg, err := loadOrDefaultConfig() if err != nil { return err } - defer resp.Body.Close() - - if resp.StatusCode != http.StatusOK { - bs, err := io.ReadAll(resp.Body) - if err != nil { - return err - } - return errors.New(string(bs)) + u, err := url.Parse(cfg.GUI().URL()) + if err != nil { + return err } + u.Path = path.Join(u.Path, "rest/system/upgrade") + target := u.String() + r, _ := http.NewRequest(http.MethodPost, target, nil) //nolint:noctx + resp, err := makeRestCall(cfg, r) + if err == nil { + resp.Body.Close() + } return err } @@ -598,7 +607,12 @@ func (c *serveCmd) syncthingMain() { if cfgWrapper.Options().StartBrowser && !c.NoBrowser && !c.InternalRestarting { // Can potentially block if the utility we are invoking doesn't // fork, and just execs, hence keep it in its own routine. - go func() { _ = openURL(cfgWrapper.GUI().URL()) }() + go func() { + cmd := browserCmd{Verify: false} + if err := cmd.Run(); err != nil { + slog.Error("Failed to open browser", slogutil.Error(err)) + } + }() } status := app.Wait() @@ -948,10 +962,11 @@ func (c browserCmd) Run() error { } url := guiCfg.URL() + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + if c.Verify { // Do an HTTP request to verify the GUI/API is up and available - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) if err != nil { return err @@ -964,6 +979,23 @@ func (c browserCmd) Run() error { } } + if guiCfg.IsAuthEnabled() { + r, err := http.NewRequestWithContext(ctx, http.MethodPost, url+"rest/auth/logintoken", nil) + if err != nil { + return err + } + resp, err := makeRestCall(cfg, r) + if err != nil { + return err + } + defer resp.Body.Close() + var data map[string]string + if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { + return err + } + url += "?token=" + data["token"] + } + return openURL(url) } diff --git a/lib/api/api.go b/lib/api/api.go index 5fa3e4335..f48427741 100644 --- a/lib/api/api.go +++ b/lib/api/api.go @@ -371,13 +371,16 @@ func (s *service) Serve(ctx context.Context) error { // Wrap everything in basic auth, if user/password is set. if guiCfg.IsAuthEnabled() { tokenCookieManager := newTokenCookieManager(s.id.Short().String(), guiCfg, s.evLogger, s.miscDB) - authMW := newBasicAuthAndSessionMiddleware(tokenCookieManager, guiCfg, s.cfg.LDAP(), handler, s.evLogger) + loginTokens := newTokenManager("logintokens", nil, 10*time.Second, 5) + authMW := newBasicAuthAndSessionMiddleware(tokenCookieManager, loginTokens, guiCfg, s.cfg.LDAP(), handler, s.evLogger) handler = authMW restMux.Handler(http.MethodPost, "/rest/noauth/auth/password", http.HandlerFunc(authMW.passwordAuthHandler)) // Logout is a no-op without a valid session cookie, so /noauth/ is fine here restMux.Handler(http.MethodPost, "/rest/noauth/auth/logout", http.HandlerFunc(authMW.handleLogout)) + + restMux.Handler(http.MethodPost, "/rest/auth/logintoken", http.HandlerFunc(authMW.newLoginToken)) } // Redirect to HTTPS if we are supposed to diff --git a/lib/api/api_auth.go b/lib/api/api_auth.go index ab4d72e16..b06c44030 100644 --- a/lib/api/api_auth.go +++ b/lib/api/api_auth.go @@ -126,15 +126,17 @@ func isNoAuthPath(path string, metricsWithoutAuth bool) bool { type basicAuthAndSessionMiddleware struct { tokenCookieManager *tokenCookieManager + loginTokens *tokenManager guiCfg config.GUIConfiguration ldapCfg config.LDAPConfiguration next http.Handler evLogger events.Logger } -func newBasicAuthAndSessionMiddleware(tokenCookieManager *tokenCookieManager, guiCfg config.GUIConfiguration, ldapCfg config.LDAPConfiguration, next http.Handler, evLogger events.Logger) *basicAuthAndSessionMiddleware { +func newBasicAuthAndSessionMiddleware(tokenCookieManager *tokenCookieManager, loginTokens *tokenManager, guiCfg config.GUIConfiguration, ldapCfg config.LDAPConfiguration, next http.Handler, evLogger events.Logger) *basicAuthAndSessionMiddleware { return &basicAuthAndSessionMiddleware{ tokenCookieManager: tokenCookieManager, + loginTokens: loginTokens, guiCfg: guiCfg, ldapCfg: ldapCfg, next: next, @@ -143,6 +145,10 @@ func newBasicAuthAndSessionMiddleware(tokenCookieManager *tokenCookieManager, gu } func (m *basicAuthAndSessionMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) { + if m.tryTokenLogin(w, r) { + return + } + if hasValidAPIKeyHeader(r, m.guiCfg) { m.next.ServeHTTP(w, r) return @@ -222,11 +228,34 @@ func attemptBasicAuth(r *http.Request, guiCfg config.GUIConfiguration, ldapCfg c return "", false } +func (m *basicAuthAndSessionMiddleware) tryTokenLogin(w http.ResponseWriter, r *http.Request) bool { + query := r.URL.Query() + if m.loginTokens.Consume(r.URL.Query().Get("token")) { + if !m.tokenCookieManager.hasValidSession(r) { + m.tokenCookieManager.createSession("", false, w, r) + } + } + + if query.Has("token") { + query.Del("token") + r.URL.RawQuery = query.Encode() + // #nosec G710 - we're just deleting a query parameter from the url and not introducing anything untrusted + http.Redirect(w, r, r.URL.String(), http.StatusTemporaryRedirect) + return true + } + return false +} + func (m *basicAuthAndSessionMiddleware) handleLogout(w http.ResponseWriter, r *http.Request) { m.tokenCookieManager.destroySession(w, r) w.WriteHeader(http.StatusNoContent) } +func (m *basicAuthAndSessionMiddleware) newLoginToken(w http.ResponseWriter, r *http.Request) { + token := m.loginTokens.New() + sendJSON(w, map[string]string{"token": token}) +} + func auth(username string, password string, guiCfg config.GUIConfiguration, ldapCfg config.LDAPConfiguration) bool { if guiCfg.AuthMode == config.AuthModeLDAP { return authLDAP(username, password, ldapCfg) diff --git a/lib/api/api_auth_test.go b/lib/api/api_auth_test.go index ee304d812..b901064aa 100644 --- a/lib/api/api_auth_test.go +++ b/lib/api/api_auth_test.go @@ -150,6 +150,17 @@ func TestTokenManager(t *testing.T) { if tm.Check(t3) { t.Errorf("token %q should be invalid", t3) } + + // The token should become invalid after consuming it + if !tm.Consume(t1) { + t.Errorf("token %q should be valid", t3) + } + if tm.Check(t1) { + t.Errorf("token %q should be invalid", t3) + } + if tm.Consume(t1) { + t.Errorf("token %q should be invalid", t3) + } } func TestTokenManagerNoExpiry(t *testing.T) { @@ -179,6 +190,23 @@ func TestTokenManagerNoExpiry(t *testing.T) { } } +func TestTokenManagerNoDB(t *testing.T) { + t.Parallel() + + clock := &mockClock{now: time.Now()} + tm := newTokenManager("testTokensNoDB", nil, 24*time.Hour, 3) + tm.timeNow = clock.Now + + token := tm.New() + if !tm.Check(token) { + t.Errorf("token %q should be valid", token) + } + clock.wind(25 * time.Hour) + if tm.Check(token) { + t.Errorf("token %q should be invalid", token) + } +} + func TestSessionCookieMaxAgeNoExpiry(t *testing.T) { t.Parallel() diff --git a/lib/api/api_test.go b/lib/api/api_test.go index bbf902e47..7d780ecd6 100644 --- a/lib/api/api_test.go +++ b/lib/api/api_test.go @@ -979,6 +979,103 @@ func TestHtmlFormLogin(t *testing.T) { }) } +func TestTokenLogin(t *testing.T) { + t.Parallel() + + cfg := newMockedConfig() + cfg.GUIReturns(config.GUIConfiguration{ + User: "üser", + Password: "$2a$10$IdIZTxTg/dCNuNEGlmLynOjqg4B1FvDKuIV5e0BB3pnWVHNb8.GSq", // bcrypt of "räksmörgås" in UTF-8 + APIKey: testAPIKey, + SendBasicAuthPrompt: false, + }) + baseURL := startHTTP(t, cfg) + + acquireToken := func() string { + resp := httpRequest(http.MethodPost, baseURL+"/rest/auth/logintoken", nil, "", "", testAPIKey, "", "", "", nil, t) + var body map[string]string + + if err := json.NewDecoder(resp.Body).Decode(&body); err != nil { + t.Errorf("Unexpected json decoding failure %v", err) + } + return body["token"] + } + + tryLoadWithToken := func(token string) *http.Response { + client := http.Client{ + CheckRedirect: func(req *http.Request, via []*http.Request) error { + return http.ErrUseLastResponse + }, + } + req, err := http.NewRequest(http.MethodGet, baseURL+"/?token="+token, nil) + if err != nil { + t.Fatal(err) + } + resp, err := client.Do(req) + if err != nil { + t.Fatal(err) + } + if !(resp.StatusCode >= 300 && resp.StatusCode <= 399) { + t.Errorf("Expected redirect, got %+v", resp) + } + if resp.Header.Get("Location") != "/" { + t.Errorf("Expected redirect to /, got %+v", resp) + } + return resp + } + + t.Run("Invalid token is redirected away without logging in", func(t *testing.T) { + t.Parallel() + + resp := tryLoadWithToken("aaaaaaa") + if hasSessionCookie(resp.Cookies()) { + t.Errorf("Unexpected session cookie for invalid token") + } + }) + + t.Run("Token auth works", func(t *testing.T) { + t.Parallel() + + token := acquireToken() + resp := tryLoadWithToken(token) + fmt.Printf("%+v\n", resp) + if !hasSessionCookie(resp.Cookies()) { + t.Errorf("Expected session cookie for valid token") + } + }) + + t.Run("Logout removes the session cookie", func(t *testing.T) { + token := acquireToken() + resp := tryLoadWithToken(token) + if !hasSessionCookie(resp.Cookies()) { + t.Errorf("Expected session cookie for valid token") + } + + logoutResp := httpPost(baseURL+"/rest/noauth/auth/logout", nil, resp.Cookies(), t) + if !hasDeleteSessionCookie(logoutResp.Cookies()) { + t.Errorf("Expected session cookie to be deleted for logout request") + } + }) + + t.Run("Same token cannot be used twice", func(t *testing.T) { + token := acquireToken() + resp := tryLoadWithToken(token) + if !hasSessionCookie(resp.Cookies()) { + t.Errorf("Expected session cookie for valid token") + } + + logoutResp := httpPost(baseURL+"/rest/noauth/auth/logout", nil, resp.Cookies(), t) + if !hasDeleteSessionCookie(logoutResp.Cookies()) { + t.Errorf("Expected session cookie to be deleted for logout request") + } + + resp = tryLoadWithToken(token) + if hasSessionCookie(resp.Cookies()) { + t.Errorf("Unexpected session cookie for reused token") + } + }) +} + func TestApiCache(t *testing.T) { t.Parallel() diff --git a/lib/api/tokenmanager.go b/lib/api/tokenmanager.go index a6f6b458b..a82d9c5ab 100644 --- a/lib/api/tokenmanager.go +++ b/lib/api/tokenmanager.go @@ -40,8 +40,10 @@ const defaultSessionCookieDurationS = 7 * 24 * 60 * 60 func newTokenManager(key string, miscDB *db.Typed, lifetime time.Duration, maxItems int) *tokenManager { var tokens apiproto.TokenSet - if bs, ok, _ := miscDB.Bytes(key); ok { - _ = proto.Unmarshal(bs, &tokens) // best effort + if miscDB != nil { + if bs, ok, _ := miscDB.Bytes(key); ok { + _ = proto.Unmarshal(bs, &tokens) // best effort + } } if tokens.Tokens == nil { tokens.Tokens = make(map[string]int64) @@ -62,6 +64,31 @@ func (m *tokenManager) Check(token string) bool { m.mut.Lock() defer m.mut.Unlock() + if !m.check(token) { + return false + } + + // Give the token further life. + m.tokens.Tokens[token] = m.newExpiryNanos() + m.saveLocked() + return true +} + +func (m *tokenManager) Consume(token string) bool { + m.mut.Lock() + defer m.mut.Unlock() + + if !m.check(token) { + return false + } + + // Consume the token. + delete(m.tokens.Tokens, token) + m.saveLocked() + return true +} + +func (m *tokenManager) check(token string) bool { expires, ok := m.tokens.Tokens[token] if !ok { return false @@ -71,10 +98,6 @@ func (m *tokenManager) Check(token string) bool { m.saveLocked() // removes expired tokens return false } - - // Give the token further life. - m.tokens.Tokens[token] = m.newExpiryNanos() - m.saveLocked() return true } @@ -136,6 +159,10 @@ func (m *tokenManager) saveLocked() { } } + if m.miscDB == nil { + return + } + // Postpone saving until one second of inactivity. if m.saveTimer == nil { m.saveTimer = time.AfterFunc(time.Second, m.scheduledSave) @@ -145,6 +172,10 @@ func (m *tokenManager) saveLocked() { } func (m *tokenManager) scheduledSave() { + if m.miscDB == nil { + return + } + m.mut.Lock() defer m.mut.Unlock()