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
230
|
============
The scrubber
============
The scrubber is an integral part of Mailman, both in the normal delivery of
messages and in components such as the archiver. Its primary purpose is to
scrub attachments from messages so that binary goop doesn't end up in an
archive message.
>>> mlist = create_list('_xtest@example.com')
>>> mlist.preferred_language = 'en'
Helper functions for getting the attachment data.
::
>>> import os, re
>>> def read_attachment(filename, remove=True):
... path = os.path.join(config.PRIVATE_ARCHIVE_FILE_DIR,
... mlist.fqdn_listname, filename)
... fp = open(path)
... try:
... data = fp.read()
... finally:
... fp.close()
... if remove:
... os.unlink(path)
... return data
>>> from urlparse import urlparse
>>> def read_url_from_message(msg):
... url = None
... for line in msg.get_payload().splitlines():
... mo = re.match('URL: <(?P<url>[^>]+)>', line)
... if mo:
... url = mo.group('url')
... break
... path = '/'.join(urlparse(url).path.split('/')[3:])
... return read_attachment(path)
Saving attachments
==================
The Scrubber handler exposes a function called ``save_attachment()`` which can
be used to strip various types of attachments and store them in the archive
directory. This is a public interface used by components outside the normal
processing pipeline.
Site administrators can decide whether the scrubber should use the attachment
filename suggested in the message's ``Content-Disposition:`` header or not.
If enabled, the filename will be used when this header attribute is present
(yes, this is an unfortunate double negative).
::
>>> config.push('test config', """
... [scrubber]
... use_attachment_filename: yes
... """)
>>> msg = message_from_string("""\
... Content-Type: image/gif; name="xtest.gif"
... Content-Transfer-Encoding: base64
... Content-Disposition: attachment; filename="xtest.gif"
...
... R0lGODdhAQABAIAAAAAAAAAAACwAAAAAAQABAAACAQUAOw==
... """)
>>> from mailman.pipeline.scrubber import save_attachment
>>> print save_attachment(mlist, msg, 'dir')
<http://www.example.com/pipermail/_xtest@example.com/dir/xtest.gif>
>>> data = read_attachment('dir/xtest.gif')
>>> print data[:6]
GIF87a
>>> len(data)
34
Saving the attachment does not alter the original message.
>>> print msg.as_string()
Content-Type: image/gif; name="xtest.gif"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="xtest.gif"
<BLANKLINE>
R0lGODdhAQABAIAAAAAAAAAAACwAAAAAAQABAAACAQUAOw==
The site administrator can also configure Mailman to ignore the
``Content-Disposition:`` filename. This is the default.
>>> config.pop('test config')
>>> config.push('test config', """
... [scrubber]
... use_attachment_filename: no
... """)
>>> msg = message_from_string("""\
... Content-Type: image/gif; name="xtest.gif"
... Content-Transfer-Encoding: base64
... Content-Disposition: attachment; filename="xtest.gif"
...
... R0lGODdhAQABAIAAAAAAAAAAACwAAAAAAQABAAACAQUAOw==
... """)
>>> print save_attachment(mlist, msg, 'dir')
<http://www.example.com/pipermail/_xtest@example.com/dir/attachment.gif>
>>> data = read_attachment('dir/xtest.gif')
Traceback (most recent call last):
IOError: [Errno ...] No such file or directory:
u'.../archives/private/_xtest@example.com/dir/xtest.gif'
>>> data = read_attachment('dir/attachment.gif')
>>> print data[:6]
GIF87a
>>> len(data)
34
Scrubbing image attachments
===========================
When scrubbing image attachments, the original message is modified to include
a reference to the attachment file as available through the on-line archive.
>>> msg = message_from_string("""\
... MIME-Version: 1.0
... Content-Type: multipart/mixed; boundary="BOUNDARY"
...
... --BOUNDARY
... Content-type: text/plain; charset=us-ascii
...
... This is a message.
... --BOUNDARY
... Content-Type: image/gif; name="xtest.gif"
... Content-Transfer-Encoding: base64
... Content-Disposition: attachment; filename="xtest.gif"
...
... R0lGODdhAQABAIAAAAAAAAAAACwAAAAAAQABAAACAQUAOw==
... --BOUNDARY--
... """)
>>> msgdata = {}
The ``Scrubber.process()`` function is different than other handler process
functions in that it returns the scrubbed message.
>>> from mailman.pipeline.scrubber import process
>>> scrubbed_msg = process(mlist, msg, msgdata)
>>> scrubbed_msg is msg
True
>>> print scrubbed_msg.as_string()
MIME-Version: 1.0
Message-ID: ...
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
<BLANKLINE>
This is a message.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: xtest.gif
Type: image/gif
Size: 34 bytes
Desc: not available
URL: <http://www.example.com/pipermail/_xtest@example.com/attachments/.../attachment.gif>
<BLANKLINE>
This is the same as the transformed message originally passed in.
>>> print msg.as_string()
MIME-Version: 1.0
Message-ID: ...
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
<BLANKLINE>
This is a message.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: xtest.gif
Type: image/gif
Size: 34 bytes
Desc: not available
URL: <http://www.example.com/pipermail/_xtest@example.com/attachments/.../attachment.gif>
<BLANKLINE>
>>> msgdata
{}
The URL will point to the attachment sitting in the archive.
>>> data = read_url_from_message(msg)
>>> data[:6]
'GIF87a'
>>> len(data)
34
Scrubbing text attachments
==========================
Similar to image attachments, text attachments will also be scrubbed, but the
placeholder will be slightly different.
>>> msg = message_from_string("""\
... MIME-Version: 1.0
... Content-Type: multipart/mixed; boundary="BOUNDARY"
...
... --BOUNDARY
... Content-type: text/plain; charset=us-ascii; format=flowed; delsp=no
...
... This is a message.
... --BOUNDARY
... Content-type: text/plain; name="xtext.txt"
... Content-Disposition: attachment; filename="xtext.txt"
...
... This is a text attachment.
... --BOUNDARY--
... """)
>>> scrubbed_msg = process(mlist, msg, {})
>>> print scrubbed_msg.as_string()
MIME-Version: 1.0
Message-ID: ...
Content-Transfer-Encoding: 7bit
Content-Type: text/plain; charset="us-ascii"; format="flowed"; delsp="no"
<BLANKLINE>
This is a message.
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: xtext.txt
URL: <http://www.example.com/pipermail/_xtest@example.com/attachments/.../attachment.txt>
<BLANKLINE>
>>> read_url_from_message(msg)
'This is a text attachment.'
Clean up
========
>>> config.pop('test config')
|