HazardableBehavior.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. App::uses('ModelBehavior', 'Model');
  3. App::uses('HazardLib', 'Tools.Lib');
  4. /**
  5. * Uses the HazardLib to test well known injection snippets of all kinds (including XSS, SQL)
  6. * and tests your db wrapper methods or populates your records with it.
  7. * Use this ONLY for testing environments and never on your live data.
  8. *
  9. * Available snippet types are XSS, PHP and SQL.
  10. *
  11. * The main concern is not just "eval" users that might try to hijack/abuse your site.
  12. * Not properly securing your view also means that strings like `some>cool<string` will most likely mess up your HTML.
  13. * The view could be rendered as a complete mess without the user or the developer knowing it. It might have even been
  14. * the admin which inserted those layout-breaking strings, after all.
  15. * That's why it is so important to follow the rule "do NOT sanitize, validate input, escape output" (there are exceptions, of course).
  16. * Also make sure, you already cover those basics in your baking template. This will save a lot of time in the long run.
  17. *
  18. * If you inserted records go and browse your backend and especially your frontend.
  19. * Everywhere where you get some alert or strange behavior, you might have forgotten to use h() or other
  20. * measures to secure your output properly.
  21. *
  22. * You can also apply this behavior globally to overwrite all strings in your application temporarily.
  23. * This way you don't need to modify the database. On output it will just inject the hazardous strings and
  24. * you can browse your website just as if they were actually stored in your db.
  25. * Either add it to some models or even the AppModel (temporarily!) as `$actsAs = array('Tools.Hazardable'))`
  26. * A known limitation of Cake behaviors is, though, that this would only apply for first-level records (not related data).
  27. * So it is usually better to insert some hazardous strings into all your tables and make your tests then as closely
  28. * to the reality as possible.
  29. *
  30. * @author Mark Scherer
  31. * @license MIT
  32. */
  33. class HazardableBehavior extends ModelBehavior {
  34. protected $_defaults = array(
  35. 'replaceFind' => false, // fake data after a find call (defaults to false)
  36. 'fields' => array(), // additional fields or custom mapping to a specific snippet type (defaults to XSS)
  37. 'skipFields' => array('id', 'slug') // fields of the schema that should be skipped
  38. );
  39. public $snippets = array();
  40. public function setup(Model $Model, $config = array()) {
  41. $this->settings[$Model->alias] = array_merge($this->_defaults, $config);
  42. }
  43. /**
  44. * beforeSave() to inject the hazardous strings into the model data for save().
  45. *
  46. * Note: Remember to disable validation as you want to insert those strings just for
  47. * testing purposes.
  48. */
  49. public function beforeSave(Model $Model, $options = array()) {
  50. $fields = $this->_fields($Model);
  51. foreach ($fields as $field) {
  52. $length = 0;
  53. $schema = $Model->schema($field);
  54. if (!empty($schema['length'])) {
  55. $length = $schema['length'];
  56. }
  57. $Model->data[$Model->alias][$field] = $this->_snippet($length);
  58. }
  59. return true;
  60. }
  61. /**
  62. * afterFind() to inject the hazardous strings into the retrieved model data.
  63. * Only activate this if you have not persistently stored any hazardous strings yet.
  64. */
  65. public function afterFind(Model $Model, $results, $primary = false) {
  66. if (empty($this->settings[$Model->alias]['replaceFind'])) {
  67. return $results;
  68. }
  69. foreach ($results as $key => $result) {
  70. foreach ($result as $model => $row) {
  71. $fields = $this->_fields($Model);
  72. foreach ($fields as $field) {
  73. $length = 0;
  74. $schema = $Model->schema($field);
  75. if (!empty($schema['length'])) {
  76. $length = $schema['length'];
  77. }
  78. $results[$key][$model][$field] = $this->_snippet($length);
  79. }
  80. }
  81. }
  82. return $results;
  83. }
  84. /**
  85. * @param integer $maxLength The lenght of the field if applicable to return a suitable snippet
  86. * @return string Hazardous string
  87. */
  88. protected function _snippet($maxLength = 0) {
  89. $snippets = $this->_snippets();
  90. $max = count($snippets) - 1;
  91. if ($maxLength) {
  92. foreach ($snippets as $key => $snippet) {
  93. if (mb_strlen($snippet) > $maxLength) {
  94. break;
  95. }
  96. $max = $key;
  97. }
  98. }
  99. $keyByChance = mt_rand(0, $max);
  100. return $snippets[$keyByChance];
  101. }
  102. /**
  103. * @return array
  104. */
  105. protected function _snippets() {
  106. if ($this->snippets) {
  107. return $this->snippets;
  108. }
  109. $snippetArray = HazardLib::xssStrings();
  110. $snippetArray[] = '<SCRIPT>alert(\'X\')</SCRIPT>';
  111. $snippetArray[] = '<';
  112. usort($snippetArray, array($this, '_sort'));
  113. $this->snippets = $snippetArray;
  114. return $snippetArray;
  115. }
  116. /**
  117. * Sort all snippets by length (ASC)
  118. */
  119. protected function _sort($a, $b) {
  120. return strlen($a) - strlen($b);
  121. }
  122. /**
  123. * @return array
  124. */
  125. protected function _fields(Model $Model) {
  126. $fields = array();
  127. $schema = $Model->schema();
  128. foreach ($schema as $key => $field) {
  129. if (!in_array($field['type'], array('text', 'string'))) {
  130. continue;
  131. }
  132. if ($this->settings[$Model->alias]['skipFields'] && in_array($key, $this->settings[$Model->alias]['skipFields'])) {
  133. continue;
  134. }
  135. $fields[] = $key;
  136. }
  137. return $fields;
  138. }
  139. }