Skip to content

Commit 6a89fa1

Browse files
committed
fix - signed author for backends logic
1 parent 4bf343a commit 6a89fa1

File tree

3 files changed

+39
-33
lines changed

3 files changed

+39
-33
lines changed

server/cmd/progressdb/main.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ import (
1818
)
1919

2020
func main() {
21-
// build metadata - set via ldflags during build/release
22-
var (
23-
version = "dev"
24-
commit = "none"
25-
buildDate = "unknown"
26-
)
21+
// build metadata - set via ldflags during build/release
22+
var (
23+
version = "dev"
24+
commit = "none"
25+
buildDate = "unknown"
26+
)
2727
// Parse flags (moved into config package to centralize flag parsing)
2828
_ = godotenv.Load(".env")
2929
addrVal, dbVal, cfgVal, setFlags := config.ParseCommandFlags()
@@ -108,27 +108,27 @@ func main() {
108108
if _, err := config.Load(cfgPath); err == nil {
109109
srcs = append(srcs, "config")
110110
}
111-
// Include version/commit info in the startup banner when present.
112-
verStr := version
113-
if commit != "none" {
114-
verStr = verStr + " (" + commit + ")"
115-
}
116-
if buildDate != "unknown" {
117-
verStr = verStr + " @ " + buildDate
118-
}
119-
banner.Print(addr, dbPath, strings.Join(srcs, ", "), verStr)
111+
// Include version/commit info in the startup banner when present.
112+
verStr := version
113+
if commit != "none" {
114+
verStr = verStr + " (" + commit + ")"
115+
}
116+
if buildDate != "unknown" {
117+
verStr = verStr + " @ " + buildDate
118+
}
119+
banner.Print(addr, dbPath, strings.Join(srcs, ", "), verStr)
120120

121121
mux := http.NewServeMux()
122122

123-
// Serve the web viewer at /viewer/
124-
mux.Handle("/viewer/", http.StripPrefix("/viewer/", http.FileServer(http.Dir("./viewer"))))
123+
// Serve the web viewer at /viewer/
124+
mux.Handle("/viewer/", http.StripPrefix("/viewer/", http.FileServer(http.Dir("./viewer"))))
125125

126-
// Liveness probe used by deployment systems and CI
127-
mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
128-
w.Header().Set("Content-Type", "application/json")
129-
w.WriteHeader(http.StatusOK)
130-
_, _ = w.Write([]byte("{\"status\":\"ok\"}"))
131-
})
126+
// Liveness probe used by deployment systems and CI
127+
mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
128+
w.Header().Set("Content-Type", "application/json")
129+
w.WriteHeader(http.StatusOK)
130+
_, _ = w.Write([]byte("{\"status\":\"ok\"}"))
131+
})
132132

133133
// API handler (catch-all under /)
134134
mux.Handle("/", api.Handler())
@@ -182,7 +182,7 @@ func main() {
182182
}
183183
config.SetRuntime(rc)
184184

185-
wrapped := security.NewMiddleware(secCfg)(mux)
185+
wrapped := security.AuthenticateRequestMiddleware(secCfg)(mux)
186186

187187
// TLS support: use values from effective cfg
188188
cert := cfg.Server.TLS.CertFile

server/pkg/auth/middleware.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ import (
1414

1515
type ctxAuthorKey struct{}
1616

17-
// (no exported getter needed; use config.GetSigningKeys and
18-
// config.GetBackendKeys as the single source of truth)
19-
2017
// RequireSignedAuthor verifies HMAC signature headers and injects the
2118
// verified author id into the request context.
2219
func RequireSignedAuthor(next http.Handler) http.Handler {
@@ -26,19 +23,28 @@ func RequireSignedAuthor(next http.Handler) http.Handler {
2623
userID := strings.TrimSpace(r.Header.Get("X-User-ID"))
2724
sig := strings.TrimSpace(r.Header.Get("X-User-Signature"))
2825

29-
// Backend/admin callers: allow missing signature but accept header if present.
26+
// Backend/admin callers: allow missing signature entirely, or accept
27+
// a header-provided author without a signature. If a signature is
28+
// present we will verify it below.
3029
if role == "backend" || role == "admin" {
31-
if userID == "" && sig == "" {
30+
if sig == "" {
3231
// No signature provided; allow the request through. Handlers may
3332
// accept an author from body or X-User-ID header as appropriate.
3433
next.ServeHTTP(w, r)
3534
return
3635
}
37-
// If a signature is provided, verify it as usual.
36+
// signature present -> fallthrough to verification logic
3837
}
3938

40-
// For frontend or when signature is present, require and verify signature.
41-
if userID == "" || sig == "" {
39+
// If we reach here and there's no signature, the caller is not a
40+
// trusted backend/admin and we must require signature headers.
41+
if sig == "" {
42+
slog.Warn("missing_signature_headers", "path", r.URL.Path, "remote", r.RemoteAddr)
43+
http.Error(w, `{"error":"missing signature headers"}`, http.StatusUnauthorized)
44+
return
45+
}
46+
// signature is present; require userID as well
47+
if userID == "" {
4248
slog.Warn("missing_signature_headers", "path", r.URL.Path, "remote", r.RemoteAddr)
4349
http.Error(w, `{"error":"missing signature headers"}`, http.StatusUnauthorized)
4450
return

server/pkg/security/middleware.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type SecConfig struct {
3232
AllowUnauth bool
3333
}
3434

35-
func NewMiddleware(cfg SecConfig) func(http.Handler) http.Handler {
35+
func AuthenticateRequestMiddleware(cfg SecConfig) func(http.Handler) http.Handler {
3636
// Rate limiters keyed by API key or remote IP
3737
limiters := &limiterPool{cfg: cfg}
3838
return func(next http.Handler) http.Handler {

0 commit comments

Comments
 (0)