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
281
282
283
284
285
286
287
288
289
290
291
//! Context-aware output encoders that prevent cross-site scripting.
//!
//! Each filter encodes only the characters needed to keep an untrusted string
//! from breaking out of a specific HTML output context. The supported contexts
//! are HTML data, HTML comments, single/double/unquoted attribute values, and
//! URIs placed inside any of those. URI filters normalize IPv6 hosts and block
//! dangerous protocols such as `javascript:` and `data:`.
//!
//! The design encodes the minimal set of characters required to thwart a
//! context break, rather than entity-escaping everything. This is faster and
//! avoids double-encoding.
//!
//! # Choosing a filter
//!
//! Match the filter to the exact place the value lands in the document.
//!
//! ```
//! use xss_filters::{in_html_data, in_double_quoted_attr, uri_in_double_quoted_attr};
//!
//! // <div>{{ value }}</div>
//! assert_eq!(in_html_data("<script>"), "<script>");
//!
//! // <input value="{{ value }}">
//! assert_eq!(in_double_quoted_attr("a\"b"), "a"b");
//!
//! // <a href="{{ value }}">
//! assert_eq!(
//! uri_in_double_quoted_attr("javascript:alert(1)"),
//! "x-javascript:alert(1)"
//! );
//! ```
//!
//! # Input contract
//!
//! All public functions take `&str` and return `String`. JavaScript-style
//! coercion of non-string values is the caller's job. Apply these filters only
//! to UTF-8 documents. Do not apply them inside scriptable contexts such as
//! `<script>`, `<style>`, `<object>`, `<embed>`, `<svg>`, or inside `style` and
//! `on*` attributes.
//!
//! # URI encoding and surrogates
//!
//! The URI filters build on encoders equivalent to JavaScript `encodeURI` and
//! `encodeURIComponent`. Those throw on a lone UTF-16 surrogate. A Rust `&str`
//! is always well-formed UTF-8, so the `&str` entry points cannot reach that
//! case. [`uri::encode_uri_units`] reproduces the throw for raw UTF-16 input.
extern crate alloc;
use String;
extern crate std;
// ---------------------------------------------------------------------------
// Generic context filters
// ---------------------------------------------------------------------------
/// Encode `<` as `<` for HTML data context.
///
/// Use inside element content such as `<div>{{ s }}</div>`. Only `<` is
/// encoded. `>`, `&`, `"`, and `'` pass through.
///
/// ```
/// assert_eq!(xss_filters::in_html_data("foo&<>'\""), "foo&<>'\"");
/// ```
/// Neutralize HTML comment closers for HTML comment context.
///
/// Use inside `<!-- {{ s }} -->`. Sequences that could close the comment
/// (`-->`, `--!>`, a leading `-*>`), collaborate to close it (a trailing `--!`,
/// `--`, or `-`), or trigger IE conditional comments (`]>` or a trailing `]`)
/// gain a space. NUL becomes the replacement character.
///
/// ```
/// assert_eq!(xss_filters::in_html_comment("foo-->bar"), "foo-- >bar");
/// ```
/// Encode `'` as `'` for single-quoted attribute context.
///
/// Use inside `<input value='{{ s }}'>`. Not for `on*` attributes. For URIs use
/// the matching `uri*_in_single_quoted_attr` filter.
///
/// ```
/// assert_eq!(xss_filters::in_single_quoted_attr("it's"), "it's");
/// ```
/// Encode `"` as `"` for double-quoted attribute context.
///
/// Use inside `<input value="{{ s }}">`. Not for `on*` attributes. For URIs use
/// the matching `uri*_in_double_quoted_attr` filter.
///
/// ```
/// assert_eq!(xss_filters::in_double_quoted_attr("a\"b"), "a"b");
/// ```
/// Encode attribute-breaking characters for unquoted attribute context.
///
/// Use inside `<input value={{ s }}>`. Whitespace, quotes, backtick, `=`, `<`,
/// `>`, and NUL are encoded. An empty input yields a single replacement
/// character so the value cannot silently swallow the next attribute.
///
/// ```
/// assert_eq!(xss_filters::in_unquoted_attr(""), "\u{FFFD}");
/// assert_eq!(xss_filters::in_unquoted_attr("a b"), "a b");
/// ```
// ---------------------------------------------------------------------------
// URI matrix: helpers
// ---------------------------------------------------------------------------
/// Compose a full or path URI attribute filter: encode, attr-escape, then guard.
// ---------------------------------------------------------------------------
// Full URI (encodeURI + IPv6 restore, protocol guard on attrs)
// ---------------------------------------------------------------------------
/// Full URI inside a single-quoted attribute.
///
/// `encodeURI` plus IPv6 restore, then single-quote escape, then protocol guard.
/// Full URI inside a double-quoted attribute.
/// Full URI inside an unquoted attribute.
/// Full URI in HTML data context. Only the URI encoder runs, since `<` is
/// already `%3C`.
/// Full URI in HTML comment context.
// ---------------------------------------------------------------------------
// URI path / query (encodeURI, no IPv6 restore, protocol guard on attrs)
// ---------------------------------------------------------------------------
/// URI path or query inside a single-quoted attribute.
/// URI path or query inside a double-quoted attribute.
/// URI path or query inside an unquoted attribute.
/// URI path or query in HTML data context.
/// URI path or query in HTML comment context.
// URI query filters are exact aliases of the path filters.
/// URI query inside a single-quoted attribute. Alias of the path filter.
/// URI query inside a double-quoted attribute. Alias of the path filter.
/// URI query inside an unquoted attribute. Alias of the path filter.
/// URI query in HTML data context. Alias of the path filter.
/// URI query in HTML comment context. Alias of the path filter.
// ---------------------------------------------------------------------------
// URI component (encodeURIComponent, no protocol guard)
// ---------------------------------------------------------------------------
/// URI component inside a single-quoted attribute.
///
/// No protocol guard runs because `encodeURIComponent` already percent-encodes
/// the `:` that a `javascript:` scheme needs.
/// URI component inside a double-quoted attribute.
/// URI component inside an unquoted attribute.
/// URI component in HTML data context.
/// URI component in HTML comment context.
// ---------------------------------------------------------------------------
// URI fragment (encodeURIComponent; protocol guard on attrs)
// ---------------------------------------------------------------------------
/// URI fragment inside a single-quoted attribute.
///
/// Like the component filter but with the protocol guard, since a fragment
/// pasted into `href` could carry `javascript:`.
/// URI fragment inside a double-quoted attribute.
/// URI fragment inside an unquoted attribute.
/// URI fragment in HTML data context. Alias of the component filter.
/// URI fragment in HTML comment context. Alias of the component filter.