github open2b/scriggo v0.62.0

one hour ago

This release fixes several issues in the contextual autoescaping of templates that could allow cross-site scripting (XSS). Upgrading is recommended for all users who show untrusted values. These fixes make the template lexer stricter, so some templates may need to be updated. See the breaking changes below.

Security

In some cases, Scriggo escaped a value for a context different from the one in which the browser interpreted it, allowing an attacker-controlled value to execute JavaScript on the page. Versions up to and including v0.61.1 are affected. The fixes cover:

  • Values shown in JavaScript template literals, where backticks, $ and { were not escaped, and values shown in JavaScript code after a template literal that contains a quote. Thanks to ucsurf for reporting this issue.
  • Values shown in JavaScript regular expressions or after a / that the lexer misinterpreted as a division operator or the start of a regular expression.
  • Values shown in or after JavaScript comments.
  • script and style elements whose type was interpreted differently by the lexer and browsers due to JavaScript types other than text/javascript and module, duplicate type attributes or differences in attribute name parsing.
  • Tag names, end tags, HTML comments and Markdown code that could make the lexer and browsers disagree on where a script or style element starts or ends.
  • Statement branches, macro and using bodies, and raw statements, after which the lexer could lose track of the context.
  • {{ render "path" }} in tags, attribute values and strings, where the rendered content was not escaped.
  • Values shown in script elements in the html bodies of macros in Markdown files.

For more details, see the security advisory GHSA-36mv-rmc7-qf5q.

Scriggo command

  • IMPORT STANDARD LIBRARY in a Scriggofile now also imports the new crypto/mldsa and uuid packages when the Scriggo command is built with Go 1.27, as is the case with the released binaries.
  • A path after AS in a Scriggofile that starts with cmp, iter, maps, slices, structs, unique, uuid or weak is now rejected, as are other paths that conflict with the Go standard library.

Other fixes and improvements

  • Fixed a panic that terminated the process when the type attribute of a script or style element contained a show, statement or comment.
  • Fixed a panic when a compilation limit was exceeded in a rendered file or in a using statement.
  • Updated github.com/yuin/goldmark to v1.8.7, github.com/fsnotify/fsnotify to v1.10.1, golang.org/x/mod to v0.43.0 and golang.org/x/tools to v0.53.0.
  • Added two new contexts to the ast package, ContextJSRegExp and ContextJSRegExpClass, for values shown in JavaScript regular expressions.
  • See the full list of changes.

Breaking changes

Some templates that compiled with v0.61.1 are now rejected, and the way some values are rendered has changed.

Templates that no longer compile

  • Branches of if, for, switch and select statements that end in different contexts, including at a break or continue statement, as in {% if c %}<b {% end %}title="x">. This includes cases where a type attribute of a script or style element is present in only some branches, as in <script {% if c %}type="text/plain"{% end %}>. The same applies to the bodies of macro and using statements that end in a context different from the one in which they start, including at a return statement.
  • In JavaScript, a / that could be either a division operator or the start of a regular expression, and a --> that could be either an operator or the start of a comment. This happens after statement branches that end differently, as in {% if c %}a{% else %};{% end %}/b/.test(s), at the beginning of a for body, and right after a {%% %%} block that contains a show statement. A / is also ambiguous after a }, after await, yield or of, and after a ) that, in a for body, closes a parenthesis opened before the loop. Parentheses can be used to disambiguate a /, as in (/re/).test(s) or ({}) / 2.
  • In JavaScript, for bodies whose lexical interpretation could depend on how the previous iteration ends, such as bodies that start with a keyword followed by /, as in {% for x in xs %}return /a/;{% end %}, or that end with a keyword such as if or with a ..
  • In JavaScript, line comments and regular expressions that could continue onto the next line because the parser removes the newline following an else, default or end statement, or a comment, when it appears on a line of its own.
  • JavaScript code that reaches the end of a script element, or of an HTML or JavaScript file, while still inside a string, template literal, regular expression or block comment, as in <script>var s = 'abc</script>. This also applies to JavaScript files that are rendered or imported, such as a file It's {{ v }} rendered inside a string, and to script elements whose type is not known at compile time, such as <script {{ attrs }} type="text/plain">It's</script>, because their content is scanned as JavaScript.
  • <!-- in JavaScript code outside classic scripts: in modules, in scripts whose type is not known at compile time, in JavaScript files, and in the bodies of macro statements and of using statements whose content is not shown in place.
  • In a JavaScript template literal, a $ followed by a show, statement or comment. Write \$ for a literal $, as in `Price: \${{ price }}`.
  • In CSS, JavaScript and JSON strings, and in JavaScript template literals and regular expressions, a \ followed by a show, statement or comment.
  • Values of types that cannot be shown in strings, such as slices, maps and structs, when shown in JavaScript template literals or regular expressions.
  • In a character class of a JavaScript regular expression, a ^ that follows the opening [ with only shows, statements or comments in between. Write \^ for a literal ^.
  • Start tags whose name begins with a show, statement or comment, or is completed by one, and could therefore be script or style, as in <{{ tag }}> and <s{{ x }}>. Names such as <h{{ n }}> are still allowed.
  • In the start tag of a script or style element, an attribute name that could be joined to a previous one, as in <script {% if c %}x{% end %}type="text/plain">, and an = after a show, statement or comment, as in <script {{ name }}="value">.
  • In the content of a script or style element, and in JavaScript, JSON and CSS files, an end tag completed by a show, statement or comment, as in '</{{ tag }}>' (write '<\/{{ tag }}>' instead), and a < followed by a statement or comment, as in a <{% if c %}b{% end %}.
  • In the content of script elements scanned as JavaScript or JSON, and in JavaScript and JSON files, a <script after a <!-- with no --> in between, even inside strings and comments. Splitting the tag, as in '<scr' + 'ipt', avoids the error.
  • In HTML and Markdown, a script or style element after a <!--, if a --> or --!> appears in the element, as in <!-- <script> -->.
  • In Markdown, script and style elements that could be part of a code span because they follow a backtick in the same paragraph and do not start a line, as well as those inside fenced code blocks or block quotes. To show these tags as code, write &lt;script in a <code> element, or use an indented code block.
  • raw statements whose content ends inside a string, a regular expression, a tag or an attribute value, and, in Markdown, whose content ends inside a script or style element or with a \.

Changes to the output

  • Strings shown in JavaScript and JSON are escaped differently: " is now escaped as \u0022 instead of \", backticks, $ and { as \u0060, \u0024 and \u007b, */ as *\/, and a / at the start of a value, map key or field name as \/. The resulting strings are equivalent, but the output differs: for example, with u set to "/api/x", var u = {{ u }}; now renders as var u = "\/api/x";, and 'x{{ u }}' as 'x\/api/x'.
  • Values, macro results and rendered files shown in the text of a JavaScript template literal are now escaped as string content, without surrounding quotes. Previously, they were rendered as in JavaScript code: for example, strings were enclosed in quotes and rendered files were not escaped.
  • Values, macro results and rendered files shown in a JavaScript regular expression are now escaped so that they match literally. An empty value is rendered as (?:), or omitted in a character class. Previously, they were rendered as in JavaScript code: for example, strings were enclosed in quotes and rendered files were not escaped.
  • {{ render "path" }} in tags, attribute values, strings and Markdown code blocks now escapes the rendered content, as it already did when the content was first assigned to a variable. As a result, a file rendered in a start tag, as in <div {{ render "attrs.html" }}>, can only add attribute names separated by spaces: characters such as =, quotes, tabs and newlines, including a newline at the end of the file, are replaced with U+FFFD.
  • The content of raw statements is now scanned, so values shown after it are escaped in the context in which the content ends. For example, in {% raw %}<script>{% end raw %}var s = {{ v }};</script>, v is now escaped as JavaScript instead of HTML.
  • The content of script elements with JavaScript types other than text/javascript and module, such as application/javascript, text/x-javascript or Module, is now scanned and escaped as JavaScript instead of HTML, so the checks listed above also apply to it.
  • The content of script elements with a type of application/json, importmap, speculationrules or any type ending in +json (except application/ld+json) is now scanned and escaped as JSON instead of HTML, so the checks listed above also apply to it.
  • When a script or style element has more than one type attribute, Scriggo now uses the first one, as browsers do, even if it has no value. Previously, it used the last one with a value.
  • In the start tag of a script or style element, a type attribute that follows a show, as in <script {{ attrs }} type="text/plain">, is now handled as if its value were not known at compile time, so the content is scanned and escaped as JavaScript or CSS.
  • In CSS, if the base64 encoding of a []byte value starts with /, the initial / is now escaped as \/.
  • In the html and markdown bodies of macro and using statements in files of a different format, values shown after a tag or a script or style element are now escaped for the type of the body instead of the format of the file. For example, in a Markdown file, v in {% macro M html %}<p>{{ v }}</p>{% end %} is no longer escaped twice.

Go version

The minimum required Go version is now Go 1.26, and the released Scriggo command binaries are built with Go 1.27.

Don't miss a new scriggo release

NewReleases is sending notifications on new releases.