]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Use a custom cookiejar to avoid error when the cookie is badly saved
authorJeremy Benoist <jeremy.benoist@gmail.com>
Wed, 24 Jul 2019 08:29:50 +0000 (10:29 +0200)
committerJeremy Benoist <jeremy.benoist@gmail.com>
Wed, 24 Jul 2019 08:42:20 +0000 (10:42 +0200)
It happens sometimes on wallabag.it, the json inside the cookie is badly saved and the json isn't valid. It generates an exception and avoid people to use the api and import contents.
To fix that, we use a dedicated `FileCookieJar`, which extends the default one from Guzzle to fix these issues.

Also updated deps

composer.lock
src/Wallabag/CoreBundle/Helper/FileCookieJar.php [new file with mode: 0644]
src/Wallabag/CoreBundle/Resources/config/services.yml

index 2bce514a124af07e7f7c2e03eb59509327c06dc9..e72ea4749303b21243fe721010a4c85fc46c0638 100644 (file)
         },
         {
             "name": "egulias/email-validator",
-            "version": "2.1.9",
+            "version": "2.1.10",
             "source": {
                 "type": "git",
                 "url": "https://github.com/egulias/EmailValidator.git",
-                "reference": "128cc721d771ec2c46ce59698f4ca42b73f71b25"
+                "reference": "a6c8d7101b19a451c1707b1b79bbbc56e4bdb7ec"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/egulias/EmailValidator/zipball/128cc721d771ec2c46ce59698f4ca42b73f71b25",
-                "reference": "128cc721d771ec2c46ce59698f4ca42b73f71b25",
+                "url": "https://api.github.com/repos/egulias/EmailValidator/zipball/a6c8d7101b19a451c1707b1b79bbbc56e4bdb7ec",
+                "reference": "a6c8d7101b19a451c1707b1b79bbbc56e4bdb7ec",
                 "shasum": ""
             },
             "require": {
             "require-dev": {
                 "dominicsayers/isemail": "dev-master",
                 "phpunit/phpunit": "^4.8.35||^5.7||^6.0",
-                "satooshi/php-coveralls": "^1.0.1"
+                "satooshi/php-coveralls": "^1.0.1",
+                "symfony/phpunit-bridge": "^4.4@dev"
             },
             "suggest": {
                 "ext-intl": "PHP Internationalization Libraries are required to use the SpoofChecking validation"
                 "validation",
                 "validator"
             ],
-            "time": "2019-06-23T10:14:27+00:00"
+            "time": "2019-07-19T20:52:08+00:00"
         },
         {
             "name": "electrolinux/php-html5lib",
         },
         {
             "name": "j0k3r/graby-site-config",
-            "version": "1.0.86",
+            "version": "1.0.88",
             "source": {
                 "type": "git",
                 "url": "https://github.com/j0k3r/graby-site-config.git",
-                "reference": "9c0ae4ee74e737c6ccf913da9bcb169850d38a30"
+                "reference": "b485a36c31736b8834ebf62f42bd97fb333eefa7"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/j0k3r/graby-site-config/zipball/9c0ae4ee74e737c6ccf913da9bcb169850d38a30",
-                "reference": "9c0ae4ee74e737c6ccf913da9bcb169850d38a30",
+                "url": "https://api.github.com/repos/j0k3r/graby-site-config/zipball/b485a36c31736b8834ebf62f42bd97fb333eefa7",
+                "reference": "b485a36c31736b8834ebf62f42bd97fb333eefa7",
                 "shasum": ""
             },
             "require": {
                 }
             ],
             "description": "Graby site config files",
-            "time": "2019-06-25T10:46:50+00:00"
+            "time": "2019-07-23T15:40:40+00:00"
         },
         {
             "name": "j0k3r/httplug-ssrf-plugin",
         },
         {
             "name": "pagerfanta/pagerfanta",
-            "version": "v2.1.2",
+            "version": "v2.1.3",
             "source": {
                 "type": "git",
                 "url": "https://github.com/whiteoctober/Pagerfanta.git",
-                "reference": "45a85ad426316ae37f2d007022e5b4c95bc3aef4"
+                "reference": "a53ff01d521648d9dbca19b93ac6bc75a59b0972"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/whiteoctober/Pagerfanta/zipball/45a85ad426316ae37f2d007022e5b4c95bc3aef4",
-                "reference": "45a85ad426316ae37f2d007022e5b4c95bc3aef4",
+                "url": "https://api.github.com/repos/whiteoctober/Pagerfanta/zipball/a53ff01d521648d9dbca19b93ac6bc75a59b0972",
+                "reference": "a53ff01d521648d9dbca19b93ac6bc75a59b0972",
                 "shasum": ""
             },
             "require": {
                 "paginator",
                 "paging"
             ],
-            "time": "2019-04-02T08:50:39+00:00"
+            "time": "2019-07-17T20:56:16+00:00"
         },
         {
             "name": "paragonie/random_compat",
         },
         {
             "name": "scheb/two-factor-bundle",
-            "version": "v3.19.1",
+            "version": "v3.21.0",
             "source": {
                 "type": "git",
                 "url": "https://github.com/scheb/two-factor-bundle.git",
-                "reference": "634b9b40ea0b6769f274ac994067f08012d96c44"
+                "reference": "94cc6f2e0fd8a7b683246d95fcad9b15ca828fc6"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/scheb/two-factor-bundle/zipball/634b9b40ea0b6769f274ac994067f08012d96c44",
-                "reference": "634b9b40ea0b6769f274ac994067f08012d96c44",
+                "url": "https://api.github.com/repos/scheb/two-factor-bundle/zipball/94cc6f2e0fd8a7b683246d95fcad9b15ca828fc6",
+                "reference": "94cc6f2e0fd8a7b683246d95fcad9b15ca828fc6",
                 "shasum": ""
             },
             "require": {
                 "two-factor",
                 "two-step"
             ],
-            "time": "2019-07-01T19:12:43+00:00"
+            "time": "2019-07-12T16:11:05+00:00"
         },
         {
             "name": "sensio/distribution-bundle",
         },
         {
             "name": "sensiolabs/security-checker",
-            "version": "v6.0.1",
+            "version": "v6.0.2",
             "source": {
                 "type": "git",
                 "url": "https://github.com/sensiolabs/security-checker.git",
                 },
                 {
                     "name": "Gert de Pagter",
-                    "email": "BackEndTea@gmail.com"
+                    "email": "backendtea@gmail.com"
                 }
             ],
             "description": "Symfony polyfill for ctype functions",
         },
         {
             "name": "zendframework/zend-diactoros",
-            "version": "2.1.2",
+            "version": "2.1.3",
             "source": {
                 "type": "git",
                 "url": "https://github.com/zendframework/zend-diactoros.git",
-                "reference": "37bf68b428850ee26ed7c3be6c26236dd95a95f1"
+                "reference": "279723778c40164bcf984a2df12ff2c6ec5e61c1"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/zendframework/zend-diactoros/zipball/37bf68b428850ee26ed7c3be6c26236dd95a95f1",
-                "reference": "37bf68b428850ee26ed7c3be6c26236dd95a95f1",
+                "url": "https://api.github.com/repos/zendframework/zend-diactoros/zipball/279723778c40164bcf984a2df12ff2c6ec5e61c1",
+                "reference": "279723778c40164bcf984a2df12ff2c6ec5e61c1",
                 "shasum": ""
             },
             "require": {
                 "psr",
                 "psr-7"
             ],
-            "time": "2019-04-29T21:11:00+00:00"
+            "time": "2019-07-10T16:13:25+00:00"
         },
         {
             "name": "zendframework/zend-eventmanager",
         },
         {
             "name": "doctrine/data-fixtures",
-            "version": "v1.3.1",
+            "version": "v1.3.2",
             "source": {
                 "type": "git",
                 "url": "https://github.com/doctrine/data-fixtures.git",
-                "reference": "3a1e2c3c600e615a2dffe56d4ca0875cc5233e0a"
+                "reference": "09b16943b27f3d80d63988d100ff256148c2f78b"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/doctrine/data-fixtures/zipball/3a1e2c3c600e615a2dffe56d4ca0875cc5233e0a",
-                "reference": "3a1e2c3c600e615a2dffe56d4ca0875cc5233e0a",
+                "url": "https://api.github.com/repos/doctrine/data-fixtures/zipball/09b16943b27f3d80d63988d100ff256148c2f78b",
+                "reference": "09b16943b27f3d80d63988d100ff256148c2f78b",
                 "shasum": ""
             },
             "require": {
             "keywords": [
                 "database"
             ],
-            "time": "2018-03-20T09:06:36+00:00"
+            "time": "2019-07-10T18:30:35+00:00"
         },
         {
             "name": "doctrine/doctrine-fixtures-bundle",
diff --git a/src/Wallabag/CoreBundle/Helper/FileCookieJar.php b/src/Wallabag/CoreBundle/Helper/FileCookieJar.php
new file mode 100644 (file)
index 0000000..52c7f5d
--- /dev/null
@@ -0,0 +1,72 @@
+<?php
+
+namespace Wallabag\CoreBundle\Helper;
+
+use GuzzleHttp\Cookie\FileCookieJar as BaseFileCookieJar;
+use GuzzleHttp\Cookie\SetCookie;
+use GuzzleHttp\Utils;
+
+/**
+ * Overidden Cookie behavior to:
+ *     - fix multiple concurrent writes (see https://github.com/guzzle/guzzle/pull/1884)
+ *     - ignore error when the cookie file is malformatted (resulting in clearing it).
+ */
+class FileCookieJar extends BaseFileCookieJar
+{
+    /**
+     * Saves the cookies to a file.
+     *
+     * @param string $filename File to save
+     *
+     * @throws \RuntimeException if the file cannot be found or created
+     */
+    public function save($filename)
+    {
+        $json = [];
+        foreach ($this as $cookie) {
+            if ($cookie->getExpires() && !$cookie->getDiscard()) {
+                $json[] = $cookie->toArray();
+            }
+        }
+
+        if (false === file_put_contents($filename, json_encode($json), LOCK_EX)) {
+            // @codeCoverageIgnoreStart
+            throw new \RuntimeException("Unable to save file {$filename}");
+            // @codeCoverageIgnoreEnd
+        }
+    }
+
+    /**
+     * Load cookies from a JSON formatted file.
+     *
+     * Old cookies are kept unless overwritten by newly loaded ones.
+     *
+     * @param string $filename cookie file to load
+     *
+     * @throws \RuntimeException if the file cannot be loaded
+     */
+    public function load($filename)
+    {
+        $json = file_get_contents($filename);
+        if (false === $json) {
+            // @codeCoverageIgnoreStart
+            throw new \RuntimeException("Unable to load file {$filename}");
+            // @codeCoverageIgnoreEnd
+        }
+
+        try {
+            $data = Utils::jsonDecode($json, true);
+        } catch (\InvalidArgumentException $e) {
+            // cookie file is invalid, just ignore the exception and it'll reset the whole cookie file
+            $data = '';
+        }
+
+        if (\is_array($data)) {
+            foreach (Utils::jsonDecode($json, true) as $cookie) {
+                $this->setCookie(new SetCookie($cookie));
+            }
+        } elseif (\strlen($data)) {
+            throw new \RuntimeException("Invalid cookie file: {$filename}");
+        }
+    }
+}
index 319869513fd35994e45fb85e1800cbe3c7e81fa5..169b67e5c17e0f853b81348990af5d437ab3915b 100644 (file)
@@ -82,7 +82,7 @@ services:
             - ["addSubscriber", ["@bd_guzzle_site_authenticator.authenticator_subscriber"]]
 
     wallabag_core.guzzle.cookie_jar:
-        class: GuzzleHttp\Cookie\FileCookieJar
+        class: Wallabag\CoreBundle\Helper\FileCookieJar
         arguments: ["%kernel.cache_dir%/cookiejar.json"]
 
     wallabag_core.content_proxy: