]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Cast client id to avoid PG error 3831/head
authorJeremy Benoist <jeremy.benoist@gmail.com>
Wed, 9 Jan 2019 22:29:30 +0000 (23:29 +0100)
committerJeremy Benoist <jeremy.benoist@gmail.com>
Wed, 9 Jan 2019 22:31:14 +0000 (23:31 +0100)
If someone send a malformated client_id when trying to authenticate using the API we got a 500 if wallabag use postgres because the request send a string instead of an integer.

src/Wallabag/ApiBundle/Entity/Client.php
src/Wallabag/ApiBundle/Repository/ClientRepository.php [new file with mode: 0644]
tests/Wallabag/ApiBundle/Controller/DeveloperControllerTest.php

index e6f98f98ca332db03ddec68659a8def8b5d70cf0..78349820a0adc7e2118557a13f5ee0cfdb497ddb 100644 (file)
@@ -11,7 +11,7 @@ use Wallabag\UserBundle\Entity\User;
 
 /**
  * @ORM\Table("oauth2_clients")
- * @ORM\Entity
+ * @ORM\Entity(repositoryClass="Wallabag\ApiBundle\Repository\ClientRepository")
  */
 class Client extends BaseClient
 {
diff --git a/src/Wallabag/ApiBundle/Repository/ClientRepository.php b/src/Wallabag/ApiBundle/Repository/ClientRepository.php
new file mode 100644 (file)
index 0000000..fc14262
--- /dev/null
@@ -0,0 +1,19 @@
+<?php
+
+namespace Wallabag\ApiBundle\Repository;
+
+use Doctrine\ORM\EntityRepository;
+
+class ClientRepository extends EntityRepository
+{
+    public function findOneBy(array $criteria, array $orderBy = null)
+    {
+        if (!empty($criteria['id'])) {
+            // cast client id to be an integer to avoid postgres error:
+            // "invalid input syntax for integer"
+            $criteria['id'] = (int) $criteria['id'];
+        }
+
+        return parent::findOneBy($criteria, $orderBy);
+    }
+}
index f58d1c12027f9bb5a6e259311f8b9ca19206832d..e1a0ac7e49d2d1ee92405e7f3c3ab03a86e6edc2 100644 (file)
@@ -56,6 +56,20 @@ class DeveloperControllerTest extends WallabagCoreTestCase
         $this->assertArrayHasKey('refresh_token', $data);
     }
 
+    public function testCreateTokenWithBadClientId()
+    {
+        $client = $this->getClient();
+        $client->request('POST', '/oauth/v2/token', [
+            'grant_type' => 'password',
+            'client_id' => '$WALLABAG_CLIENT_ID',
+            'client_secret' => 'secret',
+            'username' => 'admin',
+            'password' => 'mypassword',
+        ]);
+
+        $this->assertSame(400, $client->getResponse()->getStatusCode());
+    }
+
     public function testListingClient()
     {
         $this->logInAs('admin');