-
Notifications
You must be signed in to change notification settings - Fork 43
Fix security issues and PHP 8.4 compatibility #139
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,9 +49,9 @@ public static function withTemplateName(string $group, string $templateName): se | |
| } | ||
|
|
||
| /** | ||
| * @todo method signature to be changed in next major release as implicitly marking parameter as nullable is deprecated since PHP 8.4 | ||
| * @param bool|null $checkIdx | ||
| */ | ||
| public function setExternalId(string $idx, bool $checkIdx = null): self | ||
| public function setExternalId(string $idx, $checkIdx = null): self | ||
|
Collaborator
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. This solution is not 100% backward compatible. If someone extended these classes and methods, they would receive a warning for PHP < 8.0: Warning: Declaration of Smsapi\Client\Tests\Unit\Feature\Sms\Bag\ExtendedSendSmssBag::setExternalId(array $idx, ?bool $checkIdx = NULL): Smsapi\Client\Feature\Sms\Bag\SendSmssBag should be compatible with Smsapi\Client\Feature\Sms\Bag\SendSmssBag::setExternalId(array $idx, $checkIdx = NULL): Smsapi\Client\Feature\Sms\Bag\SendSmssBag in /app/tests/Unit/Feature/Sms/Bag/SendSmssBagTest.php on line 23and a fatal error for PHP >= 8.0. Fatal error: Declaration of Smsapi\Client\Tests\Unit\Feature\Sms\Bag\ExtendedSendSmssBag::setExternalId(array $idx, ?bool $checkIdx = null): Smsapi\Client\Feature\Sms\Bag\SendSmssBag must be compatible with Smsapi\Client\Feature\Sms\Bag\SendSmssBag::setExternalId(array $idx, $checkIdx = null): Smsapi\Client\Feature\Sms\Bag\SendSmssBag in /app/tests/Unit/Feature/Sms/Bag/SendSmssBagTest.php on line 23
Collaborator
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. Well, there is probably no better solution - I'd merge it and release it in the next version of the lib. |
||
| { | ||
| $this->idx = [$idx]; | ||
| $this->checkIdx = $checkIdx; | ||
|
|
@@ -63,7 +63,10 @@ public function setExternalId(string $idx, bool $checkIdx = null): self | |
| * @deprecated | ||
| * @see SendSmsToGroupBag::setExternalId() | ||
| */ | ||
| public function setIdx(array $idx, bool $checkIdx = null): self | ||
| /** | ||
| * @param bool|null $checkIdx | ||
| */ | ||
| public function setIdx(array $idx, $checkIdx = null): self | ||
| { | ||
| $this->idx = $idx; | ||
| $this->checkIdx = $checkIdx; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| use Psr\Http\Client\ClientInterface; | ||
| use Psr\Http\Message\RequestInterface; | ||
| use Psr\Http\Message\ResponseInterface; | ||
| use Psr\Http\Message\StreamInterface; | ||
| use Psr\Log\LoggerInterface; | ||
|
|
||
| /** | ||
|
|
@@ -26,21 +27,47 @@ public function __construct(ClientInterface $client, LoggerInterface $logger) | |
| public function sendRequest(RequestInterface $request): ResponseInterface | ||
| { | ||
| $this->logger->info('Request', [ | ||
| 'request' => $request, | ||
| 'method' => $request->getMethod(), | ||
| 'uri' => $request->getUri(), | ||
| 'headers' => $request->getHeaders(), | ||
| 'body' => $request->getBody()->getContents(), | ||
| 'headers' => $this->sanitizeHeaders($request->getHeaders()), | ||
|
Collaborator
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. The default logger is NullLogger. If you're injecting your own logger and have trouble with the logged data, your logger is the place to process it.
Collaborator
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.
Collaborator
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. Detach all that logging related stuff from this PR. All the rest is acceptable. |
||
| 'body' => $this->readBody($request->getBody()), | ||
| ]); | ||
|
|
||
| $response = $this->client->sendRequest($request); | ||
|
|
||
| $this->logger->info('Response', [ | ||
| 'response' => $response, | ||
| 'headers' => $response->getHeaders(), | ||
| 'body' => $response->getBody()->getContents(), | ||
| 'body' => $this->readBody($response->getBody()), | ||
| ]); | ||
|
|
||
| return $response; | ||
| } | ||
|
|
||
| private function readBody(StreamInterface $stream): string | ||
| { | ||
| if ($stream->isSeekable()) { | ||
| $body = (string) $stream; | ||
| $stream->rewind(); | ||
|
|
||
| return $body; | ||
| } | ||
|
|
||
| return '<non-seekable stream, size=' . ($stream->getSize() ?? 'unknown') . '>'; | ||
| } | ||
|
|
||
| private function sanitizeHeaders(array $headers): array | ||
| { | ||
| $sensitiveHeaders = ['authorization', 'proxy-authorization']; | ||
|
|
||
| foreach ($headers as $name => $values) { | ||
| if (in_array(strtolower($name), $sensitiveHeaders, true)) { | ||
| $headers[$name] = array_map(function (string $value): string { | ||
| $len = strlen($value); | ||
| return sprintf('xxxx... (len = %d)', $len); | ||
| }, $values); | ||
| } | ||
| } | ||
|
|
||
| return $headers; | ||
| } | ||
| } | ||
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.
Extract a
prepareConnectionOptionsmethod for these options.