mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-26 04:39:53 +01:00
fix(manager-mobile): 修复Node 随机源误判、垫片健壮性及补丁哈希更新
This commit is contained in:
parent
42f7e55c72
commit
7e450fca0d
6 changed files with 56 additions and 33 deletions
|
|
@ -120,6 +120,9 @@ export default defineManifestConfig({
|
||||||
/* 小程序特有相关 */
|
/* 小程序特有相关 */
|
||||||
'mp-weixin': {
|
'mp-weixin': {
|
||||||
appid: VITE_WX_APPID,
|
appid: VITE_WX_APPID,
|
||||||
|
// 最低基础库:wx.getRandomValues 要求 >= 2.17.3(sm-crypto 0.5.x 的
|
||||||
|
// 随机源垫片依赖该 API),低于此版本的客户端将无法完成登录加密
|
||||||
|
libVersion: '2.19.4',
|
||||||
setting: {
|
setting: {
|
||||||
urlCheck: false,
|
urlCheck: false,
|
||||||
// 是否启用 ES6 转 ES5
|
// 是否启用 ES6 转 ES5
|
||||||
|
|
|
||||||
|
|
@ -101,7 +101,7 @@
|
||||||
"js-cookie": "^3.0.5",
|
"js-cookie": "^3.0.5",
|
||||||
"pinia": "2.0.36",
|
"pinia": "2.0.36",
|
||||||
"pinia-plugin-persistedstate": "3.2.1",
|
"pinia-plugin-persistedstate": "3.2.1",
|
||||||
"sm-crypto": "^0.5.8",
|
"sm-crypto": "0.5.8",
|
||||||
"vue": "^3.4.21",
|
"vue": "^3.4.21",
|
||||||
"wot-design-uni": "^1.9.1",
|
"wot-design-uni": "^1.9.1",
|
||||||
"z-paging": "2.8.7"
|
"z-paging": "2.8.7"
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,12 @@
|
||||||
diff --git a/src/sm2/rng.js b/src/sm2/rng.js
|
diff --git a/src/sm2/rng.js b/src/sm2/rng.js
|
||||||
index c6572c1756579fb100c0d7bdf0cec323971ec211..2ccb9f9c69b6b38b4a10d1b37dee1c50ccadb5f4 100644
|
index c6572c1756579fb100c0d7bdf0cec323971ec211..cdb1f1fcc7d58c52138f6f9fd76240bae5dde5d4 100644
|
||||||
--- a/src/sm2/rng.js
|
--- a/src/sm2/rng.js
|
||||||
+++ b/src/sm2/rng.js
|
+++ b/src/sm2/rng.js
|
||||||
@@ -55,7 +55,14 @@ function rngSeedTime() {
|
@@ -54,8 +54,14 @@ function rngSeedTime() {
|
||||||
|
rngSeedInt(new Date().getTime())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize the pool with junk if needed.
|
-// Initialize the pool with junk if needed.
|
||||||
-if (rngPool == null) {
|
-if (rngPool == null) {
|
||||||
+// [xiaozhi patch] 熵池初始化从模块加载时推迟到首次取随机字节时(见 rngGetByte)。
|
+// [xiaozhi patch] 熵池初始化从模块加载时推迟到首次取随机字节时(见 rngGetByte)。
|
||||||
+// 原因:微信小程序等环境没有同步的 crypto.getRandomValues,模块加载即取熵会直接抛异常;
|
+// 原因:微信小程序等环境没有同步的 crypto.getRandomValues,模块加载即取熵会直接抛异常;
|
||||||
|
|
@ -18,7 +19,7 @@ index c6572c1756579fb100c0d7bdf0cec323971ec211..2ccb9f9c69b6b38b4a10d1b37dee1c50
|
||||||
rngPool = []
|
rngPool = []
|
||||||
rngPptr = 0
|
rngPptr = 0
|
||||||
let t
|
let t
|
||||||
@@ -82,6 +89,7 @@ if (rngPool == null) {
|
@@ -82,6 +88,7 @@ if (rngPool == null) {
|
||||||
|
|
||||||
function rngGetByte() {
|
function rngGetByte() {
|
||||||
if (rngState == null) {
|
if (rngState == null) {
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ patchedDependencies:
|
||||||
hash: 5763725268e9a493075be3c82d91b590b9ae30e997a04a418494dd7d6d327b9d
|
hash: 5763725268e9a493075be3c82d91b590b9ae30e997a04a418494dd7d6d327b9d
|
||||||
path: patches/@dcloudio__uni-h5.patch
|
path: patches/@dcloudio__uni-h5.patch
|
||||||
sm-crypto@0.5.8:
|
sm-crypto@0.5.8:
|
||||||
hash: 134528f8afad01074c99f1aca5fe3807eedd510809c6b845233cd81452f6177c
|
hash: 62930665cf9612107916df2f12c9deab4ad907f6ac9080520b84bffe016c3352
|
||||||
path: patches/sm-crypto@0.5.8.patch
|
path: patches/sm-crypto@0.5.8.patch
|
||||||
|
|
||||||
importers:
|
importers:
|
||||||
|
|
@ -95,8 +95,8 @@ importers:
|
||||||
specifier: 3.2.1
|
specifier: 3.2.1
|
||||||
version: 3.2.1(pinia@2.0.36(typescript@5.7.2)(vue@3.5.15(typescript@5.7.2)))
|
version: 3.2.1(pinia@2.0.36(typescript@5.7.2)(vue@3.5.15(typescript@5.7.2)))
|
||||||
sm-crypto:
|
sm-crypto:
|
||||||
specifier: ^0.5.8
|
specifier: 0.5.8
|
||||||
version: 0.5.8(patch_hash=134528f8afad01074c99f1aca5fe3807eedd510809c6b845233cd81452f6177c)
|
version: 0.5.8(patch_hash=62930665cf9612107916df2f12c9deab4ad907f6ac9080520b84bffe016c3352)
|
||||||
vue:
|
vue:
|
||||||
specifier: ^3.4.21
|
specifier: ^3.4.21
|
||||||
version: 3.5.15(typescript@5.7.2)
|
version: 3.5.15(typescript@5.7.2)
|
||||||
|
|
@ -14159,7 +14159,7 @@ snapshots:
|
||||||
ansi-styles: 6.2.1
|
ansi-styles: 6.2.1
|
||||||
is-fullwidth-code-point: 5.0.0
|
is-fullwidth-code-point: 5.0.0
|
||||||
|
|
||||||
sm-crypto@0.5.8(patch_hash=134528f8afad01074c99f1aca5fe3807eedd510809c6b845233cd81452f6177c):
|
sm-crypto@0.5.8(patch_hash=62930665cf9612107916df2f12c9deab4ad907f6ac9080520b84bffe016c3352):
|
||||||
dependencies:
|
dependencies:
|
||||||
jsbn: 1.1.0
|
jsbn: 1.1.0
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,9 +11,9 @@ import { ensureSecureRandomReady } from './secure-random'
|
||||||
* 小程序等环境没有该 API,项目中通过 pnpm patch(patches/sm-crypto@0.5.8.patch)
|
* 小程序等环境没有该 API,项目中通过 pnpm patch(patches/sm-crypto@0.5.8.patch)
|
||||||
* 将 sm-crypto 的熵池初始化推迟到首次取随机字节时,并由 secure-random.ts 提供
|
* 将 sm-crypto 的熵池初始化推迟到首次取随机字节时,并由 secure-random.ts 提供
|
||||||
* 垫片。因此小程序端首次调用 sm2 前必须 await 本方法,用 wx.getRandomValues
|
* 垫片。因此小程序端首次调用 sm2 前必须 await 本方法,用 wx.getRandomValues
|
||||||
* 预热真随机缓冲池;浏览器 / App WebView 原生支持时立即就绪。
|
* 预热真随机缓冲池;浏览器 / Node 等原生支持 globalThis.crypto 的环境立即就绪。
|
||||||
*/
|
*/
|
||||||
async function getSm2(): Promise<any> {
|
async function getSm2(): Promise<typeof smCrypto.sm2> {
|
||||||
await ensureSecureRandomReady()
|
await ensureSecureRandomReady()
|
||||||
return smCrypto.sm2
|
return smCrypto.sm2
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -42,9 +42,12 @@ function getGlobalObj(): Record<string, any> | null {
|
||||||
|
|
||||||
function hasNativeGetRandomValues(): boolean {
|
function hasNativeGetRandomValues(): boolean {
|
||||||
try {
|
try {
|
||||||
return typeof window !== 'undefined'
|
// 探测 globalThis.crypto(而非仅 window.crypto):Node、uni-app h5:ssr
|
||||||
&& !!window.crypto
|
// 等运行时有原生 CSPRNG 但没有 window,误判会导致走垫片分支,
|
||||||
&& typeof window.crypto.getRandomValues === 'function'
|
// 而 installShim 发现 globalThis.crypto 已可用时不会安装,最终误报不支持
|
||||||
|
const globalObj = getGlobalObj()
|
||||||
|
return !!globalObj?.crypto
|
||||||
|
&& typeof globalObj.crypto.getRandomValues === 'function'
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
return false
|
return false
|
||||||
|
|
@ -113,43 +116,59 @@ function scheduleRefill() {
|
||||||
return
|
return
|
||||||
refillInFlight = true
|
refillInFlight = true
|
||||||
refillPool(256)
|
refillPool(256)
|
||||||
.catch(() => {
|
.catch((err) => {
|
||||||
// 尽力而为的补充;缓冲池仍够用时静默失败,不足时由 getRandomValues 抛错兜底
|
// 尽力而为的补充;缓冲池仍够用时静默失败,不足时由 getRandomValues 抛错兜底。
|
||||||
|
// 但要留下原始错误,避免池耗尽后只剩"缓冲池未就绪"而无根因可查
|
||||||
|
console.warn('[secure-random] 补充随机数缓冲池失败', err)
|
||||||
})
|
})
|
||||||
.finally(() => {
|
.finally(() => {
|
||||||
refillInFlight = false
|
refillInFlight = false
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function createGetRandomValues<T extends ArrayBufferView>(array: T): T {
|
||||||
|
// 按字节拷贝:调用方可能传入 Uint16Array/Uint32Array 等视图,
|
||||||
|
// 直接按下标赋值会写入错误的字节序值
|
||||||
|
const view = array as unknown as ArrayBufferView
|
||||||
|
const bytes = new Uint8Array(view.buffer, view.byteOffset, view.byteLength)
|
||||||
|
if (pool.length < bytes.length) {
|
||||||
|
scheduleRefill()
|
||||||
|
throw new Error('安全随机数缓冲池未就绪或已耗尽,请先等待 ensureSecureRandomReady() 完成')
|
||||||
|
}
|
||||||
|
for (let i = 0; i < bytes.length; i++)
|
||||||
|
bytes[i] = pool[i]
|
||||||
|
pool = pool.slice(bytes.length)
|
||||||
|
if (pool.length < LOW_WATER_MARK)
|
||||||
|
scheduleRefill()
|
||||||
|
return array
|
||||||
|
}
|
||||||
|
|
||||||
function installShim() {
|
function installShim() {
|
||||||
const globalObj = getGlobalObj()
|
const globalObj = getGlobalObj()
|
||||||
if (!globalObj)
|
if (!globalObj)
|
||||||
throw new Error('无法获取全局对象,无法安装 crypto.getRandomValues 垫片')
|
throw new Error('无法获取全局对象,无法安装 crypto.getRandomValues 垫片')
|
||||||
|
|
||||||
const existing = globalObj.crypto
|
if (globalObj.crypto && typeof globalObj.crypto.getRandomValues === 'function')
|
||||||
if (existing && typeof existing.getRandomValues === 'function')
|
|
||||||
return
|
return
|
||||||
|
|
||||||
globalObj.crypto = {
|
if (globalObj.crypto) {
|
||||||
getRandomValues<T extends ArrayBufferView>(array: T): T {
|
// 宿主已有 crypto 但缺 getRandomValues:只补缺的方法,
|
||||||
const bytes = array as unknown as Uint8Array
|
// 保留 subtle、randomUUID 等原有成员;用 defineProperty 而非整体赋值,
|
||||||
if (pool.length < bytes.length) {
|
// 避免 crypto 为只读访问器时赋值失败(如 Node 的 globalThis.crypto)
|
||||||
scheduleRefill()
|
Object.defineProperty(globalObj.crypto, 'getRandomValues', {
|
||||||
throw new Error('安全随机数缓冲池未就绪或已耗尽,请先等待 ensureSecureRandomReady() 完成')
|
value: createGetRandomValues,
|
||||||
}
|
configurable: true,
|
||||||
for (let i = 0; i < bytes.length; i++)
|
writable: true,
|
||||||
bytes[i] = pool[i]
|
})
|
||||||
pool = pool.slice(bytes.length)
|
}
|
||||||
if (pool.length < LOW_WATER_MARK)
|
else {
|
||||||
scheduleRefill()
|
globalObj.crypto = { getRandomValues: createGetRandomValues }
|
||||||
return array
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 确保密码学安全随机源可用(sm-crypto 加载前必须 await 本方法)。
|
* 确保密码学安全随机源可用(sm-crypto 加载前必须 await 本方法)。
|
||||||
* - 浏览器 / App WebView 等原生支持 window.crypto 的环境:直接就绪;
|
* - 浏览器 / Node 等原生支持 globalThis.crypto 的环境:直接就绪;
|
||||||
* - 小程序等缺失环境:安装垫片并从系统 CSPRNG 预热缓冲池。
|
* - 小程序等缺失环境:安装垫片并从系统 CSPRNG 预热缓冲池。
|
||||||
* 不支持获取随机数的环境会 reject,由调用方 fail-closed 处理。
|
* 不支持获取随机数的环境会 reject,由调用方 fail-closed 处理。
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue