diff options
Diffstat (limited to 'lib/banzai')
-rw-r--r-- | lib/banzai/color_parser.rb | 20 | ||||
-rw-r--r-- | lib/banzai/filter/ascii_doc_sanitization_filter.rb | 2 | ||||
-rw-r--r-- | lib/banzai/filter/attributes_filter.rb | 6 | ||||
-rw-r--r-- | lib/banzai/filter/autolink_filter.rb | 2 | ||||
-rw-r--r-- | lib/banzai/filter/blockquote_fence_filter.rb | 2 | ||||
-rw-r--r-- | lib/banzai/filter/footnote_filter.rb | 4 | ||||
-rw-r--r-- | lib/banzai/filter/gollum_tags_filter.rb | 4 | ||||
-rw-r--r-- | lib/banzai/filter/inline_observability_filter.rb | 63 | ||||
-rw-r--r-- | lib/banzai/filter/markdown_post_escape_filter.rb | 6 | ||||
-rw-r--r-- | lib/banzai/filter/markdown_pre_escape_filter.rb | 2 | ||||
-rw-r--r-- | lib/banzai/filter/math_filter.rb | 14 | ||||
-rw-r--r-- | lib/banzai/filter/references/abstract_reference_filter.rb | 2 | ||||
-rw-r--r-- | lib/banzai/filter/references/reference_filter.rb | 1 | ||||
-rw-r--r-- | lib/banzai/filter/sanitization_filter.rb | 2 | ||||
-rw-r--r-- | lib/banzai/filter/task_list_filter.rb | 4 | ||||
-rw-r--r-- | lib/banzai/pipeline/gfm_pipeline.rb | 3 |
16 files changed, 42 insertions, 95 deletions
diff --git a/lib/banzai/color_parser.rb b/lib/banzai/color_parser.rb index cce79e73d2d..6d01d51955c 100644 --- a/lib/banzai/color_parser.rb +++ b/lib/banzai/color_parser.rb @@ -2,13 +2,13 @@ module Banzai module ColorParser - ALPHA = /0(?:\.\d+)?|\.\d+|1(?:\.0+)?/.freeze # 0.0..1.0 - PERCENTS = /(?:\d{1,2}|100)%/.freeze # 00%..100% - ALPHA_CHANNEL = /(?:,\s*(?:#{ALPHA}|#{PERCENTS}))?/.freeze - BITS = /\d{1,2}|1\d\d|2(?:[0-4]\d|5[0-5])/.freeze # 00..255 - DEGS = /-?\d+(?:deg)?/i.freeze # [-]digits[deg] - RADS = /-?(?:\d+(?:\.\d+)?|\.\d+)rad/i.freeze # [-](digits[.digits] OR .digits)rad - HEX_FORMAT = /\#(?:\h{3}|\h{4}|\h{6}|\h{8})/.freeze + ALPHA = /0(?:\.\d+)?|\.\d+|1(?:\.0+)?/ # 0.0..1.0 + PERCENTS = /(?:\d{1,2}|100)%/ # 00%..100% + ALPHA_CHANNEL = /(?:,\s*(?:#{ALPHA}|#{PERCENTS}))?/ + BITS = /\d{1,2}|1\d\d|2(?:[0-4]\d|5[0-5])/ # 00..255 + DEGS = /-?\d+(?:deg)?/i # [-]digits[deg] + RADS = /-?(?:\d+(?:\.\d+)?|\.\d+)rad/i # [-](digits[.digits] OR .digits)rad + HEX_FORMAT = /\#(?:\h{3}|\h{4}|\h{6}|\h{8})/ RGB_FORMAT = %r{ (?:rgba? \( @@ -20,7 +20,7 @@ module Banzai #{ALPHA_CHANNEL} \) ) - }xi.freeze + }xi HSL_FORMAT = %r{ (?:hsla? \( @@ -28,11 +28,11 @@ module Banzai #{ALPHA_CHANNEL} \) ) - }xi.freeze + }xi FORMATS = [HEX_FORMAT, RGB_FORMAT, HSL_FORMAT].freeze - COLOR_FORMAT = /\A(#{Regexp.union(FORMATS)})\z/ix.freeze + COLOR_FORMAT = /\A(#{Regexp.union(FORMATS)})\z/ix # Public: Analyzes whether the String is a color code. # diff --git a/lib/banzai/filter/ascii_doc_sanitization_filter.rb b/lib/banzai/filter/ascii_doc_sanitization_filter.rb index 4158aa8a5ec..3d425b9795f 100644 --- a/lib/banzai/filter/ascii_doc_sanitization_filter.rb +++ b/lib/banzai/filter/ascii_doc_sanitization_filter.rb @@ -7,7 +7,7 @@ module Banzai # Extends Banzai::Filter::BaseSanitizationFilter with specific rules. class AsciiDocSanitizationFilter < Banzai::Filter::BaseSanitizationFilter # Anchor link prefixed by "user-content-" pattern - PREFIXED_ID_PATTERN = /\A#{Gitlab::Asciidoc::DEFAULT_ADOC_ATTRS['idprefix']}(:?[[:alnum:]]|-|_)+\z/.freeze + PREFIXED_ID_PATTERN = /\A#{Gitlab::Asciidoc::DEFAULT_ADOC_ATTRS['idprefix']}(:?[[:alnum:]]|-|_)+\z/ SECTION_HEADINGS = %w[h2 h3 h4 h5 h6].freeze # Footnote link patterns diff --git a/lib/banzai/filter/attributes_filter.rb b/lib/banzai/filter/attributes_filter.rb index ab50b3d6858..98b0ed8cc22 100644 --- a/lib/banzai/filter/attributes_filter.rb +++ b/lib/banzai/filter/attributes_filter.rb @@ -16,9 +16,9 @@ module Banzai CSS = 'img' XPATH = Gitlab::Utils::Nokogiri.css_to_xpath(CSS).freeze - ATTRIBUTES_PATTERN = %r{\A(?<matched>\{(?<attributes>.{1,100})\})}.freeze - WIDTH_HEIGHT_REGEX = %r{\A(?<name>height|width)="?(?<size>[\w%]{1,10})"?\z}.freeze - VALID_SIZE_REGEX = %r{\A\d{1,4}(%|px)?\z}.freeze + ATTRIBUTES_PATTERN = %r{\A(?<matched>\{(?<attributes>.{1,100})\})} + WIDTH_HEIGHT_REGEX = %r{\A(?<name>height|width)="?(?<size>[\w%]{1,10})"?\z} + VALID_SIZE_REGEX = %r{\A\d{1,4}(%|px)?\z} def call doc.xpath(XPATH).each do |img| diff --git a/lib/banzai/filter/autolink_filter.rb b/lib/banzai/filter/autolink_filter.rb index e877e5f316c..87e2d94af94 100644 --- a/lib/banzai/filter/autolink_filter.rb +++ b/lib/banzai/filter/autolink_filter.rb @@ -36,7 +36,7 @@ module Banzai # Rubular: http://rubular.com/r/nrL3r9yUiq # Note that it's not possible to use Gitlab::UntrustedRegexp for LINK_PATTERN, # as `(?<!` is unsupported in `re2`, see https://github.com/google/re2/wiki/Syntax - LINK_PATTERN = %r{([a-z][a-z0-9\+\.-]+://[^\s>]+)(?<!\?|!|\.|,|:)}.freeze + LINK_PATTERN = %r{([a-z][a-z0-9\+\.-]+://[^\s>]+)(?<!\?|!|\.|,|:)} ENTITY_UNTRUSTED = '((?:&[\w#]+;)+)\z' ENTITY_UNTRUSTED_REGEX = Gitlab::UntrustedRegexp.new(ENTITY_UNTRUSTED, multiline: false) diff --git a/lib/banzai/filter/blockquote_fence_filter.rb b/lib/banzai/filter/blockquote_fence_filter.rb index d4ff7d4c6b5..34b9fd63b1c 100644 --- a/lib/banzai/filter/blockquote_fence_filter.rb +++ b/lib/banzai/filter/blockquote_fence_filter.rb @@ -32,7 +32,7 @@ module Banzai ) \n\ *>>>\ *(?=\n$|\z) ) - }mx.freeze + }mx def initialize(text, context = nil, result = nil) super text, context, result diff --git a/lib/banzai/filter/footnote_filter.rb b/lib/banzai/filter/footnote_filter.rb index f10efdccdf1..ada74d613f9 100644 --- a/lib/banzai/filter/footnote_filter.rb +++ b/lib/banzai/filter/footnote_filter.rb @@ -19,8 +19,8 @@ module Banzai class FootnoteFilter < HTML::Pipeline::Filter FOOTNOTE_ID_PREFIX = 'fn-' FOOTNOTE_LINK_ID_PREFIX = 'fnref-' - FOOTNOTE_LI_REFERENCE_PATTERN = /\A#{FOOTNOTE_ID_PREFIX}.+\z/.freeze - FOOTNOTE_LINK_REFERENCE_PATTERN = /\A#{FOOTNOTE_LINK_ID_PREFIX}.+\z/.freeze + FOOTNOTE_LI_REFERENCE_PATTERN = /\A#{FOOTNOTE_ID_PREFIX}.+\z/ + FOOTNOTE_LINK_REFERENCE_PATTERN = /\A#{FOOTNOTE_LINK_ID_PREFIX}.+\z/ CSS_SECTION = "section[data-footnotes]" XPATH_SECTION = Gitlab::Utils::Nokogiri.css_to_xpath(CSS_SECTION).freeze diff --git a/lib/banzai/filter/gollum_tags_filter.rb b/lib/banzai/filter/gollum_tags_filter.rb index ade4f82e54b..9c3ad4c6a0c 100644 --- a/lib/banzai/filter/gollum_tags_filter.rb +++ b/lib/banzai/filter/gollum_tags_filter.rb @@ -51,10 +51,10 @@ module Banzai # See https://github.com/gollum/gollum/wiki # # Rubular: http://rubular.com/r/7dQnE5CUCH - TAGS_PATTERN = /\[\[(.+?)\]\]/.freeze + TAGS_PATTERN = /\[\[(.+?)\]\]/ # Pattern to match allowed image extensions - ALLOWED_IMAGE_EXTENSIONS = /.+(jpg|png|gif|svg|bmp)\z/i.freeze + ALLOWED_IMAGE_EXTENSIONS = /.+(jpg|png|gif|svg|bmp)\z/i # Do not perform linking inside these tags. IGNORED_ANCESTOR_TAGS = %w[pre code tt].to_set diff --git a/lib/banzai/filter/inline_observability_filter.rb b/lib/banzai/filter/inline_observability_filter.rb deleted file mode 100644 index 8e38f689959..00000000000 --- a/lib/banzai/filter/inline_observability_filter.rb +++ /dev/null @@ -1,63 +0,0 @@ -# frozen_string_literal: true - -module Banzai - module Filter - class InlineObservabilityFilter < HTML::Pipeline::Filter - include Gitlab::Utils::StrongMemoize - - def call - return doc unless Gitlab::Observability.enabled?(group) - - doc.xpath(xpath_search).each do |node| - next unless element = element_to_embed(node) - - # We want this to follow any surrounding content. For example, - # if a link is inline in a paragraph. - node.parent.children.last.add_next_sibling(element) - end - - doc - end - - # Placeholder element for the frontend to use as an - # injection point for observability. - def create_element(url) - doc.document.create_element( - 'div', - class: 'js-render-observability', - 'data-frame-url': url - ) - end - - # Search params for selecting observability links. - def xpath_search - "descendant-or-self::a[starts-with(@href, '#{gitlab_domain}/groups/') and contains(@href,'/-/observability/')]" - end - - # Creates a new element based on the parameters - # obtained from the target link - def element_to_embed(node) - url = node['href'] - - embeddable_url = extract_embeddable_url(url) - create_element(embeddable_url) if embeddable_url - end - - private - - def extract_embeddable_url(url) - strong_memoize_with(:embeddable_url, url) do - Gitlab::Observability.embeddable_url(url) - end - end - - def group - context[:group] || context[:project]&.group - end - - def gitlab_domain - ::Gitlab.config.gitlab.url - end - end - end -end diff --git a/lib/banzai/filter/markdown_post_escape_filter.rb b/lib/banzai/filter/markdown_post_escape_filter.rb index 4d37fba33aa..90b9555df1d 100644 --- a/lib/banzai/filter/markdown_post_escape_filter.rb +++ b/lib/banzai/filter/markdown_post_escape_filter.rb @@ -5,9 +5,9 @@ module Banzai # See comments in MarkdownPreEscapeFilter for details on strategy class MarkdownPostEscapeFilter < HTML::Pipeline::Filter LITERAL_KEYWORD = MarkdownPreEscapeFilter::LITERAL_KEYWORD - LITERAL_REGEX = %r{#{LITERAL_KEYWORD}-(.*?)-#{LITERAL_KEYWORD}}.freeze - NOT_LITERAL_REGEX = %r{#{LITERAL_KEYWORD}-((%5C|\\).+?)-#{LITERAL_KEYWORD}}.freeze - SPAN_REGEX = %r{<span data-escaped-char>(.*?)</span>}.freeze + LITERAL_REGEX = %r{#{LITERAL_KEYWORD}-(.*?)-#{LITERAL_KEYWORD}} + NOT_LITERAL_REGEX = %r{#{LITERAL_KEYWORD}-((%5C|\\).+?)-#{LITERAL_KEYWORD}} + SPAN_REGEX = %r{<span data-escaped-char>(.*?)</span>} XPATH_A = Gitlab::Utils::Nokogiri.css_to_xpath('a').freeze XPATH_LANG_TAG = Gitlab::Utils::Nokogiri.css_to_xpath('pre').freeze diff --git a/lib/banzai/filter/markdown_pre_escape_filter.rb b/lib/banzai/filter/markdown_pre_escape_filter.rb index 8cc7b0defd6..b6f063ece57 100644 --- a/lib/banzai/filter/markdown_pre_escape_filter.rb +++ b/lib/banzai/filter/markdown_pre_escape_filter.rb @@ -57,7 +57,7 @@ module Banzai ].freeze TARGET_CHARS = ESCAPABLE_CHARS.pluck(:char).join.freeze - ASCII_PUNCTUATION = %r{(\\[#{TARGET_CHARS}])}.freeze + ASCII_PUNCTUATION = %r{(\\[#{TARGET_CHARS}])} LITERAL_KEYWORD = 'cmliteral' def call diff --git a/lib/banzai/filter/math_filter.rb b/lib/banzai/filter/math_filter.rb index e568f51652f..3161e030194 100644 --- a/lib/banzai/filter/math_filter.rb +++ b/lib/banzai/filter/math_filter.rb @@ -47,7 +47,7 @@ module Banzai # Add necessary classes to any existing math blocks def process_existing doc.xpath(XPATH_INLINE_CODE).each do |code| - break if @nodes_count >= RENDER_NODES_LIMIT + break if render_nodes_limit_reached?(@nodes_count) code[:class] = MATH_CLASSES @@ -58,7 +58,7 @@ module Banzai # Corresponds to the "$`...`$" syntax def process_dollar_backtick_inline doc.xpath(XPATH_CODE).each do |code| - break if @nodes_count >= RENDER_NODES_LIMIT + break if render_nodes_limit_reached?(@nodes_count) closing = code.next opening = code.previous @@ -87,6 +87,16 @@ module Banzai pre_node[:class] = TAG_CLASS end end + + def settings + Gitlab::CurrentSettings.current_application_settings + end + + def render_nodes_limit_reached?(count) + return false unless settings.math_rendering_limits_enabled? + + count >= RENDER_NODES_LIMIT + end end end end diff --git a/lib/banzai/filter/references/abstract_reference_filter.rb b/lib/banzai/filter/references/abstract_reference_filter.rb index 3e48fe33b03..c3c5103106b 100644 --- a/lib/banzai/filter/references/abstract_reference_filter.rb +++ b/lib/banzai/filter/references/abstract_reference_filter.rb @@ -20,7 +20,7 @@ module Banzai # transitory value (it never gets saved) we can initialize once, and it # doesn't matter if it changes on a restart. REFERENCE_PLACEHOLDER = "_reference_#{SecureRandom.hex(16)}_" - REFERENCE_PLACEHOLDER_PATTERN = %r{#{REFERENCE_PLACEHOLDER}(\d+)}.freeze + REFERENCE_PLACEHOLDER_PATTERN = %r{#{REFERENCE_PLACEHOLDER}(\d+)} # Public: Find references in text (like `!123` for merge requests) # diff --git a/lib/banzai/filter/references/reference_filter.rb b/lib/banzai/filter/references/reference_filter.rb index caec808ef04..e7fa287ae06 100644 --- a/lib/banzai/filter/references/reference_filter.rb +++ b/lib/banzai/filter/references/reference_filter.rb @@ -153,6 +153,7 @@ module Banzai @ignore_ancestor_query ||= begin parents = %w[pre code a style] parents << 'blockquote' if context[:ignore_blockquotes] + parents << 'span[contains(concat(" ", @class, " "), " idiff ")]' parents.map { |n| "ancestor::#{n}" }.join(' or ') end diff --git a/lib/banzai/filter/sanitization_filter.rb b/lib/banzai/filter/sanitization_filter.rb index 15013c8595e..f33bc9cd621 100644 --- a/lib/banzai/filter/sanitization_filter.rb +++ b/lib/banzai/filter/sanitization_filter.rb @@ -7,7 +7,7 @@ module Banzai # Extends Banzai::Filter::BaseSanitizationFilter with specific rules. class SanitizationFilter < Banzai::Filter::BaseSanitizationFilter # Styles used by Markdown for table alignment - TABLE_ALIGNMENT_PATTERN = /text-align: (?<alignment>center|left|right)/.freeze + TABLE_ALIGNMENT_PATTERN = /text-align: (?<alignment>center|left|right)/ def customize_allowlist(allowlist) allowlist[:allow_comments] = context[:allow_comments] diff --git a/lib/banzai/filter/task_list_filter.rb b/lib/banzai/filter/task_list_filter.rb index e8a7677b102..4f39a25ff68 100644 --- a/lib/banzai/filter/task_list_filter.rb +++ b/lib/banzai/filter/task_list_filter.rb @@ -32,7 +32,7 @@ module Banzai XPATH = 'descendant-or-self::li[input[@data-inapplicable]] | descendant-or-self::li[p[input[@data-inapplicable]]]' INAPPLICABLE = '[~]' - INAPPLICABLEPATTERN = /\[~\]/.freeze + INAPPLICABLEPATTERN = /\[~\]/ # Pattern used to identify all task list items. # Useful when you need iterate over all items. @@ -46,7 +46,7 @@ module Banzai #{INAPPLICABLEPATTERN} ) (?=\s) # followed by whitespace - /x.freeze + /x # Force the gem's constant to use our new one superclass.send(:remove_const, :ItemPattern) # rubocop: disable GitlabSecurity/PublicSend diff --git a/lib/banzai/pipeline/gfm_pipeline.rb b/lib/banzai/pipeline/gfm_pipeline.rb index 1d6269c704d..1b3905f0dde 100644 --- a/lib/banzai/pipeline/gfm_pipeline.rb +++ b/lib/banzai/pipeline/gfm_pipeline.rb @@ -32,12 +32,11 @@ module Banzai Filter::ExternalLinkFilter, Filter::SuggestionFilter, Filter::FootnoteFilter, + Filter::InlineDiffFilter, *reference_filters, Filter::EmojiFilter, Filter::CustomEmojiFilter, Filter::TaskListFilter, - Filter::InlineDiffFilter, - Filter::InlineObservabilityFilter, Filter::SetDirectionFilter, Filter::SyntaxHighlightFilter # this filter should remain at the end ] |