blob: d6be088eaffe887ae711e865465764be92ed9d24 (
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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Banzai::Filter::CodeLanguageFilter, feature_category: :team_planning 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><script>alert(1)</script></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 <s above.
expect(result.to_html).to match(%r{alert(<.*>)?\((<.*>)?1(<.*>)?\)})
end
end
context 'when no language is specified' do
it 'does nothing' do
result = filter('<pre><code>def fun end</code></pre>')
expect(result.to_html.delete("\n")).to eq('<pre><code>def fun end</code></pre>')
end
end
context 'when lang is specified on `pre`' do
it 'adds data-canonical-lang and removes lang attribute' do
result = filter('<pre lang="ruby"><code>def fun end</code></pre>')
expect(result.to_html.delete("\n"))
.to eq('<pre data-canonical-lang="ruby"><code>def fun end</code></pre>')
end
end
context 'when lang is specified on `code`' do
it 'adds data-canonical-lang to `pre` and removes lang attribute' do
result = filter('<pre><code lang="ruby">def fun end</code></pre>')
expect(result.to_html.delete("\n"))
.to eq('<pre data-canonical-lang="ruby"><code>def fun end</code></pre>')
end
end
context 'when lang has extra params' do
let_it_be(:lang_params) { 'foo-bar-kux' }
let_it_be(:xss_lang) { %(ruby data-meta="foo-bar-kux"<script>alert(1)</script>) }
let_it_be(:expected_result) do
<<~HTML
<pre data-canonical-lang="ruby" data-lang-params="#{lang_params}">
<code>This is a test</code></pre>
HTML
end
context 'when lang is specified on `pre`' do
it 'includes data-lang-params tag with extra information and removes data-meta' do
result = filter(%(<pre lang="ruby" data-meta="#{lang_params}"><code>This is a test</code></pre>))
expect(result.to_html.delete("\n")).to eq(expected_result.delete("\n"))
end
end
context 'when lang is specified on `code`' do
it 'includes data-lang-params tag with extra information and removes data-meta' do
result = filter(%(<pre><code lang="ruby" data-meta="#{lang_params}">This is a test</code></pre>))
expect(result.to_html.delete("\n")).to eq(expected_result.delete("\n"))
end
end
include_examples 'XSS prevention', 'ruby'
include_examples 'XSS prevention',
%(ruby data-meta="foo-bar-kux"<script>alert(1)</script>)
include_examples 'XSS prevention',
%(ruby data-meta="foo-bar-kux"<script>alert(1)</script>)
end
context 'when multiple param delimiters are used' do
let(:lang) { 'suggestion' }
let(:lang_params) { '-1+10' }
let(:expected_result) do
<<~HTML
<pre data-canonical-lang="#{lang}" data-lang-params="#{lang_params} more-things">
<code>This is a test</code></pre>
HTML
end
context 'when delimiter is colon' do
it 'delimits on the first appearance' do
result = filter(%(<pre lang="#{lang}:#{lang_params} more-things"><code>This is a test</code></pre>))
expect(result.to_html.delete("\n")).to eq(expected_result.delete("\n"))
end
end
end
end
|