From 92998cde5a642c9db42efa51c4077b70609d55d6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Ville=20Skytt=C3=A4?= Date: Sat, 7 May 2022 06:37:07 +0000 Subject: [PATCH] mime: skip globs2 entries that are not simple file extensions The entries in globs2 can be globs beyond simple *.ext ones. We support only simple extension based matching, so skip entries that do not represent them. Change-Id: Id5d089cb4067e53beb2471a5e67a59c13880a017 GitHub-Last-Rev: f725a910547ec52d12605760563f158dfb72e4b0 GitHub-Pull-Request: golang/go#51156 Reviewed-on: https://go-review.googlesource.com/c/go/+/385256 Run-TryBot: Ian Lance Taylor Reviewed-by: Ian Lance Taylor Run-TryBot: Ian Lance Taylor Reviewed-by: Heschi Kreinick TryBot-Result: Gopher Robot Auto-Submit: Ian Lance Taylor --- src/mime/testdata/test.types.globs2 | 3 +++ src/mime/type_unix.go | 14 +++++++++++++- src/mime/type_unix_test.go | 17 ++++++++++------- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/mime/testdata/test.types.globs2 b/src/mime/testdata/test.types.globs2 index fd9df7078b..4606d98f13 100644 --- a/src/mime/testdata/test.types.globs2 +++ b/src/mime/testdata/test.types.globs2 @@ -9,3 +9,6 @@ 50:text/plain:*,v 50:application/x-trash:*~ 30:example/do-not-use:*.t4 +10:example/glob-question-mark:*.foo?ar +10:example/glob-asterisk:*.foo*r +10:example/glob-range:*.foo[1-3] diff --git a/src/mime/type_unix.go b/src/mime/type_unix.go index e297ecf5c1..649d9001e3 100644 --- a/src/mime/type_unix.go +++ b/src/mime/type_unix.go @@ -40,7 +40,7 @@ func loadMimeGlobsFile(filename string) error { scanner := bufio.NewScanner(f) for scanner.Scan() { - // Each line should be of format: weight:mimetype:*.ext[:morefields...] + // Each line should be of format: weight:mimetype:glob[:morefields...] fields := strings.Split(scanner.Text(), ":") if len(fields) < 3 || len(fields[0]) < 1 || len(fields[2]) < 3 { continue @@ -49,6 +49,18 @@ func loadMimeGlobsFile(filename string) error { } extension := fields[2][1:] + if strings.ContainsAny(extension, "?*[") { + // Not a bare extension, but a glob. Ignore for now: + // - we do not have an implementation for this glob + // syntax (translation to path/filepath.Match could + // be possible) + // - support for globs with weight ordering would have + // performance impact to all lookups to support the + // rarely seen glob entries + // - trying to match glob metacharacters literally is + // not useful + continue + } if _, ok := mimeTypes.Load(extension); ok { // We've already seen this extension. // The file is in weight order, so we keep diff --git a/src/mime/type_unix_test.go b/src/mime/type_unix_test.go index ab14ae6f80..2e8f273fad 100644 --- a/src/mime/type_unix_test.go +++ b/src/mime/type_unix_test.go @@ -22,13 +22,16 @@ func initMimeUnixTest(t *testing.T) { func TestTypeByExtensionUNIX(t *testing.T) { initMimeUnixTest(t) typeTests := map[string]string{ - ".T1": "application/test", - ".t2": "text/test; charset=utf-8", - ".t3": "document/test", - ".t4": "example/test", - ".png": "image/png", - ",v": "", - "~": "", + ".T1": "application/test", + ".t2": "text/test; charset=utf-8", + ".t3": "document/test", + ".t4": "example/test", + ".png": "image/png", + ",v": "", + "~": "", + ".foo?ar": "", + ".foo*r": "", + ".foo[1-3]": "", } for ext, want := range typeTests { -- 2.50.0