Skip to content

Commit

Permalink
Fix invalid chars caused JSON parse failed (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonTian committed Nov 1, 2017
1 parent 3905889 commit 13cee19
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 4 deletions.
5 changes: 4 additions & 1 deletion lib/api_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// 本文件用于wechat API,基础文件,主要用于Token的处理和mixin机制
const httpx = require('httpx');
const liburl = require('url');
const {
replaceJSONCtlChars
} = require('./util');

class AccessToken {
constructor(accessToken, expireTime) {
Expand Down Expand Up @@ -123,7 +126,7 @@ class API {
if (contentType.indexOf('application/json') !== -1) {
var data;
try {
data = JSON.parse(buffer);
data = JSON.parse(replaceJSONCtlChars(buffer.toString()));
} catch (ex) {
let err = new Error('JSON.parse error. buffer is ' + buffer.toString());
err.name = 'WeChatAPIError';
Expand Down
20 changes: 20 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,23 @@ exports.postJSON = function (data) {
}
};
};


const JSONCtlCharsMap = {
'"': '\\"', // \u0022
'\\': '\\\\', // \u005c
'\b': '\\b', // \u0008
'\f': '\\f', // \u000c
'\n': '\\n', // \u000a
'\r': '\\r', // \u000d
'\t': '\\t' // \u0009
};
const JSONCtlCharsRE = /[\u0000-\u001F\u005C]/g;

function _replaceOneChar(c) {
return JSONCtlCharsMap[c] || '\\u' + (c.charCodeAt(0) + 0x10000).toString(16).substr(1);
}

exports.replaceJSONCtlChars = function (str) {
return str.replace(JSONCtlCharsRE, _replaceOneChar);
};
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "微信公共平台Node库API,ES6版本",
"main": "index.js",
"scripts": {
"test": "make test-all"
"test": "make test-all",
"test-file": "mocha -R spec"
},
"repository": {
"type": "git",
Expand All @@ -19,11 +20,10 @@
"httpx": "^2.1.1"
},
"devDependencies": {
"co-mocha": "*",
"coveralls": "*",
"eslint": "*",
"expect.js": "*",
"mocha": "*",
"mocha": "^4.0.1",
"muk": "*",
"nyc": "^10.2.0",
"rewire": "*",
Expand Down
1 change: 1 addition & 0 deletions test/api_common.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,5 @@ describe('api_common', function () {
}
});
});

});
1 change: 1 addition & 0 deletions test/fixture/invalid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"subscribe":1,"openid":"ogW8rt6i8BmyHaXvleP5SBULysSk","nickname":"Íõ¾ü","sex":1,"language":"zh_CN","city":"y?v\","province":"l?S","country":"","headimgurl":"http://wx.qlogo.cn/mmopen/Q3auHgzwzM4nXzLJEGv1SUt5ibMeibUjmaK45y06UOtaKDc2NgjvjjnsiccgwMPyKOwvstIKA85bM7zziac5m9zmfw/0","subscribe_time":1504246660,"unionid":"oKfK5s5qiqgWMHaeJ6f3sy5TYvx8","remark":"","groupid":0,"tagid_list":[]}
23 changes: 23 additions & 0 deletions test/util.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

const path = require('path');
const fs = require('fs');

const expect = require('expect.js');

const util = require('../lib/util');

const filepath = path.join(__dirname, 'fixture/invalid.json');
const str = fs.readFileSync(filepath, 'utf8').trim();

describe('util', function () {
it('json parse with invalid chars', function () {
expect(() => {
JSON.parse(str);
}).to.throwException(/Unexpected token/);

expect(() => {
JSON.parse(util.replaceJSONCtlChars(str));
}).not.to.throwException();
});
});

0 comments on commit 13cee19

Please sign in to comment.