From baa9f0e5733800ef93543a84ff4442cb5a9e7545 Mon Sep 17 00:00:00 2001 From: Zakhar Bessarab Date: Thu, 30 Mar 2023 14:18:00 +0300 Subject: [PATCH] lib/fs: verify response code when reading configuration over HTTP (#4036) Verifying status code helps to avoid misleading errors caused by attempt to parse unsuccessful response. Related issue: #4034 Signed-off-by: Zakhar Bessarab --- docs/CHANGELOG.md | 1 + lib/fs/fs.go | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 23c03e24f..152ae4fac 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -17,6 +17,7 @@ The following tip changes can be tested by building VictoriaMetrics components f * BUGFIX: [MetricsQL](https://docs.victoriametrics.com/MetricsQL.html): properly convert [VictoriaMetrics historgram buckets](https://valyala.medium.com/improving-histogram-usability-for-prometheus-and-grafana-bc7e5df0e350) to Prometheus histogram buckets when VictoriaMetrics histogram contain zero buckets. Previously these buckets were ignored, and this could lead to missing Prometheus histogram buckets after the conversion. Thanks to @zklapow for [the fix](https://github.com/VictoriaMetrics/VictoriaMetrics/pull/4021). * BUGFIX: properly support comma-separated filters inside [retention filters](https://docs.victoriametrics.com/#retention-filters). See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3915). +* BUGFIX: verify response code when fetching configuration files via HTTP. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/4034). ## [v1.87.4](https://github.com/VictoriaMetrics/VictoriaMetrics/releases/tag/v1.87.4) diff --git a/lib/fs/fs.go b/lib/fs/fs.go index 81d77911b..a42cb988c 100644 --- a/lib/fs/fs.go +++ b/lib/fs/fs.go @@ -439,6 +439,11 @@ func ReadFileOrHTTP(path string) ([]byte, error) { if err != nil { return nil, fmt.Errorf("cannot fetch %q: %w", path, err) } + + if resp.StatusCode != http.StatusOK { + return nil, fmt.Errorf("unexpected status code when fetching %q: %d, expecting %d", path, resp.StatusCode, http.StatusOK) + } + data, err := io.ReadAll(resp.Body) _ = resp.Body.Close() if err != nil {