Skip to content

Commit 9af6240

Browse files
Escape unsafe command failure diagnostics
1 parent 101ece1 commit 9af6240

4 files changed

Lines changed: 23 additions & 3 deletions

File tree

‎AGENTS.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
- Handle `preg_*` engine failures: when the result is used as data, test for
1010
`false` or `null` and throw a `\RuntimeException` including
1111
`preg_last_error_msg()`; boolean validation guards must compare strictly, such
12-
as `=== 1`, so an engine failure can only ever fail closed.
12+
as `=== 1`, so an engine failure can only ever fail closed. Diagnostic
13+
escaping is the narrow exception: use a deterministic bytewise fallback rather
14+
than throwing, so it cannot obscure the original exception.
1315
- Anchor validation patterns to the true end of input with the `D` modifier or
1416
`\z`; a bare `$` accepts a trailing newline.
1517
- Never embed raw control bytes in exception messages and other diagnostics;

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* Drop support for PHP 7.2 and 7.3
66
* Require `guzzlehttp/guzzle` ^8.0, `guzzlehttp/promises` ^3.0, and `guzzlehttp/psr7` ^3.0
7+
* Escape unsafe command names and previous messages in generated command exceptions
78
* Reject native PHP serialization of `ServiceClient`
89
* Require custom implementations and subclasses of public command APIs to match native method signatures
910
* Require service client response transformers to return `ResultInterface` values

‎src/Exception/CommandException.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use GuzzleHttp\Exception\GuzzleException;
99
use GuzzleHttp\Exception\ResponseException;
1010
use GuzzleHttp\Exception\TransferException;
11+
use GuzzleHttp\Psr7\DiagnosticValue;
1112
use Psr\Http\Client\NetworkExceptionInterface;
1213
use Psr\Http\Client\RequestExceptionInterface;
1314
use Psr\Http\Message\RequestInterface;
@@ -54,8 +55,7 @@ public static function fromPrevious(CommandInterface $command, \Exception $prev)
5455
}
5556

5657
// Prepare the message.
57-
$message = 'There was an error executing the '.$command->getName()
58-
.' command: '.$prev->getMessage();
58+
$message = \sprintf('There was an error executing the %s command: %s', DiagnosticValue::escape($command->getName()), DiagnosticValue::escape($prev->getMessage()));
5959

6060
// Create the exception.
6161
return new $class($message, $command, $prev, $request, $response);

‎tests/Exception/CommandExceptionTest.php‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,23 @@ public function testFactoryReturnsExceptionIfAlreadyCommandException(): void
5252
$this->assertSame($previous, $exception);
5353
}
5454

55+
public function testFactoryEscapesUnsafeCommandAndPreviousMessages(): void
56+
{
57+
$commandName = "command\xC2\x80";
58+
$previousMessage = "failure\xFF";
59+
$command = $this->createMock(CommandInterface::class);
60+
$command->method('getName')->willReturn($commandName);
61+
$previous = new \RuntimeException($previousMessage);
62+
63+
$exception = CommandException::fromPrevious($command, $previous);
64+
65+
$this->assertSame('There was an error executing the command\\x80 command: failure\\xFF', $exception->getMessage());
66+
$this->assertSame($command, $exception->getCommand());
67+
$this->assertSame($commandName, $command->getName());
68+
$this->assertSame($previous, $exception->getPrevious());
69+
$this->assertSame($previousMessage, $previous->getMessage());
70+
}
71+
5572
public function testFactoryReturnsClientExceptionFor400LevelStatusCode(): void
5673
{
5774
$command = $this->createMock(CommandInterface::class);

0 commit comments

Comments
 (0)