@tmsfe/tms-core 0.0.161 → 0.0.163
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/encrypt/encrypt-util.ts +33 -11
- package/src/encrypt/index.ts +4 -20
package/package.json
CHANGED
|
@@ -9,6 +9,8 @@ const ecc = require('./nacl.min.js');
|
|
|
9
9
|
const base64Util = require('./nacl-util.min.js');
|
|
10
10
|
/* eslint-enable @typescript-eslint/no-require-imports */
|
|
11
11
|
|
|
12
|
+
const logger = wx.getLogManager({});
|
|
13
|
+
|
|
12
14
|
interface BaseResp<T> {
|
|
13
15
|
success: boolean,
|
|
14
16
|
msg: string,
|
|
@@ -33,6 +35,24 @@ const SERVER_HOST_MAP = {
|
|
|
33
35
|
// 基础工具
|
|
34
36
|
const baseUtil = {
|
|
35
37
|
_isObject: (obj: any): boolean => Object.prototype.toString.call(obj) === '[object Object]',
|
|
38
|
+
// 统一格式化日志输出
|
|
39
|
+
_formatLog(args: any[]): any[] {
|
|
40
|
+
// 小程序日志管理器都只是精确到秒,我们补上毫秒方便分析
|
|
41
|
+
const time = new Date()
|
|
42
|
+
.toISOString()
|
|
43
|
+
.replace('T', ' ')
|
|
44
|
+
.substring(0, 19)
|
|
45
|
+
.replace(/-/g, '-')
|
|
46
|
+
.replace(/:/g, ':');
|
|
47
|
+
args.unshift(time);
|
|
48
|
+
return args;
|
|
49
|
+
},
|
|
50
|
+
logInfo: (...args) => {
|
|
51
|
+
args.unshift('request_encrypt_log');
|
|
52
|
+
const items = baseUtil._formatLog(args);
|
|
53
|
+
console.log(...items);
|
|
54
|
+
logger.log(...items);
|
|
55
|
+
},
|
|
36
56
|
// Uint8Array转为url安全的base64编码
|
|
37
57
|
encUrl: (input: Uint8Array): string => {
|
|
38
58
|
let base64 = base64Util.encode(input);
|
|
@@ -107,8 +127,9 @@ const eccUtil = {
|
|
|
107
127
|
method: 'POST',
|
|
108
128
|
data,
|
|
109
129
|
enableHttp2: true,
|
|
110
|
-
success: () => {
|
|
111
|
-
|
|
130
|
+
success: (res) => {
|
|
131
|
+
const success = eccUtil._updateGlobalPublicKeyInfo(false, res.header);
|
|
132
|
+
resolve(success);
|
|
112
133
|
},
|
|
113
134
|
fail: () => {
|
|
114
135
|
resolve(false);
|
|
@@ -192,6 +213,7 @@ const eccUtil = {
|
|
|
192
213
|
return pre;
|
|
193
214
|
}, [])
|
|
194
215
|
.join('&');
|
|
216
|
+
baseUtil.logInfo('---客户端签名---:before', str);
|
|
195
217
|
// 2. md5
|
|
196
218
|
const md5Str = md5(str);
|
|
197
219
|
const nonce = ecc.randomBytes(ecc.box.nonceLength);
|
|
@@ -215,7 +237,7 @@ const eccUtil = {
|
|
|
215
237
|
'x-response-header-name': formatHeader['x-response-header-name'],
|
|
216
238
|
'x-encrypted-headers': formatHeader['x-encrypted-headers'],
|
|
217
239
|
'x-crypto-enable': formatHeader['x-crypto-enable'],
|
|
218
|
-
'content-type': formatHeader['content-type'],
|
|
240
|
+
// 'content-type': formatHeader['content-type'],
|
|
219
241
|
'x-gateway-code': formatHeader['x-gateway-code'],
|
|
220
242
|
'x-crypto-pub-id': formatHeader['x-crypto-pub-id'],
|
|
221
243
|
'x-crypto-pub-key': formatHeader['x-crypto-pub-key'],
|
|
@@ -232,6 +254,7 @@ const eccUtil = {
|
|
|
232
254
|
return pre;
|
|
233
255
|
}, [])
|
|
234
256
|
.join('&');
|
|
257
|
+
baseUtil.logInfo('---验证服务端的客户端签名---:before', str, traceId);
|
|
235
258
|
const preHashArr = md5(str);
|
|
236
259
|
const verified = preHashArr.length === decrypted.length && preHashArr.every((v, i) => v === decrypted[i]);
|
|
237
260
|
return verified;
|
|
@@ -239,7 +262,6 @@ const eccUtil = {
|
|
|
239
262
|
console.error('verifyServerCryptoSign error', e);
|
|
240
263
|
return false;
|
|
241
264
|
}
|
|
242
|
-
|
|
243
265
|
},
|
|
244
266
|
/* eslint-enable complexity */
|
|
245
267
|
execEncrypt: (input: string, ignoreNull = false): BaseResp<{
|
|
@@ -281,9 +303,7 @@ const eccUtil = {
|
|
|
281
303
|
return new baseUtil.BaseRespFac('', false, `execDecrypt失败:${JSON.stringify(err)}`);;
|
|
282
304
|
}
|
|
283
305
|
},
|
|
284
|
-
checkCryptoOpen: (): boolean =>
|
|
285
|
-
return !!eccUtil._privateKeyInfo;
|
|
286
|
-
},
|
|
306
|
+
checkCryptoOpen: (): boolean => !!eccUtil._privateKeyInfo,
|
|
287
307
|
closeCrypto: () => {
|
|
288
308
|
eccUtil._privateKeyInfo = null;
|
|
289
309
|
eccUtil._updateGlobalPublicKeyInfo(true);
|
|
@@ -431,7 +451,6 @@ const reqEncrypt = (method: string, data: any, header: {
|
|
|
431
451
|
'X-Encrypt-Key': res.cryptoKeyInfo.clientPublicKey,
|
|
432
452
|
'X-Encrypt-Response': '3', // 加密,二进制
|
|
433
453
|
'X-Response-Header-Name': encryptedResponseHeaderName,
|
|
434
|
-
'Content-Type': 'text/plain',
|
|
435
454
|
};
|
|
436
455
|
const cryptoSign = eccUtil.getClientCryptoSign(baseUtil._isObject(finalData) ? finalData : {
|
|
437
456
|
body: finalData,
|
|
@@ -441,6 +460,7 @@ const reqEncrypt = (method: string, data: any, header: {
|
|
|
441
460
|
data: finalData,
|
|
442
461
|
header: {
|
|
443
462
|
...cryptoHeader,
|
|
463
|
+
'Content-Type': 'text/plain',
|
|
444
464
|
'X-Crypto-Sign': cryptoSign,
|
|
445
465
|
},
|
|
446
466
|
});
|
|
@@ -487,7 +507,7 @@ const resDecrypt = async (requestTraceId: string, header, data, cryptoKeyInfo: C
|
|
|
487
507
|
}
|
|
488
508
|
decryptedHeaders = JSON.parse(res);
|
|
489
509
|
}
|
|
490
|
-
const needDecode = contentType
|
|
510
|
+
const needDecode = contentType.indexOf('text/plain') > -1;
|
|
491
511
|
const cipher = needDecode ? baseUtil.decUrl(data) : new Uint8Array(data);
|
|
492
512
|
const { success, msg, res } = eccUtil.execDecrypt(cipher, cryptoKeyInfo);
|
|
493
513
|
if (!success) {
|
|
@@ -511,7 +531,7 @@ const resDecrypt = async (requestTraceId: string, header, data, cryptoKeyInfo: C
|
|
|
511
531
|
let dealEncryptionSwitching = false;
|
|
512
532
|
const dealEncryptionSwitch = async (path: string, traceId: string, resHeader): Promise<boolean> => {
|
|
513
533
|
if ((!resHeader || dealEncryptionSwitching)) {
|
|
514
|
-
return;
|
|
534
|
+
return true;
|
|
515
535
|
}
|
|
516
536
|
dealEncryptionSwitching = true;
|
|
517
537
|
const formatHeader = baseUtil.formatHeader(resHeader);
|
|
@@ -521,7 +541,7 @@ const dealEncryptionSwitch = async (path: string, traceId: string, resHeader): P
|
|
|
521
541
|
`${baseUtil.getSinanHost()}/user/login`,
|
|
522
542
|
`${baseUtil.getSinanHost()}/basic/crypto/lastkey2`,
|
|
523
543
|
].indexOf(path) > -1;
|
|
524
|
-
if ((eccUtil.checkCryptoOpen() && cryptoDisabled) || specialPath
|
|
544
|
+
if ((eccUtil.checkCryptoOpen() && cryptoDisabled) || specialPath) {
|
|
525
545
|
const verified = eccUtil.verifyServerCryptoSign(traceId, formatHeader);
|
|
526
546
|
if (!verified) {
|
|
527
547
|
// 验签失败,表示响应被篡改
|
|
@@ -544,6 +564,8 @@ const dealEncryptionSwitch = async (path: string, traceId: string, resHeader): P
|
|
|
544
564
|
const encryptUtil = {
|
|
545
565
|
init, // 初始化加密工具
|
|
546
566
|
isCryptoRuleMath, // 请求是否符合加密规则
|
|
567
|
+
isPerformanceRuleMath: cryptRuleUtil.isPerformanceReport, // 请求是否符合性能规则
|
|
568
|
+
logInfo: baseUtil.logInfo, // 本地日志打印
|
|
547
569
|
reqEncrypt, // 请求加密:header和data
|
|
548
570
|
resDecrypt, // 响应解密
|
|
549
571
|
dealEncryptionSwitch, // 处理加密开关
|
package/src/encrypt/index.ts
CHANGED
|
@@ -1,26 +1,9 @@
|
|
|
1
1
|
import encryptUtil from './encrypt-util';
|
|
2
2
|
import { genTraceparent } from './traceUtils';
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
|
|
5
5
|
const util = {
|
|
6
|
-
|
|
7
|
-
formatLog(args: any[]): any[] {
|
|
8
|
-
// 小程序日志管理器都只是精确到秒,我们补上毫秒方便分析
|
|
9
|
-
const time = new Date()
|
|
10
|
-
.toISOString()
|
|
11
|
-
.replace('T', ' ')
|
|
12
|
-
.substring(0, 19)
|
|
13
|
-
.replace(/-/g, '-')
|
|
14
|
-
.replace(/:/g, ':');
|
|
15
|
-
args.unshift(time);
|
|
16
|
-
return args;
|
|
17
|
-
},
|
|
18
|
-
logInfo: (...args) => {
|
|
19
|
-
args.unshift('request_encrypt_log');
|
|
20
|
-
const items = util.formatLog(args);
|
|
21
|
-
console.log(...items);
|
|
22
|
-
logger.log(...items);
|
|
23
|
-
},
|
|
6
|
+
logInfo: (...args) => encryptUtil.logInfo(...args),
|
|
24
7
|
reportFunc: (...args) => {
|
|
25
8
|
util.logInfo('reportFunc init fail:', ...args);
|
|
26
9
|
},
|
|
@@ -97,7 +80,8 @@ function proxyWxRequest(): void {
|
|
|
97
80
|
...originalOptions,
|
|
98
81
|
success: async (res) => {
|
|
99
82
|
const dealSuccess = await encryptUtil.dealEncryptionSwitch(url, traceId, res.header);
|
|
100
|
-
|
|
83
|
+
// 性能埋点接口不走验签逻辑
|
|
84
|
+
if (dealSuccess || encryptUtil.isPerformanceRuleMath(url, formatData)) {
|
|
101
85
|
success?.call(this, res);
|
|
102
86
|
} else {
|
|
103
87
|
util.reportFunc(url, traceparent, `加密验签不通过: ${JSON.stringify(res)}`);
|