PostgreSQL backup tool with Point-In-Time-Recovery and restore verification
TypeScript backupbackupsdatabasedatabase-backupdevopsdockergolangkubernetesmariadbmongodbmysqlpgpostgrespostgresqls3self-hostedsystem-administrationtoolsweb-ui
FIX (cache): Disable HTTP caching everywhere and stop trusting /system/version across requests (#654)
Rostislav Dugin2da356c530de
diff --git a/agent/verification/e2e/mock-server/main.go b/agent/verification/e2e/mock-server/main.goindex 4a5a95a1..9e9c5fbe 100644--- a/agent/verification/e2e/mock-server/main.go+++ b/agent/verification/e2e/mock-server/main.go@@ -74,9 +74,11 @@ func (s *server) handleVersion(w http.ResponseWriter, _ *http.Request) {func (s *server) handleDownload(w http.ResponseWriter, r *http.Request) {s.mu.RLock()path := s.binaryPath+ version := s.versions.mu.RUnlock()log.Printf("GET verification-agent (arch=%s) -> %s", r.URL.Query().Get("arch"), path)+ w.Header().Set("X-Databasus-Version", version)http.ServeFile(w, r, path)}diff --git a/agent/verification/internal/features/api/api.go b/agent/verification/internal/features/api/api.goindex fe889efb..f0ac3685 100644--- a/agent/verification/internal/features/api/api.go+++ b/agent/verification/internal/features/api/api.go@@ -9,6 +9,7 @@ import ("net/http""net/url""os"+ "strings""time""github.com/go-resty/resty/v2"@@ -68,6 +69,7 @@ func NewClient(host, token, agentID string, log *slog.Logger) *Client {AddRetryCondition(func(resp *resty.Response, err error) bool {return err != nil || resp.StatusCode() >= 500}).+ SetHeaders(noCacheHeaders).OnBeforeRequest(setAuth)// Claim and report own their retry policy explicitly (claim: loop retries@@ -75,6 +77,7 @@ func NewClient(host, token, agentID string, log *slog.Logger) *Client {// would make "exactly N retries / give up at the budget" untestable.noRetryClient := resty.New().SetTimeout(apiCallTimeout).+ SetHeaders(noCacheHeaders).OnBeforeRequest(setAuth)return &Client{@@ -106,35 +109,45 @@ func (c *Client) FetchServerVersion(ctx context.Context) (string, error) {return ver.Version, nil}-func (c *Client) DownloadVerificationAgentBinary(ctx context.Context, arch, destPath string) error {+func (c *Client) DownloadVerificationAgentBinary(+ ctx context.Context,+ arch, destPath, fallbackVersion string,+) (string, error) {requestURL := c.buildURL(verificationAgentBinaryPath) + "?" + url.Values{"arch": {arch}}.Encode()req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestURL, nil)if err != nil {- return fmt.Errorf("create agent download request: %w", err)+ return "", fmt.Errorf("create agent download request: %w", err)}c.setStreamHeaders(req)resp, err := c.streamHTTP.Do(req)if err != nil {- return err+ return "", err}defer func() { _ = resp.Body.Close() }()if resp.StatusCode != http.StatusOK {- return fmt.Errorf("server returned %d for verification agent download", resp.StatusCode)+ return "", fmt.Errorf("server returned %d for verification agent download", resp.StatusCode)}file, err := os.Create(destPath)if err != nil {- return err+ return "", err}defer func() { _ = file.Close() }()- _, err = io.Copy(file, resp.Body)+ if _, err := io.Copy(file, resp.Body); err != nil {+ return "", err+ }- return err+ downloadedVersion := strings.TrimSpace(resp.Header.Get(agentVersionHeader))+ if downloadedVersion == "" {+ return fallbackVersion, nil+ }++ return downloadedVersion, nil}func (c *Client) Heartbeat(@@ -287,6 +300,10 @@ func (c *Client) setStreamHeaders(req *http.Request) {if h := bearerHeader(c.token); h != "" {req.Header.Set("Authorization", h)}++ for name, value := range noCacheHeaders {+ req.Header.Set(name, value)+ }}func bearerHeader(token string) string {diff --git a/agent/verification/internal/features/api/api_test.go b/agent/verification/internal/features/api/api_test.goindex 9f97f0bf..9d61e541 100644--- a/agent/verification/internal/features/api/api_test.go+++ b/agent/verification/internal/features/api/api_test.go@@ -339,6 +339,7 @@ func Test_DownloadVerificationAgentBinary_WhenServerServesFile_WritesItToDisk(tserver := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {assert.Equal(t, "/api/v1/system/verification-agent", r.URL.Path)assert.Equal(t, "arm64", r.URL.Query().Get("arch"))+ w.Header().Set(agentVersionHeader, "v3.45.0")_, _ = w.Write(payload)}))t.Cleanup(server.Close)@@ -346,14 +347,72 @@ func Test_DownloadVerificationAgentBinary_WhenServerServesFile_WritesItToDisk(tclient := NewClient(server.URL, "", "", testutil.DiscardLogger())destPath := filepath.Join(t.TempDir(), "agent")- err := client.DownloadVerificationAgentBinary(t.Context(), "arm64", destPath)+ downloadedVersion, err := client.DownloadVerificationAgentBinary(+ t.Context(), "arm64", destPath, "v3.43.0",+ )require.NoError(t, err)+ assert.Equal(t, "v3.45.0", downloadedVersion)written, err := os.ReadFile(destPath)require.NoError(t, err)assert.Equal(t, payload, written)}+func Test_DownloadVerificationAgentBinary_WhenServerOmitsVersionHeader_ReturnsFallbackVersion(t *testing.T) {+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {+ _, _ = w.Write([]byte("binary"))+ }))+ t.Cleanup(server.Close)++ client := NewClient(server.URL, "", "", testutil.DiscardLogger())++ downloadedVersion, err := client.DownloadVerificationAgentBinary(+ t.Context(), "amd64", filepath.Join(t.TempDir(), "agent"), "v3.43.0",+ )++ require.NoError(t, err)+ assert.Equal(t, "v3.43.0", downloadedVersion)+}++func Test_DownloadVerificationAgentBinary_WhenCalled_SendsNoCacheRequestHeaders(t *testing.T) {+ var requestHeaders http.Header++ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ requestHeaders = r.Header.Clone()+ _, _ = w.Write([]byte("binary"))+ }))+ t.Cleanup(server.Close)++ client := NewClient(server.URL, "", "", testutil.DiscardLogger())++ _, err := client.DownloadVerificationAgentBinary(+ t.Context(), "amd64", filepath.Join(t.TempDir(), "agent"), "v1.0.0",+ )+ require.NoError(t, err)++ assert.Equal(t, "no-cache", requestHeaders.Get("Cache-Control"))+ assert.Equal(t, "no-cache", requestHeaders.Get("Pragma"))+}++func Test_FetchServerVersion_WhenCalled_SendsNoCacheRequestHeaders(t *testing.T) {+ var requestHeaders http.Header++ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ requestHeaders = r.Header.Clone()+ w.Header().Set("Content-Type", "application/json")+ _, _ = w.Write([]byte(`{"version":"v3.45.0"}`))+ }))+ t.Cleanup(server.Close)++ client := NewClient(server.URL, "", "", testutil.DiscardLogger())++ _, err := client.FetchServerVersion(t.Context())+ require.NoError(t, err)++ assert.Equal(t, "no-cache", requestHeaders.Get("Cache-Control"))+ assert.Equal(t, "no-cache", requestHeaders.Get("Pragma"))+}+func Test_DownloadVerificationAgentBinary_WhenServer404_ReturnsError(t *testing.T) {server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {w.WriteHeader(http.StatusNotFound)@@ -362,7 +421,9 @@ func Test_DownloadVerificationAgentBinary_WhenServer404_ReturnsError(t *testing.client := NewClient(server.URL, "", "", testutil.DiscardLogger())- err := client.DownloadVerificationAgentBinary(t.Context(), "amd64", filepath.Join(t.TempDir(), "a"))+ _, err := client.DownloadVerificationAgentBinary(+ t.Context(), "amd64", filepath.Join(t.TempDir(), "a"), "v1.0.0",+ )require.Error(t, err)}diff --git a/agent/verification/internal/features/api/constants.go b/agent/verification/internal/features/api/constants.goindex 3798d721..233f59ac 100644--- a/agent/verification/internal/features/api/constants.go+++ b/agent/verification/internal/features/api/constants.go@@ -16,8 +16,17 @@ const (retryBaseDelay = 1 * time.SecondmaxBackoff = 32 * time.Second++ agentVersionHeader = "X-Databasus-Version")+// Forces revalidation at any intermediary already holding a stale entry —+// response-side no-store only stops a proxy from caching from now on.+var noCacheHeaders = map[string]string{+ "Cache-Control": "no-cache",+ "Pragma": "no-cache",+}+// timeAfterFn is time.After in production; tests swap it to avoid real sleeps in// the report retry loop. reportRetryBudget is the report retry deadline (60s in// production); tests shrink it to exercise budget exhaustion without a 60sdiff --git a/agent/verification/internal/features/upgrade/upgrader.go b/agent/verification/internal/features/upgrade/upgrader.goindex 7eb35359..a4eccc07 100644--- a/agent/verification/internal/features/upgrade/upgrader.go+++ b/agent/verification/internal/features/upgrade/upgrader.go@@ -65,7 +65,10 @@ func CheckAndUpdate(apiClient *api.Client, currentVersion string, isDev bool, lodownloadCtx, cancelDownload := context.WithTimeout(context.Background(), binaryDownloadTimeout)defer cancelDownload()- if err := apiClient.DownloadVerificationAgentBinary(downloadCtx, runtime.GOARCH, tempPath); err != nil {+ downloadedVersion, err := apiClient.DownloadVerificationAgentBinary(+ downloadCtx, runtime.GOARCH, tempPath, serverVersion,+ )+ if err != nil {return false, fmt.Errorf("failed to download update: %w", err)}@@ -73,15 +76,20 @@ func CheckAndUpdate(apiClient *api.Client, currentVersion string, isDev bool, loreturn false, fmt.Errorf("failed to set permissions on update: %w", err)}- if err := verifyBinary(tempPath, serverVersion); err != nil {+ if err := verifyBinary(tempPath, downloadedVersion); err != nil {return false, fmt.Errorf("update verification failed: %w", err)}+ if downloadedVersion == currentVersion {+ log.Info("Agent version is up to date", "version", currentVersion)+ return false, nil+ }+if err := os.Rename(tempPath, selfPath); err != nil {return false, fmt.Errorf("failed to replace binary (try --skip-update if this persists): %w", err)}- log.Info("Agent binary updated", "version", serverVersion)+ log.Info("Agent binary updated", "version", downloadedVersion)return true, nil}diff --git a/agent/verification/internal/features/upgrade/upgrader_test.go b/agent/verification/internal/features/upgrade/upgrader_test.goindex 2eca1bf0..69f0cc77 100644--- a/agent/verification/internal/features/upgrade/upgrader_test.go+++ b/agent/verification/internal/features/upgrade/upgrader_test.go@@ -129,6 +129,65 @@ func Test_CheckAndUpdate_WhenServerVersionMatches_DoesNotDownload(t *testing.T)assert.Equal(t, int32(0), downloadHits.Load())}+type fakeBackend struct {+ versionEndpointReports string+ downloadHeaderReports string+ servedBinaryPrints string+}++// Both callers below stop before CheckAndUpdate's os.Rename, which would+// otherwise overwrite the running test binary.+func startFakeBackend(t *testing.T, backend fakeBackend) *httptest.Server {+ t.Helper()++ binary, err := os.ReadFile(buildVersionHelper(t, backend.servedBinaryPrints))+ require.NoError(t, err)++ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ if r.URL.Path == "/api/v1/system/version" {+ w.Header().Set("Content-Type", "application/json")+ _ = json.NewEncoder(w).Encode(map[string]string{"version": backend.versionEndpointReports})++ return+ }++ w.Header().Set("X-Databasus-Version", backend.downloadHeaderReports)+ _, _ = w.Write(binary)+ }))+ t.Cleanup(server.Close)++ return server+}++func Test_CheckAndUpdate_WhenServerVersionIsStaleButBinaryMatchesCurrent_SkipsUpgrade(t *testing.T) {+ server := startFakeBackend(t, fakeBackend{+ versionEndpointReports: "v3.43.0",+ downloadHeaderReports: "v3.45.0",+ servedBinaryPrints: "v3.45.0",+ })+ client := api.NewClient(server.URL, "", "", testutil.DiscardLogger())++ upgraded, err := CheckAndUpdate(client, "v3.45.0", false, testutil.DiscardLogger())++ require.NoError(t, err)+ assert.False(t, upgraded, "a stale /system/version must not make the agent re-exec into itself")+}++func Test_CheckAndUpdate_WhenDownloadedBinaryDisagreesWithVersionHeader_ReturnsMismatchError(t *testing.T) {+ server := startFakeBackend(t, fakeBackend{+ versionEndpointReports: "v3.46.0",+ downloadHeaderReports: "v3.46.0",+ servedBinaryPrints: "v3.44.0",+ })+ client := api.NewClient(server.URL, "", "", testutil.DiscardLogger())++ upgraded, err := CheckAndUpdate(client, "v3.44.0", false, testutil.DiscardLogger())++ require.Error(t, err)+ assert.False(t, upgraded)+ assert.Contains(t, err.Error(), "version mismatch")+}+func Test_CheckAndUpdate_WhenServerUnreachable_ReturnsError(t *testing.T) {server := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {}))server.Close()diff --git a/backend/cmd/main.go b/backend/cmd/main.goindex d9e6264c..7a53468e 100644--- a/backend/cmd/main.go+++ b/backend/cmd/main.go@@ -51,6 +51,7 @@ import (verification_config "databasus-backend/internal/features/verification/config"verification_runs "databasus-backend/internal/features/verification/runs"workspaces_controllers "databasus-backend/internal/features/workspaces/controllers"+ "databasus-backend/internal/middleware"cache_utils "databasus-backend/internal/util/cache"env_utils "databasus-backend/internal/util/env"files_utils "databasus-backend/internal/util/files"@@ -119,6 +120,7 @@ func main() {ginApp := gin.New()ginApp.Use(gin.Logger())ginApp.Use(ginRecoveryWithLogger(log))+ ginApp.Use(middleware.NoStoreCacheControl())// Add GZIP compression middlewareginApp.Use(gzip.Gzip(@@ -212,8 +214,6 @@ func startServerWithGracefulShutdown(log *slog.Logger, app *gin.Engine) {func setUpRoutes(r *gin.Engine) {v1 := r.Group("/api/v1")- v1.Use(noStoreCacheControl())-// Mount Swagger UIv1.GET("/docs/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))@@ -438,17 +438,6 @@ func enableCors(ginApp *gin.Engine) {}}-// noStoreCacheControl marks every API response uncacheable. The API serves-// only dynamic, often sensitive data (backup metadata, tokens, agent binaries,-// version); a stale cached response — e.g. an outdated /system/version behind-// a CDN — must never reach a client.-func noStoreCacheControl() gin.HandlerFunc {- return func(ctx *gin.Context) {- ctx.Header("Cache-Control", "no-store")- ctx.Next()- }-}-func ginRecoveryWithLogger(log *slog.Logger) gin.HandlerFunc {return func(ctx *gin.Context) {defer func() {diff --git a/backend/internal/features/system/agent/constants.go b/backend/internal/features/system/agent/constants.gonew file mode 100644index 00000000..688f51a9--- /dev/null+++ b/backend/internal/features/system/agent/constants.go@@ -0,0 +1,3 @@+package system_agent++const AgentVersionHeader = "X-Databasus-Version"diff --git a/backend/internal/features/system/agent/controller.go b/backend/internal/features/system/agent/controller.goindex 8a12d4bb..f42ea15a 100644--- a/backend/internal/features/system/agent/controller.go+++ b/backend/internal/features/system/agent/controller.go@@ -5,6 +5,8 @@ import ("os""github.com/gin-gonic/gin"++ system_version "databasus-backend/internal/features/system/version")type AgentController struct{}@@ -25,6 +27,7 @@ var verificationAgentBinaryPaths = map[string]string{// @Produce octet-stream// @Param arch query string true "Target architecture" Enums(amd64, arm64)// @Success 200 {file} binary+// @Header 200 {string} X-Databasus-Version "Version the served binary was built from"// @Failure 400 {object} map[string]string// @Failure 404 {object} map[string]string// @Router /system/verification-agent [get]@@ -42,5 +45,7 @@ func (c *AgentController) DownloadVerificationAgent(ctx *gin.Context) {ctx.Header("Content-Type", "application/octet-stream")ctx.Header("Content-Disposition", "attachment; filename=databasus-verification-agent")+ ctx.Header(AgentVersionHeader, system_version.GetAppVersion())+ctx.File(binaryPath)}diff --git a/backend/internal/features/system/agent/controller_test.go b/backend/internal/features/system/agent/controller_test.gonew file mode 100644index 00000000..f49d809a--- /dev/null+++ b/backend/internal/features/system/agent/controller_test.go@@ -0,0 +1,72 @@+package system_agent++import (+ "net/http"+ "net/http/httptest"+ "os"+ "path/filepath"+ "testing"++ "github.com/gin-gonic/gin"+ "github.com/stretchr/testify/assert"+ "github.com/stretchr/testify/require"+)++func buildAgentDownloadRouter(t *testing.T) *gin.Engine {+ t.Helper()++ gin.SetMode(gin.TestMode)++ t.Chdir(t.TempDir())+ require.NoError(t, os.MkdirAll("agent-binaries", 0o755))+ require.NoError(t, os.WriteFile(+ verificationAgentBinaryPaths["amd64"], []byte("\x7fELF fake agent"), 0o755,+ ))++ router := gin.New()+ GetAgentController().RegisterRoutes(router.Group("/api/v1"))++ return router+}++func Test_DownloadVerificationAgent_WhenBinaryExists_SendsVersionHeader(t *testing.T) {+ t.Setenv("APP_VERSION", "3.45.0")++ router := buildAgentDownloadRouter(t)++ response := httptest.NewRecorder()+ router.ServeHTTP(+ response,+ httptest.NewRequest(http.MethodGet, "/api/v1/system/verification-agent?arch=amd64", nil),+ )++ assert.Equal(t, http.StatusOK, response.Code)+ assert.Equal(t, "3.45.0", response.Header().Get(AgentVersionHeader))+ assert.Equal(t, "application/octet-stream", response.Header().Get("Content-Type"))+}++func Test_DownloadVerificationAgent_WhenArchIsUnknown_ReturnsBadRequest(t *testing.T) {+ router := buildAgentDownloadRouter(t)++ response := httptest.NewRecorder()+ router.ServeHTTP(+ response,+ httptest.NewRequest(http.MethodGet, "/api/v1/system/verification-agent?arch=riscv64", nil),+ )++ assert.Equal(t, http.StatusBadRequest, response.Code)+ assert.Empty(t, response.Header().Get(AgentVersionHeader))+}++func Test_DownloadVerificationAgent_WhenBinaryIsMissing_ReturnsNotFound(t *testing.T) {+ router := buildAgentDownloadRouter(t)+ require.NoError(t, os.Remove(filepath.Clean(verificationAgentBinaryPaths["amd64"])))++ response := httptest.NewRecorder()+ router.ServeHTTP(+ response,+ httptest.NewRequest(http.MethodGet, "/api/v1/system/verification-agent?arch=amd64", nil),+ )++ assert.Equal(t, http.StatusNotFound, response.Code)+}diff --git a/backend/internal/middleware/cache.go b/backend/internal/middleware/cache.gonew file mode 100644index 00000000..96eb091b--- /dev/null+++ b/backend/internal/middleware/cache.go@@ -0,0 +1,15 @@+package middleware++import "github.com/gin-gonic/gin"++// Nothing this server returns is cacheable: the API is dynamic and often+// sensitive, and the image rewrites index.html and runtime-config.js on every+// container start. Pragma and Expires cover HTTP/1.0 intermediaries.+func NoStoreCacheControl() gin.HandlerFunc {+ return func(ctx *gin.Context) {+ ctx.Header("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0")+ ctx.Header("Pragma", "no-cache")+ ctx.Header("Expires", "0")+ ctx.Next()+ }+}diff --git a/backend/internal/middleware/cache_test.go b/backend/internal/middleware/cache_test.gonew file mode 100644index 00000000..a97ce229--- /dev/null+++ b/backend/internal/middleware/cache_test.go@@ -0,0 +1,55 @@+package middleware++import (+ "net/http"+ "net/http/httptest"+ "testing"++ "github.com/gin-gonic/gin"+ "github.com/stretchr/testify/assert"+)++func assertNoStoreHeaders(t *testing.T, response *httptest.ResponseRecorder) {+ t.Helper()++ assert.Equal(t, "no-store, no-cache, must-revalidate, max-age=0", response.Header().Get("Cache-Control"))+ assert.Equal(t, "no-cache", response.Header().Get("Pragma"))+ assert.Equal(t, "0", response.Header().Get("Expires"))+}++func buildRouterWithNoStore() *gin.Engine {+ gin.SetMode(gin.TestMode)++ router := gin.New()+ router.Use(NoStoreCacheControl())+ router.GET("/api/v1/system/version", func(ctx *gin.Context) {+ ctx.JSON(http.StatusOK, gin.H{"version": "3.45.0"})+ })+ router.NoRoute(func(ctx *gin.Context) {+ ctx.String(http.StatusOK, "<html></html>")+ })++ return router+}++func Test_NoStoreCacheControl_OnApiRoute_SetsUncacheableHeaders(t *testing.T) {+ response := httptest.NewRecorder()+ buildRouterWithNoStore().ServeHTTP(+ response,+ httptest.NewRequest(http.MethodGet, "/api/v1/system/version", nil),+ )++ assert.Equal(t, http.StatusOK, response.Code)+ assertNoStoreHeaders(t, response)+}++func Test_NoStoreCacheControl_OnStaticFallbackRoute_SetsUncacheableHeaders(t *testing.T) {+ response := httptest.NewRecorder()+ buildRouterWithNoStore().ServeHTTP(+ response,+ httptest.NewRequest(http.MethodGet, "/assets/index-a1b2c3d4.js", nil),+ )++ assert.Equal(t, http.StatusOK, response.Code)+ assertNoStoreHeaders(t, response)+}diff --git a/frontend/src/entity/system/api/systemApi.ts b/frontend/src/entity/system/api/systemApi.tsindex 61770a8b..69d3010d 100644--- a/frontend/src/entity/system/api/systemApi.ts+++ b/frontend/src/entity/system/api/systemApi.ts@@ -3,7 +3,9 @@ import type { VersionResponse } from '../model/VersionResponse';export const systemApi = {async getVersion(): Promise<VersionResponse> {- const response = await fetch(`${getApplicationServer()}/api/v1/system/version`);+ const response = await fetch(`${getApplicationServer()}/api/v1/system/version`, {+ cache: 'no-store',+ });if (!response.ok) {throw new Error(`Failed to fetch version: ${response.status}`);diff --git a/frontend/src/shared/api/RequestOptions.ts b/frontend/src/shared/api/RequestOptions.tsindex 8c054568..8d82cffd 100644--- a/frontend/src/shared/api/RequestOptions.ts+++ b/frontend/src/shared/api/RequestOptions.ts@@ -47,7 +47,7 @@ export default class RequestOptions {const requestJsonOptions: RequestInit = {headers: headersMatrix as [string, string][],- cache: 'no-cache',+ cache: 'no-store',};if (this.method) {