Skip to content

Commit 3908634

Browse files
authored
Merge pull request #5967 from seipan/fix/url-encode
Fix infinite loop in HTTP client by validating URLs before requests
2 parents 4468c8c + 2a79ea1 commit 3908634

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

api/internal/loader/fileloader.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,11 @@ func (fl *FileLoader) httpClientGetContent(path string) ([]byte, error) {
311311
} else {
312312
hc = &http.Client{}
313313
}
314-
resp, err := hc.Get(path)
314+
parsedURL, err := url.ParseRequestURI(path)
315+
if err != nil {
316+
return nil, errors.Wrap(err)
317+
}
318+
resp, err := hc.Get(parsedURL.String())
315319
if err != nil {
316320
return nil, errors.Wrap(err)
317321
}

api/internal/loader/fileloader_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,3 +676,15 @@ func setupOnDisk(t *testing.T) (filesys.FileSystem, filesys.ConfirmedDir) {
676676
})
677677
return fSys, dir
678678
}
679+
680+
// TestLoaderHTTPMalformedURL tests that malformed URLs are properly handled
681+
// to prevent infinite loops in http.Client.Get
682+
func TestLoaderHTTPMalformedURL(t *testing.T) {
683+
require := require.New(t)
684+
malformedURL := "https://example.com/example?ref=main - ../../example/example.yaml"
685+
l1 := NewLoaderOrDie(
686+
RestrictionRootOnly, MakeFakeFs([]testData{}), filesys.Separator)
687+
_, err := l1.Load(malformedURL)
688+
require.Error(err)
689+
require.Equal("HTTP Error: status code 500 (Internal Server Error)", err.Error())
690+
}

0 commit comments

Comments
 (0)