summaryrefslogtreecommitdiff
path: root/spec/lib/banzai/filter/syntax_highlight_filter_spec.rb
blob: ef46fd62486b60dd661a2b90b307cf1556dde658 (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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Banzai::Filter::SyntaxHighlightFilter do
  include FilterSpecHelper

  shared_examples "XSS prevention" do |lang|
    it "escapes HTML tags" do
      # This is how a script tag inside a code block is presented to this filter
      # after Markdown rendering.
      result = filter(%{<pre lang="#{lang}"><code>&lt;script&gt;alert(1)&lt;/script&gt;</code></pre>})

      # `(1)` symbols are wrapped by lexer tags.
      expect(result.to_html).not_to match(%r{<script>alert.*<\/script>})

      # `<>` stands for lexer tags like <span ...>, not &lt;s above.
      expect(result.to_html).to match(%r{alert(<.*>)?\((<.*>)?1(<.*>)?\)})
    end
  end

  shared_examples_for 'renders correct markdown' do
    context "when no language is specified" do
      it "highlights as plaintext" do
        result = filter('<pre><code>def fun end</code></pre>')

        expect(result.to_html.delete("\n")).to eq('<div class="gl-relative markdown-code-block js-markdown-code"><pre class="code highlight js-syntax-highlight language-plaintext" lang="plaintext" v-pre="true"><code><span id="LC1" class="line" lang="plaintext">def fun end</span></code></pre><copy-code></copy-code></div>')
      end

      include_examples "XSS prevention", ""
    end

    context "when contains mermaid diagrams" do
      it "ignores mermaid blocks" do
        result = filter('<pre data-mermaid-style="display"><code>mermaid code</code></pre>')

        expect(result.to_html).to eq('<pre data-mermaid-style="display"><code>mermaid code</code></pre>')
      end
    end

    context "when a valid language is specified" do
      it "highlights as that language" do
        result = if Feature.enabled?(:use_cmark_renderer, default_enabled: :yaml)
                   filter('<pre lang="ruby"><code>def fun end</code></pre>')
                 else
                   filter('<pre><code lang="ruby">def fun end</code></pre>')
                 end

        expect(result.to_html.delete("\n")).to eq('<div class="gl-relative markdown-code-block js-markdown-code"><pre class="code highlight js-syntax-highlight language-ruby" lang="ruby" v-pre="true"><code><span id="LC1" class="line" lang="ruby"><span class="k">def</span> <span class="nf">fun</span> <span class="k">end</span></span></code></pre><copy-code></copy-code></div>')
      end

      include_examples "XSS prevention", "ruby"
    end

    context "when an invalid language is specified" do
      it "highlights as plaintext" do
        result = if Feature.enabled?(:use_cmark_renderer, default_enabled: :yaml)
                   filter('<pre lang="gnuplot"><code>This is a test</code></pre>')
                 else
                   filter('<pre><code lang="gnuplot">This is a test</code></pre>')
                 end

        expect(result.to_html.delete("\n")).to eq('<div class="gl-relative markdown-code-block js-markdown-code"><pre class="code highlight js-syntax-highlight language-plaintext" lang="plaintext" v-pre="true"><code><span id="LC1" class="line" lang="plaintext">This is a test</span></code></pre><copy-code></copy-code></div>')
      end

      include_examples "XSS prevention", "gnuplot"
    end

    context "languages that should be passed through" do
      let(:delimiter) { described_class::LANG_PARAMS_DELIMITER }
      let(:data_attr) { described_class::LANG_PARAMS_ATTR }

      %w(math mermaid plantuml suggestion).each do |lang|
        context "when #{lang} is specified" do
          it "highlights as plaintext but with the correct language attribute and class" do
            result = if Feature.enabled?(:use_cmark_renderer, default_enabled: :yaml)
                       filter(%{<pre lang="#{lang}"><code>This is a test</code></pre>})
                     else
                       filter(%{<pre><code lang="#{lang}">This is a test</code></pre>})
                     end

            expect(result.to_html.delete("\n")).to eq(%{<div class="gl-relative markdown-code-block js-markdown-code"><pre class="code highlight js-syntax-highlight language-#{lang}" lang="#{lang}" v-pre="true"><code><span id="LC1" class="line" lang="#{lang}">This is a test</span></code></pre><copy-code></copy-code></div>})
          end

          include_examples "XSS prevention", lang
        end

        context "when #{lang} has extra params" do
          let(:lang_params) { 'foo-bar-kux' }

          let(:xss_lang) do
            if Feature.enabled?(:use_cmark_renderer, default_enabled: :yaml)
              "#{lang} data-meta=\"foo-bar-kux\"&lt;script&gt;alert(1)&lt;/script&gt;"
            else
              "#{lang}#{described_class::LANG_PARAMS_DELIMITER}&lt;script&gt;alert(1)&lt;/script&gt;"
            end
          end

          it "includes data-lang-params tag with extra information" do
            result = if Feature.enabled?(:use_cmark_renderer, default_enabled: :yaml)
                       filter(%{<pre lang="#{lang}" data-meta="#{lang_params}"><code>This is a test</code></pre>})
                     else
                       filter(%{<pre><code lang="#{lang}#{delimiter}#{lang_params}">This is a test</code></pre>})
                     end

            expect(result.to_html.delete("\n")).to eq(%{<div class="gl-relative markdown-code-block js-markdown-code"><pre class="code highlight js-syntax-highlight language-#{lang}" lang="#{lang}" #{data_attr}="#{lang_params}" v-pre="true"><code><span id="LC1" class="line" lang="#{lang}">This is a test</span></code></pre><copy-code></copy-code></div>})
          end

          include_examples "XSS prevention", lang

          if Feature.enabled?(:use_cmark_renderer, default_enabled: :yaml)
            include_examples "XSS prevention",
                             "#{lang} data-meta=\"foo-bar-kux\"&lt;script&gt;alert(1)&lt;/script&gt;"
          else
            include_examples "XSS prevention",
                             "#{lang}#{described_class::LANG_PARAMS_DELIMITER}&lt;script&gt;alert(1)&lt;/script&gt;"
          end

          include_examples "XSS prevention",
            "#{lang} data-meta=\"foo-bar-kux\"<script>alert(1)</script>"
        end
      end

      context 'when multiple param delimiters are used' do
        let(:lang) { 'suggestion' }
        let(:lang_params) { '-1+10' }

        let(:expected_result) do
          %{<div class="gl-relative markdown-code-block js-markdown-code"><pre class="code highlight js-syntax-highlight language-#{lang}" lang="#{lang}" #{data_attr}="#{lang_params} more-things" v-pre="true"><code><span id="LC1" class="line" lang="#{lang}">This is a test</span></code></pre><copy-code></copy-code></div>}
        end

        context 'when delimiter is space' do
          it 'delimits on the first appearance' do
            if Feature.enabled?(:use_cmark_renderer, default_enabled: :yaml)
              result = filter(%{<pre lang="#{lang}" data-meta="#{lang_params} more-things"><code>This is a test</code></pre>})

              expect(result.to_html.delete("\n")).to eq(expected_result)
            else
              result = filter(%{<pre><code lang="#{lang}#{delimiter}#{lang_params}#{delimiter}more-things">This is a test</code></pre>})

              expect(result.to_html.delete("\n")).to eq(%{<div class="gl-relative markdown-code-block js-markdown-code"><pre class="code highlight js-syntax-highlight language-#{lang}" lang="#{lang}" #{data_attr}="#{lang_params}#{delimiter}more-things" v-pre="true"><code><span id="LC1" class="line" lang="#{lang}">This is a test</span></code></pre><copy-code></copy-code></div>})
            end
          end
        end

        context 'when delimiter is colon' do
          it 'delimits on the first appearance' do
            result = filter(%{<pre lang="#{lang}#{delimiter}#{lang_params} more-things"><code>This is a test</code></pre>})

            if Feature.enabled?(:use_cmark_renderer, default_enabled: :yaml)
              expect(result.to_html.delete("\n")).to eq(expected_result)
            else
              expect(result.to_html.delete("\n")).to eq(%{<div class="gl-relative markdown-code-block js-markdown-code"><pre class=\"code highlight js-syntax-highlight language-plaintext\" lang=\"plaintext\" v-pre=\"true\"><code><span id=\"LC1\" class=\"line\" lang=\"plaintext\">This is a test</span></code></pre><copy-code></copy-code></div>})
            end
          end
        end
      end
    end

    context "when sourcepos metadata is available" do
      it "includes it in the highlighted code block" do
        result = filter('<pre data-sourcepos="1:1-3:3"><code lang="plaintext">This is a test</code></pre>')

        expect(result.to_html.delete("\n")).to eq('<div class="gl-relative markdown-code-block js-markdown-code"><pre data-sourcepos="1:1-3:3" class="code highlight js-syntax-highlight language-plaintext" lang="plaintext" v-pre="true"><code><span id="LC1" class="line" lang="plaintext">This is a test</span></code></pre><copy-code></copy-code></div>')
      end
    end

    context "when Rouge lexing fails" do
      before do
        allow_next_instance_of(Rouge::Lexers::Ruby) do |instance|
          allow(instance).to receive(:stream_tokens).and_raise(StandardError)
        end
      end

      it "highlights as plaintext" do
        result = if Feature.enabled?(:use_cmark_renderer, default_enabled: :yaml)
                   filter('<pre lang="ruby"><code>This is a test</code></pre>')
                 else
                   filter('<pre><code lang="ruby">This is a test</code></pre>')
                 end

        expect(result.to_html.delete("\n")).to eq('<div class="gl-relative markdown-code-block js-markdown-code"><pre class="code highlight js-syntax-highlight" lang="" v-pre="true"><code><span id="LC1" class="line" lang="">This is a test</span></code></pre><copy-code></copy-code></div>')
      end

      include_examples "XSS prevention", "ruby"
    end

    context "when Rouge lexing fails after a retry" do
      before do
        allow_next_instance_of(Rouge::Lexers::PlainText) do |instance|
          allow(instance).to receive(:stream_tokens).and_raise(StandardError)
        end
      end

      it "does not add highlighting classes" do
        result = filter('<pre><code>This is a test</code></pre>')

        expect(result.to_html).to eq('<pre><code>This is a test</code></pre>')
      end

      include_examples "XSS prevention", "ruby"
    end
  end

  context 'using ruby-based HTML renderer' do
    before do
      stub_feature_flags(use_cmark_renderer: false)
    end

    it_behaves_like 'renders correct markdown'
  end

  context 'using c-based HTML renderer' do
    before do
      stub_feature_flags(use_cmark_renderer: true)
    end

    it_behaves_like 'renders correct markdown'
  end
end