From 78f3ed55ce429f7ac1e4148e0ef4d83f81e9eac7 Mon Sep 17 00:00:00 2001 From: Asherah Connor Date: Wed, 19 Nov 2025 16:04:49 +1100 Subject: [PATCH 1/4] Failing spec for frontmatter parser eating extras --- .../wiki_pages/front_matter_parser_spec.rb | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) 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 eb403b1fc940cb..67b955e473d5be 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 -- GitLab From ee333f611d22b72baf4c846424ecffb68b8a64d6 Mon Sep 17 00:00:00 2001 From: Asherah Connor Date: Wed, 19 Nov 2025 16:04:49 +1100 Subject: [PATCH 2/4] FrontMatterParser should strip only one frontmatter Anything after the one we insert is part of the user content. --- lib/gitlab/wiki_pages/front_matter_parser.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/gitlab/wiki_pages/front_matter_parser.rb b/lib/gitlab/wiki_pages/front_matter_parser.rb index 3fd62771a90bdf..f796aa6ff0486c 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 -- GitLab From f84dbfd5348b98194366f77de097847ee729e3b8 Mon Sep 17 00:00:00 2001 From: Asherah Connor Date: Wed, 19 Nov 2025 16:44:32 +1100 Subject: [PATCH 3/4] Failing spec for extra newline after frontmatter start with CRLF --- .../banzai/filter/front_matter_filter_spec.rb | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/spec/lib/banzai/filter/front_matter_filter_spec.rb b/spec/lib/banzai/filter/front_matter_filter_spec.rb index 81da8bb52f802f..72f772d9d96197 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 +++ -- GitLab From 9884760c631e3326d656a5a5b2e684b2d3233cb6 Mon Sep 17 00:00:00 2001 From: Asherah Connor Date: Wed, 19 Nov 2025 16:04:49 +1100 Subject: [PATCH 4/4] UntrustedRegexp's BACKSLASH_R consumes \r\n in preference to \r It will never match \r\n otherwise, so \r\n becomes in effect two newlines. --- lib/gitlab/untrusted_regexp.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/gitlab/untrusted_regexp.rb b/lib/gitlab/untrusted_regexp.rb index 006681654f315b..95a1d02e87fcd2 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 -- GitLab