1

xssの問題を防ぐためにnode.jsをプロキシとして使用する場合、jsonを転送する際に問題がありました。

受信したデータをログに記録している間、問題は見つかりませんでした。

4

1 に答える 1

3

受信したデータを表示する別の node.js サーバーを作成し、CouchDB サーバーを模倣させたときに解決策が浮かびました。

原因は、ASCII 以外の文字 (Swedish-Å) であることが判明しました。受信したデータは、気分に応じて Content-Length を不適切に計算するか、正しいものとして処理されました。;)

解決策は、Content-Length を計算する前に、Buffer を使用して生データを utf8 に変換することでした。

     :
if (request.method == 'PUT') {
    var data = '';
    request.on('data', function(dataSnippet) {
        data += dataSnippet;
        if (data.length > 1e6) {request.connection.destroy();}
    });
    request.on('end', function(dataSnippet) {
        data = new Buffer(data, 'UTF8');     //<---  This is the solution
        options.headers = {
            'Content-Encoding': 'UTF8',
            'Content-Type': 'application/json',
            'Content-Length': data.length    //<---  Where it went wrong
        }
        proxy = http.request(options, function(proxy_response) {
            proxy_response.setEncoding('UTF8');
            proxy_response.pipe(response);
        });
        proxy.write(data);
        proxy.end();
    });
}
    :
于 2012-12-17T14:21:36.660 に答える