Skip to content

Commit b65fab1

Browse files
committed
Prevent credential leaks in exception messages
1 parent 1186010 commit b65fab1

2 files changed

Lines changed: 8 additions & 11 deletions

File tree

src/Exceptions/InvalidArgumentException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class InvalidArgumentException extends \InvalidArgumentException
88
{
99
public static function apiFailed(GuzzleException $exception): InvalidArgumentException
1010
{
11-
return new self('API call returned an invalid response: ' . $exception->getMessage() . '.');
11+
return new self('API call failed: ' . $exception->getCode(), previous: $exception);
1212
}
1313

1414
public static function configVariableNotAString(): InvalidArgumentException

tests/Unit/InvalidArgumentExceptionTest.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,21 @@ public function testApiFailedMessageFormat(): void
1818

1919
$exception = InvalidArgumentException::apiFailed($guzzleException);
2020

21-
self::assertSame(
22-
'API call returned an invalid response: Connection refused.',
23-
$exception->getMessage()
24-
);
21+
self::assertSame('API call failed: 0', $exception->getMessage());
22+
self::assertSame($guzzleException, $exception->getPrevious());
2523
}
2624

27-
public function testApiFailedPreservesExceptionMessage(): void
25+
public function testApiFailedDoesNotExposeRequestDetails(): void
2826
{
2927
$guzzleException = new RequestException(
30-
'Timeout exceeded',
31-
new Request('POST', 'https://example.com')
28+
'Error with https://secret.example.com/api?key=abc123',
29+
new Request('POST', 'https://secret.example.com/api?key=abc123')
3230
);
3331

3432
$exception = InvalidArgumentException::apiFailed($guzzleException);
3533

36-
self::assertStringStartsWith('API call returned an invalid response: ', $exception->getMessage());
37-
self::assertStringEndsWith('.', $exception->getMessage());
38-
self::assertStringContainsString('Timeout exceeded', $exception->getMessage());
34+
self::assertStringNotContainsString('secret.example.com', $exception->getMessage());
35+
self::assertStringNotContainsString('abc123', $exception->getMessage());
3936
}
4037

4138
public function testConfigVariableNotAString(): void

0 commit comments

Comments
 (0)