attribute-value-checks 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. kses attribute value checks
  2. ===========================
  3. As you've probably already read in the README file, an $allowed_html array
  4. normally looks like this:
  5. $allowed = array('b' => array(),
  6. 'i' => array(),
  7. 'a' => array('href' => 1,
  8. 'title' => 1),
  9. 'p' => array('align' => 1),
  10. 'br' => array());
  11. This sets what elements and attributes are allowed.
  12. From kses 0.2.0, you can also perform some checks on the attribute values. You
  13. do it like this:
  14. $allowed = array('b' => array(),
  15. 'i' => array(),
  16. 'a' => array('href' =>
  17. array('maxlen' => 100),
  18. 'title' => 1),
  19. 'p' => array('align' => 1),
  20. 'font' => array('size' =>
  21. array('maxval' => 20)),
  22. 'br' => array());
  23. This means that kses should perform the maxlen check with the value 100 on the
  24. <a href=> value, as well as the maxval check with the value 20 on the <font
  25. size=> value.
  26. The currently implemented checks (with more to come) are 'maxlen', 'maxval',
  27. 'minlen', 'minval' and 'valueless'.
  28. 'maxlen' checks that the length of the attribute value is not greater than the
  29. given value. It is helpful against Buffer Overflows in WWW clients and various
  30. servers on the Internet. In my example above, it would mean that
  31. "<a href='ftp://ftp.v1ct1m.com/AAAA..thousands_of_A's...'>" wouldn't be
  32. accepted.
  33. Of course, this problem is even worse if you put that long URL in a <frame>
  34. tag instead, so the WWW client will fetch it automatically without a user
  35. having to click it.
  36. 'maxval' checks that the attribute value is an integer greater than or equal to
  37. zero, that it doesn't have an unreasonable amount of zeroes or whitespace (to
  38. avoid Buffer Overflows), and that it is not greater than the given value. In
  39. my example above, it would mean that "<font size='20'>" is accepted but
  40. "<font size='21'>" is not. This check helps against Denial of Service attacks
  41. against WWW clients.
  42. One example of this DoS problem is <iframe src="http://some.web.server/"
  43. width="20000" height="2000">, which makes some client machines completely
  44. overloaded.
  45. 'minlen' and 'minval' works the same as 'maxlen' and 'maxval', except that they
  46. check for minimum lengths and values instead of maximum ones.
  47. 'valueless' checks if an attribute has a value (like <a href="blah">) or not
  48. (<option selected>). If the given value is a "y" or a "Y", the attribute must
  49. not have a value to be accepted. If the given value is an "n" or an "N", the
  50. attribute must have a value. Note that <a href=""> is considered to have a
  51. value, so there's a difference between valueless attributes and attribute
  52. values with the length zero.
  53. You can combine more than one check, by putting one after the other in the
  54. inner array.