From 3f88a572e7022082f1413b847a2b283ec3544017 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Thu, 17 Mar 2022 15:13:25 +0100 Subject: [PATCH] Fix Laravel Framework issue with empty inputs string casted to null (Hashmac signature calculation) --- app/Models/Service.php | 8 ++++++- tests/Client/CrmClientV2ArcadiaTest.php | 28 +++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/app/Models/Service.php b/app/Models/Service.php index 4607990..0bae375 100644 --- a/app/Models/Service.php +++ b/app/Models/Service.php @@ -253,11 +253,17 @@ class Service extends Model */ public function authenticateApi(Request $request): bool { + // ! Hack to prevent Laravel to cast empty strings to null or the opposite. + // ! If cannot decode json user legacy all() method. + $data = \json_decode($request->getContent(), true); + $data = (\is_array($data) and \json_last_error() === \JSON_ERROR_NONE) ? + array_replace_recursive($request->all(), $data) : $request->all(); + $calculatedHash = Helper::signRequest( $this->bearer_pwd, $request->getUri(), $request->getMethod(), - $request->all() + $data ); // ! Très important hash_equals https://fr.wikipedia.org/wiki/Attaque_temporelle diff --git a/tests/Client/CrmClientV2ArcadiaTest.php b/tests/Client/CrmClientV2ArcadiaTest.php index b3ac12e..3619b31 100644 --- a/tests/Client/CrmClientV2ArcadiaTest.php +++ b/tests/Client/CrmClientV2ArcadiaTest.php @@ -118,6 +118,34 @@ class CrmClientV2ArcadiaTest extends CrmClientTester $this->assertTrue($arcadiaOrderMail->send($customer)); } + /** + * Send arcadia order default game (empty string does not convert to null). + * + * @return void + */ + public function testArcadiaSendOrderGameNullString() + { + $api = new CrmApiV2(); + $customer = $api->getCustomerOrNew(static::$info->hashcode); + $arcadiaOrderMail = new ArcadiaMailGame([ + 'email' => 'toto@gmail.com', + 'firstname' => 'Jean', + 'lastname' => 'Pouet', + 'phone' => '0678142536', + 'address' => '30 rue des totots', + 'address2' => null, + 'city' => 'Le Mans', + 'postcode' => '72000', + 'operation' => 12, + 'order_date' => '2022/03/17 11:24:55', + 'support_code' => 16, + 'label_type_support' => 'Gain du lot : Lot ', + 'code_unique' => '11N311A1', + 'code_on_pack' => '12345678' + ]); + $this->assertTrue($arcadiaOrderMail->send($customer)); + } + /** * Test arcadia game mail can serialize to json *