Skip to content

Commit

Permalink
fix(registry): ignore empty challenge fields (#1626)
Browse files Browse the repository at this point in the history
Co-authored-by: caotian <[email protected]>
  • Loading branch information
piksel and caotian authored Apr 12, 2023
1 parent 9d6b008 commit 4d661bf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
7 changes: 3 additions & 4 deletions pkg/registry/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,9 @@ func GetAuthURL(challenge string, img string) (*url.URL, error) {

for _, pair := range pairs {
trimmed := strings.Trim(pair, " ")
kv := strings.Split(trimmed, "=")
key := kv[0]
val := strings.Trim(kv[1], "\"")
values[key] = val
if key, val, ok := strings.Cut(trimmed, "="); ok {
values[key] = strings.Trim(val, `"`)
}
}
logrus.WithFields(logrus.Fields{
"realm": values["realm"],
Expand Down
17 changes: 15 additions & 2 deletions pkg/registry/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package auth_test

import (
"fmt"
"github.com/containrrr/watchtower/internal/actions/mocks"
"github.com/containrrr/watchtower/pkg/registry/auth"
"net/url"
"os"
"testing"
"time"

"github.com/containrrr/watchtower/internal/actions/mocks"
"github.com/containrrr/watchtower/pkg/registry/auth"

wtTypes "github.com/containrrr/watchtower/pkg/types"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -79,6 +80,18 @@ var _ = Describe("the auth module", func() {
Expect(err).To(HaveOccurred())
Expect(res).To(BeNil())
})
It("should not crash when an empty field is recieved", func() {
input := `bearer realm="https://ghcr.io/token",service="ghcr.io",scope="repository:user/image:pull",`
res, err := auth.GetAuthURL(input, "containrrr/watchtower")
Expect(err).NotTo(HaveOccurred())
Expect(res).NotTo(BeNil())
})
It("should not crash when a field without a value is recieved", func() {
input := `bearer realm="https://ghcr.io/token",service="ghcr.io",scope="repository:user/image:pull",valuelesskey`
res, err := auth.GetAuthURL(input, "containrrr/watchtower")
Expect(err).NotTo(HaveOccurred())
Expect(res).NotTo(BeNil())
})
})
When("getting a challenge url", func() {
It("should create a valid challenge url object based on the image ref supplied", func() {
Expand Down

0 comments on commit 4d661bf

Please sign in to comment.