diff --git a/bible-local/issue-plan-2026-10-26.md b/bible-local/issue-plan-2026-10-26.md index 56a3237..e402881 100644 --- a/bible-local/issue-plan-2026-10-26.md +++ b/bible-local/issue-plan-2026-10-26.md @@ -2,14 +2,14 @@ Repository: https://git.mchus.pro/mchus/QuoteForge -All issue pages were retrieved, including closed issues: seven total, six open. -Issue [#1](https://git.mchus.pro/mchus/QuoteForge/issues/1) was already closed and -is excluded from implementation. Work is sequential on -`fix/open-issues-security-recovery`, with one implementation commit per open issue. +Six issues (#2-#7) remain open. Their initial fixes were merged into `main` +in [PR #8](https://git.mchus.pro/mchus/QuoteForge/pulls/8), with release notes +in v3.50. Acceptance and remaining rule compliance work continue sequentially +on `fix/issue-acceptance-and-migration-safety`. | Order | Issue | Implementation and acceptance | Status | | --- | --- | --- | --- | -| 1 | [#2](https://git.mchus.pro/mchus/QuoteForge/issues/2) | Block foreign Host/Origin/Referer and cross-site browser requests in both routers; validate API body types; refuse saved-password reuse for a changed host/port/user. Middleware attack cases and setup credential tests must pass. | Implemented | +| 1 | [#2](https://git.mchus.pro/mchus/QuoteForge/issues/2) | Block foreign Host/Origin/Referer and cross-site browser requests in both routers; validate API body types; refuse saved-password reuse for a changed host/port/user. Middleware and real setup/runtime router tests reject attacks without changing saved credentials or user data. | Acceptance passed | | 2 | [#4](https://git.mchus.pro/mchus/QuoteForge/issues/4) | Move LOT-bearing cart/rental events to delegated listeners and escaped data attributes; escape BOM and sync output; share HTML escaping; render toast as text. Malicious LOT regression checks and template tests must pass. | Implemented | | 3 | [#7](https://git.mchus.pro/mchus/QuoteForge/issues/7) | Require a fresh verified archive outside rotation before reset, refuse unsynced/legacy states without explicit force, use transactional deletion, remove environment-triggered reset. Verify refusal, ZIP restore, backup failure and rollback. | Implemented | | 4 | [#5](https://git.mchus.pro/mchus/QuoteForge/issues/5) | Persist TLS mode/CA in SQLite, expose setup controls, share policy across setup/sync/migration DSNs. Verify certificate trust, hostname checks, plaintext fallback policy and settings round-trip. | Implemented | @@ -42,5 +42,18 @@ whose affected code is not called. Gin's transitive quic-go was explicitly raise to v0.59.1 after the first scan identified a reachable advisory in v0.59.0. The checks use temporary SQLite databases and synthetic certificates. They do -not exercise a live MariaDB server or browser automation. Review the draft PR -before merge; issue state changes and a production release remain separate steps. +not exercise a live MariaDB server or browser automation. The original implementation PR is merged. Remaining acceptance work and +rule compliance are tracked below; issue state changes are separate. + +## Acceptance follow-up + +### Issue #2 + +`go test ./cmd/qfs -run TestRequestBoundaryOnApplicationRouters -count=1` +exercises the production setup and runtime route constructors with temporary +SQLite state. Foreign setup forms, opaque origins and rebinding hosts are +rejected, text/plain JSON receives 415, and both setup handlers reject saved +password reuse for a changed destination. Local reads still succeed. Saved +credentials, project/configuration counts and the pending queue remain unchanged. +The setup router construction was extracted without changing its routes so +this regression exercises the same middleware registration as startup. diff --git a/cmd/qfs/main.go b/cmd/qfs/main.go index 57143a6..f6193f5 100644 --- a/cmd/qfs/main.go +++ b/cmd/qfs/main.go @@ -542,47 +542,12 @@ func nextBackupTime(now time.Time, hour, minute int) time.Time { func runSetupMode(local *localdb.LocalDB) { restartSig := make(chan struct{}, 1) - // In setup mode, we don't have a connection manager yet (will restart after setup) - templatesPath := filepath.Join("web", "templates") - setupHandler, err := handlers.NewSetupHandler(local, nil, templatesPath, restartSig) + router, err := setupSetupRouter(local, restartSig) if err != nil { - slog.Error("failed to create setup handler", "error", err) + slog.Error("failed to create setup router", "error", err) os.Exit(1) } - gin.SetMode(gin.ReleaseMode) - router := gin.New() - router.Use(gin.Recovery()) - router.Use(middleware.CORS()) - - if staticFS, err := qfassets.StaticFS(); err == nil { - router.StaticFS("/static", http.FS(staticFS)) - } else { - slog.Error("failed to load embedded static assets", "error", err) - os.Exit(1) - } - - // Setup routes only - router.GET("/", func(c *gin.Context) { - c.Redirect(http.StatusFound, "/setup") - }) - router.GET("/setup", setupHandler.ShowSetup) - router.POST("/setup", setupHandler.SaveConnection) - router.POST("/setup/test", setupHandler.TestConnection) - router.GET("/setup/status", setupHandler.GetStatus) - - settingsHandler := handlers.NewSettingsHandler(local) - router.GET("/api/settings/ui", settingsHandler.GetUI) - router.PUT("/api/settings/ui", settingsHandler.PutUI) - - // Health check - router.GET("/health", func(c *gin.Context) { - c.JSON(http.StatusOK, gin.H{ - "status": "setup_required", - "time": time.Now().UTC().Format(time.RFC3339), - }) - }) - addr := "127.0.0.1:8080" slog.Info("starting setup mode server", "address", addr, "version", Version) diff --git a/cmd/qfs/request_boundary_test.go b/cmd/qfs/request_boundary_test.go new file mode 100644 index 0000000..1fcdf42 --- /dev/null +++ b/cmd/qfs/request_boundary_test.go @@ -0,0 +1,72 @@ +package main + +import ( + "net/http/httptest" + "reflect" + "strings" + "testing" + + "git.mchus.pro/mchus/quoteforge/internal/config" + "github.com/gin-gonic/gin" +) + +func TestRequestBoundaryOnApplicationRouters(t *testing.T) { + moveToRepoRoot(t) + local, connMgr, _ := newAPITestStack(t) + if err := local.SaveSettings("db.example", 3306, "example", "example", "example"); err != nil { + t.Fatal(err) + } + cfg := &config.Config{} + setConfigDefaults(cfg) + runtimeRouter, _, err := setupRouter(cfg, local, connMgr, "example", nil) + if err != nil { + t.Fatal(err) + } + setupRouter, err := setupSetupRouter(local, nil) + if err != nil { + t.Fatal(err) + } + for name, router := range map[string]*gin.Engine{"setup": setupRouter, "runtime": runtimeRouter} { + t.Run(name, func(t *testing.T) { + before, err := local.GetSettings() + if err != nil { + t.Fatal(err) + } + projects, configs, pending := local.CountProjects(), local.CountConfigurations(), local.CountPendingChanges() + for _, tt := range []struct { + name, method, path, host, origin, contentType, body string + want int + }{ + {"foreign setup save", "POST", "/setup", "", "https://attacker.example", "application/x-www-form-urlencoded", "host=attacker.example&user=example", 403}, + {"foreign setup test", "POST", "/setup/test", "", "https://attacker.example", "application/x-www-form-urlencoded", "host=attacker.example&user=example", 403}, + {"rebind settings read", "GET", "/setup/status", "attacker.example:8080", "", "", "", 403}, + {"opaque origin", "PUT", "/api/settings/ui", "", "null", "application/json", `{"indicator_mode":"accessible"}`, 403}, + {"plain JSON", "PUT", "/api/settings/ui", "", "", "text/plain", `{"indicator_mode":"accessible"}`, 415}, + {"saved password destination save", "POST", "/setup", "", "", "application/x-www-form-urlencoded", "host=attacker.example&user=example", 422}, + {"saved password destination test", "POST", "/setup/test", "", "", "application/x-www-form-urlencoded", "host=attacker.example&user=example", 422}, + {"local read", "GET", "/api/settings/ui", "", "http://127.0.0.1:8080", "", "", 200}, + } { + t.Run(tt.name, func(t *testing.T) { + req := httptest.NewRequest(tt.method, "http://127.0.0.1:8080"+tt.path, strings.NewReader(tt.body)) + if tt.host != "" { + req.Host = tt.host + } + req.Header.Set("Origin", tt.origin) + req.Header.Set("Content-Type", tt.contentType) + w := httptest.NewRecorder() + router.ServeHTTP(w, req) + if w.Code != tt.want { + t.Fatalf("status=%d want=%d body=%s", w.Code, tt.want, w.Body.String()) + } + }) + } + after, err := local.GetSettings() + if err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(before, after) || projects != local.CountProjects() || configs != local.CountConfigurations() || pending != local.CountPendingChanges() { + t.Fatal("rejected requests changed local state") + } + }) + } +} diff --git a/cmd/qfs/setup_router.go b/cmd/qfs/setup_router.go new file mode 100644 index 0000000..b46b257 --- /dev/null +++ b/cmd/qfs/setup_router.go @@ -0,0 +1,57 @@ +package main + +import ( + "fmt" + "net/http" + "path/filepath" + "time" + + qfassets "git.mchus.pro/mchus/quoteforge" + "git.mchus.pro/mchus/quoteforge/internal/handlers" + "git.mchus.pro/mchus/quoteforge/internal/localdb" + "git.mchus.pro/mchus/quoteforge/internal/middleware" + "github.com/gin-gonic/gin" +) + +func setupSetupRouter(local *localdb.LocalDB, restartSig chan struct{}) (*gin.Engine, error) { + // In setup mode, we don't have a connection manager yet (will restart after setup) + templatesPath := filepath.Join("web", "templates") + setupHandler, err := handlers.NewSetupHandler(local, nil, templatesPath, restartSig) + if err != nil { + return nil, fmt.Errorf("create setup handler: %w", err) + } + + gin.SetMode(gin.ReleaseMode) + router := gin.New() + router.Use(gin.Recovery()) + router.Use(middleware.CORS()) + + if staticFS, err := qfassets.StaticFS(); err == nil { + router.StaticFS("/static", http.FS(staticFS)) + } else { + return nil, fmt.Errorf("load embedded static assets: %w", err) + } + + // Setup routes only + router.GET("/", func(c *gin.Context) { + c.Redirect(http.StatusFound, "/setup") + }) + router.GET("/setup", setupHandler.ShowSetup) + router.POST("/setup", setupHandler.SaveConnection) + router.POST("/setup/test", setupHandler.TestConnection) + router.GET("/setup/status", setupHandler.GetStatus) + + settingsHandler := handlers.NewSettingsHandler(local) + router.GET("/api/settings/ui", settingsHandler.GetUI) + router.PUT("/api/settings/ui", settingsHandler.PutUI) + + // Health check + router.GET("/health", func(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{ + "status": "setup_required", + "time": time.Now().UTC().Format(time.RFC3339), + }) + }) + + return router, nil +}