diff --git a/lib/gitlab/untrusted_regexp.rb b/lib/gitlab/untrusted_regexp.rb index 006681654f315bf3a9be9048f99c81341ccd148a..95a1d02e87fcd2006f2fffe3e83b3ae7bd26c3d8 100644 --- a/lib/gitlab/untrusted_regexp.rb +++ b/lib/gitlab/untrusted_regexp.rb @@ -18,7 +18,7 @@ class UntrustedRegexp # recreate Ruby's \R metacharacter # https://ruby-doc.org/3.2.2/Regexp.html#class-Regexp-label-Character+Classes - BACKSLASH_R = '(\n|\v|\f|\r|\x{0085}|\x{2028}|\x{2029}|\r\n)' + BACKSLASH_R = '(\n|\v|\f|\r\n|\r|\x{0085}|\x{2028}|\x{2029})' delegate :===, :source, to: :regexp diff --git a/lib/gitlab/wiki_pages/front_matter_parser.rb b/lib/gitlab/wiki_pages/front_matter_parser.rb index 3fd62771a90bdf6d329b8b74729f9006a287d1e3..f796aa6ff0486c1a94d5d499d1d04707fdd460d2 100644 --- a/lib/gitlab/wiki_pages/front_matter_parser.rb +++ b/lib/gitlab/wiki_pages/front_matter_parser.rb @@ -105,9 +105,7 @@ def parse_front_matter_block end def strip_front_matter_block - Gitlab::FrontMatter::PATTERN_UNTRUSTED_REGEX.replace_gsub(wiki_content) do - '' - end + Gitlab::FrontMatter::PATTERN_UNTRUSTED_REGEX.replace(wiki_content, '') end end end diff --git a/spec/lib/banzai/filter/front_matter_filter_spec.rb b/spec/lib/banzai/filter/front_matter_filter_spec.rb index 81da8bb52f802fa119959c8e072a0c5bd8724eb7..72f772d9d96197c0b600726545c5e981e200fe16 100644 --- a/spec/lib/banzai/filter/front_matter_filter_spec.rb +++ b/spec/lib/banzai/filter/front_matter_filter_spec.rb @@ -43,6 +43,26 @@ end end + it "doesn't insert an extra preceding newline when faced with CRLF line endings" do + content = <<~MD.gsub("\n", "\r\n") + --- + foo: :foo_symbol + bar: :bar_symbol + --- + + # Header + + Content + MD + + output = filter(content) + + aggregate_failures do + expect(output).not_to include '---' + expect(output).to include "```yaml:frontmatter\nfoo: :foo_symbol" + end + end + it 'converts TOML frontmatter to a fenced code block' do content = <<~MD +++ diff --git a/spec/lib/gitlab/wiki_pages/front_matter_parser_spec.rb b/spec/lib/gitlab/wiki_pages/front_matter_parser_spec.rb index eb403b1fc940cb40c1bfa1da058f0b393fa57eef..67b955e473d5bec8f580a0678fe704f3d9d48284 100644 --- a/spec/lib/gitlab/wiki_pages/front_matter_parser_spec.rb +++ b/spec/lib/gitlab/wiki_pages/front_matter_parser_spec.rb @@ -9,7 +9,7 @@ let(:end_divider) { '---' } let(:with_front_matter) do - <<~MD + <<~MD.rstrip --- a: 1 b: 2 @@ -35,7 +35,7 @@ def have_correct_front_matter it do is_expected.to have_attributes( front_matter: have_correct_front_matter, - content: content + "\n", + content: content, error: be_nil ) end @@ -53,6 +53,28 @@ def have_correct_front_matter end end + context 'the content itself appears to contain frontmatter' do + let(:content) do + <<~MD.rstrip + --- + custom: frontmatter + --- + + Yay! + MD + end + + let(:raw_content) { with_front_matter } + + it do + is_expected.to have_attributes( + front_matter: have_correct_front_matter, + content: content, + error: be_nil + ) + end + end + context 'there is no front_matter' do let(:raw_content) { content } @@ -67,7 +89,7 @@ def have_correct_front_matter it do is_expected.to have_attributes( front_matter: have_correct_front_matter, - content: content + "\n", + content: content, reason: be_nil ) end