forked from Katello/katello
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #16909 - properly escape user and pass in proxy auth
rest-client alongside URIs parse method introduces a bug where by usernames or passwords cannot have symbols in them. More Information: rest-client/rest-client#661 ManageIQ/manageiq#17318
- Loading branch information
1 parent
6720091
commit 750d247
Showing
3 changed files
with
110 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# | ||
# The Ruby URI parser doesn't decode the percent encoded characters in the URI, in particular it | ||
# doesn't decode the password which is frequently used when specifying proxy addresses and | ||
# authentication. For example, the following code: | ||
# | ||
# require 'uri' | ||
# proxy = URI.parse('http://myuser:%24%[email protected]:3128') | ||
# puts proxy.password | ||
# | ||
# Produces the following output: | ||
# | ||
# %24%3fxxxx | ||
# | ||
# But some gems, in particular `rest-client` and `kubeclient`, expect it to decode those characters, | ||
# as they use the value returned by the `password` method directly, and thus they fail to | ||
# authenticate against the proxy server when the password contains percent encoded characters. | ||
# | ||
# To address this issue this file adds a new `proxy` URI schema that almost identical to the `http` | ||
# schema, but that decodes the password before returning it. Users can use this schema instead of | ||
# `http` when they need to use percent encoded characters in the password. For example, the user | ||
# can type in the UI the following proxy URL: | ||
# | ||
# proxy://myuser:%24%[email protected]:3128 | ||
# | ||
# And the new schema will automatically translate `%24%3fxxxx` into `$?xxxx`. | ||
# This fix is derived from: https://github.com/ManageIQ/manageiq/pull/17318 | ||
require 'cgi' | ||
require 'uri' | ||
|
||
module URI | ||
class ProxyUri < HTTP | ||
def password | ||
value = super | ||
value = CGI.unescape(value) if value | ||
value | ||
end | ||
|
||
def user | ||
value = super | ||
value = CGI.unescape(value) if value | ||
value | ||
end | ||
end | ||
|
||
@@schemes['PROXY'] = ProxyUri | ||
end | ||
|
||
module URI | ||
class ProxysUri < HTTP | ||
def password | ||
value = super | ||
value = CGI.unescape(value) if value | ||
value | ||
end | ||
|
||
def user | ||
value = super | ||
value = CGI.unescape(value) if value | ||
value | ||
end | ||
end | ||
|
||
@@schemes['PROXYS'] = ProxysUri | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
require 'katello_test_helper' | ||
|
||
module Katello | ||
module Util | ||
class HttpProxyTest < ActiveSupport::TestCase | ||
include Katello::Util::HttpProxy | ||
|
||
def test_handles_no_username_test | ||
SETTINGS[:katello][:cdn_proxy] = { | ||
host: 'http://foobar.com', | ||
username: nil, | ||
password: nil | ||
} | ||
assert_equal 'proxy://foobar.com', proxy_uri | ||
end | ||
|
||
def test_properly_escapes_username | ||
SETTINGS[:katello][:cdn_proxy] = { | ||
host: 'http://foobar.com', | ||
user: 'red!hat', | ||
password: 'red@hat' | ||
} | ||
assert_equal 'proxy://red%21hat:red%[email protected]', proxy_uri | ||
|
||
uri = URI.parse(proxy_uri) | ||
assert_equal 'red!hat', uri.user | ||
assert_equal 'red@hat', uri.password | ||
end | ||
end | ||
end | ||
end |