glsxSign.test.mjs
10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/**
* @file glsx 系(老 SCRM)RPC 网关签名的真实抓包 golden 钉值测试。
*
* 2026-11-09 抓到的一次请求(method=scrm.api.shopmall.integralorder.all.page),
* 固定 timestamp 后可离线复算:sign=689F62277A050D6285B1E38BB2C3DA8D8E518EC9。
* 40 位 ⇒ SHA1(glsx-common/src/util.js#createSign 默认 way='sha1');此前 web points
* 误用 createSigner(MD5 只有 32 位)导致验签必挂。这份测试就是防回归闸门:
* 既钉死算法,也钉死「签名原文怎么拼」和端到端请求体。
*
* 凭据取自 temp/scrm-micro-points/src/config/env.development.js(与 strategy.test.mjs
* 同源,非新增密钥);抓包中的 sessionId 是一次性会话标识,仅作复算输入。
*/
import assert from 'node:assert/strict';
import { createRequire } from 'node:module';
import { existsSync, readFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import path from 'node:path';
import test from 'node:test';
const require = createRequire(import.meta.url);
const {
createSigner,
registerGateway,
prepareRpcCall,
flattenFormParams,
} = require('../dist/index.cjs');
const HERE = path.dirname(fileURLToPath(import.meta.url));
const ROOT = path.join(HERE, '../../..');
const APP_KEY = '48e5e13229b82c1b4e6e8c96151f0637';
const SECRET = 'c24619ed7fef02a0ae16328146bca5f97cc6493957a2137b';
const TIMESTAMP = 1794222639246;
const GOLDEN_SIGN = '689F62277A050D6285B1E38BB2C3DA8D8E518EC9';
const SESSION_ID = 'scrm-base-8293b9c7-3a37-4074-a055-e383076d5aa2-rop';
const METHOD = 'scrm.api.shopmall.integralorder.all.page';
/** 抓包请求体中除 sign 外的 10 个字段(空串 settlementStatus 也参与签名)。 */
const CAPTURE = {
appKey: APP_KEY,
v: '1.0.0',
format: 'json',
source: 'scrm-home',
pageSize: 10,
currentPage: 1,
settlementStatus: '',
method: METHOD,
sessionId: SESSION_ID,
timestamp: TIMESTAMP,
};
/**
* 老系统的 createSign 收的是「整个请求体」;这里按同样形状把全量字段塞进 params,
* 系统字段再单独给一遍(同名覆盖为同值,签名原文不会重复拼接)。
*/
function signInput(params = { ...CAPTURE }) {
return {
method: METHOD,
params,
timestamp: TIMESTAMP,
appKey: APP_KEY,
secret: SECRET,
version: CAPTURE.v,
format: CAPTURE.format,
};
}
/** 从生产网关源码读出 fixedParams.source,保证测试用的渠道值与实际发出的一致。 */
function productionSource() {
const rel = 'apps/web/subapps/points/src/adapter/gateways/glsx.ts';
const src = readFileSync(path.join(ROOT, rel), 'utf8');
const matched = src.match(/fixedParams:\s*\{\s*source:\s*'([^']+)'\s*\}/);
assert.ok(matched, `${rel} 应显式声明 fixedParams.source`);
return matched[1];
}
test('IT-GLSX-SIGN-001 真实抓包复算命中 SHA1 golden(40 位大写)', async () => {
const fields = await createSigner().fields(signInput());
assert.equal(fields.sign, GOLDEN_SIGN, 'glsx 网关必须是 SHA1,不是 MD5');
assert.match(fields.sign, /^[0-9A-F]{40}$/, 'sign 必须是 40 位大写十六进制');
// 系统字段也回吐给 prepareRpcCall 并进请求体
assert.equal(fields.appKey, APP_KEY);
assert.equal(fields.format, CAPTURE.format);
assert.equal(fields.method, METHOD);
assert.equal(fields.timestamp, TIMESTAMP);
assert.equal(fields.v, CAPTURE.v);
});
test('IT-GLSX-SIGN-002 MD5 预设与 ROP SHA1 预设都不得命中(防误用回归)', async () => {
const md5 = await createSigner({ kind: 'md5' }).fields(signInput());
assert.match(md5.sign, /^[0-9A-F]{32}$/, 'MD5 只有 32 位');
assert.notEqual(md5.sign, GOLDEN_SIGN);
// mp ROP 的 createSigner 排除系统 timestamp;真实 mp 请求体里也没有 timestamp
// (老系统复刻用的 signInput 把 CAPTURE 全量塞进 params,这里要按 mp 的真实形状剔掉它)。
const mpParams = { ...CAPTURE };
delete mpParams.timestamp;
const rop = await createSigner({ kind: 'rop' }).fields(signInput(mpParams));
assert.notEqual(rop.sign, GOLDEN_SIGN, 'mp ROP 预设不含 timestamp,不能复用');
});
test('IT-GLSX-SIGN-003 端到端 prepareRpcCall 请求体 = 抓包 11 字段(含 sign)', async () => {
// 这一条复算的是 2026-11-09 那次抓包的字节,所以按抓包里的渠道名登记网关;
// 生产渠道名另有钉值(见 004 与 005),它同样进签名原文,换名就要重新取一份 golden。
registerGateway({
id: 'golden-shopmall',
router: 'rop-scrm-shopmall/router',
tokenHeader: 'token',
signer: createSigner({ id: 'golden-shopmall-sha1' }),
fixedParams: { source: CAPTURE.source },
normalize: (raw) => ({ data: raw }),
});
const realNow = Date.now;
Date.now = () => TIMESTAMP;
try {
const raw = await prepareRpcCall(
{
gateway: 'golden-shopmall',
method: METHOD,
params: {
pageSize: CAPTURE.pageSize,
currentPage: CAPTURE.currentPage,
settlementStatus: CAPTURE.settlementStatus,
},
},
{
resolveCredential: () => ({
origin: 'http://gateway.test/',
appKey: APP_KEY,
secret: SECRET,
version: CAPTURE.v,
format: CAPTURE.format,
}),
// 会话标识由 facade.resolveSession 注入,签名前进请求体
session: { name: 'sessionId', value: SESSION_ID },
},
);
assert.equal(raw.url, 'http://gateway.test/rop-scrm-shopmall/router');
const body = raw.data;
const expected = { ...CAPTURE, sign: GOLDEN_SIGN };
assert.deepEqual(
Object.keys(body).sort(),
Object.keys(expected).sort(),
'请求体字段集应完全一致',
);
for (const [key, value] of Object.entries(expected)) {
assert.equal(String(body[key]), String(value), `字段 ${key} 应与抓包一致`);
}
} finally {
Date.now = realNow;
}
});
test('IT-GLSX-SIGN-004 points 网关源码不得回退到 MD5 预设或改动渠道值', () => {
const rel = 'apps/web/subapps/points/src/adapter/gateways/glsx.ts';
const file = path.join(ROOT, rel);
assert.ok(existsSync(file), `${rel} 应存在`);
const src = readFileSync(file, 'utf8');
assert.ok(
/createSigner\(\s*\)/.test(src),
`${rel} 必须用新版统一签名器且不传参(默认 glsx SHA1)`,
);
// 渠道名是本域自报的(2026-11-10 从老系统的 'scrm-home' 换成 'uj-web-points')。
// 它进签名原文、但不参与身份校验,线上实测 code 0;换名要连同 005 的钉值一起重取。
assert.equal(productionSource(), 'uj-web-points', '渠道值改动会让签名与线上钉值不一致');
assert.ok(!src.includes('createMd5Signer'), `${rel} 不得再用 MD5 旧预设`);
assert.ok(!src.includes("kind: 'md5'"), `${rel} 不得把签名器配成 MD5`);
assert.ok(!src.includes('glxsrMd5Signer'), `${rel} 不得残留旧导出名`);
});
/**
* 2026-11-10 线上取的一份「数组参数」字节,网关当场认证通过(HTTP 200、code:'0'、data.payAmount=1200)。
* 抓它是因为页面上的批量操作全部回 code 25 无效签名:签名原文里写的是 `ids[214,213,212]`
* (JSON 串),而请求体里 `ids` 这个键怎么摊开由传输层决定,网关按它收到的键名重算必然对不上。
* 老系统与二手车小程序都先摊平成 `ids[0]` 再加签,本域同一份实现见 dispatcher.flattenFormParams。
*/
const LIVE = {
method: 'scrm.api.shopmall.integral.transfer.out.pay.amount',
timestamp: 1794300000000,
sessionId: 'scrm-base-1c3b0d4d-366a-4bf2-b8f7-0572f25aa715-rop',
sign: 'CE6C23DEC362B3E3FBA0BF2793CA3128D80FA9A3',
};
test('IT-GLSX-SIGN-005 数组参数在出口摊平成 ids[0]/ids[1]/ids[2]:签名与请求体同一份字节(线上认证)', async () => {
registerGateway({
id: 'live-shopmall',
router: 'rop-scrm-shopmall/router',
tokenHeader: 'token',
signer: createSigner({ id: 'live-shopmall-sha1' }),
fixedParams: { source: productionSource() },
normalize: (raw) => ({ data: raw }),
});
const realNow = Date.now;
Date.now = () => LIVE.timestamp;
let raw;
try {
raw = await prepareRpcCall(
{
gateway: 'live-shopmall',
method: LIVE.method,
params: { ids: ['214', '213', '212'], canApprove: false, usedCarFlag: '1' },
},
{
resolveCredential: () => ({
origin: 'https://scrm.dev.glsx.com.cn',
appKey: APP_KEY,
secret: SECRET,
version: '1.0.0',
format: 'json',
}),
session: { name: 'sessionId', value: LIVE.sessionId },
},
);
} finally {
Date.now = realNow;
}
const body = raw.data;
assert.deepEqual(Object.keys(body), [
'source',
'ids[0]',
'ids[1]',
'ids[2]',
'canApprove',
'usedCarFlag',
'sessionId',
'method',
'appKey',
'format',
'timestamp',
'v',
'sign',
]);
assert.ok(!('ids' in body), '原始数组键必须已经被摊平掉,否则线上发的就是 ids[]');
assert.equal(body['ids[0]'], '214');
assert.equal(body.canApprove, 'false', '布尔摊平成字符串,与老系统 qs 的产物一致');
// 线上发出去的就是 form 串,值只能是标量;一旦还留着数组或对象,编码方式就归传输层说了算,
// 签名原文与请求体从此两套字节(就是这次 code 25 的成因)。
for (const [key, value] of Object.entries(body)) {
assert.ok(
typeof value === 'string' || typeof value === 'number',
`字段 ${key} 没摊平成标量:${JSON.stringify(value)}`,
);
}
// 这一串不是本地复算出来的:同一份字段集在线上跑通过,网关回 code 0 并给了 payAmount。
assert.equal(body.sign, LIVE.sign, '摊平顺序或值形态一变,网关重算的签名就对不上(code 25)');
});
test('IT-GLSX-SIGN-006 嵌套对象摊平成 a.b,null 留空键、undefined 与空数组整个键消失', () => {
const flat = flattenFormParams({
ids: [],
tag: { path: { id: 7 }, name: null },
drop: undefined,
list: [1, [2, 3]],
});
assert.deepEqual(flat, {
'tag.path.id': '7',
'tag.name': '',
'list[0]': '1',
'list[1][0]': '2',
'list[1][1]': '3',
});
});