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 <z.bessarab@victoriametrics.com>
This commit is contained in:
Zakhar Bessarab 2023-03-30 14:18:00 +03:00 committed by Aliaksandr Valialkin
parent 78f8e3a2c3
commit 0e042a835e
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1
2 changed files with 8 additions and 0 deletions

View file

@ -15,6 +15,8 @@ The following tip changes can be tested by building VictoriaMetrics components f
## v1.79.x long-time support release (LTS)
* BUGFIX: verify response code when fetching configuration files via HTTP. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/4034).
## [v1.79.11](https://github.com/VictoriaMetrics/VictoriaMetrics/releases/tag/v1.79.11)
Released at 2023-03-12

View file

@ -385,6 +385,12 @@ func ReadFileOrHTTP(path string) ([]byte, error) {
}
data, err := ioutil.ReadAll(resp.Body)
_ = resp.Body.Close()
if resp.StatusCode != http.StatusOK {
if len(data) > 4192 {
data = data[:4192]
}
return nil, fmt.Errorf("unexpected status code when fetching %q: %d, expecting %d; response: %q", path, resp.StatusCode, http.StatusOK, data)
}
if err != nil {
return nil, fmt.Errorf("cannot read %q: %s", path, err)
}