You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
5.8 KiB

21 years ago
  1. Input Filter Support in PHP 5
  2. -----------------------------
  3. XSS (Cross Site Scripting) hacks are becoming more and more prevalent,
  4. and can be quite difficult to prevent. Whenever you accept user data
  5. and somehow display this data back to users, you are likely vulnerable
  6. to XSS hacks.
  7. The Input Filter support in PHP 5 is aimed at providing the framework
  8. through which a company-wide or site-wide security policy can be
  9. enforced. It is implemented as a SAPI hook and is called from the
  10. treat_data and post handler functions. To implement your own security
  11. policy you will need to write a standard PHP extension. There is also
  12. a powerful standard implementation in ext/filter that should suit most
  13. peoples' needs. However, if you want to implement your own security
  14. policy, read on.
  15. A simple implementation might look like the following. This stores the
  16. original raw user data and adds a my_get_raw() function while the normal
  17. $_POST, $_GET and $_COOKIE arrays are only populated with stripped
  18. data. In this simple example all I am doing is calling strip_tags() on
  19. the data. If register_globals is turned on, the default globals that
  20. are created will be stripped ($foo) while a $RAW_foo is created with the
  21. original user input.
  22. ZEND_BEGIN_MODULE_GLOBALS(my_input_filter)
  23. zval *post_array;
  24. zval *get_array;
  25. zval *cookie_array;
  26. ZEND_END_MODULE_GLOBALS(my_input_filter)
  27. #ifdef ZTS
  28. #define IF_G(v) TSRMG(my_input_filter_globals_id, zend_my_input_filter_globals *, v)
  29. #else
  30. #define IF_G(v) (my_input_filter_globals.v)
  31. #endif
  32. ZEND_DECLARE_MODULE_GLOBALS(my_input_filter)
  33. zend_function_entry my_input_filter_functions[] = {
  34. PHP_FE(my_get_raw, NULL)
  35. {NULL, NULL, NULL}
  36. };
  37. zend_module_entry my_input_filter_module_entry = {
  38. STANDARD_MODULE_HEADER,
  39. "my_input_filter",
  40. my_input_filter_functions,
  41. PHP_MINIT(my_input_filter),
  42. PHP_MSHUTDOWN(my_input_filter),
  43. NULL,
  44. PHP_RSHUTDOWN(my_input_filter),
  45. PHP_MINFO(my_input_filter),
  46. "0.1",
  47. STANDARD_MODULE_PROPERTIES
  48. };
  49. PHP_MINIT_FUNCTION(my_input_filter)
  50. {
  51. ZEND_INIT_MODULE_GLOBALS(my_input_filter, php_my_input_filter_init_globals, NULL);
  52. REGISTER_LONG_CONSTANT("POST", PARSE_POST, CONST_CS | CONST_PERSISTENT);
  53. REGISTER_LONG_CONSTANT("GET", PARSE_GET, CONST_CS | CONST_PERSISTENT);
  54. REGISTER_LONG_CONSTANT("COOKIE", PARSE_COOKIE, CONST_CS | CONST_PERSISTENT);
  55. sapi_register_input_filter(my_sapi_input_filter);
  56. return SUCCESS;
  57. }
  58. PHP_RSHUTDOWN_FUNCTION(my_input_filter)
  59. {
  60. if(IF_G(get_array)) {
  61. zval_ptr_dtor(&IF_G(get_array));
  62. IF_G(get_array) = NULL;
  63. }
  64. if(IF_G(post_array)) {
  65. zval_ptr_dtor(&IF_G(post_array));
  66. IF_G(post_array) = NULL;
  67. }
  68. if(IF_G(cookie_array)) {
  69. zval_ptr_dtor(&IF_G(cookie_array));
  70. IF_G(cookie_array) = NULL;
  71. }
  72. return SUCCESS;
  73. }
  74. PHP_MINFO_FUNCTION(my_input_filter)
  75. {
  76. php_info_print_table_start();
  77. php_info_print_table_row( 2, "My Input Filter Support", "enabled" );
  78. php_info_print_table_row( 2, "Revision", "$Revision$");
  79. php_info_print_table_end();
  80. }
  81. /* The filter handler. If you return 1 from it, then PHP also registers the
  82. * (modified) variable. Returning 0 prevents PHP from registering the variable;
  83. * you can use this if your filter already registers the variable under a
  84. * different name, or if you just don't want the variable registered at all. */
  85. SAPI_INPUT_FILTER_FUNC(my_sapi_input_filter)
  86. {
  87. zval new_var;
  88. zval *array_ptr = NULL;
  89. char *raw_var;
  90. int var_len;
  91. assert(*val != NULL);
  92. switch(arg) {
  93. case PARSE_GET:
  94. if(!IF_G(get_array)) {
  95. ALLOC_ZVAL(array_ptr);
  96. array_init(array_ptr);
  97. INIT_PZVAL(array_ptr);
  98. }
  99. IF_G(get_array) = array_ptr;
  100. break;
  101. case PARSE_POST:
  102. if(!IF_G(post_array)) {
  103. ALLOC_ZVAL(array_ptr);
  104. array_init(array_ptr);
  105. INIT_PZVAL(array_ptr);
  106. }
  107. IF_G(post_array) = array_ptr;
  108. break;
  109. case PARSE_COOKIE:
  110. if(!IF_G(cookie_array)) {
  111. ALLOC_ZVAL(array_ptr);
  112. array_init(array_ptr);
  113. INIT_PZVAL(array_ptr);
  114. }
  115. IF_G(cookie_array) = array_ptr;
  116. break;
  117. }
  118. Z_STRLEN(new_var) = val_len;
  119. Z_STRVAL(new_var) = estrndup(*val, val_len);
  120. Z_TYPE(new_var) = IS_STRING;
  121. var_len = strlen(var);
  122. raw_var = emalloc(var_len+5); /* RAW_ and a \0 */
  123. strcpy(raw_var, "RAW_");
  124. strlcat(raw_var,var,var_len+5);
  125. php_register_variable_ex(raw_var, &new_var, array_ptr TSRMLS_DC);
  126. php_strip_tags(*val, val_len, NULL, NULL, 0);
  127. *new_val_len = strlen(*val);
  128. return 1;
  129. }
  130. PHP_FUNCTION(my_get_raw)
  131. {
  132. long arg;
  133. char *var;
  134. int var_len;
  135. zval **tmp;
  136. zval *array_ptr = NULL;
  137. HashTable *hash_ptr;
  138. char *raw_var;
  139. if(zend_parse_parameters(2 TSRMLS_CC, "ls", &arg, &var, &var_len) == FAILURE) {
  140. return;
  141. }
  142. switch(arg) {
  143. case PARSE_GET:
  144. array_ptr = IF_G(get_array);
  145. break;
  146. case PARSE_POST:
  147. array_ptr = IF_G(post_array);
  148. break;
  149. case PARSE_COOKIE:
  150. array_ptr = IF_G(post_array);
  151. break;
  152. }
  153. if(!array_ptr) RETURN_FALSE;
  154. /*
  155. * I'm changing the variable name here because when running with register_globals on,
  156. * the variable will end up in the global symbol table
  157. */
  158. raw_var = emalloc(var_len+5); /* RAW_ and a \0 */
  159. strcpy(raw_var, "RAW_");
  160. strlcat(raw_var,var,var_len+5);
  161. hash_ptr = HASH_OF(array_ptr);
  162. if(zend_hash_find(hash_ptr, raw_var, var_len+5, (void **)&tmp) == SUCCESS) {
  163. *return_value = **tmp;
  164. zval_copy_ctor(return_value);
  165. } else {
  166. RETVAL_FALSE;
  167. }
  168. efree(raw_var);
  169. }