1
0
Fork 0

GithubActionError: escape more special chars (#10243)

pull/10250/head
Markus Staab 2021-11-02 11:43:34 +01:00 committed by GitHub
parent 90087b4fb3
commit 6b62f98d3c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 30 additions and 3 deletions

View File

@ -36,9 +36,8 @@ final class GithubActionError
public function emit($message, $file = null, $line = null)
{
if (getenv('GITHUB_ACTIONS') && !getenv('COMPOSER_TESTS_ARE_RUNNING')) {
// newlines need to be encoded
// see https://github.com/actions/starter-workflows/issues/68#issuecomment-581479448
$message = str_replace("\n", '%0A', $message);
$message = $this->escapeData($message);
$file = $this->escapeProperty($file);
if ($file && $line) {
$this->io->write("::error file=". $file .",line=". $line ."::". $message);
@ -49,4 +48,32 @@ final class GithubActionError
}
}
}
/**
* @param string $data
* @return string
*/
private function escapeData($data) {
// see https://github.com/actions/toolkit/blob/4f7fb6513a355689f69f0849edeb369a4dc81729/packages/core/src/command.ts#L80-L85
$data = str_replace("%", '%25', $data);
$data = str_replace("\r", '%0D', $data);
$data = str_replace("\n", '%0A', $data);
return $data;
}
/**
* @param string $property
* @return string
*/
private function escapeProperty($property) {
// see https://github.com/actions/toolkit/blob/4f7fb6513a355689f69f0849edeb369a4dc81729/packages/core/src/command.ts#L87-L94
$property = str_replace("%", '%25', $property);
$property = str_replace("\r", '%0D', $property);
$property = str_replace("\n", '%0A', $property);
$property = str_replace(":", '%3A', $property);
$property = str_replace(",", '%2C', $property);
return $property;
}
}