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
8 changes: 2 additions & 6 deletions packages/syft-job/src/syft_job/job_repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,8 @@ def _repr_html_(self) -> str:
</div>
"""

html_content = self._convert_ansi_to_html(raw_content)
html_content = (
html_content.replace("&", "&amp;")
.replace('"', "&quot;")
.replace("'", "&#x27;")
)
# Escape job output before adding the trusted HTML generated from ANSI codes.
html_content = self._convert_ansi_to_html(escape(raw_content))

clean_content = self._strip_ansi_codes(raw_content)
lines = clean_content.split("\n")
Expand Down
14 changes: 3 additions & 11 deletions packages/syft-job/src/syft_job/job_stdout.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import html
import re
from typing import TYPE_CHECKING

Expand Down Expand Up @@ -129,17 +130,8 @@ def _repr_html_(self) -> str:
</div>
"""

# Convert ANSI codes to HTML for display
html_content = self._convert_ansi_to_html(raw_content)

# Escape any remaining HTML characters that aren't our color spans
# We need to be careful not to escape our intentional HTML
html_content = (
html_content.replace("&", "&amp;")
.replace('"', "&quot;")
.replace("'", "&#x27;")
)
# Don't escape < and > since we want our HTML spans to work
# Escape job output before adding the trusted HTML generated from ANSI codes.
html_content = self._convert_ansi_to_html(html.escape(raw_content))

# Count lines and characters (use clean content for stats)
clean_content = self._strip_ansi_codes(raw_content)
Expand Down
33 changes: 33 additions & 0 deletions packages/syft-job/tests/test_job_output_repr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from types import SimpleNamespace

import pytest

from syft_job.job_repr import StderrViewer
from syft_job.job_stdout import StdoutViewer


@pytest.mark.parametrize(
("viewer_class", "file_name"),
[
(StdoutViewer, "stdout.txt"),
(StderrViewer, "stderr.txt"),
],
)
def test_job_output_html_escapes_markup_and_preserves_ansi(
tmp_path, viewer_class, file_name
):
output_file = tmp_path / file_name
output_file.write_text(
"<class 'int'>\n\x1b[31mfailed\x1b[0m\n<script>alert('xss')</script>",
encoding="utf-8",
)
viewer = viewer_class(
SimpleNamespace(status="done", artifact_path=lambda _: output_file)
)

rendered_html = viewer._repr_html_()

assert "&lt;class &#x27;int&#x27;&gt;" in rendered_html
assert '<span style="color: #cd3131;">failed</span>' in rendered_html
assert "<script>" not in rendered_html
assert "&lt;script&gt;alert(&#x27;xss&#x27;)&lt;/script&gt;" in rendered_html
Loading