fix: 拒绝保存未来日期的记录(防止改手机时间污染数据)

用户改手机时间到未来,record.date 就会变成未来时间,
推上云后污染排行榜统计。saveRecord 增加 1 分钟宽容度的
未来时间校验,触发时 toast 提示 + 静默拒绝保存。
This commit is contained in:
2026-07-08 14:11:07 +08:00
parent c41b3e138e
commit 486390917c
+12
View File
@@ -49,6 +49,18 @@ const getRecords = () => {
const saveRecord = (record) => { const saveRecord = (record) => {
record.id = String(Date.now()) + '_' + (++_idSeq) record.id = String(Date.now()) + '_' + (++_idSeq)
// Defensive: reject records with a future date. Indicates the user has
// changed their phone clock, which would otherwise poison the cloud
// leaderboard (and their own stats) with impossible timestamps. Allow
// a 1-minute tolerance for normal clock skew.
const recordTime = new Date(record.date.replace(/-/g, '/')).getTime()
if (recordTime > Date.now() + 60 * 1000) {
console.warn('[storage] saveRecord rejected: future date', record.date)
wx.showToast({ title: '系统时间异常,请检查', icon: 'none' })
return
}
const records = getRecords() const records = getRecords()
const month = record.date.substring(0, 7) const month = record.date.substring(0, 7)
if (!records[month]) records[month] = [] if (!records[month]) records[month] = []