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
|
import base64
import requests
import botocore
from swift.common.utils import md5
import test.functional as tf
from test.functional.s3api import S3ApiBaseBoto3
class TestS3ApiXxeInjection(S3ApiBaseBoto3):
def setUp(self):
super(TestS3ApiXxeInjection, self).setUp()
self.bucket = 'test-s3api-xxe-injection'
def _create_bucket(self, **kwargs):
resp = self.conn.create_bucket(Bucket=self.bucket, **kwargs)
response_metadata = resp.pop('ResponseMetadata', {})
self.assertEqual(200, response_metadata.get('HTTPStatusCode'))
@staticmethod
def _clear_data(request, **_kwargs):
request.data = b''
def _presign_url(self, method, key=None, **kwargs):
params = {
'Bucket': self.bucket
}
if key:
params['Key'] = key
params.update(kwargs)
try:
self.conn.meta.events.register(
'before-sign.s3.*', self._clear_data)
return self.conn.generate_presigned_url(
method, Params=params, ExpiresIn=60)
finally:
self.conn.meta.events.unregister(
'before-sign.s3.*', self._clear_data)
def test_put_bucket_acl(self):
if not tf.cluster_info['s3api'].get('s3_acl'):
self.skipTest('s3_acl must be enabled')
self._create_bucket()
url = self._presign_url('put_bucket_acl')
resp = requests.put(url, data="""
<!DOCTYPE foo [<!ENTITY xxe SYSTEM "file:///etc/swift/swift.conf"> ]>
<AccessControlPolicy xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<Owner>
<DisplayName>test:tester</DisplayName>
<ID>test:tester</ID>
</Owner>
<AccessControlList>
<Grant>
<Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="CanonicalUser">
<DisplayName>name&xxe;</DisplayName>
<ID>id&xxe;</ID>
</Grantee>
<Permission>WRITE</Permission>
</Grant>
</AccessControlList>
</AccessControlPolicy>
""")
self.assertEqual(200, resp.status_code)
self.assertNotIn(b'xxe', resp.content)
self.assertNotIn(b'[swift-hash]', resp.content)
acl = self.conn.get_bucket_acl(Bucket=self.bucket)
response_metadata = acl.pop('ResponseMetadata', {})
self.assertEqual(200, response_metadata.get('HTTPStatusCode'))
self.assertDictEqual({
'Owner': {
'DisplayName': 'test:tester',
'ID': 'test:tester'
},
'Grants': [
{
'Grantee': {
'DisplayName': 'id',
'ID': 'id',
'Type': 'CanonicalUser'
},
'Permission': 'WRITE'
}
]
}, acl)
def test_create_bucket(self):
url = self._presign_url('create_bucket')
resp = requests.put(url, data="""
<!DOCTYPE foo [<!ENTITY xxe SYSTEM "file:///etc/swift/swift.conf"> ]>
<CreateBucketConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<LocationConstraint>&xxe;</LocationConstraint>
</CreateBucketConfiguration>
""")
self.assertEqual(400, resp.status_code)
self.assertNotIn(b'xxe', resp.content)
self.assertNotIn(b'[swift-hash]', resp.content)
self.assertRaisesRegex(
botocore.exceptions.ClientError, 'Not Found',
self.conn.head_bucket, Bucket=self.bucket)
def test_delete_objects(self):
self._create_bucket()
url = self._presign_url(
'delete_objects',
Delete={
'Objects': [
{
'Key': 'string',
'VersionId': 'string'
}
]
})
body = """
<!DOCTYPE foo [<!ENTITY xxe SYSTEM "file:///etc/swift/swift.conf"> ]>
<Delete xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<Object>
<Key>&xxe;</Key>
</Object>
</Delete>
"""
body = body.encode('utf-8')
content_md5 = (
base64.b64encode(md5(body, usedforsecurity=False).digest()))
resp = requests.post(
url, headers={'Content-MD5': content_md5}, data=body)
self.assertEqual(400, resp.status_code)
self.assertNotIn(b'xxe', resp.content)
self.assertNotIn(b'[swift-hash]', resp.content)
def test_complete_multipart_upload(self):
self._create_bucket()
resp = self.conn.create_multipart_upload(
Bucket=self.bucket, Key='test')
response_metadata = resp.pop('ResponseMetadata', {})
self.assertEqual(200, response_metadata.get('HTTPStatusCode'))
uploadid = resp.get('UploadId')
try:
url = self._presign_url(
'complete_multipart_upload',
Key='key',
MultipartUpload={
'Parts': [
{
'ETag': 'string',
'PartNumber': 1
}
],
},
UploadId=uploadid)
resp = requests.post(url, data="""
<!DOCTYPE foo [<!ENTITY xxe SYSTEM "file:///etc/swift/swift.conf"> ]>
<CompleteMultipartUpload xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<Part>
<ETag>"{uploadid}"</ETag>
<PartNumber>&xxe;</PartNumber>
</Part>
</CompleteMultipartUpload>
""")
self.assertEqual(404, resp.status_code)
self.assertNotIn(b'xxe', resp.content)
self.assertNotIn(b'[swift-hash]', resp.content)
resp = requests.post(url, data="""
<!DOCTYPE foo [<!ENTITY xxe SYSTEM "file:///etc/swift/swift.conf"> ]>
<CompleteMultipartUpload xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<Part>
<ETag>"&xxe;"</ETag>
<PartNumber>1</PartNumber>
</Part>
</CompleteMultipartUpload>
""")
self.assertEqual(404, resp.status_code)
self.assertNotIn(b'xxe', resp.content)
self.assertNotIn(b'[swift-hash]', resp.content)
finally:
resp = self.conn.abort_multipart_upload(
Bucket=self.bucket, Key='test', UploadId=uploadid)
response_metadata = resp.pop('ResponseMetadata', {})
self.assertEqual(204, response_metadata.get('HTTPStatusCode'))
def test_put_bucket_versioning(self):
self._create_bucket()
url = self._presign_url(
'put_bucket_versioning',
VersioningConfiguration={
'Status': 'Enabled'
})
resp = requests.put(url, data="""
<!DOCTYPE foo [<!ENTITY xxe SYSTEM "file:///etc/swift/swift.conf"> ]>
<VersioningConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<Status>&xxe;</Status>
</VersioningConfiguration>
""")
self.assertEqual(400, resp.status_code)
self.assertNotIn(b'xxe', resp.content)
self.assertNotIn(b'[swift-hash]', resp.content)
versioning = self.conn.get_bucket_versioning(Bucket=self.bucket)
response_metadata = versioning.pop('ResponseMetadata', {})
self.assertEqual(200, response_metadata.get('HTTPStatusCode'))
self.assertDictEqual({}, versioning)
|