Class: Import::ValidateRemoteGitEndpointService

Inherits:
Object
  • Object
show all
Defined in:
app/services/import/validate_remote_git_endpoint_service.rb

Constant Summary collapse

GIT_SERVICE_NAME =

Validates if the remote endpoint is a valid GIT repository Only smart protocol is supported Validation rules are taken from git-scm.com/docs/http-protocol#_smart_clients

"git-upload-pack"
GIT_EXPECTED_FIRST_PACKET_LINE =
"# service=#{GIT_SERVICE_NAME}"
GIT_BODY_MESSAGE_REGEXP =
/^[0-9a-f]{4}#{GIT_EXPECTED_FIRST_PACKET_LINE}/
GIT_PROTOCOL_PKT_LEN =
4
GIT_MINIMUM_RESPONSE_LENGTH =
GIT_PROTOCOL_PKT_LEN + GIT_EXPECTED_FIRST_PACKET_LINE.length
EXPECTED_CONTENT_TYPE =
"application/x-#{GIT_SERVICE_NAME}-advertisement"

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ ValidateRemoteGitEndpointService

Returns a new instance of ValidateRemoteGitEndpointService.



17
18
19
# File 'app/services/import/validate_remote_git_endpoint_service.rb', line 17

def initialize(params)
  @params = params
end

Instance Method Details

#executeObject



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
55
# File 'app/services/import/validate_remote_git_endpoint_service.rb', line 21

def execute
  uri = Gitlab::Utils.parse_url(@params[:url])

  if !uri || !uri.hostname || Project::VALID_IMPORT_PROTOCOLS.exclude?(uri.scheme)
    return ServiceResponse.error(message: "#{@params[:url]} is not a valid URL")
  end

  return ServiceResponse.success if uri.scheme == 'git'

  uri.fragment = nil
  url = Gitlab::Utils.append_path(uri.to_s, "/info/refs?service=#{GIT_SERVICE_NAME}")

  response_body = ''
  result = nil
  Gitlab::HTTP.try_get(url, stream_body: true, follow_redirects: false, basic_auth: auth) do |fragment|
    response_body += fragment
    next if response_body.length < GIT_MINIMUM_RESPONSE_LENGTH

    result = if status_code_is_valid(fragment) && content_type_is_valid(fragment) && response_body_is_valid(response_body)
               :success
             else
               :error
             end

    # We are interested only in the first chunks of the response
    # So we're using stream_body: true and breaking when receive enough body
    break
  end

  if result == :success
    ServiceResponse.success
  else
    ServiceResponse.error(message: "#{uri} is not a valid HTTP Git repository")
  end
end