-
Notifications
You must be signed in to change notification settings - Fork 4
Surface raw body in BringApiException when error envelope is unparsable #60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -101,6 +101,45 @@ public function testApiExceptionMessageEmbedsFirstErrorButNotRawBody(): void | |
| } | ||
| } | ||
|
|
||
| public function testUnparsableErrorBodyIsSurfacedInMessage(): void | ||
| { | ||
| // Bring occasionally returns a 4xx with a body that doesn't fit the | ||
| // errors/messages envelope (plain text, HTML, a different JSON shape). | ||
| // We must still surface enough of it that the operator can act on it | ||
| // instead of collapsing to "no parsable error body". | ||
| $body = 'Recipient postal code 1642 is not serviced by BUSINESS_PARCEL'; | ||
| $client = new RecordingClient([new Response(400, ['Content-Type' => 'text/plain'], $body)]); | ||
| $transport = new Transport($client, $this->factory, $this->factory, $this->factory, new NullAuthorization()); | ||
|
|
||
| try { | ||
| $transport->send(new PostalCodeEndpoint('0150')); | ||
| self::fail('expected BringApiException'); | ||
| } catch (BringApiException $e) { | ||
| self::assertStringContainsString('HTTP 400', $e->getMessage()); | ||
| self::assertStringContainsString('postal code 1642', $e->getMessage()); | ||
| self::assertStringNotContainsString('no parsable error body', $e->getMessage()); | ||
| } | ||
| } | ||
|
|
||
| public function testUnparsableErrorBodyRedactsCredentialShapedValues(): void | ||
| { | ||
| // If the unparsable body happens to echo credential-shaped values, | ||
| // they must be redacted before being embedded in getMessage(). | ||
| $body = '{"detail":"unauthorized","api_key":"secretKey","Authorization":"Basic abc"}'; | ||
| $client = new RecordingClient([new Response(401, ['Content-Type' => 'application/json'], $body)]); | ||
| $transport = new Transport($client, $this->factory, $this->factory, $this->factory, new NullAuthorization()); | ||
|
|
||
| try { | ||
| $transport->send(new PostalCodeEndpoint('0150')); | ||
| self::fail('expected BringApiException'); | ||
| } catch (BringApiException $e) { | ||
| self::assertStringContainsString('HTTP 401', $e->getMessage()); | ||
| self::assertStringContainsString('unauthorized', $e->getMessage()); | ||
| self::assertStringNotContainsString('secretKey', $e->getMessage()); | ||
| self::assertStringNotContainsString('Basic abc', $e->getMessage()); | ||
| } | ||
| } | ||
|
Comment on lines
+124
to
+141
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update the test to verify that the more robust redaction patterns (such as plain text headers like public function testUnparsableErrorBodyRedactsCredentialShapedValues(): void
{
// If the unparsable body happens to echo credential-shaped values,
// they must be redacted before being embedded in getMessage().
$body = 'Authorization: Bearer abc-123, access_token=xyz987, api_key: secretKey';
$client = new RecordingClient([new Response(401, ['Content-Type' => 'text/plain'], $body)]);
$transport = new Transport($client, $this->factory, $this->factory, $this->factory, new NullAuthorization());
try {
$transport->send(new PostalCodeEndpoint('0150'));
self::fail('expected BringApiException');
} catch (BringApiException $e) {
self::assertStringContainsString('HTTP 401', $e->getMessage());
self::assertStringNotContainsString('abc-123', $e->getMessage());
self::assertStringNotContainsString('xyz987', $e->getMessage());
self::assertStringNotContainsString('secretKey', $e->getMessage());
}
} |
||
|
|
||
| public function testTransportExceptionWrapsPsr18Failure(): void | ||
| { | ||
| $networkFail = new class ('boom') extends \RuntimeException implements ClientExceptionInterface { | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current redaction logic has a few gaps that could leak sensitive credentials into exception messages and application logs:
\bbeforeapi_keyortokenprevents matching keys with prefixes likeaccess_token,refresh_token, ormy_api_keybecause_is a word character (meaning there is no word boundary between_andtora).Key: Valueformat (e.g.,Authorization: Bearer <token>orapi_key: <key>). These are not matched by either the JSON regex (which expects quotes) or the form-urlencoded regex (which expects=).We can make the redaction significantly more robust by: