pfrenssen /
git-wrapper
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace GitWrapper; |
||
| 6 | |||
| 7 | final class GitCommand |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * Path to the directory containing the working copy. If this variable is |
||
| 11 | * set, then the process will change into this directory while the Git |
||
| 12 | * command is being run. |
||
| 13 | * |
||
| 14 | * @var string|null |
||
| 15 | */ |
||
| 16 | private $directory; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * The command being run, e.g. "clone", "commit", etc. |
||
| 20 | * |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | private $command = ''; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Whether command execution should be bypassed. |
||
| 27 | * |
||
| 28 | * @var bool |
||
| 29 | */ |
||
| 30 | private $bypass = false; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Whether to execute the raw command without escaping it. This is useful |
||
| 34 | * for executing arbitrary commands, e.g. "status -s". If this is true, |
||
| 35 | * any options and arguments are ignored. |
||
| 36 | * |
||
| 37 | * @var bool |
||
| 38 | */ |
||
| 39 | private $executeRaw = false; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var mixed[] |
||
| 43 | */ |
||
| 44 | private $options = []; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var mixed[] |
||
| 48 | */ |
||
| 49 | private $args = []; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @param mixed ...$argsAndOptions |
||
| 53 | */ |
||
| 54 | public function __construct(string $command = '', ...$argsAndOptions) |
||
| 55 | { |
||
| 56 | $this->command = $command; |
||
| 57 | |||
| 58 | foreach ($argsAndOptions as $argOrOption) { |
||
| 59 | if (is_array($argOrOption)) { |
||
| 60 | // If item is array, set it as the options |
||
| 61 | $this->setOptions($argOrOption); |
||
| 62 | } else { |
||
| 63 | // Pass all other as the Git command arguments |
||
| 64 | $this->addArgument($argOrOption); |
||
| 65 | } |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Returns Git command being run, e.g. "clone", "commit", etc. |
||
| 71 | */ |
||
| 72 | public function getCommand(): string |
||
| 73 | { |
||
| 74 | return $this->command; |
||
| 75 | } |
||
| 76 | |||
| 77 | public function setDirectory(?string $directory): void |
||
| 78 | { |
||
| 79 | $this->directory = $directory; |
||
| 80 | } |
||
| 81 | |||
| 82 | public function getDirectory(): ?string |
||
| 83 | { |
||
| 84 | return $this->directory; |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * A bool flagging whether to skip running the command. |
||
| 89 | */ |
||
| 90 | public function bypass(bool $bypass = true): void |
||
| 91 | { |
||
| 92 | $this->bypass = $bypass; |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Set whether to execute the command as-is without escaping it. |
||
| 97 | */ |
||
| 98 | public function executeRaw(bool $executeRaw = true): void |
||
| 99 | { |
||
| 100 | $this->executeRaw = $executeRaw; |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Returns true if the Git command should be run. |
||
| 105 | */ |
||
| 106 | public function notBypassed(): bool |
||
| 107 | { |
||
| 108 | return ! $this->bypass; |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Builds the command line options for use in the Git command. |
||
| 113 | * |
||
| 114 | * @return mixed[] |
||
| 115 | */ |
||
| 116 | public function buildOptions(): array |
||
| 117 | { |
||
| 118 | $options = []; |
||
| 119 | foreach ($this->options as $option => $values) { |
||
| 120 | foreach ((array) $values as $value) { |
||
| 121 | // Render the option. |
||
| 122 | $prefix = strlen($option) !== 1 ? '--' : '-'; |
||
| 123 | $options[] = $prefix . $option; |
||
| 124 | |||
| 125 | // Render apend the value if the option isn't a flag. |
||
| 126 | if ($value !== true) { |
||
| 127 | $options[] = $value; |
||
| 128 | } |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | return $options; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @param mixed[]|string|true $value The option's value, pass true if the options is a flag. |
||
| 137 | */ |
||
| 138 | public function setOption(string $option, $value): void |
||
| 139 | { |
||
| 140 | $this->options[$option] = $value; |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param mixed[] $options |
||
| 145 | */ |
||
| 146 | public function setOptions(array $options): void |
||
| 147 | { |
||
| 148 | foreach ($options as $option => $value) { |
||
| 149 | $this->setOption($option, $value); |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | public function setFlag(string $option): void |
||
| 154 | { |
||
| 155 | $this->setOption($option, true); |
||
|
0 ignored issues
–
show
|
|||
| 156 | } |
||
| 157 | |||
| 158 | public function getOption(string $option, $default = null) |
||
| 159 | { |
||
| 160 | return $this->options[$option] ?? $default; |
||
| 161 | } |
||
| 162 | |||
| 163 | public function addArgument(string $arg): void |
||
| 164 | { |
||
| 165 | $this->args[] = $arg; |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Renders the arguments and options for the Git command. |
||
| 170 | * |
||
| 171 | * @return string|mixed[] |
||
| 172 | */ |
||
| 173 | public function getCommandLine() |
||
| 174 | { |
||
| 175 | if ($this->executeRaw) { |
||
| 176 | return $this->getCommand(); |
||
| 177 | } |
||
| 178 | |||
| 179 | $command = array_merge([$this->getCommand()], $this->buildOptions(), $this->args); |
||
| 180 | |||
| 181 | return array_filter($command, function ($value): bool { |
||
| 182 | return strlen($value) > 0; |
||
| 183 | }); |
||
| 184 | } |
||
| 185 | } |
||
| 186 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: