Skip to content

Closing script tag in string causes data to be injected into the DOM #2039

@liamkirsh

Description

@liamkirsh

Quick Summary: If a string containing a </script> tag is passed to any function that causes the string to be inserted as-is into the JavaScript that Elm generates, the remainder of the Elm-generated code is injected into the DOM outside of a script context.

SSCCE

import Html exposing (text)

main =
  text "</script><script>alert(`This shouldnt be allowed`)</script>"

Output (line 4378):

var $author$project$Main$main = $elm$html$Html$text('</script><script>alert(`This shouldnt be allowed`)</script>');

  • Elm: 0.19.1
  • Browser: Mozilla Firefox 60.5.1esr (64-bit) and 71.0 (64-bit)
  • Operating System: Debian GNU/Linux 9 and Windows 10 Home Pro

Additional Details

This occurs because the browser's HTML parser runs before the JavaScript parser. The </script> tag closes the JavaScript context generated by the compiler. This doesn't seem to be a security issue unless the developer enters an untrusted string into Elm application code.

Another example of the behavior is if a developer calls Http.get and places the closing script tag in the URL value.

All of the following variations will reproduce the bug: </SCRIPT>, </script >, </script foo>

Probably the same bug referenced in issue #1780.

Proposed Fix Approaches

  • Escaping or filtering the tag would change the expected value of the string, which could lead to correctness issues.
  • If the JavaScript builder separates the tag in Elm into multiple JavaScript strings, this would preserve correctness. For example, the text from the SSCCE could be built into '</' + 'script><script>alert(`This shouldnt be allowed`)</' + 'script>');

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions