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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
|
# frozen_string_literal: true
require 'spec_helper'
describe Banzai::Pipeline::WikiPipeline do
let_it_be(:namespace) { create(:namespace, name: "wiki_link_ns") }
let_it_be(:project) { create(:project, :public, name: "wiki_link_project", namespace: namespace) }
let_it_be(:project_wiki) { ProjectWiki.new(project, double(:user)) }
let_it_be(:page) { build(:wiki_page, wiki: project_wiki, page: OpenStruct.new(url_path: 'nested/twice/start-page')) }
let(:prefix) { project_wiki.wiki_page_path }
describe 'TableOfContents' do
it 'replaces the tag with the TableOfContentsFilter result' do
markdown = <<-MD.strip_heredoc
[[_TOC_]]
## Header
Foo
MD
result = described_class.call(markdown, project: spy, project_wiki: spy)
aggregate_failures do
expect(result[:output].text).not_to include '[['
expect(result[:output].text).not_to include 'TOC'
expect(result[:output].to_html).to include(result[:toc])
end
end
it 'is case-sensitive' do
markdown = <<-MD.strip_heredoc
[[_toc_]]
# Header 1
Foo
MD
output = described_class.to_html(markdown, project: spy, project_wiki: spy)
expect(output).to include('[[<em>toc</em>]]')
end
it 'handles an empty pipeline result' do
# No Markdown headers in this doc, so `result[:toc]` will be empty
markdown = <<-MD.strip_heredoc
[[_TOC_]]
Foo
MD
output = described_class.to_html(markdown, project: spy, project_wiki: spy)
aggregate_failures do
expect(output).not_to include('<ul>')
expect(output).not_to include('[[<em>TOC</em>]]')
end
end
end
describe "Links" do
shared_examples 'a correct link rewrite' do
it 'rewrites links correctly' do
output = described_class.to_html(markdown, project: project, project_wiki: project_wiki, page_slug: page.slug)
expect(output).to include("href=\"#{page_href}\"")
end
end
shared_examples 'link examples' do |test_name|
let(:page_href) { "#{prefix}/#{expected_page_path}" }
context "when GitLab is hosted at a #{test_name} URL" do
before do
allow(Gitlab.config.gitlab).to receive(:relative_url_root).and_return(relative_url_root)
end
describe "linking to pages within the wiki" do
let(:markdown) { "[Page](#{nesting}page#{extension})" }
context "when creating hierarchical links to the current directory" do
let(:nesting) { './' }
context 'non file links' do
let(:extension) { '' }
let(:expected_page_path) { 'nested/twice/page' }
it_behaves_like 'a correct link rewrite'
end
context 'file-like links' do
let(:extension) { '.md' }
let(:expected_page_path) { 'nested/twice/page.md' }
it_behaves_like 'a correct link rewrite'
end
end
context "when creating hierarchical links to the parent directory" do
let(:nesting) { '../' }
context "non file links" do
let(:extension) { '' }
let(:expected_page_path) { 'nested/page' }
it_behaves_like 'a correct link rewrite'
end
context "file-like links" do
let(:extension) { '.md' }
let(:expected_page_path) { 'nested/page.md' }
it_behaves_like 'a correct link rewrite'
end
end
context "when creating hierarchical links to a sub-directory" do
let(:nesting) { './subdirectory/' }
context "non file links" do
let(:extension) { '' }
let(:expected_page_path) { 'nested/twice/subdirectory/page' }
it_behaves_like 'a correct link rewrite'
end
context 'file-like links' do
let(:extension) { '.md' }
let(:expected_page_path) { 'nested/twice/subdirectory/page.md' }
it_behaves_like 'a correct link rewrite'
end
end
describe "when creating non-hierarchical links" do
let(:nesting) { '' }
context 'non-file links' do
let(:extension) { '' }
let(:expected_page_path) { 'page' }
it_behaves_like 'a correct link rewrite'
end
context 'non-file links (with spaces)' do
let(:extension) { ' slug' }
let(:expected_page_path) { 'page%20slug' }
it_behaves_like 'a correct link rewrite'
end
context "file links" do
let(:extension) { '.md' }
let(:expected_page_path) { 'nested/twice/page.md' }
it_behaves_like 'a correct link rewrite'
end
context 'links with anchor' do
let(:extension) { '#title' }
let(:expected_page_path) { 'page#title' }
it_behaves_like 'a correct link rewrite'
end
context 'links (with spaces) with anchor' do
let(:extension) { ' two#title' }
let(:expected_page_path) { 'page%20two#title' }
it_behaves_like 'a correct link rewrite'
end
end
describe "when creating root links" do
let(:nesting) { '/' }
context 'non-file links' do
let(:extension) { '' }
let(:expected_page_path) { 'page' }
it_behaves_like 'a correct link rewrite'
end
context 'file links' do
let(:extension) { '.md' }
let(:expected_page_path) { 'page.md' }
it_behaves_like 'a correct link rewrite'
end
end
end
describe "linking to pages outside the wiki (absolute)" do
let(:markdown) { "[Link to Page](http://example.com/page)" }
let(:page_href) { 'http://example.com/page' }
it_behaves_like 'a correct link rewrite'
end
end
end
include_examples 'link examples', :root do
let(:relative_url_root) { '/' }
end
include_examples 'link examples', :relative do
let(:relative_url_root) { '/nested/relative/gitlab' }
end
describe "checking slug validity when assembling links" do
context "with a valid slug" do
let(:valid_slug) { "http://example.com" }
it "includes the slug in a (.) relative link" do
output = described_class.to_html(
"[Link](./alert(1);)",
project: project,
project_wiki: project_wiki,
page_slug: valid_slug
)
expect(output).to include(valid_slug)
end
it "includeds the slug in a (..) relative link" do
output = described_class.to_html(
"[Link](../alert(1);)",
project: project,
project_wiki: project_wiki,
page_slug: valid_slug
)
expect(output).to include(valid_slug)
end
end
context "when the slug is deemed unsafe or invalid" do
invalid_slugs = [
"javascript:",
"JaVaScRiPt:",
"\u0001java\u0003script:",
"javascript :",
"javascript: ",
"javascript : ",
":javascript:",
"javascript:",
"javascript:",
"javascript:",
"javascript:",
"java\0script:",
"  javascript:"
]
invalid_js_links = [
"alert(1);",
"alert(document.location);"
]
invalid_slugs.each do |slug|
context "with the invalid slug #{slug}" do
invalid_js_links.each do |link|
it "doesn't include a prohibited slug in a (.) relative link '#{link}'" do
output = described_class.to_html(
"[Link](./#{link})",
project: project,
project_wiki: project_wiki,
page_slug: slug
)
expect(output).not_to include(slug)
end
it "doesn't include a prohibited slug in a (..) relative link '#{link}'" do
output = described_class.to_html(
"[Link](../#{link})",
project: project,
project_wiki: project_wiki,
page_slug: slug
)
expect(output).not_to include(slug)
end
end
end
end
end
end
end
describe 'videos and audio' do
def src(file_name)
"#{prefix}/nested/twice/#{file_name}"
end
shared_examples 'correct video rewrite' do
let(:markdown) { "![video_file](#{file_name})" }
let(:video_fragment) { "<video src=\"#{prefix}/#{expected_file_path}\"" }
let(:options) do
{
project: project,
project_wiki: project_wiki,
page_slug: page.slug
}
end
it 'generates video html structure' do
output = described_class.to_html(markdown, options)
expect(output).to include(video_fragment)
end
end
context 'underscores' do
let(:file_name) { 'video_file_name.mp4' }
let(:expected_file_path) { 'nested/twice/video_file_name.mp4' }
it_behaves_like 'correct video rewrite'
end
context 'spaces' do
let(:file_name) { 'video file name.mp4' }
let(:expected_file_path) { 'nested/twice/video%20file%20name.mp4' }
it_behaves_like 'correct video rewrite'
end
it 'generates audio html structure' do
markdown = "![audio_file](audio_file_name.wav)"
safe_name = "audio_file_name.wav"
output = described_class.to_html(markdown, project: project, project_wiki: project_wiki, page_slug: page.slug)
expect(output).to include(%Q'<audio src="#{src(safe_name)}"')
end
it 'rewrites and replaces audio links names with white spaces to %20' do
markdown = "![audio file](audio file name.wav)"
safe_name = "audio%20file%20name.wav"
output = described_class.to_html(markdown, project: project, project_wiki: project_wiki, page_slug: page.slug)
expect(output).to include(%Q'<audio src="#{src(safe_name)}"')
end
end
end
|