This is a large squashed commit covering two batches of prior uncommitted work plus a full security-audit remediation pass, kept together because go.mod/go.sum and several shared files (main.go, handler.go) were touched by both and splitting risked non-building intermediate commits. Features (built earlier, previously uncommitted): - Local username/password login for single-tenant deployments with no SSO configured (api/localauth, alerting/internal/sessioncheck, sentryctl users, web/src/routes/login, metadata migrations 0040/0041). - Remotely-editable additional log file paths for agents, on top of their existing primary source (api/agents, agent/sentry-agent extra-file-path diffing, web agent config UI). - IPv4/IPv6 addresses reported alongside other host system metrics. Security audit remediation (this pass, all live-verified in production): - Critical: block ClickHouse SSRF table functions (url/remote/file/s3/...) in the raw-SQL query escape hatch. - High: deny sensitive paths and require Admin to add agent extra_file_paths (Editor could previously point an agent at /etc/shadow or an SSH key); alerting webhook targets now validate against internal/metadata/loopback addresses, both at creation and send time; alerting's session middleware now enforces an Editor+ floor on mutating requests instead of "any authenticated session"; bumped goxmldsig to close a SAML signature-verification bypass (GO-2026-4753). - Medium: per-IP login rate limiting; security response headers (HSTS/CSP/nosniff/X-Frame-Options/Referrer-Policy/Permissions-Policy) on web/nginx.conf; a DevCredentialWarnings check in every Go service's config loader, logging loudly at startup if a deployment is still on docker-compose.yml's literal dev-only credentials; dependency bumps (golang.org/x/text, grpc, x/net, quick-xml, h2) across every affected Go module and both Rust crates, including a previously-uncovered x/net vulnerability in deploy/operator; a new security-scan.yml CI workflow running cargo-deny/govulncheck/npm-audit, mirroring the existing license-compliance.yml matrix shape. - Low: removed sentryctl's plaintext --password flag (shell history/`ps` exposure) in favor of stdin and a --password-stdin flag for reset-password's optional specific-password path; a dummy bcrypt comparison closes a login response-time username-enumeration side-channel.
92 lines
3.1 KiB
Go
92 lines
3.1 KiB
Go
package sessioncheck
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
)
|
|
|
|
// sessionCookieName must match api/localauth's sessionCookieName
|
|
// exactly (unexported there too, deliberately duplicated rather than
|
|
// imported -- see this package's doc comment) -- the same cookie
|
|
// api/localauth.Handler.setCookie writes, scoped (via SESSION_COOKIE_
|
|
// DOMAIN) to cover both api's and alerting's subdomains in production.
|
|
const sessionCookieName = "sentry_local_session"
|
|
|
|
type errorResponse struct {
|
|
Error string `json:"error"`
|
|
}
|
|
|
|
func writeUnauthorized(w http.ResponseWriter) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
_ = json.NewEncoder(w).Encode(errorResponse{Error: "unauthorized"})
|
|
}
|
|
|
|
func writeForbidden(w http.ResponseWriter) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusForbidden)
|
|
_ = json.NewEncoder(w).Encode(errorResponse{Error: "forbidden"})
|
|
}
|
|
|
|
// mutatingRoleFloor is the minimum role RequireSession enforces for any
|
|
// non-read request -- closes a real gap the security audit found: this
|
|
// package used to be a pure "logged in or not" gate with no role check
|
|
// at all, meaning a Viewer-role session could create/delete alert rules
|
|
// and notification targets exactly like an Editor. GET/HEAD (read-only)
|
|
// stay at "any valid session," matching every role floor in this
|
|
// codebase's other RBAC-gated resources (queries, dashboards) using
|
|
// Viewer as their read bar.
|
|
const mutatingRoleFloor = "editor"
|
|
|
|
func isReadOnly(method string) bool {
|
|
return method == http.MethodGet || method == http.MethodHead
|
|
}
|
|
|
|
func credentialFromRequest(r *http.Request) string {
|
|
if auth := r.Header.Get("Authorization"); auth != "" {
|
|
const prefix = "Bearer "
|
|
if len(auth) > len(prefix) && auth[:len(prefix)] == prefix {
|
|
return auth[len(prefix):]
|
|
}
|
|
}
|
|
if cookie, err := r.Cookie(sessionCookieName); err == nil {
|
|
return cookie.Value
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// RequireSession wraps next so every request needs a valid local-login
|
|
// session -- a blanket gate, not per-route roles: alerting has no
|
|
// role-check plumbing at all today (unlike api/authz's per-route
|
|
// RequireRole), and building a full parallel system just for this
|
|
// feature is out of scope (see /docs/agent-management-design.md-style
|
|
// "resist scope creep" discipline this codebase applies everywhere).
|
|
// GET /healthz is deliberately exempt -- Docker's HEALTHCHECK execs
|
|
// this same binary against itself over loopback (cmd/alerting/main.go's
|
|
// runHealthcheck), pre-auth, and must keep working regardless of
|
|
// whether local auth is enabled.
|
|
func RequireSession(checker *Checker, next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path == "/healthz" {
|
|
next.ServeHTTP(w, r)
|
|
return
|
|
}
|
|
|
|
raw := credentialFromRequest(r)
|
|
if raw == "" {
|
|
writeUnauthorized(w)
|
|
return
|
|
}
|
|
role, err := checker.Validate(r.Context(), raw)
|
|
if err != nil {
|
|
writeUnauthorized(w)
|
|
return
|
|
}
|
|
if !isReadOnly(r.Method) && !roleSatisfies(role, mutatingRoleFloor) {
|
|
writeForbidden(w)
|
|
return
|
|
}
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|