Skip to content
Merged
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
20 changes: 15 additions & 5 deletions tensorboard/plugin_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import threading

import bleach
from bleach.sanitizer import Cleaner

# pylint: disable=g-bad-import-order
# Google-only: import markdown_freewisdom
Expand Down Expand Up @@ -64,7 +64,7 @@
]

# Cache Markdown converter to avoid expensive initialization at each
# call to `markdown_to_safe_html`.
# call to `markdown_to_safe_html`. Cache a different instance per thread.
class _MarkdownStore(threading.local):
def __init__(self):
self.markdown = markdown.Markdown(
Expand All @@ -75,6 +75,18 @@ def __init__(self):
_MARKDOWN_STORE = _MarkdownStore()


# Cache Cleaner to avoid expensive initialization at each call to `clean`.
# Cache a different instance per thread.
class _CleanerStore(threading.local):
def __init__(self):
self.cleaner = Cleaner(
tags=_ALLOWED_TAGS, attributes=_ALLOWED_ATTRIBUTES
)


_CLEANER_STORE = _CleanerStore()


def markdown_to_safe_html(markdown_string):
"""Convert Markdown to HTML that's safe to splice into the DOM.

Expand Down Expand Up @@ -126,9 +138,7 @@ def markdowns_to_safe_html(markdown_strings, combine):
unsafe_htmls.append(unsafe_html)

unsafe_combined = combine(unsafe_htmls)
sanitized_combined = bleach.clean(
unsafe_combined, tags=_ALLOWED_TAGS, attributes=_ALLOWED_ATTRIBUTES
)
sanitized_combined = _CLEANER_STORE.cleaner.clean(unsafe_combined)

warning = ""
if total_null_bytes:
Expand Down