mirror of
https://github.com/usestrix/strix.git
synced 2026-08-19 01:55:46 +02:00
fix(reports): strip markdown code fence from poc_script_code before rendering
This commit is contained in:
@@ -39,6 +39,7 @@ from strix.interface.tui.live_view import TuiLiveView
|
||||
from strix.interface.tui.messages import send_user_message_to_agent
|
||||
from strix.interface.tui.renderers import render_tool_widget
|
||||
from strix.interface.tui.renderers.agent_message_renderer import AgentMessageRenderer
|
||||
from strix.interface.tui.renderers.fenced import parse_fenced_code
|
||||
from strix.interface.tui.renderers.user_message_renderer import UserMessageRenderer
|
||||
from strix.interface.utils import build_tui_stats_text
|
||||
from strix.report.state import ReportState, set_global_report_state
|
||||
@@ -330,12 +331,16 @@ class VulnerabilityDetailScreen(ModalScreen): # type: ignore[misc]
|
||||
return "#65a30d"
|
||||
return "#6b7280"
|
||||
|
||||
def _highlight_python(self, code: str) -> Text:
|
||||
def _highlight_python(self, code: str, language: str | None = None) -> Text:
|
||||
try:
|
||||
from pygments.lexers import PythonLexer
|
||||
from pygments.lexers import PythonLexer, get_lexer_by_name
|
||||
from pygments.styles import get_style_by_name
|
||||
from pygments.util import ClassNotFound
|
||||
|
||||
lexer = PythonLexer()
|
||||
if language:
|
||||
with contextlib.suppress(ClassNotFound):
|
||||
lexer = get_lexer_by_name(language)
|
||||
style = get_style_by_name("native")
|
||||
colors = {
|
||||
token: f"#{style_def['color']}" for token, style_def in style if style_def["color"]
|
||||
@@ -501,10 +506,11 @@ class VulnerabilityDetailScreen(ModalScreen): # type: ignore[misc]
|
||||
|
||||
poc_script_code = vuln.get("poc_script_code", "")
|
||||
if poc_script_code:
|
||||
poc_language, poc_code = parse_fenced_code(poc_script_code)
|
||||
text.append("\n\n")
|
||||
text.append("PoC Code", style=self.FIELD_STYLE)
|
||||
text.append("\n")
|
||||
text.append_text(self._highlight_python(poc_script_code))
|
||||
text.append_text(self._highlight_python(poc_code, poc_language))
|
||||
|
||||
remediation_steps = vuln.get("remediation_steps", "")
|
||||
if remediation_steps:
|
||||
@@ -601,8 +607,9 @@ class VulnerabilityDetailScreen(ModalScreen): # type: ignore[misc]
|
||||
lines.append(vuln["poc_description"])
|
||||
lines.append("")
|
||||
if vuln.get("poc_script_code"):
|
||||
lines.append("```python")
|
||||
lines.append(vuln["poc_script_code"])
|
||||
poc_language, poc_code = parse_fenced_code(vuln["poc_script_code"])
|
||||
lines.append(f"```{poc_language or 'python'}")
|
||||
lines.append(poc_code)
|
||||
lines.append("```")
|
||||
|
||||
if vuln.get("code_locations"):
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
import re
|
||||
|
||||
|
||||
_FENCE_RE = re.compile(r"^```([^\n`]*)\n(.*?)\n?```$", re.DOTALL)
|
||||
|
||||
|
||||
def parse_fenced_code(raw: str) -> tuple[str | None, str]:
|
||||
"""Split an optionally fenced code string into ``(language, code)``.
|
||||
|
||||
Agent-generated code fields (e.g. ``poc_script_code``) are stored wrapped in
|
||||
a markdown fence carrying the language, like ``` ```python\n...\n``` ```.
|
||||
Return the fence's language tag and the inner code, or ``(None, raw)`` when
|
||||
the value isn't fenced.
|
||||
"""
|
||||
match = _FENCE_RE.match(raw.strip())
|
||||
if not match:
|
||||
return None, raw
|
||||
info = match.group(1).strip()
|
||||
language = info.split()[0] if info else None
|
||||
return (language or None), match.group(2)
|
||||
@@ -1,12 +1,15 @@
|
||||
from functools import cache
|
||||
from typing import Any, ClassVar
|
||||
|
||||
from pygments.lexers import PythonLexer
|
||||
from pygments.lexer import Lexer
|
||||
from pygments.lexers import PythonLexer, get_lexer_by_name
|
||||
from pygments.styles import get_style_by_name
|
||||
from pygments.util import ClassNotFound
|
||||
from rich.text import Text
|
||||
from textual.widgets import Static
|
||||
|
||||
from .base_renderer import BaseToolRenderer
|
||||
from .fenced import parse_fenced_code
|
||||
from .registry import register_tool_renderer
|
||||
|
||||
|
||||
@@ -61,8 +64,17 @@ class CreateVulnerabilityReportRenderer(BaseToolRenderer):
|
||||
return None
|
||||
|
||||
@classmethod
|
||||
def _highlight_python(cls, code: str) -> Text:
|
||||
lexer = PythonLexer()
|
||||
def _get_lexer(cls, language: str | None) -> Lexer:
|
||||
if language:
|
||||
try:
|
||||
return get_lexer_by_name(language)
|
||||
except ClassNotFound:
|
||||
pass
|
||||
return PythonLexer()
|
||||
|
||||
@classmethod
|
||||
def _highlight_code(cls, code: str, language: str | None) -> Text:
|
||||
lexer = cls._get_lexer(language)
|
||||
text = Text()
|
||||
|
||||
for token_type, token_value in lexer.get_tokens(code):
|
||||
@@ -234,10 +246,11 @@ class CreateVulnerabilityReportRenderer(BaseToolRenderer):
|
||||
text.append(poc_description)
|
||||
|
||||
if poc_script_code:
|
||||
poc_language, poc_code = parse_fenced_code(poc_script_code)
|
||||
text.append("\n\n")
|
||||
text.append("PoC Code", style=FIELD_STYLE)
|
||||
text.append("\n")
|
||||
text.append_text(cls._highlight_python(poc_script_code))
|
||||
text.append_text(cls._highlight_code(poc_code, poc_language))
|
||||
|
||||
if remediation_steps:
|
||||
text.append("\n\n")
|
||||
|
||||
Reference in New Issue
Block a user