Welcome to mirror list, hosted at ThFree Co, Russian Federation.

pathological_markdown_filter.rb « filter « banzai « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0f94150c7a1fdea31780b9e56ee9d2cac70d6445 (plain)
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
# frozen_string_literal: true

module Banzai
  module Filter
    class PathologicalMarkdownFilter < HTML::Pipeline::TextFilter
      # It's not necessary for this to be precise - we just need to detect
      # when there are a non-trivial number of unclosed image links.
      # So we don't really care about code blocks, etc.
      # See https://gitlab.com/gitlab-org/gitlab/-/issues/370428
      REGEX = /!\[(?:[^\]])+?!\[/.freeze
      DETECTION_MAX = 10

      def call
        count = 0

        @text.scan(REGEX) do |_match|
          count += 1
          break if count > DETECTION_MAX
        end

        return @text if count <= DETECTION_MAX

        "_Unable to render markdown - too many unclosed markdown image links detected._"
      end
    end
  end
end