功能:收口小程序前后台安全与防重复提交

This commit is contained in:
xiaowang
2026-04-20 16:02:57 +08:00
parent eb1c209d79
commit a3df262fae
237 changed files with 14068 additions and 728 deletions
+66 -4
View File
@@ -1,4 +1,61 @@
const { STORAGE_KEYS, getStorage, removeStorage, setStorage } = require('./storage')
const SIGNED_URL_SAFE_BUFFER_MS = 60 * 1000
function parseSignedUrlExpireAt(url) {
const rawUrl = String(url || '').trim()
if (!rawUrl || rawUrl.indexOf('?') < 0) {
return 0
}
const queryString = rawUrl.slice(rawUrl.indexOf('?') + 1)
const params = {}
queryString.split('&').forEach(segment => {
if (!segment) {
return
}
const dividerIndex = segment.indexOf('=')
const key = dividerIndex >= 0 ? segment.slice(0, dividerIndex) : segment
const value = dividerIndex >= 0 ? segment.slice(dividerIndex + 1) : ''
params[decodeURIComponent(key)] = decodeURIComponent(value)
})
const awsDate = params['X-Amz-Date']
const awsExpireSeconds = Number(params['X-Amz-Expires'] || 0)
if (awsDate && awsExpireSeconds > 0 && awsDate.length >= 16) {
const year = Number(awsDate.slice(0, 4))
const month = Number(awsDate.slice(4, 6)) - 1
const day = Number(awsDate.slice(6, 8))
const hour = Number(awsDate.slice(9, 11))
const minute = Number(awsDate.slice(11, 13))
const second = Number(awsDate.slice(13, 15))
const baseTimestamp = Date.UTC(year, month, day, hour, minute, second)
if (!Number.isNaN(baseTimestamp)) {
return baseTimestamp + awsExpireSeconds * 1000
}
}
const expires = Number(params.Expires || 0)
if (expires > 0) {
return expires * 1000
}
return 0
}
function sanitizeStoredUserInfo(userInfo) {
if (!userInfo || typeof userInfo !== 'object') {
return userInfo
}
const nextUserInfo = {
...userInfo
}
const avatarExpireAt = parseSignedUrlExpireAt(nextUserInfo.avatar)
if (avatarExpireAt > 0 && avatarExpireAt <= Date.now() + SIGNED_URL_SAFE_BUFFER_MS) {
nextUserInfo.avatar = ''
}
return nextUserInfo
}
function getAccessToken() {
return getStorage(STORAGE_KEYS.ACCESS_TOKEN, '')
@@ -9,7 +66,11 @@ function getRefreshToken() {
}
function getStoredUserInfo() {
return getStorage(STORAGE_KEYS.USER_INFO, null)
const userInfo = sanitizeStoredUserInfo(getStorage(STORAGE_KEYS.USER_INFO, null))
if (userInfo) {
setStorage(STORAGE_KEYS.USER_INFO, userInfo)
}
return userInfo
}
function getNeedSetPassword() {
@@ -33,7 +94,7 @@ function setSession(payload) {
setStorage(STORAGE_KEYS.REFRESH_TOKEN, payload.refreshToken)
if (payload.userInfo) {
setStorage(STORAGE_KEYS.USER_INFO, payload.userInfo)
setStorage(STORAGE_KEYS.USER_INFO, sanitizeStoredUserInfo(payload.userInfo))
}
if (payload.needSetPassword) {
@@ -49,7 +110,7 @@ function updateStoredUserInfo(userInfo) {
return
}
setStorage(STORAGE_KEYS.USER_INFO, userInfo)
setStorage(STORAGE_KEYS.USER_INFO, sanitizeStoredUserInfo(userInfo))
}
function setNeedSetPassword(value) {
@@ -88,5 +149,6 @@ module.exports = {
updateStoredUserInfo,
setNeedSetPassword,
setPreferredMiniLoginShopId,
clearSession
clearSession,
sanitizeStoredUserInfo
}