-
-
Notifications
You must be signed in to change notification settings - Fork 84
fix: the MIME type of an SVG file should be image/svg+xml #286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+164
−9
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a9b4419
fix: the MIME type of an SVG file should be image/svg+xml
bf8ec84
Stricter SVG MIME type checking
c6eb46d
fix lint error
f5a1b76
Update util/fsutil/fsutil.go
1be177d
Update util/fsutil/fsutil.go
46d10b4
Optimize detection logic
c256b6a
fsutil: simplify svg sniffing
System-Glitch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| package fsutil | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "io" | ||
| "io/fs" | ||
| "net/http" | ||
|
|
@@ -117,7 +118,9 @@ func GetMIMEType(filesystem fs.FS, file string) (contentType string, size int64, | |
| // DetectContentType by sniffing the first 512 bytes of the given reader using `http.DetectContentType`. | ||
| // | ||
| // If the detected content type is `"application/octet-stream"` or `"text/plain"`, this function will attempt to | ||
| // find a more precise one using `fsutil.DetectContentTypeByExtension`. | ||
| // find a more precise one using `fsutil.DetectContentTypeByExtension`, unless `fileName` is empty. | ||
| // If the detected content type is `"text/xml"` or `"application/xml"`, this function promotes it to | ||
| // `"image/svg+xml"` only if the content signature indicates SVG. | ||
| // The header parameter is retained (e.g: `charset=utf-8`). | ||
| // | ||
| // If there is no error, this function always returns a valid MIME type. If it cannot determine a more specific one, | ||
|
|
@@ -140,11 +143,47 @@ func DetectContentType(r io.Reader, fileName string) (string, error) { | |
| contentType := http.DetectContentType(buffer) | ||
| if strings.HasPrefix(contentType, "application/octet-stream") || strings.HasPrefix(contentType, "text/plain") { | ||
| contentType = detectContentTypeByExtension(fileName, contentType) | ||
| } else if (strings.HasPrefix(contentType, "text/xml") || strings.HasPrefix(contentType, "application/xml")) && hasSVGSignature(buffer) { | ||
| contentType = "image/svg+xml" | ||
| } | ||
| return contentType, nil | ||
| } | ||
|
|
||
| func hasSVGSignature(buffer []byte) bool { | ||
| start := bytes.IndexRune(buffer, '<') | ||
| if start == -1 { | ||
| return false | ||
| } | ||
|
|
||
| content := strings.TrimSpace(strings.ToLower(string(buffer[start:]))) | ||
| // Skip optional XML tag and possible comments | ||
| for { | ||
| if strings.HasPrefix(content, "<?xml") { | ||
| end := strings.Index(content, "?>") | ||
| if end == -1 { | ||
| break | ||
| } | ||
| content = strings.TrimSpace(content[end+2:]) | ||
| continue | ||
| } | ||
| if strings.HasPrefix(content, "<!--") { | ||
| end := strings.Index(content, "-->") | ||
| if end == -1 { | ||
| break | ||
| } | ||
| content = strings.TrimSpace(content[end+3:]) | ||
| continue | ||
| } | ||
| break | ||
| } | ||
|
|
||
| return strings.HasPrefix(content, "<svg") | ||
| } | ||
|
|
||
| func detectContentTypeByExtension(fileName, contentType string) string { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add an early return here if
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
| if fileName == "" { | ||
| return contentType | ||
| } | ||
| for ext, t := range contentTypeByExtension { | ||
| if strings.HasSuffix(fileName, ext) { | ||
| tmp := t | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.