Skip to content

Commit 137c8fa

Browse files
committed
Use only alphanumeric characters for genBoundary
Some services / sites fail when non-alphanumeric values are used for the boundary (even though the RFC permits them). Even Chromium has had to work around this. Fixes #30
1 parent 360981f commit 137c8fa

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/Servant/Multipart.hs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -323,18 +323,24 @@ instance (ToMultipart tag a, HasClient m api, MultipartBackend tag)
323323
-- | Generates a boundary to be used to separate parts of the multipart.
324324
-- Requires 'IO' because it is randomized.
325325
genBoundary :: IO LBS.ByteString
326-
genBoundary = LBS.pack
327-
. foldr (\x acc -> validChars ! x : acc) []
326+
genBoundary = LBS.pack
327+
. map (validChars !)
328328
<$> indices
329329
where
330330
-- the standard allows up to 70 chars, but most implementations seem to be
331331
-- in the range of 40-60, so we pick 55
332-
indices = replicateM 55 . getStdRandom $ randomR (0,73)
333-
-- '()+_,=./+?0123456789:ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
334-
validChars = listArray (0 :: Int, 73)
335-
[ 0x27, 0x28, 0x29, 0x2b, 0x5f, 0x2c, 0x3d, 0x2e
336-
, 0x2f, 0x2b, 0x3f, 0x30, 0x31, 0x32, 0x33, 0x34
337-
, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x41, 0x42
332+
indices = replicateM 55 . getStdRandom $ randomR (0,61)
333+
-- Following Chromium on this one:
334+
-- > The RFC 2046 spec says the alphanumeric characters plus the
335+
-- > following characters are legal for boundaries: '()+_,-./:=?
336+
-- > However the following characters, though legal, cause some sites
337+
-- > to fail: (),./:=+
338+
-- https://github.com/chromium/chromium/blob/6efa1184771ace08f3e2162b0255c93526d1750d/net/base/mime_util.cc#L662-L670
339+
validChars = listArray (0 :: Int, 61)
340+
-- 0-9
341+
[ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
342+
, 0x38, 0x39, 0x41, 0x42
343+
-- A-Z, a-z
338344
, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a
339345
, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52
340346
, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a

0 commit comments

Comments
 (0)