From 6af44f8432fe07c0ce1feba885399be3511b8a73 Mon Sep 17 00:00:00 2001 From: Luiz Aoqui Date: Fri, 9 Apr 2021 14:37:27 -0400 Subject: [PATCH] limit bytes passed to http.DetectContentType (#10348) --- client/allocdir/alloc_dir.go | 4 ++-- client/allocdir/alloc_dir_test.go | 1 + client/allocdir/input/test.hcl | 3 +++ 3 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 client/allocdir/input/test.hcl diff --git a/client/allocdir/alloc_dir.go b/client/allocdir/alloc_dir.go index a51ba5c34a13..bd16f1d59bb9 100644 --- a/client/allocdir/alloc_dir.go +++ b/client/allocdir/alloc_dir.go @@ -426,9 +426,9 @@ func detectContentType(fileInfo os.FileInfo, path string) string { // We ignore errors because this is optional information if err == nil { fileBytes := make([]byte, 512) - _, err := f.Read(fileBytes) + n, err := f.Read(fileBytes) if err == nil { - contentType = http.DetectContentType(fileBytes) + contentType = http.DetectContentType(fileBytes[:n]) } f.Close() } diff --git a/client/allocdir/alloc_dir_test.go b/client/allocdir/alloc_dir_test.go index 82b3ae1cb13b..8dc61b5cd622 100644 --- a/client/allocdir/alloc_dir_test.go +++ b/client/allocdir/alloc_dir_test.go @@ -493,6 +493,7 @@ func TestAllocDir_DetectContentType(t *testing.T) { "input/test.json": "application/json", "input/test.txt": "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 { fileInfo, err := os.Stat(file) diff --git a/client/allocdir/input/test.hcl b/client/allocdir/input/test.hcl new file mode 100644 index 000000000000..64aa42c4da13 --- /dev/null +++ b/client/allocdir/input/test.hcl @@ -0,0 +1,3 @@ +test "test" { + test = "test" +}