Skip to content

net/http: content type detection does not work correctly for some documents that start with HTML comments #25771

Closed
@guliyevemil1

Description

@guliyevemil1

Some website documents start with "<!--[if IE]" or "<--comment with no space" and some close it without leaving an actual comment like this: "<!--->".

None of these cases are detected correctly because "<!--" htmlSig is treated like a regular HTML tag when in reality behaves slightly differently.

What version of Go are you using (go version)?

❯ go version
go version go1.10.2 darwin/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

❯ go env
GOARCH="amd64"
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"

What did you do?

package main

import (
	"fmt"
	"net/http"
)

func main() {
	fmt.Println(http.DetectContentType([]byte(`<!--[if !IE]> This has been served from cache <![endif]--><html><body>Hello,world</body></html>`)))
	fmt.Println(http.DetectContentType([]byte(`<!-- [if !IE]> This has been served from cache <![endif]--><html><body>Hello,world</body></html>`)))
}

https://play.golang.org/p/f6-N3OA9FGU

What did you expect to see?

text/html; charset=utf-8
text/html; charset=utf-8

What did you see instead?

text/plain; charset=utf-8
text/html; charset=utf-8

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions