https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112017-03-14T22:57:29ZRuby Issue Tracking SystemRuby master - Bug #12642: Net::HTTP populates host header incorrectly when using an IPv6 Addresshttps://redmine.ruby-lang.org/issues/12642?journal_id=636022017-03-14T22:57:29Zandy (Andy Brody)
<ul><li><strong>File</strong> <i>ipv6_test.rb</i> added</li></ul> Ruby master - Bug #12642: Net::HTTP populates host header incorrectly when using an IPv6 Addresshttps://redmine.ruby-lang.org/issues/12642?journal_id=636032017-03-14T23:04:59Zandy (Andy Brody)
<ul><li><strong>File</strong> <a href="/attachments/6431">ipv6_test.rb</a> <a class="icon-only icon-download" title="Download" href="/attachments/download/6431/ipv6_test.rb">ipv6_test.rb</a> added</li></ul><a name="Missing-brackets-from-Host-header-for-IPv6-addresses"></a>
<h1 >Missing brackets from Host header for IPv6 addresses<a href="#Missing-brackets-from-Host-header-for-IPv6-addresses" class="wiki-anchor">¶</a></h1>
<p>This is a regression from Ruby 2.1. The bug appears to have been introduced in ruby 2.2.0 by this commit: <a href="https://github.com/ruby/ruby/commit/70a2eb63999265ff7e8d46d1f5b410c8ee3d30d7" class="external">https://github.com/ruby/ruby/commit/70a2eb63999265ff7e8d46d1f5b410c8ee3d30d7</a></p>
<p>The change was added in response to this bug: <a href="https://bugs.ruby-lang.org/issues/10054" class="external">https://bugs.ruby-lang.org/issues/10054</a></p>
<p>Attached is a test script demonstrating the bug "<code>ipv6_test.rb</code>".</p>
<p>Correct behavior (present in 2.0 from 2.0.0-p643 onward and in 2.1 from 2.1.6 onward) for sending a request to <code>http://[::1]:8080</code></p>
<pre><code>Host: [::1]:8080
</code></pre>
<p>Incorrect behavior (present in 2.2.0 through trunk)</p>
<pre><code>Host: ::1:8080
</code></pre>
<p>See also <a href="https://github.com/rest-client/rest-client/issues/583" class="external">https://github.com/rest-client/rest-client/issues/583</a></p>
<p>I've added tests for rest-client that show the affected Ruby versions. <code>Net::HTTP</code> support for IPv6 URIs was broken in 2.0 before 2.0.0-p643 and 2.1 before 2.1.6, so there is a narrow window of versions that work.</p>
<p>Full test output is here: <a href="https://travis-ci.org/rest-client/rest-client/builds/211090377" class="external">https://travis-ci.org/rest-client/rest-client/builds/211090377</a></p>
<a name="Broken-handling-of-explicit-IPv6-Host-header"></a>
<h1 >Broken handling of explicit IPv6 Host header<a href="#Broken-handling-of-explicit-IPv6-Host-header" class="wiki-anchor">¶</a></h1>
<p>There is a separate bug that makes it impossible to explicitly set an IPv6 host header in affected ruby versions. Because Net::HTTP slices the end of the provided Host header after the first <code>:</code>, an IPv6 Host header will be rendered invalid.</p>
<p>If you attempt to set <code>req['Host'] = '[::1]:8080'</code>, it will be rendered as <code>[</code> due to <a href="https://github.com/ruby/ruby/blob/v2_4_0/lib/net/http/generic_request.rb#L144" class="external">https://github.com/ruby/ruby/blob/v2_4_0/lib/net/http/generic_request.rb#L144</a></p>
<pre><code>$ ruby ipv6_test.rb --set-host
/versions/2.3.1/lib/ruby/2.3.0/uri/generic.rb:595:in `check_host': bad component(expected host component): [ (URI::InvalidComponentError)
from /versions/2.3.1/lib/ruby/2.3.0/uri/generic.rb:636:in `host='
from /versions/2.3.1/lib/ruby/2.3.0/net/http/generic_request.rb:151:in `update_uri'
from /versions/2.3.1/lib/ruby/2.3.0/net/http.rb:1493:in `begin_transport'
from /versions/2.3.1/lib/ruby/2.3.0/net/http.rb:1433:in `transport_request'
from /versions/2.3.1/lib/ruby/2.3.0/net/http.rb:1407:in `request'
from ipv6_test.rb:42:in `block in <main>'
from /versions/2.3.1/lib/ruby/2.3.0/net/http.rb:853:in `start'
</code></pre> Ruby master - Bug #12642: Net::HTTP populates host header incorrectly when using an IPv6 Addresshttps://redmine.ruby-lang.org/issues/12642?journal_id=636042017-03-14T23:05:08Zandy (Andy Brody)
<ul><li><strong>File</strong> deleted (<del><i>ipv6_test.rb</i></del>)</li></ul> Ruby master - Bug #12642: Net::HTTP populates host header incorrectly when using an IPv6 Addresshttps://redmine.ruby-lang.org/issues/12642?journal_id=642832017-04-17T07:36:39Znaruse (Yui NARUSE)naruse@airemix.jp
<ul><li><strong>Assignee</strong> set to <i>naruse (Yui NARUSE)</i></li></ul> Ruby master - Bug #12642: Net::HTTP populates host header incorrectly when using an IPv6 Addresshttps://redmine.ruby-lang.org/issues/12642?journal_id=645482017-04-28T13:45:42Zshyouhei (Shyouhei Urabe)shyouhei@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Assigned</i></li></ul> Ruby master - Bug #12642: Net::HTTP populates host header incorrectly when using an IPv6 Addresshttps://redmine.ruby-lang.org/issues/12642?journal_id=652512017-06-03T01:07:30Zanuraguniyal (anurag uniyal)anuraguniyal@gmail.com
<ul></ul><p>Is there a workaround for this? How should we connect to ipv6 servers?</p> Ruby master - Bug #12642: Net::HTTP populates host header incorrectly when using an IPv6 Addresshttps://redmine.ruby-lang.org/issues/12642?journal_id=652872017-06-05T21:32:03Zanuraguniyal (anurag uniyal)anuraguniyal@gmail.com
<ul></ul><p>Below monkey patching code works for me. Is there a better way?</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">module</span> <span class="nn">MonkeyPatch</span>
<span class="k">module</span> <span class="nn">Net</span>
<span class="k">module</span> <span class="nn">HTTP</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">module</span> <span class="nn">MonkeyPatch::Net::HTTP</span>
<span class="c1"># workaround for https://bugs.ruby-lang.org/issues/12642</span>
<span class="c1"># Ruby does not pass Host header with brackets if host is ipv6 address</span>
<span class="k">def</span> <span class="nf">addr_port</span>
<span class="n">addr</span> <span class="o">=</span> <span class="n">address</span><span class="p">()</span>
<span class="c1"># if it is ipv6 add brackets around it</span>
<span class="k">if</span> <span class="n">addr</span> <span class="o">=~</span> <span class="no">Resolv</span><span class="o">::</span><span class="no">IPv6</span><span class="o">::</span><span class="no">Regex</span>
<span class="n">addr</span> <span class="o">=</span> <span class="s2">"[</span><span class="si">#{</span><span class="n">addr</span><span class="si">}</span><span class="s2">]"</span>
<span class="k">end</span>
<span class="k">if</span> <span class="n">use_ssl?</span>
<span class="n">addr</span> <span class="o">+</span> <span class="p">(</span><span class="n">port</span> <span class="o">==</span> <span class="no">Net</span><span class="o">::</span><span class="no">HTTP</span><span class="p">.</span><span class="nf">https_default_port</span> <span class="p">?</span> <span class="s1">''</span> <span class="p">:</span> <span class="s2">":</span><span class="si">#{</span><span class="n">port</span><span class="p">()</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
<span class="k">else</span>
<span class="n">addr</span> <span class="o">+</span> <span class="p">(</span><span class="n">port</span> <span class="o">==</span> <span class="no">Net</span><span class="o">::</span><span class="no">HTTP</span><span class="p">.</span><span class="nf">http_default_port</span> <span class="p">?</span> <span class="s1">''</span> <span class="p">:</span> <span class="s2">":</span><span class="si">#{</span><span class="n">port</span><span class="p">()</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="no">Net</span><span class="o">::</span><span class="no">HTTP</span><span class="p">.</span><span class="nf">prepend</span><span class="p">(</span><span class="no">MonkeyPatch</span><span class="o">::</span><span class="no">Net</span><span class="o">::</span><span class="no">HTTP</span><span class="p">)</span>
</code></pre> Ruby master - Bug #12642: Net::HTTP populates host header incorrectly when using an IPv6 Addresshttps://redmine.ruby-lang.org/issues/12642?journal_id=676082017-10-26T15:29:47Znaruse (Yui NARUSE)naruse@airemix.jp
<ul><li><strong>Status</strong> changed from <i>Assigned</i> to <i>Closed</i></li></ul><p>Applied in changeset trunk|r60455.</p>
<hr>
<p>Host header should add branckets to IPv6 address [Bug <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: Net::HTTP populates host header incorrectly when using an IPv6 Address (Closed)" href="https://redmine.ruby-lang.org/issues/12642">#12642</a>]</p>