forked from phalcon/cphalcon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
escaper.c
405 lines (324 loc) · 10.6 KB
/
escaper.c
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
+------------------------------------------------------------------------+
| Phalcon Framework |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2014 Phalcon Team (http://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file docs/LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to license@phalconphp.com so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: Andres Gutierrez <andres@phalconphp.com> |
| Eduar Carvajal <eduar@phalconphp.com> |
+------------------------------------------------------------------------+
*/
#include "escaper.h"
#include "escaperinterface.h"
#include "escaper/exception.h"
#include <ext/standard/html.h>
#include "kernel/main.h"
#include "kernel/memory.h"
#include "kernel/exception.h"
#include "kernel/object.h"
#include "kernel/fcall.h"
#include "kernel/filter.h"
#include "kernel/string.h"
#include "kernel/framework/url.h"
/**
* Phalcon\Escaper
*
* Escapes different kinds of text securing them. By using this component you may
* prevent XSS attacks.
*
* This component only works with UTF-8. The PREG extension needs to be compiled with UTF-8 support.
*
*<code>
* $escaper = new Phalcon\Escaper();
* $escaped = $escaper->escapeCss("font-family: <Verdana>");
* echo $escaped; // font\2D family\3A \20 \3C Verdana\3E
*</code>
*/
zend_class_entry *phalcon_escaper_ce;
PHP_METHOD(Phalcon_Escaper, setEncoding);
PHP_METHOD(Phalcon_Escaper, getEncoding);
PHP_METHOD(Phalcon_Escaper, setHtmlQuoteType);
PHP_METHOD(Phalcon_Escaper, detectEncoding);
PHP_METHOD(Phalcon_Escaper, normalizeEncoding);
PHP_METHOD(Phalcon_Escaper, escapeHtml);
PHP_METHOD(Phalcon_Escaper, escapeHtmlAttr);
PHP_METHOD(Phalcon_Escaper, escapeCss);
PHP_METHOD(Phalcon_Escaper, escapeJs);
PHP_METHOD(Phalcon_Escaper, escapeUrl);
ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_escaper_detectencoding, 0, 0, 1)
ZEND_ARG_INFO(0, str)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_escaper_normalizeencoding, 0, 0, 1)
ZEND_ARG_INFO(0, str)
ZEND_END_ARG_INFO()
static const zend_function_entry phalcon_escaper_method_entry[] = {
PHP_ME(Phalcon_Escaper, setEncoding, arginfo_phalcon_escaperinterface_setencoding, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Escaper, getEncoding, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Escaper, setHtmlQuoteType, arginfo_phalcon_escaperinterface_sethtmlquotetype, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Escaper, detectEncoding, arginfo_phalcon_escaper_detectencoding, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Escaper, normalizeEncoding, arginfo_phalcon_escaper_normalizeencoding, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Escaper, escapeHtml, arginfo_phalcon_escaperinterface_escapehtml, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Escaper, escapeHtmlAttr, arginfo_phalcon_escaperinterface_escapehtmlattr, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Escaper, escapeCss, arginfo_phalcon_escaperinterface_escapecss, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Escaper, escapeJs, arginfo_phalcon_escaperinterface_escapejs, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Escaper, escapeUrl, arginfo_phalcon_escaperinterface_escapeurl, ZEND_ACC_PUBLIC)
PHP_FE_END
};
/**
* Phalcon\Escaper initializer
*/
PHALCON_INIT_CLASS(Phalcon_Escaper){
PHALCON_REGISTER_CLASS(Phalcon, Escaper, escaper, phalcon_escaper_method_entry, 0);
zend_declare_property_string(phalcon_escaper_ce, SL("_encoding"), "utf-8", ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_escaper_ce, SL("_htmlEscapeMap"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_long(phalcon_escaper_ce, SL("_htmlQuoteType"), 3, ZEND_ACC_PROTECTED TSRMLS_CC);
zend_class_implements(phalcon_escaper_ce TSRMLS_CC, 1, phalcon_escaperinterface_ce);
return SUCCESS;
}
/**
* Sets the encoding to be used by the escaper
*
*<code>
* $escaper->setEncoding('utf-8');
*</code>
*
* @param string $encoding
*/
PHP_METHOD(Phalcon_Escaper, setEncoding){
zval **encoding;
phalcon_fetch_params_ex(1, 0, &encoding);
PHALCON_ENSURE_IS_STRING(encoding);
phalcon_update_property_this(this_ptr, SL("_encoding"), *encoding TSRMLS_CC);
}
/**
* Returns the internal encoding used by the escaper
*
* @return string
*/
PHP_METHOD(Phalcon_Escaper, getEncoding){
RETURN_MEMBER(this_ptr, "_encoding");
}
/**
* Sets the HTML quoting type for htmlspecialchars
*
*<code>
* $escaper->setHtmlQuoteType(ENT_XHTML);
*</code>
*
* @param int $quoteType
*/
PHP_METHOD(Phalcon_Escaper, setHtmlQuoteType){
zval **quote_type;
phalcon_fetch_params_ex(1, 0, "e_type);
PHALCON_ENSURE_IS_LONG(quote_type);
phalcon_update_property_this(this_ptr, SL("_htmlQuoteType"), *quote_type TSRMLS_CC);
}
/**
* Detect the character encoding of a string to be handled by an encoder
* Special-handling for chr(172) and chr(128) to chr(159) which fail to be detected by mb_detect_encoding()
*
* @param string $str
* @param string $charset
* @return string
*/
PHP_METHOD(Phalcon_Escaper, detectEncoding){
zval *str, *charset = NULL, *strict_check, *detected = NULL;
PHALCON_MM_GROW();
phalcon_fetch_params(1, 1, 0, &str);
/**
* Check if charset is ASCII or ISO-8859-1
*/
PHALCON_INIT_VAR(charset);
phalcon_is_basic_charset(charset, str);
if (Z_TYPE_P(charset) == IS_STRING) {
RETURN_CTOR(charset);
}
/**
* We require mbstring extension here
*/
if (phalcon_function_exists_ex(SS("mb_detect_encoding") TSRMLS_CC) == FAILURE) {
RETURN_MM_NULL();
}
/**
* Strict encoding detection with fallback to non-strict detection.
*/
strict_check = PHALCON_GLOBAL(z_true);
PHALCON_INIT_NVAR(charset);
ZVAL_STRING(charset, "UTF-32", 1);
/**
* Check for UTF-32 encoding
*/
PHALCON_CALL_FUNCTION(&detected, "mb_detect_encoding", str, charset, strict_check);
if (zend_is_true(detected)) {
RETURN_CTOR(charset);
}
PHALCON_INIT_NVAR(charset);
ZVAL_STRING(charset, "UTF-16", 1);
/**
* Check for UTF-16 encoding
*/
PHALCON_CALL_FUNCTION(&detected, "mb_detect_encoding", str, charset, strict_check);
if (zend_is_true(detected)) {
RETURN_CTOR(charset);
}
PHALCON_INIT_NVAR(charset);
ZVAL_STRING(charset, "UTF-8", 1);
/**
* Check for UTF-8 encoding
*/
PHALCON_CALL_FUNCTION(&detected, "mb_detect_encoding", str, charset, strict_check);
if (zend_is_true(detected)) {
RETURN_CTOR(charset);
}
PHALCON_INIT_NVAR(charset);
ZVAL_STRING(charset, "ISO-8859-1", 1);
/**
* Check for ISO-8859-1 encoding
*/
PHALCON_CALL_FUNCTION(&detected, "mb_detect_encoding", str, charset, strict_check);
if (zend_is_true(detected)) {
RETURN_CTOR(charset);
}
PHALCON_INIT_NVAR(charset);
ZVAL_STRING(charset, "ASCII", 1);
/**
* Check for ASCII encoding
*/
PHALCON_CALL_FUNCTION(&detected, "mb_detect_encoding", str, charset, strict_check);
if (zend_is_true(detected)) {
RETURN_CTOR(charset);
}
/**
* Fallback to global detection
*/
PHALCON_RETURN_CALL_FUNCTION("mb_detect_encoding", str);
RETURN_MM();
}
/**
* Utility to normalize a string's encoding to UTF-32.
*
* @param string $str
* @return string
*/
PHP_METHOD(Phalcon_Escaper, normalizeEncoding){
zval *str, *encoding = NULL, *charset;
PHALCON_MM_GROW();
phalcon_fetch_params(1, 1, 0, &str);
/**
* mbstring is required here
*/
if (phalcon_function_exists_ex(SS("mb_convert_encoding") TSRMLS_CC) == FAILURE) {
PHALCON_THROW_EXCEPTION_STR(phalcon_escaper_exception_ce, "Extension 'mbstring' is required");
return;
}
PHALCON_CALL_METHOD(&encoding, this_ptr, "detectencoding", str);
PHALCON_INIT_VAR(charset);
ZVAL_STRING(charset, "UTF-32", 1);
/**
* Convert to UTF-32 (4 byte characters, regardless of actual number of bytes in
* the character).
*/
PHALCON_RETURN_CALL_FUNCTION("mb_convert_encoding", str, charset, encoding);
RETURN_MM();
}
/**
* Escapes a HTML string. Internally uses htmlspecialchars
*
* @param string $text
* @return string
*/
PHP_METHOD(Phalcon_Escaper, escapeHtml){
zval *text;
zval *html_quote_type, *encoding;
phalcon_fetch_params(0, 1, 0, &text);
if (Z_TYPE_P(text) == IS_STRING) {
html_quote_type = phalcon_fetch_nproperty_this(this_ptr, SL("_htmlQuoteType"), PH_NOISY TSRMLS_CC);
encoding = phalcon_fetch_nproperty_this(this_ptr, SL("_encoding"), PH_NOISY TSRMLS_CC);
phalcon_htmlspecialchars(return_value, text, html_quote_type, encoding TSRMLS_CC);
return;
}
RETURN_ZVAL(text, 1, 0);
}
/**
* Escapes a HTML attribute string
*
* @param string $attribute
* @return string
*/
PHP_METHOD(Phalcon_Escaper, escapeHtmlAttr){
zval *attribute, *encoding;
phalcon_fetch_params(0, 1, 0, &attribute);
if (Z_TYPE_P(attribute) == IS_STRING && zend_is_true(attribute)) {
zval quoting;
INIT_ZVAL(quoting);
ZVAL_LONG("ing, ENT_QUOTES);
encoding = phalcon_fetch_nproperty_this(this_ptr, SL("_encoding"), PH_NOISY TSRMLS_CC);
phalcon_htmlspecialchars(return_value, attribute, "ing, encoding TSRMLS_CC);
return;
}
RETURN_ZVAL(attribute, 1, 0);
}
/**
* Escape CSS strings by replacing non-alphanumeric chars by their hexadecimal escaped representation
*
* @param string $css
* @return string
*/
PHP_METHOD(Phalcon_Escaper, escapeCss){
zval *css, *normalized = NULL;
phalcon_fetch_params(0, 1, 0, &css);
if (Z_TYPE_P(css) == IS_STRING && zend_is_true(css)) {
PHALCON_MM_GROW();
/**
* Normalize encoding to UTF-32
*/
PHALCON_CALL_METHOD(&normalized, this_ptr, "normalizeencoding", css);
/**
* Escape the string
*/
phalcon_escape_css(return_value, normalized);
RETURN_MM();
}
RETURN_ZVAL(css, 1, 0);
}
/**
* Escape javascript strings by replacing non-alphanumeric chars by their hexadecimal escaped representation
*
* @param string $js
* @return string
*/
PHP_METHOD(Phalcon_Escaper, escapeJs){
zval *js, *normalized = NULL;
phalcon_fetch_params(0, 1, 0, &js);
if (Z_TYPE_P(js) == IS_STRING && zend_is_true(js)) {
PHALCON_MM_GROW();
/**
* Normalize encoding to UTF-32
*/
PHALCON_CALL_METHOD(&normalized, this_ptr, "normalizeencoding", js);
/**
* Escape the string
*/
phalcon_escape_js(return_value, normalized);
RETURN_MM();
}
RETURN_ZVAL(js, 1, 0);
}
/**
* Escapes a URL. Internally uses rawurlencode
*
* @param string $url
* @return string
*/
PHP_METHOD(Phalcon_Escaper, escapeUrl){
zval *url;
phalcon_fetch_params(0, 1, 0, &url);
phalcon_raw_url_encode(return_value, url);
}