保存本机管理命令与关闭旧重置路由检查点

This commit is contained in:
xuans committed 2026-09-21 01:12:17 +08:00
1 parent 55c56b518f
commit 325928ee1f
18 files changed
+1242 -32

No files matched your search

@@ -0,0 +1,65 @@
package httpapi
import (
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"git.pcux.cn/xuans/Scert/backend/internal/admin"
)
// newLegacyResetServer 只保留旧处理函数的独立回归,不代表正式产品路由或生产验收。
func newLegacyResetServer(dependencies Dependencies) *Server {
server := New(dependencies)
server.router.GET(admin.ResetPath, server.resetPage)
server.router.POST(admin.ResetPath, server.resetSubmit)
return server
}
func TestProductionResetPathClosedEvenWithLegacyActivation(t *testing.T) {
cfg := readyTestConfig(t.TempDir())
cfg.Web.Hostname = "scert.example.test"
staticDir := t.TempDir()
if err := os.WriteFile(filepath.Join(staticDir, "index.html"), []byte("<html>scert-test-shell</html>"), 0600); err != nil {
t.Fatal(err)
}
controller := admin.New(cfg)
if _, err := controller.Start(); err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = controller.Stop() })
server := New(Dependencies{Config: cfg, FrontendDir: staticDir, Repository: &stubDomainRepository{}, ResetStatePath: admin.StatePath(cfg.Paths.Data)})
for _, route := range server.router.Routes() {
if route.Path == "/reset" || strings.HasPrefix(route.Path, "/reset/") {
t.Fatal("正式路由注册了旧密码重置处理器")
}
}
for _, method := range []string{http.MethodGet, http.MethodPost, http.MethodHead} {
for _, path := range []string{"/reset", "/reset/", "/reset/anything", "/%72eset"} {
for _, authenticated := range []bool{false, true} {
request := httptest.NewRequest(method, "http://scert.example.test"+path, strings.NewReader("reset_code=test&new_password=TestPass123"))
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
response := httptest.NewRecorder()
if authenticated {
serveAdminHTTP(server, response, request)
} else {
server.Handler().ServeHTTP(response, request)
}
if response.Code != http.StatusNotFound {
t.Fatalf("正式重置路径必须关闭:%s %s authenticated=%v status=%d", method, path, authenticated, response.Code)
}
if strings.Contains(response.Body.String(), "scert-test-shell") || len(response.Result().Cookies()) != 0 {
t.Fatal("旧重置路径回退到页面或写入了重置 Cookie")
}
}
}
}
response := httptest.NewRecorder()
server.Handler().ServeHTTP(response, httptest.NewRequest(http.MethodGet, "http://scert.example.test/login", nil))
if response.Code != http.StatusOK || !strings.Contains(response.Body.String(), "scert-test-shell") {
t.Fatal("边界测试没有使用启用的真实页面回退处理器")
}
}
+1 -4
View File
@@ -19,7 +19,6 @@ import (
"github.com/gin-gonic/gin"
"git.pcux.cn/xuans/Scert/backend/internal/admin"
"git.pcux.cn/xuans/Scert/backend/internal/buildinfo"
"git.pcux.cn/xuans/Scert/backend/internal/certificate"
"git.pcux.cn/xuans/Scert/backend/internal/config"
@@ -171,8 +170,6 @@ func (s *Server) registerRoutes() {
// 主路由保持独立,初始化路由由专用模块注册。
s.router.GET("/healthz", s.health)
s.router.GET("/readyz", s.ready)
s.router.GET(admin.ResetPath, s.resetPage)
s.router.POST(admin.ResetPath, s.resetSubmit)
s.router.GET("/api/v1/status", s.status)
s.router.GET("/api/v1/auth/session", s.authSession)
s.router.POST("/api/v1/auth/login", s.requireSameOrigin, s.authLogin)
@@ -500,7 +497,7 @@ func (s *Server) status(c *gin.Context) {
if _, authenticated := s.currentAuthSession(c); authenticated {
payload["https_mode"] = cfg.Server.HTTPSMode
payload["database_driver"] = cfg.Database.Driver
payload["reset_url"] = cfg.ResetURL()
payload["reset_method"] = "local_admin_command"
}
c.JSON(http.StatusOK, payload)
}
+13 -13
View File
@@ -66,14 +66,14 @@ func TestHealthAndDomainAPI(t *testing.T) {
}
}
func TestResetPathRequiresScertAdminActivation(t *testing.T) {
func TestLegacyResetPathRequiresScertAdminActivation(t *testing.T) {
dir := t.TempDir()
cfg := readyTestConfig(dir)
cfg.SetupReady = true
cfg.Web.Hostname = "scert.example.test"
cfg.Server.Port = 8443
statePath := admin.StatePath(cfg.Paths.Data)
server := New(Dependencies{Config: cfg, Repository: &stubDomainRepository{}, ResetStatePath: statePath})
server := newLegacyResetServer(Dependencies{Config: cfg, Repository: &stubDomainRepository{}, ResetStatePath: statePath})
inactive := httptest.NewRecorder()
serveAdminHTTP(server, inactive, httptest.NewRequest(http.MethodGet, "/reset", nil))
@@ -103,7 +103,7 @@ func TestResetPathRequiresScertAdminActivation(t *testing.T) {
}
}
func TestResetPasswordUpdatesConfigAndConsumesSession(t *testing.T) {
func TestLegacyResetPasswordUpdatesConfigAndConsumesSession(t *testing.T) {
dir := t.TempDir()
cfg := readyTestConfig(dir)
cfg.SetupReady = true
@@ -125,7 +125,7 @@ func TestResetPasswordUpdatesConfigAndConsumesSession(t *testing.T) {
if err != nil {
t.Fatalf("启动测试重置会话失败:%v", err)
}
server := New(Dependencies{Config: cfg, ConfigPath: configPath, Repository: &stubDomainRepository{}, ResetStatePath: admin.StatePath(cfg.Paths.Data)})
server := newLegacyResetServer(Dependencies{Config: cfg, ConfigPath: configPath, Repository: &stubDomainRepository{}, ResetStatePath: admin.StatePath(cfg.Paths.Data)})
get := httptest.NewRecorder()
serveAdminHTTP(server, get, httptest.NewRequest(http.MethodGet, "http://scert.example.test/reset", nil))
if get.Code != http.StatusOK {
@@ -174,7 +174,7 @@ func TestResetPasswordUpdatesConfigAndConsumesSession(t *testing.T) {
}
}
func TestResetPasswordSynchronizesPrimarySuperAdminUser(t *testing.T) {
func TestLegacyResetPasswordSynchronizesPrimarySuperAdminUser(t *testing.T) {
dir := t.TempDir()
cfg := readyTestConfig(dir)
cfg.SetupReady = true
@@ -211,7 +211,7 @@ func TestResetPasswordSynchronizesPrimarySuperAdminUser(t *testing.T) {
if err != nil {
t.Fatalf("启动测试重置会话失败:%v", err)
}
server := New(Dependencies{
server := newLegacyResetServer(Dependencies{
Config: cfg, ConfigPath: configPath, Repository: &stubDomainRepository{}, Users: users,
ResetStatePath: admin.StatePath(cfg.Paths.Data),
})
@@ -243,7 +243,7 @@ func TestResetPasswordSynchronizesPrimarySuperAdminUser(t *testing.T) {
}
}
func TestResetRejectsCrossSiteAndLimitsFailures(t *testing.T) {
func TestLegacyResetRejectsCrossSiteAndLimitsFailures(t *testing.T) {
dir := t.TempDir()
cfg := readyTestConfig(dir)
cfg.SetupReady = true
@@ -257,7 +257,7 @@ func TestResetRejectsCrossSiteAndLimitsFailures(t *testing.T) {
if err != nil {
t.Fatalf("启动测试重置会话失败:%v", err)
}
server := New(Dependencies{Config: cfg, ConfigPath: filepath.Join(dir, "config", "scert.json"), Repository: &stubDomainRepository{}, ResetStatePath: admin.StatePath(cfg.Paths.Data)})
server := newLegacyResetServer(Dependencies{Config: cfg, ConfigPath: filepath.Join(dir, "config", "scert.json"), Repository: &stubDomainRepository{}, ResetStatePath: admin.StatePath(cfg.Paths.Data)})
get := httptest.NewRecorder()
serveAdminHTTP(server, get, httptest.NewRequest(http.MethodGet, "http://scert.example.test/reset", nil))
cookie := get.Result().Cookies()[0]
@@ -376,7 +376,7 @@ func TestServerAndDomainAssociationAPI(t *testing.T) {
t.Fatalf("服务器删除后域名关联未清理:%d %s", listResponse.Code, listResponse.Body.String())
}
}
func TestResetConfigSaveFailureKeepsSessionAndPassword(t *testing.T) {
func TestLegacyResetConfigSaveFailureKeepsSessionAndPassword(t *testing.T) {
fixture := newResetTestFixture(t)
blockedPath := filepath.Join(t.TempDir(), "config-target")
if err := os.MkdirAll(blockedPath, 0o700); err != nil {
@@ -415,7 +415,7 @@ func TestResetConfigSaveFailureKeepsSessionAndPassword(t *testing.T) {
}
}
func TestResetRejectsOversizedRequestBody(t *testing.T) {
func TestLegacyResetRejectsOversizedRequestBody(t *testing.T) {
fixture := newResetTestFixture(t)
form := resetForm(fixture.session.Code, fixture.cookie.Value, "NewPass1234")
form.Set("padding", strings.Repeat("x", resetMaxBodyBytes))
@@ -437,7 +437,7 @@ func TestResetRejectsOversizedRequestBody(t *testing.T) {
}
}
func TestResetCSRFCookieHasRestrictedAttributes(t *testing.T) {
func TestLegacyResetCSRFCookieHasRestrictedAttributes(t *testing.T) {
fixture := newResetTestFixture(t)
cookie := fixture.cookie
if !cookie.Secure || !cookie.HttpOnly || cookie.SameSite != http.SameSiteStrictMode {
@@ -451,7 +451,7 @@ func TestResetCSRFCookieHasRestrictedAttributes(t *testing.T) {
}
}
func TestResetConcurrentSubmissionsAllowOnlyOneSuccess(t *testing.T) {
func TestLegacyResetConcurrentSubmissionsAllowOnlyOneSuccess(t *testing.T) {
fixture := newResetTestFixture(t)
form := resetForm(fixture.session.Code, fixture.cookie.Value, "NewPass1234")
start := make(chan struct{})
@@ -536,7 +536,7 @@ func newResetTestFixture(t *testing.T) resetTestFixture {
if err != nil {
t.Fatalf("启动测试重置会话失败:%v", err)
}
server := New(Dependencies{Config: cfg, ConfigPath: configPath, Repository: &stubDomainRepository{}, ResetStatePath: admin.StatePath(cfg.Paths.Data)})
server := newLegacyResetServer(Dependencies{Config: cfg, ConfigPath: configPath, Repository: &stubDomainRepository{}, ResetStatePath: admin.StatePath(cfg.Paths.Data)})
get := httptest.NewRecorder()
serveAdminHTTP(server, get, httptest.NewRequest(http.MethodGet, "https://scert.example.test/reset", nil))
if get.Code != http.StatusOK {