Skip to content

Commit a94d31e

Browse files
Add black referer (#334)
1 parent d3f670e commit a94d31e

File tree

4 files changed

+200
-2
lines changed

4 files changed

+200
-2
lines changed

oss2/models.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -733,16 +733,19 @@ class BucketReferer(object):
733733
734734
:param bool allow_empty_referer: 是否允许空的Referer。
735735
:param referers: Referer列表,每个元素是一个str。
736+
:param black_referers: BlackReferer列表,每个元素是一个str。
736737
"""
737-
def __init__(self, allow_empty_referer, referers):
738+
def __init__(self, allow_empty_referer, referers, allow_truncate_query_string=None, black_referers=None):
738739
self.allow_empty_referer = allow_empty_referer
739740
self.referers = referers
741+
self.allow_truncate_query_string = allow_truncate_query_string
742+
self.black_referers = black_referers
740743

741744

742745
class GetBucketRefererResult(RequestResult, BucketReferer):
743746
def __init__(self, resp):
744747
RequestResult.__init__(self, resp)
745-
BucketReferer.__init__(self, False, [])
748+
BucketReferer.__init__(self, False, [], None, [])
746749

747750
class Condition(object):
748751
""" 匹配规则

oss2/xml_utils.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,11 @@ def parse_get_bucket_referer(result, body):
418418
result.allow_empty_referer = _find_bool(root, 'AllowEmptyReferer')
419419
result.referers = _find_all_tags(root, 'RefererList/Referer')
420420

421+
if root.find("AllowTruncateQueryString") is not None:
422+
result.allow_truncate_query_string = _find_bool(root, 'AllowTruncateQueryString')
423+
if root.find("RefererBlacklist/Referer") is not None:
424+
result.black_referers = _find_all_tags(root, 'RefererBlacklist/Referer')
425+
421426
return result
422427

423428
def parse_condition_include_header(include_header_node):
@@ -869,6 +874,15 @@ def to_put_bucket_referer(bucket_referer):
869874
for r in bucket_referer.referers:
870875
_add_text_child(list_node, 'Referer', r)
871876

877+
if bucket_referer.allow_truncate_query_string is not None:
878+
_add_text_child(root, 'AllowTruncateQueryString', str(bucket_referer.allow_truncate_query_string).lower())
879+
880+
if bucket_referer.black_referers:
881+
black_referer_node = ElementTree.SubElement(root, 'RefererBlacklist')
882+
883+
for r in bucket_referer.black_referers:
884+
_add_text_child(black_referer_node, 'Referer', r)
885+
872886
return _node_to_string(root)
873887

874888

tests/test_bucket.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,57 @@ def test_referer(self):
667667
self.assertTrue(result.allow_empty_referer)
668668
self.assertEqual(sorted(to_string(r) for r in referers), sorted(to_string(r) for r in result.referers))
669669

670+
def test_black_referer(self):
671+
referers = ['http://hello.com', 'mibrowser:home', '中文+referer', u'中文+referer']
672+
black_referers = ['http://hello2.com', 'mibrowser2:home', '中文2+referer', u'中文2+referer']
673+
allow_empty_referer = True
674+
allow_truncate_query_string = True
675+
676+
# black referer 1
677+
config = oss2.models.BucketReferer(allow_empty_referer, referers, allow_truncate_query_string)
678+
self.bucket.put_bucket_referer(config)
679+
wait_meta_sync()
680+
681+
result = self.bucket.get_bucket_referer()
682+
self.assertTrue(result.allow_empty_referer)
683+
self.assertTrue(result.allow_truncate_query_string)
684+
self.assertEqual(sorted(to_string(r) for r in referers), sorted(to_string(r) for r in result.referers))
685+
686+
# black referer 2
687+
allow_empty_referer = False
688+
allow_truncate_query_string = False
689+
config = oss2.models.BucketReferer(allow_empty_referer, referers, allow_truncate_query_string, black_referers)
690+
self.bucket.put_bucket_referer(config)
691+
wait_meta_sync()
692+
693+
result = self.bucket.get_bucket_referer()
694+
self.assertFalse(result.allow_empty_referer)
695+
self.assertFalse(result.allow_truncate_query_string)
696+
self.assertEqual(sorted(to_string(r) for r in referers), sorted(to_string(r) for r in result.referers))
697+
self.assertEqual(sorted(to_string(r) for r in black_referers), sorted(to_string(r) for r in result.black_referers))
698+
699+
# black referer 3
700+
config = oss2.models.BucketReferer(allow_empty_referer, referers, black_referers=None)
701+
self.bucket.put_bucket_referer(config)
702+
wait_meta_sync()
703+
704+
result = self.bucket.get_bucket_referer()
705+
self.assertFalse(result.allow_empty_referer)
706+
self.assertEqual(True, result.allow_truncate_query_string)
707+
self.assertEqual(sorted(to_string(r) for r in referers), sorted(to_string(r) for r in result.referers))
708+
self.assertEqual([], result.black_referers)
709+
710+
# black referer 4
711+
config = oss2.models.BucketReferer(allow_empty_referer, referers, black_referers=[])
712+
self.bucket.put_bucket_referer(config)
713+
wait_meta_sync()
714+
715+
result = self.bucket.get_bucket_referer()
716+
self.assertFalse(result.allow_empty_referer)
717+
self.assertEqual(True, result.allow_truncate_query_string)
718+
self.assertEqual(sorted(to_string(r) for r in referers), sorted(to_string(r) for r in result.referers))
719+
self.assertEqual([], result.black_referers)
720+
670721
def test_location(self):
671722
result = self.bucket.get_bucket_location()
672723
self.assertTrue(result.location)

unittests/test_bucket.py

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2271,5 +2271,135 @@ def test_delete_bucket_style(self, do_request):
22712271

22722272
self.assertRequest(req_info, request_text)
22732273

2274+
2275+
@patch('oss2.Session.do_request')
2276+
def test_put_black_referer(self, do_request):
2277+
from oss2.models import BucketReferer
2278+
2279+
request_text = '''PUT /?referer= HTTP/1.1
2280+
Host: ming-oss-share.oss-cn-hangzhou.aliyuncs.com
2281+
Accept-Encoding: identity
2282+
Connection: keep-alive
2283+
Content-Length: 249
2284+
date: Sat, 12 Dec 2015 00:35:46 GMT
2285+
User-Agent: aliyun-sdk-python/2.0.2(Windows/7/;3.3.3)
2286+
Accept: */*
2287+
authorization: OSS ZCDmm7TPZKHtx77j:Kq2RS9nmT44C1opXGbcLzNdTt1A=
2288+
2289+
<RefererConfiguration>
2290+
<AllowEmptyReferer>true</AllowEmptyReferer>
2291+
<RefererList>
2292+
<Referer>http://hello.com</Referer>
2293+
<Referer>mibrowser:home</Referer>
2294+
<Referer>{0}</Referer>
2295+
</RefererList>
2296+
<AllowTruncateQueryString>true</AllowTruncateQueryString>
2297+
<RefererBlacklist>
2298+
<Referer>http://hello2.com</Referer>
2299+
<Referer>mibrowser2:home</Referer>
2300+
<Referer>{1}</Referer>
2301+
</RefererBlacklist>
2302+
</RefererConfiguration>'''
2303+
2304+
response_text = '''HTTP/1.1 200 OK
2305+
Server: AliyunOSS
2306+
Date: Sat, 12 Dec 2015 00:35:46 GMT
2307+
Content-Length: 0
2308+
Connection: keep-alive
2309+
x-oss-request-id: 566B6BE244ABFA2608E5A8AD'''
2310+
2311+
req_info = mock_response(do_request, response_text)
2312+
2313+
bucket().put_bucket_referer(BucketReferer(True, ['http://hello.com', 'mibrowser:home', '阿里巴巴'], True, ['http://hello2.com', 'mibrowser2:home', '阿里巴巴2']))
2314+
2315+
self.assertRequest(req_info, request_text.format('阿里巴巴', '阿里巴巴2'))
2316+
2317+
2318+
@patch('oss2.Session.do_request')
2319+
def test_get_black_referer(self, do_request):
2320+
request_text = '''GET /?referer= HTTP/1.1
2321+
Host: ming-oss-share.oss-cn-hangzhou.aliyuncs.com
2322+
Accept-Encoding: identity
2323+
Connection: keep-alive
2324+
date: Sat, 12 Dec 2015 00:35:47 GMT
2325+
User-Agent: aliyun-sdk-python/2.0.2(Windows/7/;3.3.3)
2326+
Accept: */*
2327+
authorization: OSS ZCDmm7TPZKHtx77j:nWqS3JExf/lsVxm+Sbxbg2cQyrc='''
2328+
2329+
response_text = '''HTTP/1.1 200 OK
2330+
Server: AliyunOSS
2331+
Date: Sat, 12 Dec 2015 00:35:47 GMT
2332+
Content-Type: application/xml
2333+
Content-Length: 319
2334+
Connection: keep-alive
2335+
x-oss-request-id: 566B6BE3BCD1D4FE65D449A2
2336+
2337+
<?xml version="1.0" encoding="UTF-8"?>
2338+
<RefererConfiguration>
2339+
<AllowEmptyReferer>true</AllowEmptyReferer>
2340+
<AllowTruncateQueryString>false</AllowTruncateQueryString>
2341+
<RefererList>
2342+
<Referer>http://hello.com</Referer>
2343+
<Referer>mibrowser:home</Referer>
2344+
<Referer>{0}</Referer>
2345+
</RefererList>
2346+
<RefererBlacklist>
2347+
<Referer>http://www.aliyun.com</Referer>
2348+
<Referer>mibrowser:home.com</Referer>
2349+
</RefererBlacklist>
2350+
</RefererConfiguration>'''.format('阿里巴巴')
2351+
2352+
req_info = mock_response(do_request, response_text)
2353+
2354+
result = bucket().get_bucket_referer()
2355+
2356+
self.assertRequest(req_info, request_text)
2357+
2358+
self.assertEqual(result.allow_empty_referer, True)
2359+
self.assertEqual(result.allow_truncate_query_string, False)
2360+
self.assertSortedListEqual(result.referers, ['http://hello.com', 'mibrowser:home', '阿里巴巴'])
2361+
self.assertSortedListEqual(result.black_referers, ['http://www.aliyun.com', 'mibrowser:home.com'])
2362+
2363+
2364+
@patch('oss2.Session.do_request')
2365+
def test_get_black_referer2(self, do_request):
2366+
request_text = '''GET /?referer= HTTP/1.1
2367+
Host: ming-oss-share.oss-cn-hangzhou.aliyuncs.com
2368+
Accept-Encoding: identity
2369+
Connection: keep-alive
2370+
date: Sat, 12 Dec 2015 00:35:47 GMT
2371+
User-Agent: aliyun-sdk-python/2.0.2(Windows/7/;3.3.3)
2372+
Accept: */*
2373+
authorization: OSS ZCDmm7TPZKHtx77j:nWqS3JExf/lsVxm+Sbxbg2cQyrc='''
2374+
2375+
response_text = '''HTTP/1.1 200 OK
2376+
Server: AliyunOSS
2377+
Date: Sat, 12 Dec 2015 00:35:47 GMT
2378+
Content-Type: application/xml
2379+
Content-Length: 319
2380+
Connection: keep-alive
2381+
x-oss-request-id: 566B6BE3BCD1D4FE65D449A2
2382+
2383+
<?xml version="1.0" encoding="UTF-8"?>
2384+
<RefererConfiguration>
2385+
<AllowEmptyReferer>true</AllowEmptyReferer>
2386+
<RefererList>
2387+
<Referer>http://hello.com</Referer>
2388+
<Referer>mibrowser:home</Referer>
2389+
<Referer>{0}</Referer>
2390+
</RefererList>
2391+
2392+
</RefererConfiguration>'''.format('阿里巴巴')
2393+
2394+
req_info = mock_response(do_request, response_text)
2395+
2396+
result = bucket().get_bucket_referer()
2397+
2398+
self.assertRequest(req_info, request_text)
2399+
self.assertEqual(result.allow_empty_referer, True)
2400+
self.assertSortedListEqual(result.referers, ['http://hello.com', 'mibrowser:home', '阿里巴巴'])
2401+
2402+
2403+
22742404
if __name__ == '__main__':
22752405
unittest.main()

0 commit comments

Comments
 (0)