-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.py
83 lines (69 loc) · 3.16 KB
/
test.py
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
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
import ntpath
import posixpath
import unittest
from unittest import mock
from werkzeug import test
from werkzeug import wrappers
from tensorboard.plugins import base_plugin
from tensorboard_plugin_customizable_plots import plugin
def is_path_safe(path):
"""Returns the result depending on the plugin's static file handler."""
cp_plugin = plugin.CustomizablePlots(base_plugin.TBContext())
serve_static_file = cp_plugin._serve_static_file
client = test.Client(serve_static_file, wrappers.Response)
response = client.get(plugin._PLUGIN_DIRECTORY_PATH_PART + path)
return response.status_code == 200
class UrlSafetyTest(unittest.TestCase):
def test_path_traversal(self):
"""Properly check whether a URL can be served from the static folder."""
with mock.patch("builtins.open", mock.mock_open(read_data="data")):
self.assertTrue(is_path_safe("static/index.js"))
self.assertTrue(is_path_safe("./static/index.js"))
self.assertTrue(is_path_safe("static/../static/index.js"))
self.assertFalse(is_path_safe("../static/index.js"))
self.assertFalse(is_path_safe("../index.js"))
self.assertFalse(is_path_safe("static2/index.js"))
self.assertFalse(is_path_safe("notstatic/index.js"))
self.assertFalse(is_path_safe("static/../../index.js"))
self.assertFalse(is_path_safe("..%2findex.js"))
self.assertFalse(is_path_safe("%2e%2e/index.js"))
self.assertFalse(is_path_safe("%2e%2e%2findex.js"))
self.assertFalse(
is_path_safe(
"static/../..\\org_tensorflow_tensorboard\\static\\index.js"
)
)
self.assertFalse(
is_path_safe(
"static/../../org_tensorflow_tensorboard/static/index.js"
)
)
self.assertFalse(
is_path_safe(
"static/%2e%2e%2f%2e%2e%5corg_tensorflow_tensorboard%5cstatic%5cindex.js"
)
)
self.assertFalse(
is_path_safe(
"static/%2e%2e%2f%2e%2e%2forg_tensorflow_tensorboard%2fstatic%2findex.js"
)
)
# Test with OS specific path modules.
with mock.patch("os.path", posixpath):
self.assertTrue(is_path_safe("static/\\index.js"))
with mock.patch("os.path", ntpath):
self.assertFalse(is_path_safe("static/\\index.js"))
if __name__ == "__main__":
unittest.main()