Validate headers before reading url body
authorjoshuaruesweg <ruesweg@woltlab.com>
Thu, 4 Mar 2021 16:01:44 +0000 (17:01 +0100)
committerjoshuaruesweg <ruesweg@woltlab.com>
Tue, 16 Mar 2021 15:19:13 +0000 (16:19 +0100)
wcfsetup/install/files/lib/system/message/unfurl/UnfurlResponse.class.php

index 69866c3ff16cca2aa1839ca73c4846e6e582e031..d26bbe21b53ae74e096f5dd523f82060d5aa8fba 100644 (file)
@@ -51,6 +51,11 @@ final class UnfurlResponse
      */
     private $response;
 
+    /**
+     * @var string
+     */
+    private $responseCharset = "UTF-8";
+
     /**
      * @var \DOMDocument
      */
@@ -118,6 +123,8 @@ final class UnfurlResponse
      */
     private function readBody(): void
     {
+        $this->validateHeaders();
+
         $this->body = "";
         while (!$this->response->getBody()->eof()) {
             $this->body .= $this->response->getBody()->read(8192);
@@ -128,9 +135,9 @@ final class UnfurlResponse
         }
         $this->response->getBody()->close();
 
-        if ($this->getCharset() !== 'UTF-8') {
+        if ($this->responseCharset !== 'UTF-8') {
             try {
-                $this->body = StringUtil::convertEncoding($this->getCharset(), 'UTF-8', $this->body);
+                $this->body = StringUtil::convertEncoding($this->responseCharset, 'UTF-8', $this->body);
             } catch (Exception $e) {
                 throw new ParsingFailed(
                     "Could not parse body, due an invalid charset.",
@@ -141,7 +148,7 @@ final class UnfurlResponse
         }
     }
 
-    private function getCharset(): string
+    private function validateHeaders(): void
     {
         $headers = $this->response->getHeader('content-type');
         if (\count($headers) !== 1) {
@@ -153,6 +160,7 @@ final class UnfurlResponse
         if ($contentType !== 'text/html') {
             throw new ParsingFailed("Expected 'text/html' as the 'content-type'.");
         }
+
         $charset = null;
         foreach ($pieces as $parameter) {
             $parts = ArrayUtil::trim(\explode('=', $parameter, 2));
@@ -167,11 +175,9 @@ final class UnfurlResponse
             }
         }
 
-        if (!$charset) {
-            $charset = 'UTF-8';
+        if ($charset) {
+            $this->responseCharset = \mb_strtoupper($charset);
         }
-
-        return \mb_strtoupper($charset);
     }
 
     /**