Skip to content

Commit

Permalink
SEC: Dolibarr#28168 Correct protocol limitations (PHP7.4/Win)
Browse files Browse the repository at this point in the history
# SEC: Dolibarr#28168 Correct protocol limitations (PHP7.4/Win)

Protocol limitation was not active during test on windows platform.
Moving the application of the limitation just before the curl_exec
instruction made the limitation effective.

Also extended the code to enable allowing ftp and ftps and extended
the code for [CURLOPT_REDIR_PROTOCOLS_STR](https://www.php.net/manual/en/curl.constants.php#constant.curlopt-redir-protocols-str).
  • Loading branch information
mdeweerd committed Feb 14, 2024
1 parent a6ef430 commit 5ec2c17
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
23 changes: 19 additions & 4 deletions htdocs/core/lib/geturl.lib.php
@@ -1,5 +1,6 @@
<?php
/* Copyright (C) 2008-2020 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -91,17 +92,23 @@ function getURLContent($url, $postorget = 'GET', $param = '', $followlocation =

// Restrict use to some protocols only
$protocols = 0;
$redir_list = array();
if (is_array($allowedschemes)) {
foreach ($allowedschemes as $allowedscheme) {
if ($allowedscheme == 'http') {
$protocols |= CURLPROTO_HTTP;
}
if ($allowedscheme == 'https') {
$redir_list["HTTP"] = 1;
} elseif ($allowedscheme == 'https') {
$protocols |= CURLPROTO_HTTPS;
$redir_list["HTTPS"] = 1;
} elseif ($allowedscheme == 'ftp') {
$protocols |= CURLPROTO_FTP;
$redir_list["FTP"] = 1;
} elseif ($allowedscheme == 'ftps') {
$protocols |= CURLPROTO_FTPS;
$redir_list["FTPS"] = 1;
}
}
curl_setopt($ch, CURLOPT_PROTOCOLS, $protocols);
curl_setopt($ch, CURLOPT_REDIR_PROTOCOLS, $protocols);
}

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, !getDolGlobalString('MAIN_USE_CONNECT_TIMEOUT') ? 5 : $conf->global->MAIN_USE_CONNECT_TIMEOUT);
Expand Down Expand Up @@ -219,6 +226,14 @@ function getURLContent($url, $postorget = 'GET', $param = '', $followlocation =
}
}

// Moving these just before the curl_exec option really limits
// on windows PHP 7.4.
curl_setopt($ch, CURLOPT_PROTOCOLS, $protocols);
curl_setopt($ch, CURLOPT_REDIR_PROTOCOLS, $protocols);
if (version_compare(PHP_VERSION, '8.3.0', '>=') && version_compare(curl_version()['version'], '7.85.0', '>=')) {
curl_setopt($ch, CURLOPT_REDIR_PROTOCOLS_STR, implode(",", array_keys($redir_list)));

Check failure on line 234 in htdocs/core/lib/geturl.lib.php

View workflow job for this annotation

GitHub Actions / php-stan (8.2)

Constant CURLOPT_REDIR_PROTOCOLS_STR not found.
}

// Getting response from server
$response = curl_exec($ch);

Expand Down
2 changes: 1 addition & 1 deletion test/phpunit/SecurityTest.php
Expand Up @@ -918,7 +918,7 @@ public function testGetURLContent()
$url = 'ftp://mydomain.com';
$tmp = getURLContent($url);
print __METHOD__." url=".$url."\n";
$this->assertGreaterThan(0, strpos($tmp['curl_error_msg'], 'not supported')); // Test error if return does not contains 'not supported'
$this->assertTrue(false !== preg_match("/not supported/", $tmp['curl_error_msg']), "Did not get 'not supported' in {$tmp['curl_error_msg']}"); // Test error if return does not contains 'not supported'

$url = 'https://www.dolibarr.fr'; // This is a redirect 301 page
$tmp = getURLContent($url, 'GET', '', 0); // We do NOT follow
Expand Down

0 comments on commit 5ec2c17

Please sign in to comment.