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.
169 lines
5.5 KiB
Go
169 lines
5.5 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
var httpClient = &http.Client{Timeout: 30 * time.Second}
|
|
|
|
// setAuth attaches SENTRYCTL_TOKEN (see resolveToken) as a Bearer
|
|
// credential, a no-op when token is empty -- matches every backend's
|
|
// nil-authorizer no-op default (see api/internal/authz.RequireRole*).
|
|
func setAuth(req *http.Request, token string) {
|
|
if token != "" {
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
}
|
|
}
|
|
|
|
// httpGetJSON GETs path and prints the pretty-printed JSON response to
|
|
// stdout, or the error body/status to stderr. Shared by dashboards/alerts
|
|
// list and get, which otherwise differ only in path and resource name.
|
|
func httpGetJSON(baseURL, path, token string, stdout, stderr io.Writer) int {
|
|
req, err := http.NewRequest(http.MethodGet, baseURL+path, nil)
|
|
if err != nil {
|
|
fmt.Fprintf(stderr, "building request: %v\n", err)
|
|
return 1
|
|
}
|
|
setAuth(req, token)
|
|
resp, err := httpClient.Do(req)
|
|
if err != nil {
|
|
fmt.Fprintf(stderr, "request failed: %v\n", err)
|
|
return 1
|
|
}
|
|
defer resp.Body.Close()
|
|
return printJSONResponse(resp, stdout, stderr)
|
|
}
|
|
|
|
// httpPostFileJSON reads file (a JSON document, e.g. an exported
|
|
// dashboard or a rule definition) and POSTs it to path as-is -- no
|
|
// reshaping, since the file's shape already matches what the endpoint
|
|
// expects (the same JSON the web UI's export button and POST /rules
|
|
// produce/accept respectively). This is what makes "apply" the seed of a
|
|
// future Terraform provider: one JSON contract, multiple callers.
|
|
func httpPostFileJSON(baseURL, path, token, file string, stdout, stderr io.Writer) int {
|
|
body, err := os.ReadFile(file)
|
|
if err != nil {
|
|
fmt.Fprintf(stderr, "reading %s: %v\n", file, err)
|
|
return 1
|
|
}
|
|
req, err := http.NewRequest(http.MethodPost, baseURL+path, bytes.NewReader(body))
|
|
if err != nil {
|
|
fmt.Fprintf(stderr, "building request: %v\n", err)
|
|
return 1
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
setAuth(req, token)
|
|
resp, err := httpClient.Do(req)
|
|
if err != nil {
|
|
fmt.Fprintf(stderr, "request failed: %v\n", err)
|
|
return 1
|
|
}
|
|
defer resp.Body.Close()
|
|
return printJSONResponse(resp, stdout, stderr)
|
|
}
|
|
|
|
// httpMutateNoBody sends method to path with an optional JSON body
|
|
// ("" for none, e.g. DELETE) and expects a 2xx with no meaningful
|
|
// response body -- PUT/DELETE /dashboards/{id}/permissions/{userId}
|
|
// both respond 204 No Content, so there's nothing for printJSONResponse
|
|
// to pretty-print here. Prints successMsg to stdout on success, the
|
|
// same {"error": "..."} parsing every other helper in this file uses
|
|
// otherwise.
|
|
func httpMutateNoBody(method, baseURL, path, token, body, successMsg string, stdout, stderr io.Writer) int {
|
|
var reqBody io.Reader
|
|
if body != "" {
|
|
reqBody = strings.NewReader(body)
|
|
}
|
|
req, err := http.NewRequest(method, baseURL+path, reqBody)
|
|
if err != nil {
|
|
fmt.Fprintf(stderr, "building request: %v\n", err)
|
|
return 1
|
|
}
|
|
if body != "" {
|
|
req.Header.Set("Content-Type", "application/json")
|
|
}
|
|
setAuth(req, token)
|
|
resp, err := httpClient.Do(req)
|
|
if err != nil {
|
|
fmt.Fprintf(stderr, "request failed: %v\n", err)
|
|
return 1
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
|
respBody, _ := io.ReadAll(resp.Body)
|
|
var errResp errorResponseBody
|
|
if json.Unmarshal(respBody, &errResp) == nil && errResp.Error != "" {
|
|
fmt.Fprintf(stderr, "request failed: %s\n", errResp.Error)
|
|
} else {
|
|
fmt.Fprintf(stderr, "request failed: status %d\n", resp.StatusCode)
|
|
}
|
|
return 1
|
|
}
|
|
fmt.Fprintln(stdout, successMsg)
|
|
return 0
|
|
}
|
|
|
|
// httpPutJSON PUTs body (already-encoded JSON) to path and prints the
|
|
// pretty-printed JSON response -- same shape as httpPostFileJSON, but
|
|
// for callers that construct the body themselves rather than reading it
|
|
// from a file (agents config set, agents restart).
|
|
func httpPutJSON(baseURL, path, token, body string, stdout, stderr io.Writer) int {
|
|
return httpSendJSON(http.MethodPut, baseURL, path, token, body, stdout, stderr)
|
|
}
|
|
|
|
// httpPostJSON is httpPutJSON's POST sibling -- for callers creating a
|
|
// resource from a body they built themselves rather than reading it
|
|
// from a file (users create, users reset-password).
|
|
func httpPostJSON(baseURL, path, token, body string, stdout, stderr io.Writer) int {
|
|
return httpSendJSON(http.MethodPost, baseURL, path, token, body, stdout, stderr)
|
|
}
|
|
|
|
func httpSendJSON(method, baseURL, path, token, body string, stdout, stderr io.Writer) int {
|
|
req, err := http.NewRequest(method, baseURL+path, strings.NewReader(body))
|
|
if err != nil {
|
|
fmt.Fprintf(stderr, "building request: %v\n", err)
|
|
return 1
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
setAuth(req, token)
|
|
resp, err := httpClient.Do(req)
|
|
if err != nil {
|
|
fmt.Fprintf(stderr, "request failed: %v\n", err)
|
|
return 1
|
|
}
|
|
defer resp.Body.Close()
|
|
return printJSONResponse(resp, stdout, stderr)
|
|
}
|
|
|
|
func printJSONResponse(resp *http.Response, stdout, stderr io.Writer) int {
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
fmt.Fprintf(stderr, "reading response: %v\n", err)
|
|
return 1
|
|
}
|
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
|
var errResp errorResponseBody
|
|
if json.Unmarshal(body, &errResp) == nil && errResp.Error != "" {
|
|
fmt.Fprintf(stderr, "request failed: %s\n", errResp.Error)
|
|
} else {
|
|
fmt.Fprintf(stderr, "request failed: status %d\n", resp.StatusCode)
|
|
}
|
|
return 1
|
|
}
|
|
var pretty bytes.Buffer
|
|
if json.Indent(&pretty, body, "", " ") == nil {
|
|
stdout.Write(pretty.Bytes())
|
|
} else {
|
|
stdout.Write(body)
|
|
}
|
|
fmt.Fprintln(stdout)
|
|
return 0
|
|
}
|