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

Allow parameterized application/json content types #386

Merged
merged 1 commit into from
Feb 5, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 15 additions & 2 deletions pkg/api/ca.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"mime"
"net/http"
"strings"

Expand Down Expand Up @@ -110,14 +111,26 @@ func actualAuthorize(req *http.Request) (*oidc.IDToken, error) {
return verifier.Verify(req.Context(), token)
}

func verifyContentType(contentType string) error {
gotContentType, _, perr := mime.ParseMediaType(contentType)
if perr != nil {
return fmt.Errorf("could not parse Content-Type %q", contentType)
}
wantContentType := "application/json"
if gotContentType != wantContentType {
return fmt.Errorf("signing cert handler must receive %q, got %q", wantContentType, gotContentType)
}
return nil
}

func (a *api) signingCert(w http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodPost {
err := fmt.Errorf("signing cert handler must receive POSTs, got %s", req.Method)
handleFulcioAPIError(w, req, http.StatusMethodNotAllowed, err, err.Error())
return
}
if gotContentType, wantContentType := req.Header.Get("Content-Type"), "application/json"; gotContentType != wantContentType {
err := fmt.Errorf("signing cert handler must receive %q, got %q", wantContentType, gotContentType)

if err := verifyContentType(req.Header.Get("Content-Type")); err != nil {
handleFulcioAPIError(w, req, http.StatusUnsupportedMediaType, err, err.Error())
return
}
Expand Down
42 changes: 42 additions & 0 deletions pkg/api/ca_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2022 The Sigstore Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package api

import "testing"

func TestVerifyContentType(t *testing.T) {
testCases := []struct {
contentType string
shouldPass bool
}{
{"application/json", true},
{"application/json; charset=UTF-8", true},
{"application/unsupported", false},
{"some nonsense", false},
}

for _, tc := range testCases {
t.Run(tc.contentType, func(t *testing.T) {
err := verifyContentType(tc.contentType)
if err != nil && tc.shouldPass {
t.Fatalf("Expected no error but got %q", err)
}
if err == nil && !tc.shouldPass {
t.Fatalf("Expected error but got none")
}
})
}
}