fix: validate SSE event data before parsing

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Dorian
2026-03-08 21:49:30 +00:00
co-authored by Claude Opus 4.6
parent 7de503a487
commit 94910820f8
+22 -10
View File
@@ -401,32 +401,41 @@ function connectSSE() {
addSSEListener(eventSource, 'spectator_count', (e) => { addSSEListener(eventSource, 'spectator_count', (e) => {
try { try {
if (!e.data) return
const data = JSON.parse(e.data) const data = JSON.parse(e.data)
spectatorCount.value = data.count || 0 if (typeof data.count === 'number') spectatorCount.value = data.count
} catch { /* ignore */ } } catch (err) {
console.warn('[SSE] spectator_count parse error:', err)
}
}) })
addSSEListener(eventSource, 'ping', (e) => { addSSEListener(eventSource, 'ping', (e) => {
try { try {
if (e.data) { if (!e.data) return
const data = JSON.parse(e.data) const data = JSON.parse(e.data)
if (data.spectators !== undefined) spectatorCount.value = data.spectators if (typeof data.spectators === 'number') spectatorCount.value = data.spectators
} } catch (err) {
} catch { /* ignore */ } console.warn('[SSE] ping parse error:', err)
}
}) })
addSSEListener(eventSource, 'reaction', (e) => { addSSEListener(eventSource, 'reaction', (e) => {
try { try {
if (!e.data) return
const data = JSON.parse(e.data) const data = JSON.parse(e.data)
if (data.emoji && data.counts) { if (typeof data.emoji === 'string' && data.counts) {
spawnLiveReactionEmoji(data.emoji) spawnLiveReactionEmoji(data.emoji)
} }
} catch { /* ignore */ } } catch (err) {
console.warn('[SSE] reaction parse error:', err)
}
}) })
addSSEListener(eventSource, 'round_start', (e) => { addSSEListener(eventSource, 'round_start', (e) => {
try { try {
if (!e.data) return
const data = JSON.parse(e.data) const data = JSON.parse(e.data)
if (!data.challenge || !data.round) return
currentChallengeInfo.value = { type: data.challenge.type, label: data.challenge.label } currentChallengeInfo.value = { type: data.challenge.type, label: data.challenge.label }
liveLogItems.value.push({ liveLogItems.value.push({
type: 'challenge', type: 'challenge',
@@ -436,12 +445,13 @@ function connectSSE() {
}) })
scrollLiveLog() scrollLiveLog()
} catch (err) { } catch (err) {
console.warn('[FightPage] SSE round_start parse failed:', err) console.warn('[SSE] round_start parse error:', err)
} }
}) })
addSSEListener(eventSource, 'human_challenge', async (e) => { addSSEListener(eventSource, 'human_challenge', async (e) => {
try { try {
if (!e.data) return
const sseData = JSON.parse(e.data) const sseData = JSON.parse(e.data)
if (myBot.value) { if (myBot.value) {
const res = await fetch(`/api/fights/${fightId.value}/challenge/${myBot.value.id}`) const res = await fetch(`/api/fights/${fightId.value}/challenge/${myBot.value.id}`)
@@ -469,6 +479,7 @@ function connectSSE() {
addSSEListener(eventSource, 'round_end', (e) => { addSSEListener(eventSource, 'round_end', (e) => {
try { try {
if (!e.data) return
const data = JSON.parse(e.data) const data = JSON.parse(e.data)
if (data.spectators !== undefined) spectatorCount.value = data.spectators if (data.spectators !== undefined) spectatorCount.value = data.spectators
handleRoundEnd(data).catch(() => {}) handleRoundEnd(data).catch(() => {})
@@ -479,6 +490,7 @@ function connectSSE() {
addSSEListener(eventSource, 'fight_end', (e) => { addSSEListener(eventSource, 'fight_end', (e) => {
try { try {
if (!e.data) return
const data = JSON.parse(e.data) const data = JSON.parse(e.data)
if (data.spectators !== undefined) spectatorCount.value = data.spectators if (data.spectators !== undefined) spectatorCount.value = data.spectators
handleFightEnd(data).catch(() => {}) handleFightEnd(data).catch(() => {})