wefork / wekan

The open-source Trello-like kanban (built with Meteor)
https://wekan.io
MIT License
61 stars 12 forks source link

Download file(unicode filename) action cause server process crashed with exception. #87

Closed yatusiter closed 7 years ago

yatusiter commented 7 years ago

When click download from card with unicode(Chinese) filename (upload is OK), the server process crash with following exception :

./wekan/bundle/programs/server/node_modules/fibers/future.js:280 throw(ex); ^ TypeError: The header content contains invalid characters at ServerResponse.OutgoingMessage.setHeader (http.js:733:13) at ServerResponse.res.setHeader (/home/sjyb/install/wekan/bundle/programs/server/npm/node_modules/meteor/webapp/node_modules/connect/lib/patch.js:134:22) at packages/cfshttp-methods/http.methods.server.api.js:599:1 at Function..each._.forEach (packages/underscore/underscore.js:113:1) at packages/cfs_http-methods/http.methods.server.api.js:595:1


After googled around, it's seems the HTTP header "Content-Disposition" cannot accept unicode encoding characters, and should be binary or URI encoded.

https://en.wikipedia.org/wiki/List_of_HTTP_header_fields https://my.oschina.net/jsan/blog/180333

Temporary fix with this solution:

bundle/programs/server/packages/cfs_access-point.js add after line 444, FS.HTTP.Handlers.Get function:

const originalHandler = FS.HTTP.Handlers.Get;
FS.HTTP.Handlers.Get = function (ref) {
//console.log(ref.filename);
  try {
     var userAgent = (this.requestHeaders['user-agent']||'').toLowerCase();

        if(userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) {
            ref.filename =  encodeURIComponent(ref.filename);
        } else if(userAgent.indexOf('firefox') >= 0) {
            ref.filename = new Buffer(ref.filename).toString('binary');
        } else {
            /* safari*/
            ref.filename = new Buffer(ref.filename).toString('binary');
        }   
   } catch (ex){
        ref.filename = 'tempfix';
   } 
   return originalHandler.call(this, ref);
};
Daniel231 commented 7 years ago

@yatusiter i tried your temporary fix but i cant edit the package, can you be more specific where did you add the code? (i mean after which function)

yatusiter commented 7 years ago

@Daniel231 just after the original definiton of FS.HTTP.Handlers.Get

bundle/programs/server/packages/cfs_access-point.js add after line 444, FS.HTTP.Handlers.Get function:

Daniel231 commented 7 years ago

@yatusiter Success! thx :D

xet7 commented 7 years ago

Moved to https://github.com/wekan/wekan/issues/784