Skip to content

Commit 69c44f4

Browse files
committed
fix(openapi): filter key with undefined-value in sig calculation
1 parent b9d6dd0 commit 69c44f4

File tree

2 files changed

+50
-21
lines changed

2 files changed

+50
-21
lines changed

bin/tsw/util/openapi.js

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,37 @@
77
*/
88
'use strict';
99

10-
1110
const crypto = require('crypto');
1211

1312
/**
1413
* 计算openapi签名
15-
* @param {[string]} method 请求方法 GET/POST
16-
* @param {[string]} pathname 请求路径
17-
* @param {[object]} data 请求数据
18-
* @param {[string]} appkey 应用appkey
19-
* @return {[string]} sig 签名结果
14+
* @param {object} opt 签名数据
15+
* @param {string} opt.method 请求方法 GET/POST
16+
* @param {string} opt.pathname 请求路径
17+
* @param {object} opt.data 请求数据
18+
* @param {string} opt.appkey 应用appkey
19+
* @return {string} sig 签名结果
2020
*/
21-
this.signature = function(opt) {
21+
this.signature = opt => {
2222
opt = opt || {};
2323

2424
const queryArray = [];
2525

2626
const busidataArr = [opt.method, encode(opt.pathname)]; // HTTP请求方式 & encode(uri) & encode(a=x&b=y&...)
2727

28-
let i;
29-
for (i in opt.data) {
30-
i !== 'sig' && queryArray.push(i + '=' + opt.data[i]);
28+
for (const i in opt.data) {
29+
// i !== 'sig' && queryArray.push(i + '=' + opt.data[i]); // 过滤掉undefined value的key,因为发送的时候data,会做JSON.stringify, 没有定义值的key会被过滤掉
30+
if (typeof opt.data[i] !== 'undefined' && i !== 'sig') {
31+
queryArray.push(i + '=' + opt.data[i]);
32+
}
3133
}
3234

33-
queryArray.sort(function(val1, val2) {
35+
queryArray.sort((val1, val2) => {
3436
if (val1 > val2) {
3537
return 1;
36-
}
37-
38-
if (val1 < val2) {
38+
} else if (val1 < val2) {
3939
return -1;
4040
}
41-
4241
return 0;
4342
});
4443

@@ -49,14 +48,14 @@ this.signature = function(opt) {
4948
};
5049

5150
// encode
52-
function encode(str) {
51+
const encode = (str = '') => {
5352
let res = encodeURIComponent(str);
5453

5554
// 0~9 a~z A~Z !*()
56-
res = res.replace(/[^0-9a-zA-Z\-_.%]/g, function($0) {
57-
// 不用考虑一位数了
58-
return '%' + $0.charCodeAt(0).toString(16).toUpperCase();
59-
});
55+
// 不用考虑一位数了
56+
res = res.replace(/[^0-9a-zA-Z\-_.%]/g, ($0) =>
57+
'%' + $0.charCodeAt(0).toString(16).toUpperCase()
58+
);
6059

6160
return res;
62-
}
61+
};

test/bin/tsw/util/openapi.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,35 @@ describe('test openapi', () => {
4949
};
5050
expect(openapi.signature(opt)).to.equal('RaeBv27oo3hOlDAYLXorp4zYhbs=');
5151
});
52+
53+
it('#test undefined value key', () => {
54+
let undefinedValue;
55+
const opt = {
56+
method: 'get',
57+
pathname: '/api',
58+
data: { a: 1, key: undefinedValue }
59+
};
60+
expect(openapi.signature(opt)).to.equal('grX3/m7QAZ19L5WXM6xDzmXQa8s=');
61+
});
62+
63+
it('#test null value key', () => {
64+
const undefinedValue = null;
65+
const opt = {
66+
method: 'get',
67+
pathname: '/api',
68+
data: { a: 1, key: undefinedValue }
69+
};
70+
expect(openapi.signature(opt)).to.equal('3hfVrbEv5VKMYu9QP4kB6rtBd4o=');
71+
});
72+
73+
it('#test empty value key', () => {
74+
const undefinedValue = '';
75+
const opt = {
76+
method: 'get',
77+
pathname: '/api',
78+
data: { a: 1, key: undefinedValue }
79+
};
80+
expect(openapi.signature(opt)).to.equal('AGMogi7bjCvtnnCecK+TnZGzR3M=');
81+
});
5282
});
5383
});

0 commit comments

Comments
 (0)