feat(check): check JSON APIs against conditions on response values

A JSON check requests a URL with GET or POST, optional headers and body,
and requires the expected status and every condition: paths like
checks[0].state compared with ==, !=, <, <=, >, >=, contains, exists or
absent. Failures name the path, the value found and the expectation.
HTTP and JSON checks share the request code and its network guard.

Refs #26

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
Sebastian UnterschützandClaude Opus 5.5 committed 2026-11-22 11:45:11 +01:00
1 parent 8fe56546a0
commit 0815148ea1
5 files changed
+474 -30

No files matched your search

+66 -29
View File
@@ -36,8 +36,7 @@ const maxBody = 1 << 20
type httpCheck struct{}
func (httpCheck) validate(target string, config json.RawMessage) (json.RawMessage, error) {
u, err := url.Parse(target)
if err != nil || (u.Scheme != "http" && u.Scheme != "https") || u.Host == "" || u.User != nil {
if !validURL(target) {
return nil, invalid("invalid_target")
}
c := HTTPConfig{Method: http.MethodGet, ExpectStatus: []string{"2xx", "3xx"}, FollowRedirects: true, VerifyTLS: true}
@@ -59,13 +58,11 @@ func (httpCheck) validate(target string, config json.RawMessage) (json.RawMessag
return nil, invalid("invalid_expect_status")
}
}
if len(c.Keyword) > 500 || len(c.Headers) > 20 {
if len(c.Keyword) > 500 {
return nil, invalid("invalid_config")
}
for k, v := range c.Headers {
if k == "" || strings.ContainsAny(k+v, "\r\n") || strings.EqualFold(k, "Host") {
return nil, invalid("invalid_header")
}
if !validHeaders(c.Headers) {
return nil, invalid("invalid_header")
}
return encode(c), nil
}
@@ -93,28 +90,7 @@ func (httpCheck) run(ctx context.Context, s Spec) Outcome {
if err != nil {
return Outcome{Error: "invalid configuration"}
}
d := netguard.Dialer(s.AllowPrivate, s.Timeout)
tr := &http.Transport{
DialContext: d.DialContext,
TLSClientConfig: &tls.Config{InsecureSkipVerify: !c.VerifyTLS}, //nolint:gosec // opt-out chosen by the customer per monitor
DisableKeepAlives: true,
TLSHandshakeTimeout: s.Timeout,
ForceAttemptHTTP2: true,
}
defer tr.CloseIdleConnections()
client := &http.Client{Transport: tr}
if !c.FollowRedirects {
client.CheckRedirect = func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }
}
req, err := http.NewRequestWithContext(ctx, c.Method, s.Target, nil)
if err != nil {
return Outcome{Error: "invalid target"}
}
req.Header.Set("User-Agent", "uptimesebi-checker")
for k, v := range c.Headers {
req.Header.Set(k, v)
}
resp, err := client.Do(req)
resp, err := fetch(ctx, s, request{Method: c.Method, Headers: c.Headers, VerifyTLS: c.VerifyTLS, FollowRedirects: c.FollowRedirects})
if err != nil {
return Outcome{Error: describeNetErr(err)}
}
@@ -144,6 +120,67 @@ func (httpCheck) run(ctx context.Context, s Spec) Outcome {
return o
}
// request describes an HTTP request of a check.
type request struct {
Method string
Headers map[string]string
Body string
VerifyTLS bool
FollowRedirects bool
}
// fetch sends r to s.Target through the network guard. The caller closes
// the response body.
func fetch(ctx context.Context, s Spec, r request) (*http.Response, error) {
d := netguard.Dialer(s.AllowPrivate, s.Timeout)
tr := &http.Transport{
DialContext: d.DialContext,
TLSClientConfig: &tls.Config{InsecureSkipVerify: !r.VerifyTLS}, //nolint:gosec // opt-out chosen by the customer per monitor
DisableKeepAlives: true,
TLSHandshakeTimeout: s.Timeout,
ForceAttemptHTTP2: true,
}
client := &http.Client{Transport: tr}
if !r.FollowRedirects {
client.CheckRedirect = func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }
}
var body io.Reader
if r.Body != "" {
body = strings.NewReader(r.Body)
}
req, err := http.NewRequestWithContext(ctx, r.Method, s.Target, body)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", "uptimesebi-checker")
if r.Body != "" {
req.Header.Set("Content-Type", "application/json")
}
for k, v := range r.Headers {
req.Header.Set(k, v)
}
return client.Do(req)
}
// validURL accepts absolute http(s) URLs without credentials.
func validURL(target string) bool {
u, err := url.Parse(target)
return err == nil && (u.Scheme == "http" || u.Scheme == "https") && u.Host != "" && u.User == nil
}
// validHeaders rejects headers that could smuggle requests or redirect them.
func validHeaders(h map[string]string) bool {
if len(h) > 20 {
return false
}
for k, v := range h {
if k == "" || strings.ContainsAny(k+v, "\r\n") || strings.EqualFold(k, "Host") {
return false
}
}
return true
}
// describeNetErr turns transport errors into short, stable messages.
func describeNetErr(err error) string {
var dnsErr *net.DNSError