The vulnerability lies in the html/template package's parser, specifically how it handles HTML-like comments (<!--, -->) and hashbang comments (#!) within <script> contexts. The provided patch directly addresses this by modifying the state machine and transition functions within the package.
- New States: The patch introduces
stateJSHTMLOpenCmt and stateJSHTMLCloseCmt in context.go to represent these comment types within JavaScript.
- Modified Transition Logic:
transition.go's tJS function is updated to detect these comment tokens (<, -, #) and transition into the new states or the existing stateJSLineCmt for hashbang comments.
transition.go's tLineCmt function is updated to treat these new JavaScript comment states like regular JavaScript line comments, terminating them at line endings.
- Modified Escaping Logic:
escape.go's (*escaper).escapeText function, which orchestrates the escaping process, is updated to correctly calculate offsets when these new comment types are encountered, ensuring that content around them is preserved or handled appropriately.
The functions (*escaper).escapeText, tJS, and tLineCmt are central to parsing and sanitizing template content, especially within script tags. The modifications directly show how the vulnerable parsing behavior was fixed. Therefore, these functions would be active and appear in a runtime profile when processing input that could trigger the vulnerability (i.e., input containing these special comment sequences in script contexts).