Skip to content

Commit 2d1a334

Browse files
authored
Merge 2d3f9aa into a7fa0d6
2 parents a7fa0d6 + 2d3f9aa commit 2d1a334

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

source/ui.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@
2121
from comtypes import automation
2222
from comtypes import COMError
2323
from html import escape
24+
25+
import nh3
2426
from logHandler import log
2527
import gui
2628
import speech
2729
import braille
2830
from config.configFlags import TetherTo
2931
import globalVars
30-
from typing import Optional
32+
from typing import Callable, Optional
3133

3234
from utils.security import isRunningOnSecureDesktop
3335

@@ -135,6 +137,7 @@ def browseableMessage(
135137
isHtml: bool = False,
136138
closeButton: bool = False,
137139
copyButton: bool = False,
140+
sanitizeHtmlFunc: Callable[[str], str] = nh3.clean,
138141
) -> None:
139142
"""Present a message to the user that can be read in browse mode.
140143
The message will be presented in an HTML document.
@@ -144,6 +147,10 @@ def browseableMessage(
144147
:param isHtml: Whether the message is html, defaults to False.
145148
:param closeButton: Whether to include a "close" button, defaults to False.
146149
:param copyButton: Whether to include a "copy" (to clipboard) button, defaults to False.
150+
:param sanitizeHtmlFunc: How to sanitize the html message, if isHtml is True.
151+
Defaults to `nh3.clean` with default arguments.
152+
Ensure to sanitize the html message if the source of it could be untrusted.
153+
Any translatable string, or user generated content should be sanitized.
147154
"""
148155
if isRunningOnSecureDesktop():
149156
_warnBrowsableMessageNotAvailableOnSecureScreens(title)
@@ -179,10 +186,11 @@ def browseableMessage(
179186
d.add("title", title)
180187

181188
if not isHtml:
182-
message = f"<pre>{escape(message)}</pre>"
189+
messageSanitized = f"<pre>{escape(message)}</pre>"
183190
else:
184-
log.warning("Passing raw HTML to ui.browseableMessage!")
185-
d.add("message", message)
191+
log.warning("Sanitizing raw HTML before passing to ui.browseableMessage!")
192+
messageSanitized = sanitizeHtmlFunc(message)
193+
d.add("message", messageSanitized)
186194

187195
# Translators: A notice to the user that a copy operation succeeded.
188196
d.add("copySuccessfulAlertText", _("Text copied."))

user_docs/en/changes.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ As the NVDA update check URL is now configurable directly within NVDA, no replac
160160
* The following symbols have been removed with no replacement: `languageHandler.getLanguageCliArgs`, `__main__.quitGroup` and `__main__.installGroup` . (#17486, @CyrilleB79)
161161
* Prefix matching on command line flags, e.g. using `--di` for `--disable-addons` is no longer supported. (#11644, @CyrilleB79)
162162
* The `useAsFallBack` keyword argument of `bdDetect.DriverRegistrar` has been renamed to `useAsFallback`. (#17521, @LeonarddeR)
163+
* In `NVDAObjects.window.scintilla.ScintillaTextInfo`, if no text is selected, the `collapse` method is overriden to expand to line if the `end` parameter is set to `True`. (#17431, @nvdaes)
164+
* `ui.browseableMessage` now takes a parameter `sanitizeHtmlFunc`.
165+
This defaults to `nh3.clean` with default arguments.
166+
This means any HTML passed into `ui.browseableMessage` using `isHtml=True` is now sanitized by default.
167+
To change sanitization rules, such as whitelisting tags or attributes, create a function that calls `nh3.clean` with the desired parameters. (#16985)
163168

164169
#### Deprecations
165170

0 commit comments

Comments
 (0)