File: hostname_override_patch.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (54 lines) | stat: -rw-r--r-- 1,475 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# frozen_string_literal: true

# This override allows passing `@hostname_override` to the SNI protocol,
# which is used to lookup the correct SSL certificate in the
# request handshake process.
#
# Given we've forced the HTTP request to be sent to the resolved
# IP address in a few scenarios (e.g.: `Gitlab::HTTP_V2` through
# `UrlBlocker.validate!`), we need to provide the _original_
# hostname via SNI in order to have a clean connection setup.
#
# This is ultimately needed in order to avoid DNS rebinding attacks
# through HTTP requests.

require 'net/http'

class OpenSSL::SSL::SSLContext
  attr_accessor :hostname_override
end

class OpenSSL::SSL::SSLSocket
  module HostnameOverride
    # rubocop: disable Gitlab/ModuleWithInstanceVariables
    def hostname=(hostname)
      super(@context.hostname_override || hostname)
    end

    def post_connection_check(hostname)
      super(@context.hostname_override || hostname)
    end
    # rubocop: enable Gitlab/ModuleWithInstanceVariables
  end

  prepend HostnameOverride
end

class Net::HTTP
  attr_accessor :hostname_override

  SSL_IVNAMES << :@hostname_override
  SSL_ATTRIBUTES << :hostname_override

  module HostnameOverride
    def addr_port
      return super unless hostname_override

      addr = hostname_override
      default_port = use_ssl? ? Net::HTTP.https_default_port : Net::HTTP.http_default_port
      default_port == port ? addr : "#{addr}:#{port}"
    end
  end

  prepend HostnameOverride
end