Skip to content

Commit

Permalink
GHSL-2021-1013
Browse files Browse the repository at this point in the history
  • Loading branch information
martinRenou authored and SylvainCorlay committed Aug 9, 2022
1 parent 8fa95b8 commit d09000b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
3 changes: 2 additions & 1 deletion nbconvert/filters/highlight.py
Expand Up @@ -10,6 +10,7 @@
# because errors should be raised at runtime if it's actually needed,
# not import time, when it may not be needed.

from html import escape
from warnings import warn

from traitlets import observe
Expand Down Expand Up @@ -67,7 +68,7 @@ def __call__(self, source, language=None, metadata=None):
return _pygments_highlight(
source if len(source) > 0 else " ",
# needed to help post processors:
HtmlFormatter(cssclass=" highlight hl-" + language, **self.extra_formatter_options),
HtmlFormatter(cssclass=escape(f" highlight hl-{language}"), **self.extra_formatter_options),
language,
metadata,
)
Expand Down
7 changes: 7 additions & 0 deletions nbconvert/filters/tests/test_highlight.py
Expand Up @@ -16,6 +16,8 @@

import xml

import pytest

from ...tests.base import TestsBase
from ..highlight import Highlight2HTML, Highlight2Latex

Expand Down Expand Up @@ -72,6 +74,11 @@ def test_parse_html_many_lang(self):
root = xml.etree.ElementTree.fromstring(lang)
self.assertEqual(self._extract_tokens(root, "k"), set(tkns))

@pytest.mark.filterwarnings("ignore")
def test_inject_html(self):
out = highlight2html(self.tests[0], 'ipython3-foo"><script>alert(1)</script>')
assert '<script>alert(1)</script>' not in out

def _extract_tokens(self, root, cls):
return set(map(lambda x: x.text, root.findall(".//*[@class='" + cls + "']")))

Expand Down
1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -237,6 +237,7 @@ def get_data_files():
)

setup_args["install_requires"] = [
"lxml",
"beautifulsoup4",
"bleach",
"defusedxml",
Expand Down

0 comments on commit d09000b

Please sign in to comment.