Bug discovered: Base64 line breaks

You must Login before you can answer or comment on any questions.

Hello all,

apparently the base64encode method per default inserts line breaks into strings which encoded are longer than 72 chars, which may not always be desirable. Example:

var xhr = Ti.Network.createHTTPClient();
xhr.open('GET', 'http://some.resource.com');
authstr = Titanium.Utils.base64encode(username.value + ':' + password.value).toString();
// If username and password are long, the resulting encoded string will be long, 
// and a line break will be inserted after the 72nd character
 
// Host requires HTTP Basic Authentication
xhr.setRequestHeader('Authorization', 'Basic ' + authstr); 
xhr.send();
The problem here is that xhr.setRequestHeader will not work because of the line break. The Authorization header will not be set, and the host will not recognize the user and send a 401 back.

A workaround is to remove the line breaks from the string:

authstr.replace(/(\r\n|\n|\r)/gm,"");
However, in my opinion the base64encode method should at least provide the option to return the string without line breaks. It seems that the default way of using base64 encoding is with line breaks, but usually it's possible to get the line breaks removed directly in the encoding as well. The way it is right now, headers mysteriously get lost and it takes quite a lot of debugging with HTTP sniffers and trial/error coding to work out why.

I have only tested this on iPhone, not on Android, but I suspect this is an all-round issue.

1 Answer

Of course, the bug is probably not so much in the fact that the base64encode function is too simple and more in the fact that you can pass a value to the setRequestHeader function and it will just silently fail without so much as a warning.

I'd love to see a HTTP sniffer built into the Titanium Developer by the way - with one of those I could have found the error a lot sooner than I did.

— answered 2 years ago by Jacob Avlund
answer permalink
4 Comments
  • Please don't answer your own questions - use the comment function instead. Also, remember you can update your original post, if the situation warrants it.

    — commented 2 years ago by Paul Dowsett

  • I'll keep that in mind, sorry. It would be nice with a real bug tracking system to put this thing into though.

    — commented 2 years ago by Jacob Avlund

  • Jacob

    There is a bug tracking system, and anyone can create tickets in it. A word of warning, however - it is absolutely imperative that anyone wishing to do so follows the Submitting Bug Reports guide precisely, or else they not only risk distracting the core dev team from their most important task (fixing bugs and coding enhancements) but also no doubt risk the company revoking this privilege from the whole community. In other words, creating tickets should not be taken lightly.

    Cheers

    — commented 2 years ago by Paul Dowsett

  • Show 1 more comment

Your Answer

This question has been locked and cannot accept new answers.