Files
Pulse/internal/securityutil/control_plane_http.go
pulse-triage[bot] 47c605e279 Keep remote-config redirect destinations out of agent diagnostics
Use the existing pre-parse no-follow boundary for startup and periodic hostname/config reads, preserving TLS, signatures, local authority and fallback. Share it with the already protected host consumers and bind real report closure, startup, refresh and same-credential recovery to registered regression proof.

Change-source: pulse-maintainer
2026-11-12 11:57:12 +00:00

65 lines
2.2 KiB
Go

package securityutil
import (
"errors"
"fmt"
"net/http"
)
// AgentControlPlaneRedirectError contains only local text and a numeric status. A
// reverse proxy's Location can echo credentials or private endpoint details.
type AgentControlPlaneRedirectError struct {
StatusCode int
}
func (e *AgentControlPlaneRedirectError) Error() string {
if e.StatusCode == 0 {
return "server returned a redirect - use the final Pulse URL explicitly"
}
return fmt.Sprintf("server returned HTTP %d redirect - use the final Pulse URL explicitly", e.StatusCode)
}
// DoAgentControlPlaneRequest preserves the supplied client's TLS, proxy, timeout and
// cookie handling without allowing redirects. Reject before net/http parses a
// Location: even a parse failure or a fixed CheckRedirect error can otherwise
// put the untrusted destination in an error that runtime callers log.
func DoAgentControlPlaneRequest(client *http.Client, request *http.Request) (*http.Response, error) {
guarded := *client
transport := guarded.Transport
if transport == nil {
transport = http.DefaultTransport
}
guarded.Transport = controlPlaneNoRedirectTransport{base: transport}
response, err := guarded.Do(request)
var redirect *AgentControlPlaneRedirectError
if errors.As(err, &redirect) {
// net/http wraps RoundTrip errors in a URL error. Release only our
// fixed diagnostic, not that wrapper or any upstream text.
return nil, redirect
}
return response, err
}
type controlPlaneNoRedirectTransport struct {
base http.RoundTripper
}
func (t controlPlaneNoRedirectTransport) RoundTrip(request *http.Request) (*http.Response, error) {
response, err := t.base.RoundTrip(request)
if err != nil || response == nil {
return response, err
}
switch response.StatusCode {
case http.StatusMovedPermanently, http.StatusFound, http.StatusSeeOther, http.StatusTemporaryRedirect, http.StatusPermanentRedirect:
if response.Header.Get("Location") != "" {
// No destination parsing, response body reading or raw close-error
// propagation. The caller receives no open rejected response.
if response.Body != nil {
_ = response.Body.Close()
}
return nil, &AgentControlPlaneRedirectError{StatusCode: response.StatusCode}
}
}
return response, nil
}