Skip to content

Commit 0c91f33

Browse files
committed
bug #11610 [Console] fixed output escaping when using the process helper (fabpot)
This PR was merged into the 2.6-dev branch. Discussion ---------- [Console] fixed output escaping when using the process helper | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a When displaying the output of a process run, we must escape the `<` to avoid any formatting. Commits ------- bf7a90e [Console] fixed output escaping when using the process helper
2 parents 50f77fb + c4bb422 commit 0c91f33

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

Helper/ProcessHelper.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function run(OutputInterface $output, $cmd, $error = null, $callback = nu
4848
}
4949

5050
if ($verbosity <= $output->getVerbosity()) {
51-
$output->write($formatter->start(spl_object_hash($process), $process->getCommandLine()));
51+
$output->write($formatter->start(spl_object_hash($process), $this->escapeString($process->getCommandLine())));
5252
}
5353

5454
if ($output->isDebug()) {
@@ -63,7 +63,7 @@ public function run(OutputInterface $output, $cmd, $error = null, $callback = nu
6363
}
6464

6565
if (!$process->isSuccessful() && null !== $error) {
66-
$output->writeln(sprintf('<error>%s</error>', $error));
66+
$output->writeln(sprintf('<error>%s</error>', $this->escapeString($error)));
6767
}
6868

6969
return $process;
@@ -111,15 +111,26 @@ public function wrapCallback(OutputInterface $output, Process $process, $callbac
111111
{
112112
$formatter = $this->getHelperSet()->get('debug_formatter');
113113

114-
return function ($type, $buffer) use ($output, $process, $callback, $formatter) {
115-
$output->write($formatter->progress(spl_object_hash($process), $buffer, Process::ERR === $type));
114+
$that = $this;
115+
return function ($type, $buffer) use ($output, $process, $callback, $formatter, $that) {
116+
$output->write($formatter->progress(spl_object_hash($process), $that->escapeString($buffer), Process::ERR === $type));
116117

117118
if (null !== $callback) {
118119
call_user_func($callback, $type, $buffer);
119120
}
120121
};
121122
}
122123

124+
/**
125+
* This method is public for PHP 5.3 compatibility, it should be private.
126+
*
127+
* @internal
128+
*/
129+
public function escapeString($str)
130+
{
131+
return str_replace('<', '\\<', $str);
132+
}
133+
123134
/**
124135
* {@inheritdoc}
125136
*/

Tests/Helper/ProcessHelperTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ public function provideCommandsAndOutput()
5757
OUT 42
5858
RES Command ran successfully
5959
60+
EOT;
61+
$successOutputDebugWithTags = <<<EOT
62+
RUN php -r "echo \"<info>42</info>\";"
63+
OUT <info>42</info>
64+
RES Command ran successfully
65+
6066
EOT;
6167
$successOutputProcessDebug = <<<EOT
6268
RUN 'php' '-r' 'echo 42;'
@@ -86,6 +92,7 @@ public function provideCommandsAndOutput()
8692
array('', 'php -r "echo 42;"', StreamOutput::VERBOSITY_VERBOSE, null),
8793
array($successOutputVerbose, 'php -r "echo 42;"', StreamOutput::VERBOSITY_VERY_VERBOSE, null),
8894
array($successOutputDebug, 'php -r "echo 42;"', StreamOutput::VERBOSITY_DEBUG, null),
95+
array($successOutputDebugWithTags, 'php -r "echo \"<info>42</info>\";"', StreamOutput::VERBOSITY_DEBUG, null),
8996
array('', 'php -r "syntax error"', StreamOutput::VERBOSITY_VERBOSE, null),
9097
array($syntaxErrorOutputVerbose, 'php -r "fwrite(STDERR, \'error message\');usleep(50000);fwrite(STDOUT, \'out message\');exit(252);"', StreamOutput::VERBOSITY_VERY_VERBOSE, null),
9198
array($syntaxErrorOutputDebug, 'php -r "fwrite(STDERR, \'error message\');usleep(50000);fwrite(STDOUT, \'out message\');exit(252);"', StreamOutput::VERBOSITY_DEBUG, null),

0 commit comments

Comments
 (0)