Skip to content

Commit e2e07a3

Browse files
committed
Proposed fix for #2069
1 parent 59dea8c commit e2e07a3

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

src/PHPMailer.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1753,6 +1753,23 @@ protected static function isPermittedPath($path)
17531753
return !preg_match('#^[a-z]+://#i', $path);
17541754
}
17551755

1756+
/**
1757+
* Check whether a file path is safe, accessible, and readable.
1758+
*
1759+
* @param string $path A relative or absolute path to a file
1760+
*
1761+
* @return bool
1762+
*/
1763+
protected static function fileIsAccessible($path)
1764+
{
1765+
$readable = file_exists($path);
1766+
//If not a UNC path (expected to start with \\), check read permission, see #2069
1767+
if (strpos($path, '\\\\') !== 0) {
1768+
$readable = $readable && is_readable($path);
1769+
}
1770+
return static::isPermittedPath($path) && $readable;
1771+
}
1772+
17561773
/**
17571774
* Send mail using the PHP mail() function.
17581775
*
@@ -2141,7 +2158,7 @@ public function setLanguage($langcode = 'en', $lang_path = '')
21412158
// There is no English translation file
21422159
if ('en' !== $langcode) {
21432160
// Make sure language file path is readable
2144-
if (!static::isPermittedPath($lang_file) || !file_exists($lang_file)) {
2161+
if (!static::fileIsAccessible($lang_file)) {
21452162
$foundlang = false;
21462163
} else {
21472164
// Overwrite language-specific strings.
@@ -2970,7 +2987,7 @@ public function addAttachment(
29702987
$disposition = 'attachment'
29712988
) {
29722989
try {
2973-
if (!static::isPermittedPath($path) || !@is_file($path) || !is_readable($path)) {
2990+
if (!static::fileIsAccessible($path)) {
29742991
throw new Exception($this->lang('file_access') . $path, self::STOP_CONTINUE);
29752992
}
29762993

@@ -3144,7 +3161,7 @@ protected function attachAll($disposition_type, $boundary)
31443161
protected function encodeFile($path, $encoding = self::ENCODING_BASE64)
31453162
{
31463163
try {
3147-
if (!static::isPermittedPath($path) || !file_exists($path) || !is_readable($path)) {
3164+
if (!static::fileIsAccessible($path)) {
31483165
throw new Exception($this->lang('file_open') . $path, self::STOP_CONTINUE);
31493166
}
31503167
$file_buffer = file_get_contents($path);
@@ -3530,7 +3547,7 @@ public function addEmbeddedImage(
35303547
$disposition = 'inline'
35313548
) {
35323549
try {
3533-
if (!static::isPermittedPath($path) || !@is_file($path) || !is_readable($path)) {
3550+
if (!static::fileIsAccessible($path)) {
35343551
throw new Exception($this->lang('file_access') . $path, self::STOP_CONTINUE);
35353552
}
35363553

test/PHPMailerTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,6 +1476,9 @@ public function testAltBodyAttachment()
14761476
return;
14771477
}
14781478

1479+
//Test using non-existent UNC path
1480+
self::assertFalse($this->Mail->addAttachment('\\\\nowhere\nothing'));
1481+
14791482
$this->buildBody();
14801483
self::assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
14811484
}

0 commit comments

Comments
 (0)