Skip to content

Commit 62b28f6

Browse files
authored
Merge pull request #3 from cebe/patch-1
Fixes RCE security issue by adding shell argument escaping
2 parents 6919691 + b3ea6c7 commit 62b28f6

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/Pygmentize/Pygmentize.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,18 @@ public static function highlight($source, $language, $encoding = "utf-8", $forma
4545
2 => array('pipe', 'w'), // stderr
4646
);
4747

48-
if (!empty($language))
49-
$args = sprintf(" -f %s -l %s -O encoding=%s,style=%s,lineos=1,startinline=true", $formatter, $language, $encoding, $style);
50-
else
51-
$args = sprintf(" -f %s -g -O encoding=%s,style=%s,lineos=1", $formatter, $encoding, $style);
48+
$args = array(
49+
'-f ' . escapeshellarg($formatter)
50+
);
51+
if (!empty($language)) {
52+
$args[] = '-l ' . escapeshellarg($language);
53+
$args[] = '-O ' . escapeshellarg(sprintf('encoding=%s,style=%s,lineos=1,startinline=true', $encoding, $style));
54+
} else {
55+
$args[] = '-g';
56+
$args[] = '-O ' . escapeshellarg(sprintf('encoding=%s,style=%s,lineos=1', $encoding, $style));
57+
}
5258

53-
$proc = proc_open(self::PIGMENTS_BINARY.$args, $dspec, $pipes);
59+
$proc = proc_open(self::PIGMENTS_BINARY.' '.implode(' ', $args), $dspec, $pipes);
5460

5561
if (is_resource($proc)) {
5662
// Reads the stdout output.
@@ -84,4 +90,4 @@ public static function highlight($source, $language, $encoding = "utf-8", $forma
8490

8591
}
8692

87-
}
93+
}

0 commit comments

Comments
 (0)