Spaces:
Runtime error
Runtime error
Pete Christianson Claude Opus 4.6 commited on
Commit ·
477bb9e
1
Parent(s): a79a97a
Fix XSS in memory-browser-panel
Browse filesReplace dangerouslySetInnerHTML with React elements for inline
formatting (bold/italic). New renderInlineFormatting() helper returns
React nodes instead of raw HTML strings, eliminating XSS risk from
user-controlled memory content.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
src/components/panels/memory-browser-panel.tsx
CHANGED
|
@@ -271,6 +271,30 @@ export function MemoryBrowserPanel() {
|
|
| 271 |
))
|
| 272 |
}
|
| 273 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
const renderMarkdown = (content: string) => {
|
| 275 |
// Improved markdown rendering with proper line handling
|
| 276 |
const lines = content.split('\n')
|
|
@@ -323,20 +347,10 @@ export function MemoryBrowserPanel() {
|
|
| 323 |
elements.push(<div key={`${i}-space`} className="mb-2"></div>)
|
| 324 |
} else if (trimmedLine.length > 0) {
|
| 325 |
if (inList) inList = false
|
| 326 |
-
// Handle inline formatting — escape HTML entities first to prevent XSS
|
| 327 |
-
let content = trimmedLine
|
| 328 |
-
.replace(/&/g, '&')
|
| 329 |
-
.replace(/</g, '<')
|
| 330 |
-
.replace(/>/g, '>')
|
| 331 |
-
.replace(/"/g, '"')
|
| 332 |
-
.replace(/'/g, ''')
|
| 333 |
-
// Simple bold formatting
|
| 334 |
-
content = content.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>')
|
| 335 |
-
// Simple italic formatting
|
| 336 |
-
content = content.replace(/\*(.*?)\*/g, '<em>$1</em>')
|
| 337 |
-
|
| 338 |
elements.push(
|
| 339 |
-
<p key={`${i}-p`} className="mb-2"
|
|
|
|
|
|
|
| 340 |
)
|
| 341 |
}
|
| 342 |
}
|
|
|
|
| 271 |
))
|
| 272 |
}
|
| 273 |
|
| 274 |
+
const renderInlineFormatting = (text: string): React.ReactNode[] => {
|
| 275 |
+
const parts: React.ReactNode[] = []
|
| 276 |
+
const regex = /(\*\*.*?\*\*|\*.*?\*)/g
|
| 277 |
+
let lastIndex = 0
|
| 278 |
+
let match: RegExpExecArray | null
|
| 279 |
+
let key = 0
|
| 280 |
+
while ((match = regex.exec(text)) !== null) {
|
| 281 |
+
if (match.index > lastIndex) {
|
| 282 |
+
parts.push(text.slice(lastIndex, match.index))
|
| 283 |
+
}
|
| 284 |
+
const m = match[0]
|
| 285 |
+
if (m.startsWith('**') && m.endsWith('**')) {
|
| 286 |
+
parts.push(<strong key={key++}>{m.slice(2, -2)}</strong>)
|
| 287 |
+
} else if (m.startsWith('*') && m.endsWith('*')) {
|
| 288 |
+
parts.push(<em key={key++}>{m.slice(1, -1)}</em>)
|
| 289 |
+
}
|
| 290 |
+
lastIndex = regex.lastIndex
|
| 291 |
+
}
|
| 292 |
+
if (lastIndex < text.length) {
|
| 293 |
+
parts.push(text.slice(lastIndex))
|
| 294 |
+
}
|
| 295 |
+
return parts
|
| 296 |
+
}
|
| 297 |
+
|
| 298 |
const renderMarkdown = (content: string) => {
|
| 299 |
// Improved markdown rendering with proper line handling
|
| 300 |
const lines = content.split('\n')
|
|
|
|
| 347 |
elements.push(<div key={`${i}-space`} className="mb-2"></div>)
|
| 348 |
} else if (trimmedLine.length > 0) {
|
| 349 |
if (inList) inList = false
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 350 |
elements.push(
|
| 351 |
+
<p key={`${i}-p`} className="mb-2">
|
| 352 |
+
{renderInlineFormatting(trimmedLine)}
|
| 353 |
+
</p>
|
| 354 |
)
|
| 355 |
}
|
| 356 |
}
|