relative_link_filter.rb 4.9 KB
Newer Older
1 2
# frozen_string_literal: true

3 4
require 'uri'

5 6
module Banzai
  module Filter
7
    # HTML filter that "fixes" relative links to uploads or files in a repository.
8 9 10
    #
    # Context options:
    #   :commit
11
    #   :group
12 13 14
    #   :project
    #   :project_wiki
    #   :ref
15
    #   :requested_path
16
    class RelativeLinkFilter < HTML::Pipeline::Filter
17
      include Gitlab::Utils::StrongMemoize
18

19
      def call
20 21
        return doc if context[:system_note]

22
        @uri_types = {}
23
        clear_memoization(:linkable_files)
24

25
        doc.search('a:not(.gfm)').each do |el|
26 27 28
          process_link_attr el.attribute('href')
        end

29
        doc.css('img, video').each do |el|
30
          process_link_attr el.attribute('src')
31
          process_link_attr el.attribute('data-src')
32 33 34 35 36 37 38
        end

        doc
      end

      protected

39
      def linkable_files?
40 41 42
        strong_memoize(:linkable_files) do
          context[:project_wiki].nil? && repository.try(:exists?) && !repository.empty?
        end
43 44
      end

45 46
      def process_link_attr(html_attr)
        return if html_attr.blank?
47
        return if html_attr.value.start_with?('//')
48

49 50 51 52 53 54 55 56
        if html_attr.value.start_with?('/uploads/')
          process_link_to_upload_attr(html_attr)
        elsif linkable_files?
          process_link_to_repository_attr(html_attr)
        end
      end

      def process_link_to_upload_attr(html_attr)
57
        path_parts = [Addressable::URI.unescape(html_attr.value)]
58

59
        if project
60
          path_parts.unshift(relative_url_root, project.full_path)
61 62
        elsif group
          path_parts.unshift(relative_url_root, 'groups', group.full_path, '-')
63 64
        else
          path_parts.unshift(relative_url_root)
65 66
        end

67 68 69 70 71
        begin
          path = Addressable::URI.escape(File.join(*path_parts))
        rescue Addressable::URI::InvalidURIError
          return
        end
72 73 74 75 76

        html_attr.value =
          if context[:only_path]
            path
          else
77
            Addressable::URI.join(Gitlab.config.gitlab.base_url, path).to_s
78
          end
79 80 81
      end

      def process_link_to_repository_attr(html_attr)
82 83 84 85
        uri = URI(html_attr.value)
        if uri.relative? && uri.path.present?
          html_attr.value = rebuild_relative_uri(uri).to_s
        end
86
      rescue URI::Error, Addressable::URI::InvalidURIError
87
        # noop
88 89 90
      end

      def rebuild_relative_uri(uri)
91
        file_path = relative_file_path(uri)
92 93 94

        uri.path = [
          relative_url_root,
95
          project.full_path,
96
          uri_type(file_path),
97
          Addressable::URI.escape(ref).gsub('#', '%23'),
98
          Addressable::URI.escape(file_path)
99 100 101 102 103
        ].compact.join('/').squeeze('/').chomp('/')

        uri
      end

104
      def relative_file_path(uri)
105
        path = Addressable::URI.unescape(uri.path).delete("\0")
106 107
        request_path = Addressable::URI.unescape(context[:requested_path])
        nested_path = build_relative_path(path, request_path)
108 109 110
        file_exists?(nested_path) ? nested_path : path
      end

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
      # Convert a relative path into its correct location based on the currently
      # requested path
      #
      # path         - Relative path String
      # request_path - Currently-requested path String
      #
      # Examples:
      #
      #   # File in the same directory as the current path
      #   build_relative_path("users.md", "doc/api/README.md")
      #   # => "doc/api/users.md"
      #
      #   # File in the same directory, which is also the current path
      #   build_relative_path("users.md", "doc/api")
      #   # => "doc/api/users.md"
      #
      #   # Going up one level to a different directory
      #   build_relative_path("../update/7.14-to-8.0.md", "doc/api/README.md")
      #   # => "doc/update/7.14-to-8.0.md"
      #
      # Returns a String
      def build_relative_path(path, request_path)
133 134
        return request_path if path.empty?
        return path unless request_path
135
        return path[1..-1] if path.start_with?('/')
136 137

        parts = request_path.split('/')
138
        parts.pop if uri_type(request_path) != :tree
139

140
        path.sub!(%r{\A\./}, '')
141

142
        while path.start_with?('../')
143 144 145 146
          parts.pop
          path.sub!('../', '')
        end

147 148 149 150
        parts.push(path).join('/')
      end

      def file_exists?(path)
151
        path.present? && !!uri_type(path)
152 153
      end

154
      def uri_type(path)
155
        # https://gitlab.com/gitlab-org/gitlab-ce/issues/58657
156 157 158
        Gitlab::GitalyClient.allow_n_plus_1_calls do
          @uri_types[path] ||= current_commit.uri_type(path)
        end
159 160
      end

161
      def current_commit
162
        @current_commit ||= context[:commit] || repository.commit(ref)
163 164 165 166 167 168
      end

      def relative_url_root
        Gitlab.config.gitlab.relative_url_root.presence || '/'
      end

169
      def ref
170 171 172 173 174 175 176 177 178
        context[:ref] || project.default_branch
      end

      def group
        context[:group]
      end

      def project
        context[:project]
179 180 181
      end

      def repository
182
        @repository ||= project&.repository
183 184 185 186
      end
    end
  end
end