瀏覽代碼

sanitizing email output to fix NULL in mail

Medowar 1 月之前
父節點
當前提交
54bdeea360
共有 1 個文件被更改,包括 24 次插入6 次删除
  1. 24 6
      src/mail/mimemailbuilder.php

+ 24 - 6
src/mail/mimemailbuilder.php

@@ -22,32 +22,32 @@ final class MimeMailBuilder
 
     public function setFrom(string $address, string $name = ''): self
     {
-        $this->from = $address;
-        $this->fromName = $name;
+        $this->from = $this->sanitizeAddress($address);
+        $this->fromName = $this->sanitizeHeaderText($name);
         return $this;
     }
 
     public function setTo(string $address): self
     {
-        $this->to = $address;
+        $this->to = $this->sanitizeAddress($address);
         return $this;
     }
 
     public function setSubject(string $subject): self
     {
-        $this->subject = $subject;
+        $this->subject = $this->sanitizeHeaderText($subject);
         return $this;
     }
 
     public function setHtmlBody(string $html): self
     {
-        $this->htmlBody = $html;
+        $this->htmlBody = $this->sanitizeBodyText($html);
         return $this;
     }
 
     public function setTextBody(string $text): self
     {
-        $this->textBody = $text;
+        $this->textBody = $this->sanitizeBodyText($text);
         return $this;
     }
 
@@ -173,6 +173,24 @@ final class MimeMailBuilder
 
     private function sanitizeFilename(string $name): string
     {
+        $name = $this->sanitizeHeaderText($name);
         return preg_replace('/[^\w.\-äöüÄÖÜß]+/u', '_', $name) ?: 'attachment';
     }
+
+    private function sanitizeAddress(string $value): string
+    {
+        $value = preg_replace('/[\x00-\x1F\x7F]+/', '', $value) ?? '';
+        return trim($value);
+    }
+
+    private function sanitizeHeaderText(string $value): string
+    {
+        $value = preg_replace('/[\x00-\x1F\x7F]+/', ' ', $value) ?? '';
+        return trim($value);
+    }
+
+    private function sanitizeBodyText(string $value): string
+    {
+        return preg_replace('/[\x00\x0B\x0C]/', '', $value) ?? '';
+    }
 }