Files
thzxx 41cffdfdb6 v0.1.36b: 真根因修复——CSP unsafe-eval 拦截 Function() 字符串求值(推进崩溃+定制白屏同根)
【根因】Electron 打包 renderer CSP 禁 unsafe-eval;buildings.bonusOf / exprToTable / modManager.booleanExprTable
三处用 Function()+正则白名单字符串求值——任何调用点抛 EvalError:
·推进崩溃=每月月率 bonusOf('juling','expBonus') 等(MarketPanel craft 表头同源)
·定制白屏=craft 页渲染行内 bonusOf('danfang','craftChance') → React 崩 → 只剩背景
【修复】全新 arith.ts 纯解析器(令牌化+递归下降:数字/L/四则/括号/幂/负号——白名单天然,字母函数名一律 NaN),
三处替换零 Function/eval;非法输入 NaN 不抛(调用方 Number.isFinite 跳过)
【测试】arith 4 例(四则/幂右结合/负号/除零/非法输入防注入)+全量 7011 绿
【版本】0.1.36(同版本内提交——不另立迭代)
2026-08-24 22:20:25 +08:00

30 lines
1.2 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, expect, it } from 'vitest'
import { arith } from '../src/renderer/game/data/arith'
describe('0.1.36 arith 纯解析器(CSP safe——零 Function/eval', () => {
it('基础四则与变量', () => {
expect(arith('0.5 + 0.1*L', 3)).toBeCloseTo(0.8)
expect(arith('10 * L', 2)).toBe(20)
expect(arith('4 * L ^ 2', 3)).toBe(36)
expect(arith('80 * 1.6^(L-1)', 3)).toBeCloseTo(204.8, 3)
expect(arith('(1 + 2) * 3')).toBe(9)
})
it('非法输入→NaN(不抛异常)', () => {
expect(Number.isNaN(arith(''))).toBe(true)
expect(Number.isNaN(arith('L + foo'))).toBe(true)
expect(Number.isNaN(arith('1 + '))).toBe(true)
expect(Number.isNaN(arith('alert(1)'))).toBe(true)
expect(Number.isNaN(arith('0.5 + 0.1*L^', 3))).toBe(true)
expect(Number.isNaN(arith('L/0 +1', 3))).toBe(true)
})
it('负号/右结合幂/除零', () => {
expect(arith('-3 + 5')).toBe(2)
expect(arith('2 ^ 3 ^ 2')).toBe(512)
expect(Number.isNaN(arith('1/0'))).toBe(true)
})
it('与旧 bonusOf 语义一致(0.5+0.1*L @3→0.8', () => {
expect(arith('0.5 + 0.1*L', 3)).toBeCloseTo(0.8)
expect(arith('0.35*L', 2)).toBeCloseTo(0.7)
})
})