fix(externalIpProvider/plain): when reading response body (#54)
All checks were successful
CD / Check changes (push) Successful in 6s
CD / test (push) Successful in 2m48s
CD / Build and push (amd64) (push) Successful in 40s
CD / Build and push (arm64) (push) Successful in 2m40s
CD / Create manifest (push) Successful in 8s

Fixing an error where the service crashes reading the response body, when no content-length header was provided in the response.

Reviewed-on: #54
Reviewed-by: branch-buddy <branch-buddy@t00n.de>
Co-authored-by: Timo Behrendt <t.behrendt@t00n.de>
Co-committed-by: Timo Behrendt <t.behrendt@t00n.de>
This commit was merged in pull request #54.
This commit is contained in:
2025-11-07 17:14:14 +01:00
committed by t.behrendt
parent 660f2eac0d
commit a79ce64e82

View File

@@ -2,6 +2,7 @@ package externalIpProvider
import ( import (
"errors" "errors"
"io"
"net" "net"
"net/http" "net/http"
"net/url" "net/url"
@@ -40,12 +41,15 @@ func (p *ExternalIpProviderImplPlain) GetExternalIp() (net.IP, error) {
} }
if res.StatusCode != 200 { if res.StatusCode != 200 {
res.Body.Close()
return nil, errors.New("unexpected status code") return nil, errors.New("unexpected status code")
} }
responseBody := make([]byte, res.ContentLength) responseBody, err := io.ReadAll(res.Body)
res.Body.Read(responseBody) res.Body.Close()
defer res.Body.Close() if err != nil {
return nil, err
}
parsedIp := net.ParseIP(string(responseBody)) parsedIp := net.ParseIP(string(responseBody))
if parsedIp == nil { if parsedIp == nil {