Skip to content

Commit

Permalink
fix: add more tsa url sanity check (notaryproject#37)
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Zheng <[email protected]>
  • Loading branch information
Two-Hearts authored Oct 29, 2024
1 parent a614afb commit 718b4b8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
9 changes: 8 additions & 1 deletion http.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,16 @@ func NewHTTPTimestamper(httpClient *http.Client, endpoint string) (Timestamper,
if httpClient == nil {
httpClient = &http.Client{Timeout: 5 * time.Second}
}
if _, err := url.Parse(endpoint); err != nil {
tsaURL, err := url.Parse(endpoint)
if err != nil {
return nil, err
}
if tsaURL.Scheme != "http" && tsaURL.Scheme != "https" {
return nil, fmt.Errorf("endpoint %q: scheme must be http or https, but got %q", endpoint, tsaURL.Scheme)
}
if tsaURL.Host == "" {
return nil, fmt.Errorf("endpoint %q: host cannot be empty", endpoint)
}
return &httpTimestamper{
httpClient: httpClient,
endpoint: endpoint,
Expand Down
18 changes: 18 additions & 0 deletions http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,24 @@ func TestNewHTTPTimestamper(t *testing.T) {
if _, err := NewHTTPTimestamper(nil, malformedURL); err == nil || err.Error() != expectedErrMsg {
t.Fatalf("expected error %s, but got %v", expectedErrMsg, err)
}

malformedURL = "invalid"
expectedErrMsg = `endpoint "invalid": scheme must be http or https, but got ""`
if _, err := NewHTTPTimestamper(nil, malformedURL); err == nil || err.Error() != expectedErrMsg {
t.Fatalf("expected error %s, but got %v", expectedErrMsg, err)
}

malformedURL = "invalid://"
expectedErrMsg = `endpoint "invalid://": scheme must be http or https, but got "invalid"`
if _, err := NewHTTPTimestamper(nil, malformedURL); err == nil || err.Error() != expectedErrMsg {
t.Fatalf("expected error %s, but got %v", expectedErrMsg, err)
}

malformedURL = "https://"
expectedErrMsg = `endpoint "https://": host cannot be empty`
if _, err := NewHTTPTimestamper(nil, malformedURL); err == nil || err.Error() != expectedErrMsg {
t.Fatalf("expected error %s, but got %v", expectedErrMsg, err)
}
}

func TestHttpTimestamperTimestamp(t *testing.T) {
Expand Down

0 comments on commit 718b4b8

Please sign in to comment.