-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlib.rs
280 lines (272 loc) · 11.8 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
use std::borrow::Cow;
use std::collections::{HashMap, HashSet};
use pyo3::exceptions::PyTypeError;
use pyo3::prelude::*;
use pyo3::types::{PyString, PyTuple};
/// Sanitize an HTML fragment according to the given options.
///
/// :param html: Input HTML fragment
/// :type html: ``str``
/// :param tags: Sets the tags that are allowed.
/// :type tags: ``set[str]``, optional
/// :param clean_content_tags: Sets the tags whose contents will be completely removed from the output.
/// :type clean_content_tags: ``set[str]``, optional
/// :param attributes: Sets the HTML attributes that are allowed on specific tags,
/// ``*`` key means the attributes are allowed on any tag.
/// :type attributes: ``dict[str, set[str]]``, optional
/// :param attribute_filter: Allows rewriting of all attributes using a callback.
/// The callback takes name of the element, attribute and its value.
/// Returns ``None`` to remove the attribute, or a value to use.
/// :type attribute_filter: ``Callable[[str, str, str], str | None]``, optional
/// :param strip_comments: Configures the handling of HTML comments, defaults to ``True``.
/// :type strip_comments: ``bool``
/// :param link_rel: Configures a ``rel`` attribute that will be added on links, defaults to ``noopener noreferrer``.
/// To turn on rel-insertion, pass a space-separated list.
/// If ``rel`` is in the generic or tag attributes, this must be set to ``None``. Common ``rel`` values to include:
///
/// - ``noopener``: This prevents a particular type of XSS attack, and should usually be turned on for untrusted HTML.
/// - ``noreferrer``: This prevents the browser from sending the source URL to the website that is linked to.
/// - ``nofollow``: This prevents search engines from using this link for ranking, which disincentivizes spammers.
/// :type link_rel: ``str``
/// :param generic_attribute_prefixes: Sets the prefix of attributes that are allowed on any tag.
/// :type generic_attribute_prefixes: ``set[str]``, optional
/// :param tag_attribute_values: Sets the values of HTML attributes that are allowed on specific tags.
/// The value is structured as a map from tag names to a map from attribute names to a set of attribute values.
/// If a tag is not itself whitelisted, adding entries to this map will do nothing.
/// :type tag_attribute_values: ``dict[str, dict[str, set[str]]]``, optional
/// :param set_tag_attribute_values: Sets the values of HTML attributes that are to be set on specific tags.
/// The value is structured as a map from tag names to a map from attribute names to an attribute value.
/// If a tag is not itself whitelisted, adding entries to this map will do nothing.
/// :type set_tag_attribute_values: ``dict[str, dict[str, str]]``, optional
/// :param url_schemes: Sets the URL schemes permitted on ``href`` and ``src`` attributes.
/// :type url_schemes: ``set[str]``, optional
/// :return: Sanitized HTML fragment
/// :rtype: ``str``
///
/// For example:
///
/// .. code-block:: pycon
///
/// >>> import nh3
/// >>> nh3.clean("<unknown>hi")
/// 'hi'
/// >>> nh3.clean("<b><img src='' onerror='alert(\\'hax\\')'>XSS?</b>")
/// '<b><img src="">XSS?</b>'
#[pyfunction(signature = (
html,
tags = None,
clean_content_tags = None,
attributes = None,
attribute_filter = None,
strip_comments = true,
link_rel = "noopener noreferrer",
generic_attribute_prefixes = None,
tag_attribute_values = None,
set_tag_attribute_values = None,
url_schemes = None,
))]
#[allow(clippy::too_many_arguments)]
fn clean(
py: Python,
html: &str,
tags: Option<HashSet<String>>,
clean_content_tags: Option<HashSet<String>>,
mut attributes: Option<HashMap<String, HashSet<String>>>,
attribute_filter: Option<PyObject>,
strip_comments: bool,
link_rel: Option<&str>,
generic_attribute_prefixes: Option<HashSet<String>>,
tag_attribute_values: Option<HashMap<String, HashMap<String, HashSet<String>>>>,
set_tag_attribute_values: Option<HashMap<String, HashMap<String, String>>>,
url_schemes: Option<HashSet<String>>,
) -> PyResult<String> {
if let Some(callback) = attribute_filter.as_ref() {
if !callback.bind(py).is_callable() {
return Err(PyTypeError::new_err("attribute_filter must be callable"));
}
}
let cleaned = py.allow_threads(|| {
if tags.is_some()
|| clean_content_tags.is_some()
|| attributes.is_some()
|| attribute_filter.is_some()
|| !strip_comments
|| link_rel.as_deref() != Some("noopener noreferrer")
|| generic_attribute_prefixes.is_some()
|| tag_attribute_values.is_some()
|| set_tag_attribute_values.is_some()
|| url_schemes.is_some()
{
let generic_attrs = attributes.as_mut().and_then(|attrs| attrs.remove("*"));
let mut cleaner = ammonia::Builder::default();
if let Some(tags) = tags.as_ref() {
let tags: HashSet<&str> = tags.iter().map(|s| s.as_str()).collect();
cleaner.tags(tags);
}
if let Some(tags) = clean_content_tags.as_ref() {
let tags: HashSet<&str> = tags.iter().map(|s| s.as_str()).collect();
cleaner.clean_content_tags(tags);
}
if let Some(attrs) = attributes.as_ref() {
let attrs: HashMap<&str, HashSet<&str>> = attrs
.iter()
.map(|(k, v)| (k.as_str(), v.iter().map(|s| s.as_str()).collect()))
.collect();
cleaner.tag_attributes(attrs);
if let Some(generic_attrs) = generic_attrs.as_ref() {
let generic_attrs: HashSet<&str> =
generic_attrs.iter().map(|s| s.as_str()).collect();
cleaner.generic_attributes(generic_attrs);
}
}
if let Some(prefixes) = generic_attribute_prefixes.as_ref() {
let prefixes: HashSet<&str> = prefixes.iter().map(|s| s.as_str()).collect();
cleaner.generic_attribute_prefixes(prefixes);
}
if let Some(values) = tag_attribute_values.as_ref() {
let values: HashMap<&str, HashMap<&str, HashSet<&str>>> = values
.iter()
.map(|(tag, attrs)| {
let inner: HashMap<&str, HashSet<&str>> = attrs
.iter()
.map(|(attr, vals)| {
(attr.as_str(), vals.iter().map(|v| v.as_str()).collect())
})
.collect();
(tag.as_str(), inner)
})
.collect();
cleaner.tag_attribute_values(values);
}
if let Some(values) = set_tag_attribute_values.as_ref() {
let values: HashMap<&str, HashMap<&str, &str>> = values
.iter()
.map(|(tag, attrs)| {
let inner: HashMap<&str, &str> = attrs
.iter()
.map(|(attr, val)| (attr.as_str(), val.as_str()))
.collect();
(tag.as_str(), inner)
})
.collect();
cleaner.set_tag_attribute_values(values);
}
if let Some(callback) = attribute_filter {
cleaner.attribute_filter(move |element, attribute, value| {
Python::with_gil(|py| {
let res = callback.call(
py,
PyTuple::new(
py,
[
PyString::new(py, element),
PyString::new(py, attribute),
PyString::new(py, value),
],
)
.unwrap(),
None,
);
let err = match res {
Ok(val) => {
if val.is_none(py) {
return None;
} else if let Ok(s) = val.extract::<String>(py) {
return Some(Cow::<str>::Owned(s));
} else {
PyTypeError::new_err(
"expected attribute_filter to return str or None",
)
}
}
Err(err) => err,
};
err.write_unraisable(
py,
Some(
&PyTuple::new(
py,
[
PyString::new(py, element),
PyString::new(py, attribute),
PyString::new(py, value),
],
)
.unwrap(),
),
);
Some(value.into())
})
});
}
cleaner.strip_comments(strip_comments);
cleaner.link_rel(link_rel.as_deref());
if let Some(url_schemes) = url_schemes.as_ref() {
let url_schemes: HashSet<_> = url_schemes.iter().map(|s| s.as_str()).collect();
cleaner.url_schemes(url_schemes);
}
cleaner.clean(html).to_string()
} else {
ammonia::clean(html)
}
});
Ok(cleaned)
}
/// Turn an arbitrary string into unformatted HTML.
///
/// Roughly equivalent to Python’s html.escape() or PHP’s htmlspecialchars and
/// htmlentities. Escaping is as strict as possible, encoding every character
/// that has special meaning to the HTML parser.
///
/// :param html: Input HTML fragment
/// :type html: ``str``
/// :return: Cleaned text
/// :rtype: ``str``
///
/// For example:
///
/// .. code-block:: pycon
///
/// >>> import nh3
/// >>> nh3.clean_text('Robert"); abuse();//')
/// 'Robert"); abuse();//'
#[pyfunction]
fn clean_text(py: Python, html: &str) -> String {
py.allow_threads(|| ammonia::clean_text(html))
}
/// Determine if a given string contains HTML.
///
/// This function parses the full string and checks for any HTML syntax.
///
/// Note: This function will return True for strings that contain invalid HTML syntax
/// like ``<g>`` and even ``Vec::<u8>::new()``.
///
/// :param html: Input string
/// :type html: ``str``
/// :rtype: ``bool``
///
/// For example:
///
/// .. code-block:: pycon
///
/// >>> nh3.is_html("plain text")
/// False
/// >>> nh3.is_html("<p>html!</p>")
/// True
#[pyfunction]
fn is_html(py: Python, html: &str) -> bool {
py.allow_threads(|| ammonia::is_html(html))
}
/// Python bindings to the ammonia HTML sanitization library ( https://github.com/rust-ammonia/ammonia ).
#[pymodule(gil_used = false)]
fn nh3(_py: Python, m: &Bound<PyModule>) -> PyResult<()> {
m.add("__version__", env!("CARGO_PKG_VERSION"))?;
m.add_function(wrap_pyfunction!(clean, m)?)?;
m.add_function(wrap_pyfunction!(clean_text, m)?)?;
m.add_function(wrap_pyfunction!(is_html, m)?)?;
let a = ammonia::Builder::default();
m.add("ALLOWED_TAGS", a.clone_tags())?;
m.add("ALLOWED_ATTRIBUTES", a.clone_tag_attributes())?;
m.add("ALLOWED_URL_SCHEMES", a.clone_url_schemes())?;
Ok(())
}