forked from aploium/zmirror
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_custom_content_injection.py
156 lines (137 loc) · 5.39 KB
/
test_custom_content_injection.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
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
# coding=utf-8
from flask import Response
from .base_class import ZmirrorTestBase
from .utils import *
class TestContentInjection(ZmirrorTestBase):
"""testing using https://bugzilla.kernel.org"""
class C(ZmirrorTestBase.C):
my_host_name = 'b.test.com'
my_host_scheme = 'https://'
target_domain = 'www.kernel.org'
target_scheme = 'http://'
force_https_domains = ('bugzilla.kernel.org',)
enable_automatic_domains_whitelist = True
domains_whitelist_auto_add_glob_list = ('*.kernel.org',)
# verbose_level = 4
# developer_do_not_verify_ssl = True
# is_use_proxy = True
# requests_proxies = dict(
# http='http://127.0.0.1:8882',
# https='https://127.0.0.1:8882',
# )
def test_head_first_injection_with_script_in_head(self):
"""https://bugzilla.kernel.org/"""
self.reload_zmirror(configs_dict={
"custom_inject_content": {
"head_first": [
{
"content": r'''Love''',
"url_regex": None,
},
{
"content": r'''Luciaz''',
"url_regex": None,
},
{
"content": r'''Forever''',
# 测试没有 url_regex 字段
},
{
"content": r'''!''',
"url_regex": r"^bugzilla\.kernel\.org/?", # 一个匹配的正则
},
{
"content": r'''ThisShouldNotBeInjected''',
"url_regex": r"wtf_regex", # 一个不匹配的正则
},
]
}
}
)
self.rv = self.client.get(
self.url("/extdomains/bugzilla.kernel.org/"),
environ_base=env(),
headers=headers(),
) # type: Response
text = self.rv.data.decode(encoding="utf-8")
self.assertIn('LoveLuciazForever!', text, msg=self.dump())
self.assertNotIn('ThisShouldNotBeInjected', text, msg=self.dump())
self.assertLess(text.find("LoveLuciazForever!"), text.find("<script"))
self.assertLess(text.find("LoveLuciazForever!"), text.find("</head"))
def test_head_first_injection_without_script_in_head(self):
"""https://www.kernel.org/pub/"""
self.reload_zmirror(configs_dict={
"custom_inject_content": {
"head_first": [
{
"content": r'''Love''',
"url_regex": None,
},
{
"content": r'''Luciaz''',
"url_regex": None,
},
{
"content": r'''Forever''',
# 测试没有 url_regex 字段
},
{
"content": r'''!''',
"url_regex": r"^www\.kernel\.org/pub/?", # 一个匹配的正则
},
{
"content": r'''ThisShouldNotBeInjected''',
"url_regex": r"wtf_regex", # 一个不匹配的正则
},
]
}
}
)
self.rv = self.client.get(
self.url("/pub/"),
environ_base=env(),
headers=headers(),
) # type: Response
text = self.rv.data.decode(encoding="utf-8")
self.assertIn('LoveLuciazForever!', text, msg=self.dump())
self.assertNotIn('ThisShouldNotBeInjected', text, msg=self.dump())
self.assertLess(text.find("LoveLuciazForever!"), text.find("</head"))
def test_head_last_injection_with_script_in_head(self):
"""https://bugzilla.kernel.org/"""
self.reload_zmirror(configs_dict={
"custom_inject_content": {
"head_last": [
{
"content": r'''Love''',
"url_regex": None,
},
{
"content": r'''Luciaz''',
"url_regex": None,
},
{
"content": r'''Forever''',
# 测试没有 url_regex 字段
},
{
"content": r'''!''',
"url_regex": r"^bugzilla\.kernel\.org/?", # 一个匹配的正则
},
{
"content": r'''ThisShouldNotBeInjected''',
"url_regex": r"wtf_regex", # 一个不匹配的正则
},
]
}
}
)
self.rv = self.client.get(
self.url("/extdomains/bugzilla.kernel.org/"),
environ_base=env(),
headers=headers(),
) # type: Response
text = self.rv.data.decode(encoding="utf-8")
self.assertIn('LoveLuciazForever!', text, msg=self.dump())
self.assertNotIn('ThisShouldNotBeInjected', text, msg=self.dump())
self.assertGreater(text.find("LoveLuciazForever!"), text.find("<script"))
self.assertLess(text.find("LoveLuciazForever!"), text.find("</head"))