limit bytes passed to http.DetectContentType (#10348)

This commit is contained in:
Luiz Aoqui 2021-04-09 14:37:27 -04:00 committed by GitHub
parent 3113cced7b
commit 37f1c37fab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 6 additions and 2 deletions

View File

@ -426,9 +426,9 @@ func detectContentType(fileInfo os.FileInfo, path string) string {
// We ignore errors because this is optional information // We ignore errors because this is optional information
if err == nil { if err == nil {
fileBytes := make([]byte, 512) fileBytes := make([]byte, 512)
_, err := f.Read(fileBytes) n, err := f.Read(fileBytes)
if err == nil { if err == nil {
contentType = http.DetectContentType(fileBytes) contentType = http.DetectContentType(fileBytes[:n])
} }
f.Close() f.Close()
} }

View File

@ -493,6 +493,7 @@ func TestAllocDir_DetectContentType(t *testing.T) {
"input/test.json": "application/json", "input/test.json": "application/json",
"input/test.txt": "text/plain; charset=utf-8", "input/test.txt": "text/plain; charset=utf-8",
"input/test.go": "text/plain; charset=utf-8", "input/test.go": "text/plain; charset=utf-8",
"input/test.hcl": "text/plain; charset=utf-8",
} }
for _, file := range testFiles { for _, file := range testFiles {
fileInfo, err := os.Stat(file) fileInfo, err := os.Stat(file)

View File

@ -0,0 +1,3 @@
test "test" {
test = "test"
}