Skip to content
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

html/template: srcset incorrectly escaped #17441

Closed
okdave opened this issue Oct 14, 2016 · 15 comments
Closed

html/template: srcset incorrectly escaped #17441

okdave opened this issue Oct 14, 2016 · 15 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@okdave
Copy link
Contributor

okdave commented Oct 14, 2016

tl;dr: https://play.golang.org/p/8A4TQ1-Kvt

The html/template defaults to the contentTypeURL content type for any attribute that contains the substring "src".

However the srcset attribute is a set of URLs which are separated by whitespace and optional extra size/density indicators (see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attr-srcset).

For example "image1.jpg w200, image2.jpg w400".

When this is escaped as the contentTypeURL the spaces are encoded, the whole thing looks like a single URL, and the resource fails to load.

tmpl := template.Must(template.New("foo").Parse(`<img srcset="{{.}}">`))
tmpl.Execute(os.Stdout, "1.jpg w200, 2.jpg w200")

Got <img srcset="1.jpg%20w200,%202.jpg%20w200">
Want <img srcset="1.jpg w200, 2.jpg w200">

@okdave
Copy link
Contributor Author

okdave commented Oct 14, 2016

Also, I can't figure out how to override the escaping using the types in the package. The two that I thought would work (HTMLAttr and URL) both have no effect: https://play.golang.org/p/ZSSV7AneLL

@bradfitz bradfitz added this to the Go1.8Maybe milestone Oct 15, 2016
@bradfitz
Copy link
Contributor

/cc @robpike @adg

@cespare
Copy link
Contributor

cespare commented Oct 15, 2016

@okdave I don't know the answer to your question, but you can work around the issue by using HTMLAttr with the whole attribute: https://play.golang.org/p/uZUPL7OgA2

@okdave
Copy link
Contributor Author

okdave commented Oct 16, 2016

Thanks @cespare that's exactly what I had to end up doing, though it felt wrong.

@quentinmit quentinmit added the NeedsFix The path to resolution is known, but the work has not been done. label Oct 17, 2016
@rsc
Copy link
Contributor

rsc commented Oct 20, 2016

This looks like it will need to introduce new states into the HTML analyzer.

/cc @mikesamuel for advice.

@rsc rsc modified the milestones: Go1.9Early, Go1.8Maybe Nov 2, 2016
@zombiezen
Copy link
Contributor

I would be interested in picking this up. Any advice on where to start?

@rsc
Copy link
Contributor

rsc commented Feb 7, 2017

The html/template doc comment explains a lot about what is going on in this package.

@mikesamuel
Copy link
Contributor

@zombiezen

https://golang.org/src/html/template/content.go probably needs a new content type for space delimited URLs.

The table of attribute types in https://golang.org/src/html/template/attr.go probably needs a mapping for srcset.

url.go would then probably need a URL escaper variant that allows [\t\n\r ] through unescaped.

I forgot quite where content types are mapped to escapers, but some grepping around should find it easily enough.

@zombiezen
Copy link
Contributor

Cool. I'll take a go at this.

@zombiezen zombiezen self-assigned this Feb 7, 2017
@gopherbot
Copy link

CL https://golang.org/cl/38324 mentions this issue.

@bradfitz bradfitz modified the milestones: Go1.9, Go1.9Early May 3, 2017
@odeke-em
Copy link
Member

I've ran the trybots on that CL so that to reignite the conversation on @zombiezen's CL as I see it is marked for Go1.9, what do you think @bradfitz?

@zombiezen
Copy link
Contributor

I'm happy to carry this forward, but I need review from someone who understands the security implications.

@odeke-em
Copy link
Member

Punting this to Go1.10 as per @rsc's comment on the CL.

@odeke-em odeke-em modified the milestones: Go1.10, Go1.9 Jun 23, 2017
@namusyaka
Copy link
Member

@rsc @odeke-em @zombiezen I've tried to upload patch set for adding @rsc's comments in tests.
Could you take a look at the CL? Sorry if there is a problem with the way.

@mikesamuel
Copy link
Contributor

Per the discussion about URL filtering in srcset at 38324/4, here's a patch: url.go.diff.txt.

I tried to upload a patch set, but need to sort out some Gerrit credential issue.

@golang golang locked and limited conversation to collaborators Dec 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

10 participants