~tribaal/txaws/xss-hardening

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
# Copyright (C) 2009 Canonical Ltd
# Copyright (C) 2009 Duncan McGreggor <oubiwann@adytum.us>
# Copyright (C) 2011 Drew Smathers <drew.smathers@gmail.com>
# Copyright (C) 2012 New Dream Network (DreamHost)
# Licenced under the txaws licence available at /LICENSE in the txaws source.
from txaws.util import XML


class Bucket(object):
    """
    An Amazon S3 storage bucket.
    """
    def __init__(self, name, creation_date):
        self.name = name
        self.creation_date = creation_date


class ItemOwner(object):
    """
    The owner of a content item.
    """
    def __init__(self, id, display_name):
        self.id = id
        self.display_name = display_name


class BucketItem(object):
    """
    The contents of an Amazon S3 bucket.
    """
    def __init__(self, key, modification_date, etag, size, storage_class,
                 owner=None):
        self.key = key
        self.modification_date = modification_date
        self.etag = etag
        self.size = size
        self.storage_class = storage_class
        self.owner = owner


class BucketListing(object):
    """
    A mapping for the data in a bucket listing.
    """
    def __init__(self, name, prefix, marker, max_keys, is_truncated,
                 contents=None, common_prefixes=None):
        self.name = name
        self.prefix = prefix
        self.marker = marker
        self.max_keys = max_keys
        self.is_truncated = is_truncated
        self.contents = contents
        self.common_prefixes = common_prefixes


class LifecycleConfiguration(object):
    """
    Returns the lifecycle configuration information set on the bucket.
    """
    def __init__(self, rules):
        self.rules = rules


class LifecycleConfigurationRule(object):
    """
    Container for elements that describe a lifecycle rule.
    """
    def __init__(self, id, prefix, status, expiration):
        self.id = id
        self.prefix = prefix
        self.status = status
        self.expiration = expiration


class WebsiteConfiguration(object):
    """
    A mapping for the data in a bucket website configuration.
    """
    def __init__(self, index_suffix, error_key=None):
        self.index_suffix = index_suffix
        self.error_key = error_key


class NotificationConfiguration(object):
    """
    A mapping for the data in a bucket notification configuration.
    """
    def __init__(self, topic=None, event=None):
        self.topic = topic
        self.event = event


class VersioningConfiguration(object):
    """
    Container for the bucket versioning configuration.

    According to Amazon:

    C{MfaDelete}: This element is only returned if the bucket has been
    configured with C{MfaDelete}. If the bucket has never been so configured,
    this element is not returned. The possible values are None, "Disabled" or
    "Enabled".

    C{Status}: If the bucket has never been so configured, this element is not
    returned. The possible values are None, "Suspended" or "Enabled".
    """
    def __init__(self, mfa_delete=None, status=None):
        self.mfa_delete = mfa_delete
        self.status = status


class FileChunk(object):
    """
    An Amazon S3 file chunk.

    S3 returns file chunks, 10 MB at a time, until the entire file is returned.
    These chunks need to be assembled once they are all returned.
    """


class RequestPayment(object):
    """
    A payment request.

    @param payer: One of 'Requester' or 'BucketOwner'.
    """

    payer_choices = ("Requester", "BucketOwner")

    def __init__(self, payer):
        if payer not in self.payer_choices:
            raise ValueError("Invalid value for payer: `%s`. Must be one of "
                             "%s." % (payer, ",".join(self.payer_choices)))
        self.payer = payer

    def to_xml(self):
        """
        Convert this request into a C{RequestPaymentConfiguration} XML
        document.
        """
        return ("<RequestPaymentConfiguration "
                  'xmlns="http://s3.amazonaws.com/doc/2006-03-01/">\n'
                "  <Payer>%s</Payer>\n"
                "</RequestPaymentConfiguration>" % self.payer)

    @classmethod
    def from_xml(cls, xml_bytes):
        """
        Create an instance from a C{RequestPaymentConfiguration} XML document.
        """
        root = XML(xml_bytes)
        return cls(root.findtext("Payer"))


class MultipartInitiationResponse(object):
    """
    A response to Initiate Multipart Upload
    """

    def __init__(self, bucket, object_name, upload_id):
        """
        @param bucket: The bucket name
        @param object_name: The object name
        @param upload_id: The upload id
        """
        self.bucket = bucket
        self.object_name = object_name
        self.upload_id = upload_id

    @classmethod
    def from_xml(cls, xml_bytes):
        """
        Create an instance of this from XML bytes.

        @param xml_bytes: C{str} bytes of XML to parse
        @return: an instance of L{MultipartInitiationResponse}
        """
        root = XML(xml_bytes)
        return cls(root.findtext('Bucket'),
                   root.findtext('Key'),
                   root.findtext('UploadId'))


class MultipartCompletionResponse(object):
    """
    Represents a response to Complete Multipart Upload
    """

    def __init__(self, location, bucket, object_name, etag):
        """
        @param location: The URI identifying newly created object
        @param bucket: The bucket name
        @param object_name: The object name / key
        @param etag: The entity tag
        """
        self.location = location
        self.bucket = bucket
        self.object_name = object_name
        self.etag = etag

    @classmethod
    def from_xml(cls, xml_bytes):
        """
        Create an instance of this class from XML bytes.

        @param xml_bytes: C{str} bytes of XML to parse
        @return: an instance of L{MultipartCompletionResponse}
        """
        root = XML(xml_bytes)
        return cls(root.findtext('Location'),
                   root.findtext('Bucket'),
                   root.findtext('Key'),
                   root.findtext('ETag'))