[erlang-questions] base64:decode fails on facebook base64 encoded strings

Max Bourinov <>
Thu Dec 29 05:30:09 CET 2011


So Erlangers,

I wrote about this little bug in Facebook and I got an answer!

>>I believe the trailing = is an optional padding in base64 your code needs
to accept base64 with and without trailing '=' characters. See last part of
http://email.about.com/cs/standards/a/base64_encoding.htm

This is what they said. That is very interesting, I personally would not
omit data from protocol specification, but it appears that if it possible.
Anyway I didn't see anything about omitting here
http://tools.ietf.org/html/rfc1421 or http://tools.ietf.org/html/rfc2045

So, if you ever face such a loose implementation of base64 here is a
snippet that might help you to deal with it:

-spec fb_decode_base64(Base64 :: list()) -> binary() | error.
fb_decode_base64(Base64) when is_list(Base64) ->
    try base64:decode(Base64)
    catch
        error:_ -> % could be missing =
        	try base64:decode(Base64 ++ "=")
        	catch
        		error:_ -> % could be missing ==
        			try base64:decode(Base64 ++ "==")
        			catch
        				error:_ -> % base64 is really wrong. we cannot fix it
        					error
        			end
        	end

    end.



On Wed, Dec 21, 2011 at 7:54 PM, Heinz N. Gies <> wrote:

> As an addition look closely at the decoding on the webside, it also fails
> the last two characters are missing unless you add the missing '=' to the
> base64 encoded data.
>
> Regards,
> Heinz
>
> --
> Heinz N. Gies
> 
> http://licenser.net
>
> On Dec 21, 2011, at 16:44, Max Bourinov wrote:
>
> Hi guys,
>
> Does anybody knows why base64:decode fails on strings like this:
>
>
> eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsImNyZWRpdHMiOnsib3JkZXJfZGV0YWlscyI6IntcIm9yZGVyX2lkXCI6Mjg1NTAwMDgxNTAxMTU4LFwiYnV5ZXJcIjoxMDI3NDE5NzkzLFwiYXBwXCI6MTY4MzExOTc2NTgwNDUwLFwicmVjZWl2ZXJcIjoxMDI3NDE5NzkzLFwiYW1vdW50XCI6MSxcInVwZGF0ZV90aW1lXCI6MTMyNDQ3OTA3NixcInRpbWVfcGxhY2VkXCI6MTMyNDQ3OTA3NCxcImRhdGFcIjpcIlwiLFwiaXRlbXNcIjpbe1wiaXRlbV9pZFwiOlwiMFwiLFwidGl0bGVcIjpcIkEgRmFjZWJvb2sgSGF0XCIsXCJkZXNjcmlwdGlvblwiOlwiVGhlIGNvb2xlc3QgaGF0IHlvdSd2ZSBldmVyIHNlZW4uXCIsXCJpbWFnZV91cmxcIjpcImh0dHA6XFxcL1xcXC93d3cuZmFjZWJvb2suY29tXFxcL2ltYWdlc1xcXC9naWZ0c1xcXC83NDAucG5nXCIsXCJwcm9kdWN0X3VybFwiOlwiaHR0cDpcXFwvXFxcL3d3dy5mYWNlYm9vay5jb21cXFwvaW1hZ2VzXFxcL2dpZnRzXFxcLzc0MC5wbmdcIixcInByaWNlXCI6MSxcImRhdGFcIjpcIlwifV0sXCJzdGF0dXNcIjpcInBsYWNlZFwifSIsInN0YXR1cyI6InBsYWNlZCIsIm9yZGVyX2lkIjoyODU1MDAwODE1MDExNTgsInRlc3RfbW9kZSI6MX0sImV4cGlyZXMiOjEzMjQ0ODMyMDAsImlzc3VlZF9hdCI6MTMyNDQ3OTA3Niwib2F1dGhfdG9rZW4iOiJBQUFDWkFGQ1pCVFZXSUJBQzBhYkhWb3dLRWdTVHVOQ3ZDYjRTR21hWkE4STdYc3Z0bXBZZU1hTWx3clNia0U5dkxKTHRldUdraWpNakh4aVpBVWY5am9RcG9aQ3ZWYW9YVUN6N21zU3Bjb0FuWGhKRDVRU0lRIiwidXNlciI6eyJjb3VudHJ5IjoicnUiLCJsb2NhbGUiOiJlbl9VUyIsImFnZSI6eyJtaW4iOjIxfX0sInVzZXJfaWQiOiIxMDI3NDE5NzkzIn0
>
> You can easily decode it with online tool like this:
> http://www.motobit.com/util/base64-decoder-encoder.asp
>
> But lovely Erlang says:
>
> ** exception error: no function clause matching base64:decode("Jye",
>
> [133,177,157,189,201,165,209,161,180,136,232,137,33,53,5,12,
>
>  181,77,33,4,200,212,216,136,176,137,141|...])
>      in function  base64:decode/1
>
> Seems something wrong with pattern matching or what could it be? And most
> interesting question - how to find a proper work around?
>
>
> Best regards,
> Max
>
>
>  _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20111229/ebb41f34/attachment.html>


More information about the erlang-questions mailing list