Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the error message when URL form encoding is received. #806

Merged
merged 1 commit into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pkg/interceptors/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ import (
"k8s.io/client-go/kubernetes"
)

// ErrInvalidContentType is returned when the content-type is not a JSON body.
var ErrInvalidContentType = errors.New("form parameter encoding not supported, please change the hook to send JSON payloads")

type Interceptor struct {
KubeClientSet kubernetes.Interface
Logger *zap.SugaredLogger
Expand All @@ -49,6 +52,9 @@ func NewInterceptor(gh *triggersv1.GitHubInterceptor, k kubernetes.Interface, ns
func (w *Interceptor) ExecuteTrigger(request *http.Request) (*http.Response, error) {
payload := []byte{}
var err error
if v := request.Header.Get("Content-Type"); v == "application/x-www-form-urlencoded" {
return nil, ErrInvalidContentType
}

if request.Body != nil {
defer request.Body.Close()
Expand Down
23 changes: 23 additions & 0 deletions pkg/interceptors/github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,26 @@ func TestInterceptor_ExecuteTrigger_Signature(t *testing.T) {
})
}
}

func TestInterceptor_ExecuteTrigger_with_invalid_content_type(t *testing.T) {
ctx, _ := rtesting.SetupFakeContext(t)
logger, _ := logging.NewLogger("", "")
kubeClient := fakekubeclient.Get(ctx)
request := &http.Request{
Body: ioutil.NopCloser(bytes.NewBufferString("somepayload")),
Header: http.Header{
"Content-Type": []string{"application/x-www-form-urlencoded"},
"X-Hub-Signature": []string{"foo"},
},
}
w := &Interceptor{
KubeClientSet: kubeClient,
GitHub: &triggersv1.GitHubInterceptor{},
Logger: logger,
EventListenerNamespace: metav1.NamespaceDefault,
}
_, err := w.ExecuteTrigger(request)
if err != ErrInvalidContentType {
t.Fatalf("got error %v, want %v", err, ErrInvalidContentType)
}
}