Skip to content

Commit 1238d61

Browse files
Et43NGPixel
andauthored
Merge pull request from GHSA-xjcj-p2qv-q3rf
* Update render.js # Improved handling of mustache expressions and v-pre attribute assignment ## Changes Made: - Ensured that the parent tag of such text nodes is explicitly set to a `<p>` tag with the `v-pre` attribute. - Added debug messages for better understanding of the script execution flow [THIS SHOULD REMOVED WHEN PUSHING TO PRODUCTION]. ## Why it Works: - When a mustache expression is found, the script either wraps it in a new `<p>` tag with the `v-pre` attribute or adds the `v-pre` attribute to the existing parent `<p>` tag. - This approach ensures that the template code is not removed but encapsulated within `<p>` tags with the `v-pre` attribute, as required. ## Test Cases Passed: 1. `<xyz>{{ constructor.constructor('alert(1)')() }}</xyz>` 2. `<xyz>{{ constructor.constructor('alert(1)')() }}</xyz>` 3. `<p><xyz>{{ constructor.constructor('alert(1)')() }}</p>` 4. `<p><xyz>{{ constructor.constructor('alert(1)')() }}</xyz></p>` 5. `<p>&lt;xyz&gt;{{constructor.constructor('alert("Test Case 8")')()}}&lt;xyz&gt;{{constructor.constructor('alert("Test Case 9")')()}}&lt;/xyz&gt;</p>` This commit enhances the robustness and reliability of handling mustache expressions and ensures proper assignment of the `v-pre` attribute, to ensure that there is no room for the weaponization of the template code later in the rendering process. * fix: move template expressions after dom-purify + handle text nodes without parent --------- Co-authored-by: NGPixel <github@ngpixel.com>
1 parent 98c04fe commit 1238d61

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

server/modules/rendering/html-core/renderer.js

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const mustacheRegExp = /(\{|&#x7b;?){2}(.+?)(\}|&#x7d;?){2}/i
1010

1111
module.exports = {
1212
async render() {
13-
const $ = cheerio.load(this.input, {
13+
let $ = cheerio.load(this.input, {
1414
decodeEntities: true
1515
})
1616

@@ -253,17 +253,35 @@ module.exports = {
253253
}
254254
})
255255

256+
// --------------------------------
257+
// STEP: POST
258+
// --------------------------------
259+
260+
let output = decodeEscape($.html('body').replace('<body>', '').replace('</body>', ''))
261+
262+
for (let child of _.sortBy(_.filter(this.children, ['step', 'post']), ['order'])) {
263+
const renderer = require(`../${_.kebabCase(child.key)}/renderer.js`)
264+
output = await renderer.init(output, child.config)
265+
}
266+
256267
// --------------------------------
257268
// Escape mustache expresions
258269
// --------------------------------
259270

271+
$ = cheerio.load(output, {
272+
decodeEntities: true
273+
})
274+
260275
function iterateMustacheNode (node) {
261-
const list = $(node).contents().toArray()
262-
list.forEach(item => {
276+
$(node).contents().each((idx, item) => {
263277
if (item && item.type === 'text') {
264278
const rawText = $(item).text().replace(/\r?\n|\r/g, '')
265279
if (mustacheRegExp.test(rawText)) {
266-
$(item).parent().attr('v-pre', true)
280+
if (!item.parent || item.parent.name === 'body') {
281+
$(item).wrap($('<p>').attr('v-pre', true))
282+
} else {
283+
$(item).parent().attr('v-pre', true)
284+
}
267285
}
268286
} else {
269287
iterateMustacheNode(item)
@@ -276,18 +294,7 @@ module.exports = {
276294
$(elm).attr('v-pre', true)
277295
})
278296

279-
// --------------------------------
280-
// STEP: POST
281-
// --------------------------------
282-
283-
let output = decodeEscape($.html('body').replace('<body>', '').replace('</body>', ''))
284-
285-
for (let child of _.sortBy(_.filter(this.children, ['step', 'post']), ['order'])) {
286-
const renderer = require(`../${_.kebabCase(child.key)}/renderer.js`)
287-
output = await renderer.init(output, child.config)
288-
}
289-
290-
return output
297+
return decodeEscape($.html('body').replace('<body>', '').replace('</body>', ''))
291298
}
292299
}
293300

0 commit comments

Comments
 (0)