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.
scriptandstyleelements whose type was interpreted differently by the lexer and browsers due to JavaScript types other thantext/javascriptandmodule, duplicatetypeattributes 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
scriptorstyleelement starts or ends. - Statement branches,
macroandusingbodies, andrawstatements, 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
scriptelements in thehtmlbodies of macros in Markdown files.
For more details, see the security advisory GHSA-36mv-rmc7-qf5q.
Scriggo command
IMPORT STANDARD LIBRARYin a Scriggofile now also imports the newcrypto/mldsaanduuidpackages when the Scriggo command is built with Go 1.27, as is the case with the released binaries.- A path after
ASin a Scriggofile that starts withcmp,iter,maps,slices,structs,unique,uuidorweakis 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
typeattribute of ascriptorstyleelement contained a show, statement or comment. - Fixed a panic when a compilation limit was exceeded in a rendered file or in a
usingstatement. - Updated
github.com/yuin/goldmarkto v1.8.7,github.com/fsnotify/fsnotifyto v1.10.1,golang.org/x/modto v0.43.0 andgolang.org/x/toolsto v0.53.0. - Added two new contexts to the
astpackage,ContextJSRegExpandContextJSRegExpClass, 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,switchandselectstatements that end in different contexts, including at abreakorcontinuestatement, as in{% if c %}<b {% end %}title="x">. This includes cases where atypeattribute of ascriptorstyleelement is present in only some branches, as in<script {% if c %}type="text/plain"{% end %}>. The same applies to the bodies ofmacroandusingstatements that end in a context different from the one in which they start, including at areturnstatement. - 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 aforbody, and right after a{%% %%}block that contains ashowstatement. A/is also ambiguous after a}, afterawait,yieldorof, and after a)that, in aforbody, closes a parenthesis opened before the loop. Parentheses can be used to disambiguate a/, as in(/re/).test(s)or({}) / 2. - In JavaScript,
forbodies 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 asifor 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,defaultorendstatement, or a comment, when it appears on a line of its own. - JavaScript code that reaches the end of a
scriptelement, 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 fileIt's {{ v }}rendered inside a string, and toscriptelements 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 ofmacrostatements and ofusingstatements 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
scriptorstyle, as in<{{ tag }}>and<s{{ x }}>. Names such as<h{{ n }}>are still allowed. - In the start tag of a
scriptorstyleelement, 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
scriptorstyleelement, 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 ina <{% if c %}b{% end %}. - In the content of
scriptelements scanned as JavaScript or JSON, and in JavaScript and JSON files, a<scriptafter 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
scriptorstyleelement after a<!--, if a-->or--!>appears in the element, as in<!-- <script> -->. - In Markdown,
scriptandstyleelements 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<scriptin a<code>element, or use an indented code block. rawstatements whose content ends inside a string, a regular expression, a tag or an attribute value, and, in Markdown, whose content ends inside ascriptorstyleelement or with a\.
Changes to the output
- Strings shown in JavaScript and JSON are escaped differently:
"is now escaped as\u0022instead of\", backticks,$and{as\u0060,\u0024and\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, withuset to"/api/x",var u = {{ u }};now renders asvar 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
rawstatements 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>,vis now escaped as JavaScript instead of HTML. - The content of
scriptelements with JavaScript types other thantext/javascriptandmodule, such asapplication/javascript,text/x-javascriptorModule, is now scanned and escaped as JavaScript instead of HTML, so the checks listed above also apply to it. - The content of
scriptelements with a type ofapplication/json,importmap,speculationrulesor any type ending in+json(exceptapplication/ld+json) is now scanned and escaped as JSON instead of HTML, so the checks listed above also apply to it. - When a
scriptorstyleelement has more than onetypeattribute, 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
scriptorstyleelement, atypeattribute 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
[]bytevalue starts with/, the initial/is now escaped as\/. - In the
htmlandmarkdownbodies ofmacroandusingstatements in files of a different format, values shown after a tag or ascriptorstyleelement are now escaped for the type of the body instead of the format of the file. For example, in a Markdown file,vin{% 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.