-
-
Notifications
You must be signed in to change notification settings - Fork 108
/
Filters.php
283 lines (228 loc) · 6.75 KB
/
Filters.php
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
<?php
/**
* This file is part of the Latte (https://latte.nette.org)
* Copyright (c) 2008 David Grudl (https://davidgrudl.com)
*/
declare(strict_types=1);
namespace Latte\Runtime;
use Latte;
use Latte\ContentType;
use Latte\RuntimeException;
use Nette;
/**
* Escaping & sanitization filters.
* @internal
*/
class Filters
{
/** @deprecated */
public static string $dateFormat = "j.\u{a0}n.\u{a0}Y";
/**
* Escapes string for use everywhere inside HTML (except for comments).
*/
public static function escapeHtml($s): string
{
return htmlspecialchars((string) $s, ENT_QUOTES | ENT_HTML5 | ENT_SUBSTITUTE, 'UTF-8');
}
/**
* Escapes string for use inside HTML text.
*/
public static function escapeHtmlText($s): string
{
if ($s instanceof HtmlStringable || $s instanceof Nette\HtmlStringable) {
return $s->__toString();
}
$s = htmlspecialchars((string) $s, ENT_NOQUOTES | ENT_SUBSTITUTE, 'UTF-8');
$s = strtr($s, ['{{' => '{<!-- -->{', '{' => '{']);
return $s;
}
/**
* Escapes string for use inside HTML attribute value.
*/
public static function escapeHtmlAttr($s, bool $double = true): string
{
$double = $double && $s instanceof HtmlStringable ? false : $double;
$s = (string) $s;
if (str_contains($s, '`') && strpbrk($s, ' <>"\'') === false) {
$s .= ' '; // protection against innerHTML mXSS vulnerability nette/nette#1496
}
$s = htmlspecialchars($s, ENT_QUOTES | ENT_HTML5 | ENT_SUBSTITUTE, 'UTF-8', $double);
$s = str_replace('{', '{', $s);
return $s;
}
/**
* Escapes string for use inside HTML tag.
*/
public static function escapeHtmlTag($s): string
{
$s = (string) $s;
$s = htmlspecialchars($s, ENT_QUOTES | ENT_HTML5 | ENT_SUBSTITUTE, 'UTF-8');
return preg_replace_callback(
'#[=/\s]#',
fn($m) => '&#' . ord($m[0]) . ';',
$s,
);
}
/**
* Escapes string for use inside HTML/XML comments.
*/
public static function escapeHtmlComment($s): string
{
$s = (string) $s;
if ($s && ($s[0] === '-' || $s[0] === '>' || $s[0] === '!')) {
$s = ' ' . $s;
}
$s = str_replace('--', '- - ', $s);
if (substr($s, -1) === '-') {
$s .= ' ';
}
return $s;
}
/**
* Escapes HTML for usage in <script type=text/html>
*/
public static function escapeHtmlRawTextHtml($s): string
{
if ($s instanceof HtmlStringable || $s instanceof Nette\HtmlStringable) {
return self::convertHtmlToHtmlRawText($s->__toString());
}
return htmlspecialchars((string) $s, ENT_QUOTES | ENT_HTML5 | ENT_SUBSTITUTE, 'UTF-8');
}
/**
* Escapes only quotes.
*/
public static function escapeHtmlQuotes($s): string
{
return strtr((string) $s, ['"' => '"', "'" => ''']);
}
/**
* Escapes string for use everywhere inside XML (except for comments and tags).
*/
public static function escapeXml($s): string
{
if ($s instanceof HtmlStringable) {
return $s->__toString();
}
// XML 1.0: \x09 \x0A \x0D and C1 allowed directly, C0 forbidden
// XML 1.1: \x00 forbidden directly and as a character reference,
// \x09 \x0A \x0D \x85 allowed directly, C0, C1 and \x7F allowed as character references
$s = preg_replace('#[\x00-\x08\x0B\x0C\x0E-\x1F]#', "\u{FFFD}", (string) $s);
return htmlspecialchars($s, ENT_QUOTES | ENT_XML1 | ENT_SUBSTITUTE, 'UTF-8');
}
/**
* Escapes string for use inside XML tag.
*/
public static function escapeXmlTag($s): string
{
$s = self::escapeXml((string) $s);
return preg_replace_callback(
'#[=/\s]#',
fn($m) => '&#' . ord($m[0]) . ';',
$s,
);
}
/**
* Escapes string for use inside CSS template.
*/
public static function escapeCss($s): string
{
// http://www.w3.org/TR/2006/WD-CSS21-20060411/syndata.html#q6
return addcslashes((string) $s, "\x00..\x1F!\"#$%&'()*+,./:;<=>?@[\\]^`{|}~");
}
/**
* Escapes variables for use inside <script>.
*/
public static function escapeJs(mixed $s): string
{
if ($s instanceof HtmlStringable || $s instanceof Nette\HtmlStringable) {
$s = $s->__toString();
}
$json = json_encode($s, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_INVALID_UTF8_SUBSTITUTE);
if ($error = json_last_error()) {
throw new Latte\RuntimeException(json_last_error_msg());
}
return str_replace([']]>', '<!', '</'], [']]\u003E', '\u003C!', '<\/'], $json);
}
/**
* Escapes string for use inside iCal template.
*/
public static function escapeICal($s): string
{
// https://www.ietf.org/rfc/rfc5545.txt
$s = str_replace("\r", '', (string) $s);
$s = preg_replace('#[\x00-\x08\x0B-\x1F]#', "\u{FFFD}", (string) $s);
return addcslashes($s, "\";\\,:\n");
}
/**
* Converts ... to ...
*/
public static function convertTo(FilterInfo $info, string $dest, string $s): string
{
$source = $info->contentType ?: ContentType::Text;
if ($source === $dest) {
return $s;
} elseif ($conv = Latte\Compiler\Escaper::getConvertor($source, $dest)) {
$info->contentType = $dest;
return $conv($s);
} else {
throw new RuntimeException('Filters: unable to convert content type ' . strtoupper($source) . ' to ' . strtoupper($dest));
}
}
public static function nop($s): string
{
return (string) $s;
}
/**
* Converts JS and CSS for usage in <script> or <style>
*/
public static function convertJSToHtmlRawText($s): string
{
return preg_replace('#</(script|style)#i', '<\/$1', (string) $s);
}
/**
* Sanitizes <script> in <script type=text/html>
*/
public static function convertHtmlToHtmlRawText(string $s): string
{
return preg_replace('#(</?)(script)#i', '$1x-$2', $s);
}
/**
* Converts HTML text to quoted attribute. The quotation marks need to be escaped.
*/
public static function convertHtmlToHtmlAttr(string $s): string
{
return self::escapeHtmlAttr($s, false);
}
/**
* Converts HTML to plain text.
*/
public static function convertHtmlToText(string $s): string
{
$s = strip_tags($s);
return html_entity_decode($s, ENT_QUOTES | ENT_HTML5, 'UTF-8');
}
/**
* Sanitizes string for use inside href attribute.
*/
public static function safeUrl($s): string
{
$s = $s instanceof HtmlStringable
? self::convertHtmlToText((string) $s)
: (string) $s;
return preg_match('~^(?:(?:https?|ftp)://[^@]+(?:/.*)?|(?:mailto|tel|sms):.+|[/?#].*|[^:]+)$~Di', $s) ? $s : '';
}
/**
* Validates HTML tag name.
*/
public static function safeTag(mixed $name, bool $xml = false): string
{
if (!is_string($name)) {
throw new Latte\RuntimeException('Tag name must be string, ' . get_debug_type($name) . ' given');
} elseif (!preg_match('~' . Latte\Compiler\TemplateLexer::ReTagName . '$~DA', $name)) {
throw new Latte\RuntimeException("Invalid tag name '$name'");
} elseif (!$xml && in_array(strtolower($name), ['style', 'script'], true)) {
throw new Latte\RuntimeException("Forbidden variable tag name <$name>");
}
return $name;
}
}