[wp-trac] [WordPress Trac] #37494: Proxy authentication is ignored by Requests (was: Wordpress 4.6 Beta 4: Proxy setting not work)
WordPress Trac
noreply at wordpress.org
Thu Jul 28 10:09:03 UTC 2016
#37494: Proxy authentication is ignored by Requests
------------------------------+--------------------
Reporter: francescobagnoli | Owner:
Type: defect (bug) | Status: new
Priority: normal | Milestone: 4.6
Component: HTTP API | Version: trunk
Severity: normal | Resolution:
Keywords: has-patch | Focuses:
------------------------------+--------------------
Changes (by ocean90):
* keywords: => has-patch
* milestone: Awaiting Review => 4.6
Old description:
> in wp-config.php
>
> {{{
> define('WP_PROXY_HOST', '172.16.XXX.XXX');
> define('WP_PROXY_PORT', '3128');
> define('WP_PROXY_USERNAME', 'XXX');
> define('WP_PROXY_PASSWORD', 'XXX');
> }}}
>
> in function.php
>
> {{{
> $headers['X-Forwarded-For'] = $_SERVER['REMOTE_ADDR'];
> $response = wp_remote_get('http://example.com/', array('headers' =>
> $headers));
> }}}
>
> the proxy server raise an exception on authentication failed.
>
> debugging i see in Requests_Proxy_HTTP class, $use_authentication
> property is always false and in curl_before_send() method if
> $use_authentication is false curl_setopt CURLOPT_PROXYUSERPWD is never
> setted. If I force $use_authentication to be true, everything works.
>
> the fix for me is edit /wp-includes/class-http.php line 354:
>
> {{{#!php
> $options['proxy'] = new Requests_Proxy_HTTP( $proxy->host() . ':' .
> $proxy->port() );
> if ( $proxy->use_authentication() ) {
> $options['proxy']->user = $proxy->username();
> $options['proxy']->pass = $proxy->password();
> }
> }}}
>
> change to
>
> {{{#!php
> if ( $proxy->use_authentication() ) {
> $options['proxy']->user = $proxy->username();
> $options['proxy']->pass = $proxy->password();
>
> $options['proxy'] = new Requests_Proxy_HTTP( array($proxy->host() .
> ':' . $proxy->port(), $proxy->username(), $proxy->password()) );
> } else {
> $options['proxy'] = new Requests_Proxy_HTTP( $proxy->host() . ':' .
> $proxy->port() );
> }
> }}}
>
> This because Requests_Proxy_HTTP set $use_authentication property to
> true, only if Requests_Proxy_HTTP::__construct() has 3 parameters
>
> {{{#!php
> public function __construct($args = null) {
> if (is_string($args)) {
> $this->proxy = $args;
> }
> elseif (is_array($args)) {
> if (count($args) == 1) {
> list($this->proxy) = $args;
> }
> elseif (count($args) == 3) {
> list($this->proxy, $this->user, $this->pass) = $args;
> $this->use_authentication = true;
> }
> else {
> throw new Requests_Exception('Invalid number of arguments',
> 'proxyhttpbadargs');
> }
> }
> }
> }}}
New description:
in wp-config.php
{{{
define('WP_PROXY_HOST', '172.16.XXX.XXX');
define('WP_PROXY_PORT', '3128');
define('WP_PROXY_USERNAME', 'XXX');
define('WP_PROXY_PASSWORD', 'XXX');
}}}
in function.php
{{{
$headers['X-Forwarded-For'] = $_SERVER['REMOTE_ADDR'];
$response = wp_remote_get('http://example.com/', array('headers' =>
$headers));
}}}
the proxy server raise an exception on authentication failed.
debugging i see in `Requests_Proxy_HTTP` class, `$use_authentication`
property is always false and in `curl_before_send()` method if
`$use_authentication` is false `curl_setopt` `CURLOPT_PROXYUSERPWD` is
never set. If I force `$use_authentication` to be true, everything works.
the fix for me is edit /wp-includes/class-http.php line 354:
{{{#!php
$options['proxy'] = new Requests_Proxy_HTTP( $proxy->host() . ':' .
$proxy->port() );
if ( $proxy->use_authentication() ) {
$options['proxy']->user = $proxy->username();
$options['proxy']->pass = $proxy->password();
}
}}}
change to
{{{#!php
if ( $proxy->use_authentication() ) {
$options['proxy']->user = $proxy->username();
$options['proxy']->pass = $proxy->password();
$options['proxy'] = new Requests_Proxy_HTTP( array($proxy->host() .
':' . $proxy->port(), $proxy->username(), $proxy->password()) );
} else {
$options['proxy'] = new Requests_Proxy_HTTP( $proxy->host() . ':' .
$proxy->port() );
}
}}}
This because `Requests_Proxy_HTTP` set `$use_authentication` property to
true, only if `Requests_Proxy_HTTP::__construct()` has 3 parameters
{{{#!php
public function __construct($args = null) {
if (is_string($args)) {
$this->proxy = $args;
}
elseif (is_array($args)) {
if (count($args) == 1) {
list($this->proxy) = $args;
}
elseif (count($args) == 3) {
list($this->proxy, $this->user, $this->pass) = $args;
$this->use_authentication = true;
}
else {
throw new Requests_Exception('Invalid number of arguments',
'proxyhttpbadargs');
}
}
}
}}}
--
Comment:
Hello @francescobagnoli, welcome to our Trac!
Thanks for your detailed report, I've added [attachment:37494.patch] which
includes your suggested fix.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/37494#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list