Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions scrapling/core/ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from mcp.types import ImageContent, TextContent
from pydantic import BaseModel, Field

from scrapling.core.shell import Convertor
from scrapling.core.shell import Convertor, _CONTROL_CHARS_PATTERN
from scrapling.engines.toolbelt.custom import Response as _ScraplingResponse
from scrapling.engines.static import ImpersonateType
from scrapling.fetchers import (
Expand Down Expand Up @@ -81,14 +81,15 @@ def _translate_response(
main_content_only: bool,
) -> ResponseModel:
"""Extract content from a response and translate it to a ResponseModel."""
content = list(
Convertor._extract_content(
content = [
_CONTROL_CHARS_PATTERN.sub("", chunk)
for chunk in Convertor._extract_content(
page,
css_selector=css_selector,
extraction_type=extraction_type,
main_content_only=main_content_only,
)
)
]
return ResponseModel(status=page.status, content=content, url=page.url)


Expand Down
8 changes: 5 additions & 3 deletions scrapling/core/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
" | .//template"
)
_ZWC_PATTERN = re_compile(r"[\u200b\u200c\u200d\ufeff\u2060\u180e]")
_CONTROL_CHARS_PATTERN = re_compile(r"[\x00-\x08\x0b\x0c\x0e-\x1f]")


# Suppress exit on error to handle parsing errors gracefully
Expand Down Expand Up @@ -599,16 +600,17 @@ def _sanitize_for_ai(cls, page: Selector) -> Selector:
"""Strip hidden content that could be used for prompt injection.

Removes CSS-hidden elements, aria-hidden elements, <template> tags,
HTML comments, and zero-width Unicode characters.
HTML comments, zero-width Unicode characters, and XML-incompatible
control characters.
"""
clean_root = deepcopy(page._root)
for element in cast(list, _HIDDEN_XPATH(clean_root)):
element.drop_tree()
for element in clean_root.iter():
if element.text:
element.text = _ZWC_PATTERN.sub("", element.text)
element.text = _CONTROL_CHARS_PATTERN.sub("", _ZWC_PATTERN.sub("", element.text))
if element.tail:
element.tail = _ZWC_PATTERN.sub("", element.tail)
element.tail = _CONTROL_CHARS_PATTERN.sub("", _ZWC_PATTERN.sub("", element.tail))
return Selector(root=clean_root, url=page.url, keep_comments=False)

@classmethod
Expand Down
15 changes: 15 additions & 0 deletions tests/ai/test_ai_mcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,29 @@
import pytest_httpbin
from mcp.types import ImageContent, TextContent

from scrapling.parser import Selector
from scrapling.core.ai import (
ScraplingMCPServer,
ResponseModel,
SessionInfo,
SessionCreatedModel,
SessionClosedModel,
_normalize_credentials,
_translate_response,
)


def test_translate_response_strips_control_characters():
"""Pages with control chars like U+0008 must not crash the get/fetch path (issue #366)"""
html = "<html><body><p>Hello\x08World</p>\t\n<div>Foo\x0cbar</div></body></html>"
page = Selector(html, url="https://jfinal.com/doc/1-5")
page.status = 200

result = _translate_response(page, "markdown", None, main_content_only=True)

joined = "".join(result.content)
assert "HelloWorld" in joined and "Foobar" in joined
assert not any(ord(c) < 0x20 and c not in "\t\n\r" for c in joined)
from scrapling.engines.toolbelt.custom import Response


Expand Down
Loading