Skip to content

Commit ed82f7c

Browse files
fix(metadata): report malformed status report urls (#738)
A status report url which failed to parse and already carried a scheme took an empty branch, so the error was assigned to the named return but never returned and the url came back nil with nothing to indicate anything had gone wrong. The guard selecting that branch matched any value beginning with the letters http rather than one carrying an http scheme, so a scheme-less host such as httpbin.example.com was never retried as HTTPS. It now matches the scheme itself, leaving such hosts to the retry and reporting only the values which are genuinely malformed.
1 parent 8be5355 commit ed82f7c

2 files changed

Lines changed: 28 additions & 6 deletions

File tree

metadata/metadata.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -874,12 +874,15 @@ func (j StatusReportJSON) Parse() (report StatusReport, err error) {
874874
var uri *url.URL
875875

876876
if len(j.URL) != 0 {
877-
if uri, err = url.ParseRequestURI(j.URL); err != nil {
878-
if !strings.HasPrefix(j.URL, "http") {
879-
var e error
880-
if uri, e = url.ParseRequestURI(fmt.Sprintf("https://%s", j.URL)); e != nil {
881-
return report, fmt.Errorf("error occurred parsing URL value: %w", err)
882-
}
877+
var uerr error
878+
879+
if uri, uerr = url.ParseRequestURI(j.URL); uerr != nil {
880+
if strings.HasPrefix(j.URL, "http://") || strings.HasPrefix(j.URL, "https://") {
881+
return report, fmt.Errorf("error occurred parsing URL value: %w", uerr)
882+
}
883+
884+
if uri, err = url.ParseRequestURI(fmt.Sprintf("https://%s", j.URL)); err != nil {
885+
return report, fmt.Errorf("error occurred parsing URL value: %w", uerr)
883886
}
884887
}
885888
}

metadata/parse_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,25 @@ func TestStatusReportJSON_Parse(t *testing.T) {
360360
},
361361
err: "error occurred parsing URL value: parse \"\\x7f\": net/url: invalid control character in URL",
362362
},
363+
{
364+
name: "ShouldFailInvalidURLWithScheme",
365+
have: StatusReportJSON{
366+
EffectiveDate: "2025-01-01",
367+
URL: "https://" + string([]byte{0x7f}),
368+
},
369+
err: "error occurred parsing URL value: parse \"https://\\x7f\": net/url: invalid control character in URL",
370+
},
371+
{
372+
name: "ShouldSucceedHostPrefixedWithHTTP",
373+
have: StatusReportJSON{
374+
EffectiveDate: "2025-01-01",
375+
URL: "httpbin.example.com/update",
376+
},
377+
expected: StatusReport{
378+
EffectiveDate: timePtr(time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC)),
379+
},
380+
expectedURL: "https://httpbin.example.com/update",
381+
},
363382
{
364383
name: "ShouldSucceedMinimal",
365384
have: StatusReportJSON{

0 commit comments

Comments
 (0)