HazardableBehavior.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. * 2013-02-25 ms
  33. */
  34. class HazardableBehavior extends ModelBehavior {
  35. protected $_defaults = array(
  36. 'replaceFind' => false, // fake data after a find call (defaults to false)
  37. 'fields' => array(), // additional fields or custom mapping to a specific snippet type (defaults to XSS)
  38. 'skipFields' => array('id', 'slug') // fields of the schema that should be skipped
  39. );
  40. public $snippets = array();
  41. public function setup(Model $Model, $config = array()) {
  42. $this->settings[$Model->alias] = array_merge($this->_defaults, $config);
  43. }
  44. /**
  45. * beforeSave() to inject the hazardous strings into the model data for save().
  46. *
  47. * Note: Remember to disable validation as you want to insert those strings just for
  48. * testing purposes.
  49. */
  50. public function beforeSave(Model $Model) {
  51. $fields = $this->_fields($Model);
  52. foreach ($fields as $field) {
  53. $length = 0;
  54. $schema = $Model->schema($field);
  55. if (!empty($schema['length'])) {
  56. $length = $schema['length'];
  57. }
  58. $Model->data[$Model->alias][$field] = $this->_snippet($length);
  59. }
  60. return true;
  61. }
  62. /**
  63. * afterFind() to inject the hazardous strings into the retrieved model data.
  64. * Only activate this if you have not persistently stored any hazardous strings yet.
  65. */
  66. public function afterFind(Model $Model, $results, $primary) {
  67. if (empty($this->settings[$Model->alias]['replaceFind'])) {
  68. return $results;
  69. }
  70. foreach ($results as $key => $result) {
  71. foreach ($result as $model => $row) {
  72. $fields = $this->_fields($Model);
  73. foreach ($fields as $field) {
  74. $length = 0;
  75. $schema = $Model->schema($field);
  76. if (!empty($schema['length'])) {
  77. $length = $schema['length'];
  78. }
  79. $results[$key][$model][$field] = $this->_snippet($length);
  80. }
  81. }
  82. }
  83. return $results;
  84. }
  85. /**
  86. * @param int $maxLength The lenght of the field if applicable to return a suitable snippet
  87. * @return string Hazardous string
  88. */
  89. protected function _snippet($maxLength = 0) {
  90. $snippets = $this->_snippets();
  91. $max = count($snippets) - 1;
  92. if ($maxLength) {
  93. foreach ($snippets as $key => $snippet) {
  94. if (mb_strlen($snippet) > $maxLength) {
  95. break;
  96. }
  97. $max = $key;
  98. }
  99. }
  100. $keyByChance = mt_rand(0, $max);
  101. return $snippets[$keyByChance];
  102. }
  103. /**
  104. * @return array
  105. */
  106. protected function _snippets() {
  107. if ($this->snippets) {
  108. return $this->snippets;
  109. }
  110. $snippetArray = HazardLib::xssStrings();
  111. $snippetArray[] = '<SCRIPT>alert(\'X\')</SCRIPT>';
  112. $snippetArray[] = '<';
  113. usort($snippetArray, array($this, '_sort'));
  114. $this->snippets = $snippetArray;
  115. return $snippetArray;
  116. }
  117. /**
  118. * Sort all snippets by length (ASC)
  119. */
  120. protected function _sort($a, $b) {
  121. return strlen($a) - strlen($b);
  122. }
  123. /**
  124. * @return array
  125. */
  126. protected function _fields(Model $Model) {
  127. $fields = array();
  128. $schema = $Model->schema();
  129. foreach ($schema as $key => $field) {
  130. if (!in_array($field['type'], array('text', 'string'))) {
  131. continue;
  132. }
  133. if ($this->settings[$Model->alias]['skipFields'] && in_array($key, $this->settings[$Model->alias]['skipFields'])) {
  134. continue;
  135. }
  136. $fields[] = $key;
  137. }
  138. return $fields;
  139. }
  140. }