forked from asciidoctor/asciidoctor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paths_test.rb
376 lines (310 loc) · 16.9 KB
/
paths_test.rb
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# frozen_string_literal: true
require_relative 'test_helper'
context 'Path Resolver' do
context 'Web Paths' do
def setup
@resolver = Asciidoctor::PathResolver.new
end
test 'target with absolute path' do
assert_equal '/images', @resolver.web_path('/images')
assert_equal '/images', @resolver.web_path('/images', '')
assert_equal '/images', @resolver.web_path('/images', nil)
end
test 'target with relative path' do
assert_equal 'images', @resolver.web_path('images')
assert_equal 'images', @resolver.web_path('images', '')
assert_equal 'images', @resolver.web_path('images', nil)
end
test 'target with hidden relative path' do
assert_equal '.images', @resolver.web_path('.images')
assert_equal '.images', @resolver.web_path('.images', '')
assert_equal '.images', @resolver.web_path('.images', nil)
end
test 'target with path relative to current directory' do
assert_equal './images', @resolver.web_path('./images')
assert_equal './images', @resolver.web_path('./images', '')
assert_equal './images', @resolver.web_path('./images', nil)
end
test 'target with absolute path ignores start path' do
assert_equal '/images', @resolver.web_path('/images', 'foo')
assert_equal '/images', @resolver.web_path('/images', '/foo')
assert_equal '/images', @resolver.web_path('/images', './foo')
end
test 'target with relative path appended to start path' do
assert_equal 'assets/images', @resolver.web_path('images', 'assets')
assert_equal '/assets/images', @resolver.web_path('images', '/assets')
#assert_equal '/assets/images/tiger.png', @resolver.web_path('tiger.png', '/assets//images')
assert_equal './assets/images', @resolver.web_path('images', './assets')
assert_equal '/theme.css', @resolver.web_path('theme.css', '/')
assert_equal '/css/theme.css', @resolver.web_path('theme.css', '/css/')
end
test 'target with path relative to current directory appended to start path' do
assert_equal 'assets/images', @resolver.web_path('./images', 'assets')
assert_equal '/assets/images', @resolver.web_path('./images', '/assets')
assert_equal './assets/images', @resolver.web_path('./images', './assets')
end
test 'target with relative path appended to url start path' do
assert_equal 'http://www.example.com/assets/images', @resolver.web_path('images', 'http://www.example.com/assets')
end
# enable if we want to allow web_path to detect and preserve a target URI
#test 'target with file url appended to relative path' do
# assert_equal 'file:///home/username/styles/asciidoctor.css', @resolver.web_path('file:///home/username/styles/asciidoctor.css', '.')
#end
# enable if we want to allow web_path to detect and preserve a target URI
#test 'target with http url appended to relative path' do
# assert_equal 'http://example.com/asciidoctor.css', @resolver.web_path('http://example.com/asciidoctor.css', '.')
#end
test 'normalize target' do
assert_equal '../images', @resolver.web_path('../images/../images')
end
test 'append target to start path and normalize' do
assert_equal '../images', @resolver.web_path('../images/../images', '../images')
assert_equal '../../images', @resolver.web_path('../images', '..')
end
test 'normalize parent directory that follows root' do
assert_equal '/tiger.png', @resolver.web_path('/../tiger.png')
assert_equal '/tiger.png', @resolver.web_path('/../../tiger.png')
end
test 'uses start when target is empty' do
assert_equal 'assets/images', @resolver.web_path('', 'assets/images')
assert_equal 'assets/images', @resolver.web_path(nil, 'assets/images')
end
test 'posixifies windows paths' do
@resolver.file_separator = '\\'
assert_equal '/images', @resolver.web_path('\\images')
assert_equal '../images', @resolver.web_path('..\\images')
assert_equal '/images', @resolver.web_path('\\..\\images')
assert_equal 'assets/images', @resolver.web_path('assets\\images')
assert_equal '../assets/images', @resolver.web_path('assets\\images', '..\\images\\..')
end
test 'URL encode spaces in path' do
assert_equal 'assets%20and%20stuff/lots%20of%20images', @resolver.web_path('lots of images', 'assets and stuff')
end
end
context 'System Paths' do
JAIL = '/home/doctor/docs'
default_logger = Asciidoctor::LoggerManager.logger
def setup
@resolver = Asciidoctor::PathResolver.new
@logger = (Asciidoctor::LoggerManager.logger = Asciidoctor::MemoryLogger.new)
end
teardown do
Asciidoctor::LoggerManager.logger = default_logger
end
test 'raises security error if jail is not an absolute path' do
begin
@resolver.system_path('images/tiger.png', '/etc', 'foo')
flunk 'Expecting SecurityError to be raised'
rescue SecurityError
end
end
#test 'raises security error if jail is not a canoncial path' do
# begin
# @resolver.system_path('images/tiger.png', '/etc', %(#{JAIL}/../foo))
# flunk 'Expecting SecurityError to be raised'
# rescue SecurityError
# end
#end
test 'prevents access to paths outside of jail' do
result = @resolver.system_path '../../../../../css', %(#{JAIL}/assets/stylesheets), JAIL
assert_equal %(#{JAIL}/css), result
assert_message @logger, :WARN, 'path has illegal reference to ancestor of jail; recovering automatically'
@logger.clear
result = @resolver.system_path '/../../../../../css', %(#{JAIL}/assets/stylesheets), JAIL
assert_equal %(#{JAIL}/css), result
assert_message @logger, :WARN, 'path is outside of jail; recovering automatically'
@logger.clear
result = @resolver.system_path '../../../css', '../../..', JAIL
assert_equal %(#{JAIL}/css), result
assert_message @logger, :WARN, 'path has illegal reference to ancestor of jail; recovering automatically'
end
test 'throws exception for illegal path access if recover is false' do
begin
@resolver.system_path('../../../../../css', "#{JAIL}/assets/stylesheets", JAIL, recover: false)
flunk 'Expecting SecurityError to be raised'
rescue SecurityError
end
end
test 'resolves start path if target is empty' do
assert_equal "#{JAIL}/assets/stylesheets", @resolver.system_path('', "#{JAIL}/assets/stylesheets", JAIL)
assert_equal "#{JAIL}/assets/stylesheets", @resolver.system_path(nil, "#{JAIL}/assets/stylesheets", JAIL)
end
test 'expands parent references in start path if target is empty' do
assert_equal "#{JAIL}/stylesheets", @resolver.system_path('', "#{JAIL}/assets/../stylesheets", JAIL)
end
test 'expands parent references in start path if target is not empty' do
assert_equal "#{JAIL}/stylesheets/site.css", @resolver.system_path('site.css', "#{JAIL}/assets/../stylesheets", JAIL)
end
test 'resolves start path if target is dot' do
assert_equal "#{JAIL}/assets/stylesheets", @resolver.system_path('.', "#{JAIL}/assets/stylesheets", JAIL)
assert_equal "#{JAIL}/assets/stylesheets", @resolver.system_path('./', "#{JAIL}/assets/stylesheets", JAIL)
end
test 'treats absolute target outside of jail as relative when jail is specified' do
result = @resolver.system_path '/', "#{JAIL}/assets/stylesheets", JAIL
assert_equal JAIL, result
assert_message @logger, :WARN, 'path is outside of jail; recovering automatically'
@logger.clear
result = @resolver.system_path '/foo', "#{JAIL}/assets/stylesheets", JAIL
assert_equal "#{JAIL}/foo", result
assert_message @logger, :WARN, 'path is outside of jail; recovering automatically'
@logger.clear
result = @resolver.system_path '/../foo', "#{JAIL}/assets/stylesheets", JAIL
assert_equal "#{JAIL}/foo", result
assert_message @logger, :WARN, 'path is outside of jail; recovering automatically'
@logger.clear
@resolver.file_separator = '\\'
result = @resolver.system_path 'baz.adoc', 'C:/foo', 'C:/bar'
assert_equal 'C:/bar/baz.adoc', result
assert_message @logger, :WARN, 'path is outside of jail; recovering automatically'
end
test 'allows use of absolute target or start if resolved path is sub-path of jail' do
assert_equal "#{JAIL}/my/path", @resolver.system_path("#{JAIL}/my/path", '', JAIL)
assert_equal "#{JAIL}/my/path", @resolver.system_path("#{JAIL}/my/path", nil, JAIL)
assert_equal "#{JAIL}/my/path", @resolver.system_path('', "#{JAIL}/my/path", JAIL)
assert_equal "#{JAIL}/my/path", @resolver.system_path(nil, "#{JAIL}/my/path", JAIL)
assert_equal "#{JAIL}/my/path", @resolver.system_path('path', "#{JAIL}/my", JAIL)
assert_equal '/foo/bar/baz.adoc', @resolver.system_path('/foo/bar/baz.adoc', nil, '/')
assert_equal '/foo/bar/baz.adoc', @resolver.system_path('baz.adoc', '/foo/bar', '/')
assert_equal '/foo/bar/baz.adoc', @resolver.system_path('baz.adoc', 'foo/bar', '/')
end
test 'uses jail path if start path is empty' do
assert_equal "#{JAIL}/images/tiger.png", @resolver.system_path('images/tiger.png', '', JAIL)
assert_equal "#{JAIL}/images/tiger.png", @resolver.system_path('images/tiger.png', nil, JAIL)
end
test 'warns if start is not contained within jail' do
result = @resolver.system_path 'images/tiger.png', '/etc', JAIL
assert_equal %(#{JAIL}/images/tiger.png), result
assert_message @logger, :WARN, 'path is outside of jail; recovering automatically'
@logger.clear
result = @resolver.system_path '.', '/etc', JAIL
assert_equal JAIL, result
assert_message @logger, :WARN, 'path is outside of jail; recovering automatically'
@logger.clear
@resolver.file_separator = '\\'
result = @resolver.system_path '.', 'C:/foo', 'C:/bar'
assert_equal 'C:/bar', result
assert_message @logger, :WARN, 'path is outside of jail; recovering automatically'
end
test 'allows start path to be parent of jail if resolved target is inside jail' do
assert_equal "#{JAIL}/foo/path", @resolver.system_path('foo/path', JAIL, "#{JAIL}/foo")
@resolver.file_separator = '\\'
assert_equal "C:/dev/project/README.adoc", @resolver.system_path('project/README.adoc', 'C:/dev', 'C:/dev/project')
end
test 'relocates target to jail if resolved value fails outside of jail' do
result = @resolver.system_path 'bar/baz.adoc', JAIL, "#{JAIL}/foo"
assert_equal %(#{JAIL}/foo/bar/baz.adoc), result
assert_message @logger, :WARN, 'path is outside of jail; recovering automatically'
@logger.clear
@resolver.file_separator = '\\'
result = @resolver.system_path 'bar/baz.adoc', 'D:/', 'C:/foo'
assert_equal 'C:/foo/bar/baz.adoc', result
assert_message @logger, :WARN, '~outside of jail root'
end
test 'raises security error if start is not contained within jail and recover is disabled' do
begin
@resolver.system_path('images/tiger.png', '/etc', JAIL, recover: false)
flunk 'Expecting SecurityError to be raised'
rescue SecurityError
end
begin
@resolver.system_path('.', '/etc', JAIL, recover: false)
flunk 'Expecting SecurityError to be raised'
rescue SecurityError
end
end
test 'expands parent references in absolute path if jail is not specified' do
assert_equal '/etc/stylesheet.css', @resolver.system_path('/usr/share/../../etc/stylesheet.css')
end
test 'resolves absolute directory if jail is not specified' do
assert_equal '/usr/share/stylesheet.css', @resolver.system_path('/usr/share/stylesheet.css', '/home/dallen/docs/assets/stylesheets')
end
test 'resolves ancestor directory of start if jail is not specified' do
assert_equal '/usr/share/stylesheet.css', @resolver.system_path('../../../../../usr/share/stylesheet.css', '/home/dallen/docs/assets/stylesheets')
end
test 'resolves absolute path if start is absolute and target is relative' do
assert_equal '/usr/share/assets/stylesheet.css', @resolver.system_path('assets/stylesheet.css', '/usr/share')
end
test 'File.dirname preserves UNC path root on Windows' do
assert_equal File.dirname('\\\\server\\docs\\file.html'), '\\\\server\\docs'
end if windows?
test 'File.dirname preserves posix-style UNC path root on Windows' do
assert_equal File.dirname('//server/docs/file.html'), '//server/docs'
end if windows?
test 'resolves UNC path if start is absolute and target is relative' do
assert_equal '//QA/c$/users/asciidoctor/assets/stylesheet.css', @resolver.system_path('assets/stylesheet.css', '//QA/c$/users/asciidoctor')
end
test 'resolves UNC path if target is UNC path' do
@resolver.file_separator = '\\'
assert_equal '//server/docs/output.html', @resolver.system_path('\\\\server\\docs\\output.html')
end
test 'resolves UNC path if target is posix-style UNC path' do
assert_equal '//server/docs/output.html', @resolver.system_path('//server/docs/output.html')
end
test 'resolves relative target relative to current directory if start is empty' do
pwd = File.expand_path(Dir.pwd)
assert_equal "#{pwd}/images/tiger.png", @resolver.system_path('images/tiger.png', '')
assert_equal "#{pwd}/images/tiger.png", @resolver.system_path('images/tiger.png', nil)
assert_equal "#{pwd}/images/tiger.png", @resolver.system_path('images/tiger.png')
end
test 'resolves relative hidden target relative to current directory if start is empty' do
pwd = File.expand_path(Dir.pwd)
assert_equal "#{pwd}/.images/tiger.png", @resolver.system_path('.images/tiger.png', '')
assert_equal "#{pwd}/.images/tiger.png", @resolver.system_path('.images/tiger.png', nil)
end
test 'resolves and normalizes start when target is empty' do
pwd = File.expand_path Dir.pwd
assert_equal '/home/doctor/docs', (@resolver.system_path '', '/home/doctor/docs')
assert_equal '/home/doctor/docs', (@resolver.system_path '', '/home/doctor/./docs')
assert_equal '/home/doctor/docs', (@resolver.system_path nil, '/home/doctor/docs')
assert_equal '/home/doctor/docs', (@resolver.system_path nil, '/home/doctor/./docs')
assert_equal %(#{pwd}/assets/images), (@resolver.system_path nil, 'assets/images')
@resolver.system_path '', '../assets/images', JAIL
assert_message @logger, :WARN, 'path has illegal reference to ancestor of jail; recovering automatically'
end
test 'posixifies windows paths' do
@resolver.file_separator = '\\'
assert_equal "#{JAIL}/assets/css", @resolver.system_path('..\\css', 'assets\\stylesheets', JAIL)
end
test 'resolves windows paths when file separator is backlash' do
@resolver.file_separator = '\\'
assert_equal 'C:/data/docs', (@resolver.system_path '..', 'C:\\data\\docs\\assets', 'C:\\data\\docs')
result = @resolver.system_path '..\\..', 'C:\\data\\docs\\assets', 'C:\\data\\docs'
assert_equal 'C:/data/docs', result
assert_message @logger, :WARN, 'path has illegal reference to ancestor of jail; recovering automatically'
@logger.clear
result = @resolver.system_path '..\\..\\css', 'C:\\data\\docs\\assets', 'C:\\data\\docs'
assert_equal 'C:/data/docs/css', result
assert_message @logger, :WARN, 'path has illegal reference to ancestor of jail; recovering automatically'
end
test 'should calculate relative path' do
filename = @resolver.system_path('part1/chapter1/section1.adoc', nil, JAIL)
assert_equal "#{JAIL}/part1/chapter1/section1.adoc", filename
assert_equal 'part1/chapter1/section1.adoc', @resolver.relative_path(filename, JAIL)
end
test 'should resolve relative path to filename outside of base directory' do
filename = '/home/shared/partials'
base_dir = '/home/user/docs'
result = @resolver.relative_path filename, base_dir
assert_equal '../../shared/partials', result
end
test 'should return original path if relative path cannot be computed' do
filename = 'D:/path/to/include/file.txt'
base_dir = 'C:/docs'
result = @resolver.relative_path filename, base_dir
assert_equal 'D:/path/to/include/file.txt', result
end if windows?
test 'should resolve relative path relative to base dir in unsafe mode' do
base_dir = fixture_path 'base'
doc = empty_document base_dir: base_dir, safe: Asciidoctor::SafeMode::UNSAFE
expected = ::File.join base_dir, 'images', 'tiger.png'
actual = doc.normalize_system_path 'tiger.png', 'images'
assert_equal expected, actual
end
test 'should resolve absolute path as absolute in unsafe mode' do
base_dir = fixture_path 'base'
doc = empty_document base_dir: base_dir, safe: Asciidoctor::SafeMode::UNSAFE
actual = doc.normalize_system_path 'tiger.png', '/etc/images'
assert_equal '/etc/images/tiger.png', actual
end
end
end