Sfoglia il codice sorgente

fix: handle YOURLS 200 response without shorturl gracefully

When YOURLS replies with statusCode 200 but no "shorturl" field,
YourlsProxy::_extractShortUrl() returned int 0 for its ?string return
type. Under the file's declare(strict_types=1) this raises an uncaught
TypeError, aborting paste creation instead of surfacing the intended
"Error parsing proxy response" message. Fall back to null (matching
ShlinkProxy) so the existing graceful error path is taken.

Co-Authored-By: Claude <noreply@anthropic.com>
TowyTowy 2 settimane fa
parent
commit
a0c0d154ee
3 ha cambiato i file con 14 aggiunte e 1 eliminazioni
  1. 1 0
      CHANGELOG.md
  2. 1 1
      lib/Proxy/YourlsProxy.php
  3. 12 0
      tst/YourlsProxyTest.php

+ 1 - 0
CHANGELOG.md

@@ -1,6 +1,7 @@
 # PrivateBin version history
 # PrivateBin version history
 
 
 ## 2.0.6 (not yet released)
 ## 2.0.6 (not yet released)
+* FIXED: Gracefully handle YOURLS replies with a 200 status code but no shorturl, instead of raising a TypeError
 
 
 ## 2.0.5 (2026-07-11)
 ## 2.0.5 (2026-07-11)
 * CHANGED: Show OS-specific copy hotkey hint (Cmd+c on Mac, Ctrl+c on others) (#1506)
 * CHANGED: Show OS-specific copy hotkey hint (Cmd+c on Mac, Ctrl+c on others) (#1506)

+ 1 - 1
lib/Proxy/YourlsProxy.php

@@ -66,7 +66,7 @@ class YourlsProxy extends AbstractProxy
     protected function _extractShortUrl(array $data): ?string
     protected function _extractShortUrl(array $data): ?string
     {
     {
         if ((int) ($data['statusCode'] ?? 0) === 200) {
         if ((int) ($data['statusCode'] ?? 0) === 200) {
-            return $data['shorturl'] ?? 0;
+            return $data['shorturl'] ?? null;
         }
         }
         return null;
         return null;
     }
     }

+ 12 - 0
tst/YourlsProxyTest.php

@@ -135,6 +135,18 @@ class YourlsProxyTest extends TestCase
         $this->assertEquals($yourls->getError(), 'Proxy error: Error parsing proxy response. This can be a configuration issue, like wrong or missing config keys.');
         $this->assertEquals($yourls->getError(), 'Proxy error: Error parsing proxy response. This can be a configuration issue, like wrong or missing config keys.');
     }
     }
 
 
+    public function testYourlsSuccessWithoutShortUrl()
+    {
+        // YOURLS may reply with statusCode 200 but without a shorturl field;
+        // this must be handled gracefully as an error instead of raising a
+        // TypeError (the method is declared to return ?string)
+        file_put_contents($this->_mock_yourls_service, '{"statusCode":200}');
+
+        $yourls = new YourlsProxy($this->_conf, 'https://example.com/?foo#bar');
+        $this->assertTrue($yourls->isError());
+        $this->assertEquals($yourls->getError(), 'Proxy error: Error parsing proxy response. This can be a configuration issue, like wrong or missing config keys.');
+    }
+
     public function testServerError()
     public function testServerError()
     {
     {
         // simulate some other server error that results in a non-JSON reply
         // simulate some other server error that results in a non-JSON reply