cmd/local-user-authenticator: protect against nil-body

I saw this while reading other TokenReview code.
This commit is contained in:
Andrew Keesler 2020-09-11 12:44:45 -04:00
parent 4e40c0320e
commit 17d40b7a73
No known key found for this signature in database
GPG Key ID: 27CE0444346F9413

View File

@ -109,12 +109,11 @@ func (w *webhook) start(ctx context.Context, l net.Listener) error {
}
func (w *webhook) ServeHTTP(rsp http.ResponseWriter, req *http.Request) {
defer req.Body.Close()
username, password, err := getUsernameAndPasswordFromRequest(rsp, req)
if err != nil {
return
}
defer req.Body.Close()
secret, err := w.secretInformer.Lister().Secrets(namespace).Get(username)
notFound := k8serrors.IsNotFound(err)
@ -184,6 +183,12 @@ func getUsernameAndPasswordFromRequest(rsp http.ResponseWriter, req *http.Reques
return "", "", invalidRequest
}
if req.Body == nil {
klog.InfoS("invalid nil body")
rsp.WriteHeader(http.StatusBadRequest)
return "", "", invalidRequest
}
var body authenticationv1beta1.TokenReview
if err := json.NewDecoder(req.Body).Decode(&body); err != nil {
klog.InfoS("failed to decode body", "err", err)