Skip to content

Commit 7868ab3

Browse files
committed
Make InterpreterBlockRule regex only match on valid interpreter names
Summary: With this patch, the underlying exception described in T15372#8537 still remains. However, with this patch, the bug is more contained as it is not triggered when not calling an interpreter (`cowsay`, `figlet`), so Phorge does not crash rendering `noValidInterpreter {{{foo}}} bar` lines but renders them as is (for whatever reasons such lines may exist). See T15372 Test Plan: Enter strings into a comment: * `invalid {{{saysay}}} foo` now renders as plain text instead of crashing * `invalid (invalid) {{{saysay}}} foo` now renders as plain text instead of crashing * `cowsay (invalid) {{{saysay}}} foo` will still crash as before Reviewers: O1 Blessed Committers, valerio.bozzolan Reviewed By: O1 Blessed Committers, valerio.bozzolan Subscribers: tobiaswiese, valerio.bozzolan, Matthew, Cigaryno Differential Revision: https://we.phorge.it/D25415
1 parent dc10a7e commit 7868ab3

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

src/infrastructure/markup/blockrule/PhutilRemarkupInterpreterBlockRule.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,35 @@
22

33
final class PhutilRemarkupInterpreterBlockRule extends PhutilRemarkupBlockRule {
44

5-
const START_BLOCK_PATTERN = '/^([\w]+)\s*(?:\(([^)]+)\)\s*)?{{{/';
5+
/**
6+
* Second part of the regex to find stuff like:
7+
* interpreterName {{{ stuff }}}
8+
* interpreterName (options) {{{ stuff }}}
9+
* You have found the kernel of cowsay and figlet.
10+
*/
611
const END_BLOCK_PATTERN = '/}}}\s*$/';
712

13+
/**
14+
* Constructs the first part of the regex to find stuff like:
15+
* interpreterName {{{ stuff }}}
16+
* interpreterName (options) {{{ stuff }}}
17+
* The exact regex is constructed from the available interpreters.
18+
* @return string First part of interpreters regex
19+
*/
20+
private function getStartBlockPattern() {
21+
$interpreters = id(new PhutilClassMapQuery())
22+
->setAncestorClass('PhutilRemarkupBlockInterpreter')
23+
->execute();
24+
$interpreters_regex = mpull($interpreters, 'getInterpreterName');
25+
$interpreters_regex = array_map('preg_quote', $interpreters_regex);
26+
$interpreters_regex = implode('|', $interpreters_regex);
27+
return "/^($interpreters_regex)\s*(?:\(([^)]+)\)\s*)?{{{/";
28+
}
29+
830
public function getMatchingLineCount(array $lines, $cursor) {
931
$num_lines = 0;
1032

11-
if (preg_match(self::START_BLOCK_PATTERN, $lines[$cursor])) {
33+
if (preg_match(self::getStartBlockPattern(), $lines[$cursor])) {
1234
$num_lines++;
1335

1436
while (isset($lines[$cursor])) {
@@ -33,7 +55,7 @@ public function markupText($text, $children) {
3355
}
3456
$matches = null;
3557

36-
preg_match(self::START_BLOCK_PATTERN, head($lines), $matches);
58+
preg_match(self::getStartBlockPattern(), head($lines), $matches);
3759

3860
$argv = array();
3961
if (isset($matches[2])) {
@@ -49,7 +71,7 @@ public function markupText($text, $children) {
4971
}
5072

5173
$lines[$first_key] = preg_replace(
52-
self::START_BLOCK_PATTERN,
74+
self::getStartBlockPattern(),
5375
'',
5476
$lines[$first_key]);
5577
$lines[$last_key] = preg_replace(

0 commit comments

Comments
 (0)