增加 CRC Hex 输入并修复依赖漏洞

This commit is contained in:
mid
2026-08-19 15:54:22 +08:00
parent 0ae6db36cb
commit 8bc6032f38
13 changed files with 268 additions and 37 deletions
+2
View File
@@ -28,6 +28,8 @@ describe('parseHex', () => {
expect(() => parseHex('0011', 16)).toThrow('当前为 2 字节')
expect(() => parseHex(',AA')).toThrow('片段缺少')
expect(() => parseHex('AA,')).toThrow('片段缺少')
expect(() => parseHex('AA,,BB')).toThrow('片段缺少')
expect(() => parseHex('AA, ,BB')).toThrow('片段缺少')
})
it('supports bounded parsing without allocating oversized output', () => {
+34 -1
View File
@@ -1,6 +1,11 @@
import { describe, expect, it } from 'vitest'
import { calculateCrc, calculateCrcChunks, CRC_PRESETS, formatCrc } from '../src/lib/crc'
import { calculateCrcReport } from '../src/lib/crcWorker'
import {
calculateCrcHexInput,
calculateCrcReport,
MAX_INLINE_CRC_DATA_BYTES,
MAX_INLINE_CRC_INPUT_CHARS,
} from '../src/lib/crcWorker'
import { getFirmwareFormat, parseFirmwareContent } from '../src/lib/firmware'
import { concatenateBlocks, parseIntelHex } from '../src/lib/intelHex'
import { textToBytes } from '../src/lib/bytes'
@@ -48,6 +53,34 @@ describe('CRC', () => {
expect(calculateCrcChunks(chunks, preset)).toBe(calculateCrc(textToBytes('123456789'), preset))
})
it('calculates inline Hex with the same separators used by other tools', () => {
const preset = CRC_PRESETS[0]
const inputs = [
'A51F7ADD',
'A5 1F 7A DD',
'A5,1F,7A,DD',
'A5,1F,7A,DD',
'0xA5 0x1F 0x7A 0xDD',
'0xa5, 1F\n0x7a,DD',
]
for (const input of inputs) {
expect(calculateCrcHexInput(input, preset)).toEqual({
combinedResult: '6139ED73',
totalBytes: 4,
})
}
})
it('rejects empty, malformed, and oversized inline Hex', () => {
const preset = CRC_PRESETS[0]
expect(() => calculateCrcHexInput(' ', preset)).toThrow('请输入 Hex 数据')
expect(() => calculateCrcHexInput('ABC', preset)).toThrow('偶数')
expect(() => calculateCrcHexInput('A5,,1F', preset)).toThrow('片段缺少')
expect(() => calculateCrcHexInput('GG', preset)).toThrow('无效')
expect(() => calculateCrcHexInput('AA'.repeat(MAX_INLINE_CRC_DATA_BYTES + 1), preset)).toThrow('Hex 数据不能超过 1 MiB')
expect(() => calculateCrcHexInput(' '.repeat(MAX_INLINE_CRC_INPUT_CHARS + 1), preset)).toThrow('输入不能超过')
})
it('rejects malformed runtime parameters instead of coercing them', () => {
const preset = CRC_PRESETS[0]
expect(() => calculateCrc(new Uint8Array(), { ...preset, inputReflected: undefined as never })).toThrow('布尔值')