encoding-probe.mjs
4.25 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
// 一次性取证脚本:同一批参数、同一个 sign,只换「请求体怎么编码」,看网关分别回什么。
// 走只读的 pay.amount,不写任何数据。密钥只从 .env 读,不打印。
// 用法:node out/.temp/encoding-probe.mjs <sessionId> [origin]
import { readFileSync } from 'node:fs';
import { createHash } from 'node:crypto';
import path from 'node:path';
const ROOT = path.resolve(import.meta.dirname, '../..');
const env = Object.fromEntries(
readFileSync(path.join(ROOT, '.env'), 'utf8')
.split(/\r?\n/)
.filter((l) => /^[A-Z][A-Z0-9_]*=/.test(l))
.map((l) => [l.slice(0, l.indexOf('=')), l.slice(l.indexOf('=') + 1).trim()]),
);
const origin = (process.argv[3] || env.VITE_POINTS_GATEWAY_ORIGIN).replace(/\/+$/, '');
const appKey = env.VITE_POINTS_GATEWAY_APP_KEY;
const secret = env.VITE_POINTS_GATEWAY_SECRET;
const v = env.VITE_POINTS_GATEWAY_VERSION || '1.0.0';
const format = env.VITE_POINTS_GATEWAY_FORMAT || 'json';
const sessionId = process.argv[2] || '';
if (!sessionId) {
console.log('用法:node out/.temp/encoding-probe.mjs <sessionId> [origin]');
process.exit(1);
}
const METHOD = 'scrm.api.shopmall.integral.transfer.out.pay.amount';
const IDS = [214, 213, 212];
/** 生产签名器对非字符串取值的口径见 packages/api-core/src/signing/signers.ts */
const canonical = (value) =>
typeof value === 'string'
? value
: typeof value === 'number' || typeof value === 'boolean'
? String(value)
: JSON.stringify(value);
/** 与 dispatcher.ts#flattenFormParams 同规则;这里不 import 生产实现,保持独立 oracle。 */
function flatten(params) {
const out = {};
const walk = (value, key) => {
if (value === undefined) return;
if (value === null) {
out[key] = '';
return;
}
if (Array.isArray(value)) {
value.forEach((item, i) => walk(item, `${key}[${i}]`));
return;
}
if (typeof value === 'object') {
Object.entries(value).forEach(([name, nested]) =>
walk(nested, key ? `${key}.${name}` : name),
);
return;
}
out[key] = String(value);
};
Object.entries(params).forEach(([key, value]) => walk(value, key));
return out;
}
const signOf = (map) =>
createHash('sha1')
.update(
secret +
Object.keys(map)
.sort()
.reduce((acc, k) => acc.concat(k, canonical(map[k] ?? '')), [])
.join('') +
secret,
)
.digest('hex')
.toUpperCase();
const encode = (map) =>
Object.entries(map)
.map(([k, val]) => `${encodeURIComponent(k)}=${encodeURIComponent(val)}`)
.join('&');
const envelope = {
source: env.VITE_POINTS_GATEWAY_SOURCE || 'uj-web-points',
usedCarFlag: 1,
ids: IDS,
sessionId,
method: METHOD,
appKey,
format,
v,
timestamp: String(Date.now()),
};
const flat = flatten(envelope);
// C:数组不摊平,成一个 JSON 字面量键;sign 跟着这份字节一起算对,用来区分「签名错」和「参数形态非法」。
const jsonLiteral = flatten({ ...envelope, ids: undefined });
jsonLiteral.ids = JSON.stringify(IDS);
const cases = [
{
name: 'A 摊平,但请求体发 JSON(少 form 头时落成的样子)',
map: flat,
bodyOf: (m) => JSON.stringify(m),
contentType: 'application/json',
},
{
name: 'B 摊平 + form 体(生产出口)',
map: flat,
contentType: 'application/x-www-form-urlencoded',
},
{
name: 'C ids=[214,213,212] 不摊平,sign 与字节一致 + form 体',
map: jsonLiteral,
contentType: 'application/x-www-form-urlencoded',
},
];
for (const c of cases) {
// A 与 B 共用同一个 map,所以 sign 逐字相同,两边只差在请求体编码。
const map = { ...c.map, sign: signOf(c.map) };
const body = c.bodyOf ? c.bodyOf(map) : encode(map);
const res = await fetch(`${origin}/rop-scrm-shopmall/router`, {
method: 'POST',
headers: { 'content-type': c.contentType },
body,
}).then((r) => r.text());
let parsed;
try {
parsed = JSON.parse(res);
} catch {
parsed = { code: 'NOT_JSON', message: res.replace(/\s+/g, ' ').slice(0, 120) };
}
console.log(
`${c.name}\n sign=${map.sign}\n body=${body.slice(0, 90)}…\n → code=${parsed.code} message=${parsed.message} data=${JSON.stringify(parsed.data)}`,
);
}