Skip to content

Commit

Permalink
Prevent HTML and JS injection in section names
Browse files Browse the repository at this point in the history
Before this change, HTML and some JS code (as far as it was not escaped
by json_encode) could be injected into the output as the closing pattern
that is checked by the regex is not escaped in JSON (see test case).
  • Loading branch information
michitux committed Apr 15, 2018
1 parent 75c3272 commit ada0d77
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
11 changes: 11 additions & 0 deletions _test/tests/inc/html_secedit_pattern.test.php
Expand Up @@ -56,4 +56,15 @@ public function testSecEditPattern($text, $expectedMatches, $msg) {
}
}

public function testSecEditHTMLInjection() {
$ins = p_get_instructions("====== Foo ======\n\n===== } --> <script> =====\n\n===== Bar =====\n");
$info = array();
$xhtml = p_render('xhtml', $ins, $info);

$this->assertNotNull($xhtml);

$xhtml_without_secedit = html_secedit($xhtml, false);

$this->assertFalse(strpos($xhtml_without_secedit, '<script>'), 'Plain <script> tag found in output - HTML/JS injection might be possible!');
}
}
3 changes: 2 additions & 1 deletion inc/html.php
Expand Up @@ -118,7 +118,8 @@ function html_secedit($text,$show=true){
* @triggers HTML_SECEDIT_BUTTON
*/
function html_secedit_button($matches){
$data = json_decode($matches[1], true);
$json = htmlspecialchars_decode($matches[1], ENT_QUOTES);
$data = json_decode($json, true);
if ($data == NULL) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion inc/parser/xhtml.php
Expand Up @@ -103,7 +103,7 @@ public function finishSectionEdit($end = null, $hid = null) {
}
$data['range'] = $data['start'].'-'.(is_null($end) ? '' : $end);
unset($data['start']);
$this->doc .= '<!-- EDIT'.json_encode ($data).' -->';
$this->doc .= '<!-- EDIT'.hsc(json_encode ($data)).' -->';
}

/**
Expand Down

0 comments on commit ada0d77

Please sign in to comment.