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: truncated output for URL #6701

Closed
robpike opened this issue Oct 31, 2013 · 12 comments
Closed

html/template: truncated output for URL #6701

robpike opened this issue Oct 31, 2013 · 12 comments

Comments

@robpike
Copy link
Contributor

robpike commented Oct 31, 2013

Reported at tip with 6g

changeset:   18385:db021a4c7b4a


This program:

======
package main

import (
    "html/template"
    "os"
)

func main() {
    template.Must(template.New("test").Parse(text)).Execute(os.Stdout, nil)
}

const text = `<!DOCTYPE html>
<html>
  <body>
    <script id="test" type="html/template">
      <a href="https://twitter.com/share";></a>
      <a href="https://www.facebook.com/sharer/sharer.php";></a>
      <a href="https://plus.google.com/share";></a>
    </script>
  </body>
</html>
`
======

Produces this output:

======
<!DOCTYPE html>
<html>
  <body>
    <script id="test" type="html/template">
      <a href="https://twitter.com/share";></a>
      <a href="https://www.facebook.com/sharer/sharer.php";></a>
      <a href="https:
    </script>
  </body>
</html>
======

Notice that the third URL is truncated. Deleting either of the other two from the input
restores the final one.
@gopherbot
Copy link

Comment 1 by mikesamuel:

One possibility:
Comment elision is kicking in because the template content is being interpreted as JS
despite the presence of type="html/template".
If the body is interpreted as JS, then you see the tokens
 '<', 'a', 'href', '=', a quoted string, '>', '<',
a regular expression running from /a>\n<a href="https:/,
'/', 'www', '.', 'facebook', '.', 'com', '/', 'sharer', '/', 'sharer.php',
a quoted string "></a>\n<a href=",
'https', ':', a line comment
So I don't think this indicates a bug in the JS handling, but maybe we should not treat
the body of a script tag with a non javascript type attribute as having JS content.

Status changed to New.

@robpike
Copy link
Contributor Author

robpike commented Oct 31, 2013

Comment 2:

Status changed to Accepted.

@donovanhide
Copy link
Contributor

Comment 3:

With HTML 5 the default script type is "text/javascript"
http://www.w3.org/TR/html5/scripting-1.html
So if a script tag does not have a type attribute, or the type attribute is
"text/javascript", then the current behaviour is correct. If the specified MIME type is
something else it becomes a challenge to know how to correctly interpret it. Internet
Explorer has vbscript, but I doubt anyone wants to go there!
Reading the code, seems an additional state is required other than stateJS
http://tip.golang.org/src/pkg/html/template/context.go#L105
perhaps stateMIME?

@gopherbot
Copy link

Comment 4 by mikesamuel:

Quoting the relevant portions of the spec that Donovan pointed to
http://www.w3.org/TR/html5/scripting-1.html#the-script-block's-type
"""
If either:
the script element has a type attribute and its value is the empty string, or
the script element has no type attribute but it has a language attribute and that
attribute's value is the empty string, or
the script element has neither a type attribute nor a language attribute, then
...let the script block's type for this script element be "text/javascript".
...
If the user agent does not support the scripting language given by the script block's
type for this script element, then the user agent must abort these steps at this point.
The script is not executed.
"""

@rsc
Copy link
Contributor

rsc commented Nov 27, 2013

Comment 5:

Labels changed: added go1.3maybe.

@rsc
Copy link
Contributor

rsc commented Dec 4, 2013

Comment 6:

Labels changed: added release-none, removed go1.3maybe.

@rsc
Copy link
Contributor

rsc commented Dec 4, 2013

Comment 7:

Labels changed: added repo-main.

@dsymonds
Copy link
Contributor

dsymonds commented Aug 7, 2014

Comment 8:

How safe would it be to not escape things if html/template doesn't understand the script
type? #4 quotes the HTML5 spec, but when it comes to security we need to play to what's
actually running in the world, not what the spec says.
Returning an error would be a safe fallback option.

Labels changed: added security.

@gopherbot
Copy link

Comment 9 by mikesamuel:

It would be unsafe if there is an extension that supports another scripting language:
    <script language="text/vbscript">...</script>
but those are rare.

@gopherbot
Copy link

Comment 10 by saint.abroad:

Rather than escape as javascript, can we just discard script element contents with type
attribute specified as other than "text/javascript"?
Such behaviour would more closely mirror the way browsers treat the element, and prevent
confusion where it "sometimes works" for other types.

@nodirt
Copy link
Contributor

nodirt commented Sep 6, 2015

CL 14336 would fix this bug, as a part of fixing its duplicate #12149. It does not treat script tag content as JS if the tag has type attribute != "text/javascript". I can expand it to "text/vbscript", etc

@gopherbot
Copy link

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

@golang golang locked and limited conversation to collaborators Sep 29, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants