|
| 1 | +From e45ae8f935f6f87b91929b2ba48b57e5ba174435 Mon Sep 17 00:00:00 2001 |
| 2 | +From: AllSpark <allspark@microsoft.com> |
| 3 | +Date: Thu, 2 Apr 2026 15:18:26 +0000 |
| 4 | +Subject: [PATCH] build_exclusive_url: Guard against protocol-relative URLs by |
| 5 | + normalising to relative path; update rubocop todo and add specs |
| 6 | + (GHSA-33mh-2634-fwr2) |
| 7 | + |
| 8 | +Signed-off-by: Azure Linux Security Servicing Account <azurelinux-security@microsoft.com> |
| 9 | +Upstream-reference: AI Backport of https://github.com/lostisland/faraday/commit/a6d3a3a0bf59c2ab307d0abd91bc126aef5561bc.patch |
| 10 | +--- |
| 11 | + .rubocop_todo.yml | 2 +- |
| 12 | + lib/faraday/connection.rb | 3 +++ |
| 13 | + spec/faraday/connection_spec.rb | 33 +++++++++++++++++++++++++++++++++ |
| 14 | + 3 files changed, 37 insertions(+), 1 deletion(-) |
| 15 | + |
| 16 | +diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml |
| 17 | +index fbec6de..3c75338 100644 |
| 18 | +--- a/.rubocop_todo.yml |
| 19 | ++++ b/.rubocop_todo.yml |
| 20 | +@@ -31,7 +31,7 @@ Metrics/AbcSize: |
| 21 | + # Offense count: 4 |
| 22 | + # Configuration parameters: CountComments, CountAsOne. |
| 23 | + Metrics/ClassLength: |
| 24 | +- Max: 230 |
| 25 | ++ Max: 235 |
| 26 | + |
| 27 | + # Offense count: 9 |
| 28 | + # Configuration parameters: AllowedMethods, AllowedPatterns, IgnoredMethods. |
| 29 | +diff --git a/lib/faraday/connection.rb b/lib/faraday/connection.rb |
| 30 | +index 1984f87..7056572 100644 |
| 31 | +--- a/lib/faraday/connection.rb |
| 32 | ++++ b/lib/faraday/connection.rb |
| 33 | +@@ -473,6 +473,9 @@ module Faraday |
| 34 | + if url && !base.path.end_with?('/') |
| 35 | + base.path = "#{base.path}/" # ensure trailing slash |
| 36 | + end |
| 37 | ++ # Ensure relative url will be parsed correctly (such as `service:search` or `//evil.com`) |
| 38 | ++ url = "./#{url}" if url.respond_to?(:start_with?) && |
| 39 | ++ (!url.start_with?('http://', 'https://', '/', './', '../') || url.start_with?('//')) |
| 40 | + url = url.to_s.gsub(':', '%3A') if URI.parse(url.to_s).opaque |
| 41 | + uri = url ? base + url : base |
| 42 | + if params |
| 43 | +diff --git a/spec/faraday/connection_spec.rb b/spec/faraday/connection_spec.rb |
| 44 | +index d4ccb23..51392f1 100644 |
| 45 | +--- a/spec/faraday/connection_spec.rb |
| 46 | ++++ b/spec/faraday/connection_spec.rb |
| 47 | +@@ -310,6 +310,39 @@ |
| 48 | + expect(uri.to_s).to eq('http://service.com/api/service%3Asearch?limit=400') |
| 49 | + end |
| 50 | + end |
| 51 | ++ context 'with protocol-relative URL (GHSA-33mh-2634-fwr2)' do |
| 52 | ++ it 'does not allow host override with //evil.com/path' do |
| 53 | ++ conn.url_prefix = 'http://httpbingo.org/api' |
| 54 | ++ uri = conn.build_exclusive_url('//evil.com/path') |
| 55 | ++ expect(uri.host).to eq('httpbingo.org') |
| 56 | ++ end |
| 57 | ++ |
| 58 | ++ it 'does not allow host override with //evil.com:8080/path' do |
| 59 | ++ conn.url_prefix = 'http://httpbingo.org/api' |
| 60 | ++ uri = conn.build_exclusive_url('//evil.com:8080/path') |
| 61 | ++ expect(uri.host).to eq('httpbingo.org') |
| 62 | ++ end |
| 63 | ++ |
| 64 | ++ it 'does not allow host override with //user:pass@evil.com/path' do |
| 65 | ++ conn.url_prefix = 'http://httpbingo.org/api' |
| 66 | ++ uri = conn.build_exclusive_url('//user:pass@evil.com/path') |
| 67 | ++ expect(uri.host).to eq('httpbingo.org') |
| 68 | ++ end |
| 69 | ++ |
| 70 | ++ it 'does not allow host override with ///evil.com' do |
| 71 | ++ conn.url_prefix = 'http://httpbingo.org/api' |
| 72 | ++ uri = conn.build_exclusive_url('///evil.com') |
| 73 | ++ expect(uri.host).to eq('httpbingo.org') |
| 74 | ++ end |
| 75 | ++ |
| 76 | ++ it 'still allows single-slash absolute paths' do |
| 77 | ++ conn.url_prefix = 'http://httpbingo.org/api' |
| 78 | ++ uri = conn.build_exclusive_url('/safe/path') |
| 79 | ++ expect(uri.host).to eq('httpbingo.org') |
| 80 | ++ expect(uri.path).to eq('/safe/path') |
| 81 | ++ end |
| 82 | ++ end |
| 83 | ++ |
| 84 | + |
| 85 | + context 'with a custom `default_uri_parser`' do |
| 86 | + let(:url) { 'http://httpbingo.org' } |
| 87 | +-- |
| 88 | +2.45.4 |
0 commit comments