sanitizers.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. var validator = require('../validator')
  2. , format = require('util').format;
  3. function test(options) {
  4. var args = options.args || [];
  5. args.unshift(null);
  6. Object.keys(options.expect).forEach(function (input) {
  7. args[0] = input;
  8. var result = validator[options.sanitizer].apply(validator, args)
  9. , expected = options.expect[input];
  10. if (isNaN(result) && !result.length && isNaN(expected)) {
  11. return;
  12. }
  13. if (result !== expected) {
  14. var warning = format('validator.%s(%s) returned "%s" but should have returned "%s"',
  15. options.sanitizer, args.join(', '), result, expected);
  16. throw new Error(warning);
  17. }
  18. });
  19. }
  20. describe('Sanitizers', function () {
  21. it('should sanitize boolean strings', function () {
  22. test({
  23. sanitizer: 'toBoolean'
  24. , expect: {
  25. '0': false
  26. , '': false
  27. , '1': true
  28. , 'true': true
  29. , 'foobar': true
  30. , ' ': true
  31. }
  32. });
  33. test({
  34. sanitizer: 'toBoolean'
  35. , args: [ true ] //strict
  36. , expect: {
  37. '0': false
  38. , '': false
  39. , '1': true
  40. , 'true': true
  41. , 'foobar': false
  42. , ' ': false
  43. }
  44. });
  45. });
  46. it('should trim whitespace', function () {
  47. test({
  48. sanitizer: 'trim'
  49. , expect: { ' \r\n\tfoo \r\n\t ': 'foo' }
  50. });
  51. test({
  52. sanitizer: 'ltrim'
  53. , expect: { ' \r\n\tfoo \r\n\t ': 'foo \r\n\t ' }
  54. });
  55. test({
  56. sanitizer: 'rtrim'
  57. , expect: { ' \r\n\tfoo \r\n\t ': ' \r\n\tfoo' }
  58. });
  59. });
  60. it('should trim custom characters', function () {
  61. test({
  62. sanitizer: 'trim'
  63. , args: [ '01' ]
  64. , expect: { '010100201000': '2' }
  65. });
  66. test({
  67. sanitizer: 'ltrim'
  68. , args: [ '01' ]
  69. , expect: { '010100201000': '201000' }
  70. });
  71. test({
  72. sanitizer: 'rtrim'
  73. , args: [ '01' ]
  74. , expect: { '010100201000': '0101002' }
  75. });
  76. });
  77. it('should convert strings to integers', function () {
  78. test({
  79. sanitizer: 'toInt'
  80. , expect: {
  81. '3': 3
  82. , ' 3 ': 3
  83. , '2.4': 2
  84. , 'foo': NaN
  85. }
  86. });
  87. test({
  88. sanitizer: 'toInt'
  89. , args: [ 16 ]
  90. , expect: { 'ff': 255 }
  91. });
  92. });
  93. it('should convert strings to floats', function () {
  94. test({
  95. sanitizer: 'toFloat'
  96. , expect: {
  97. '2': 2.0
  98. , '2.': 2.0
  99. , '-2.5': -2.5
  100. , '.5': 0.5
  101. , 'foo': NaN
  102. }
  103. });
  104. });
  105. it('should escape HTML', function () {
  106. test({
  107. sanitizer: 'escape'
  108. , expect: {
  109. '<img alt="foo&bar">': '&lt;img alt=&quot;foo&amp;bar&quot;&gt;'
  110. , "<img alt='foo&bar'>": '&lt;img alt=&#x27;foo&amp;bar&#x27;&gt;'
  111. }
  112. });
  113. });
  114. it('should remove control characters (<32 and 127)', function () {
  115. // Check basic functionality
  116. test({
  117. sanitizer: 'stripLow'
  118. , expect: {
  119. "foo\x00": "foo"
  120. , "\x7Ffoo\x02": "foo"
  121. , "\x01\x09": ""
  122. , "foo\x0A\x0D": "foo"
  123. }
  124. });
  125. // Unicode safety
  126. test({
  127. sanitizer: 'stripLow'
  128. , expect: {
  129. "perch\u00e9": "perch\u00e9"
  130. , "\u20ac": "\u20ac"
  131. , "\u2206\x0A": "\u2206"
  132. , "\ud83d\ude04": "\ud83d\ude04"
  133. }
  134. });
  135. // Preserve newlines
  136. test({
  137. sanitizer: 'stripLow'
  138. , args: [ true ] //keep_new_lines
  139. , expect: {
  140. "foo\x0A\x0D": "foo\x0A\x0D"
  141. , "\x03foo\x0A\x0D": "foo\x0A\x0D"
  142. }
  143. });
  144. });
  145. it('should sanitize a string based on a whitelist', function () {
  146. test({
  147. sanitizer: 'whitelist'
  148. , args: [ 'abc' ]
  149. , expect: {
  150. 'abcdef': 'abc'
  151. , 'aaaaaaaaaabbbbbbbbbb': 'aaaaaaaaaabbbbbbbbbb'
  152. , 'a1b2c3': 'abc'
  153. , ' ': ''
  154. }
  155. });
  156. });
  157. it('should sanitize a string based on a blacklist', function () {
  158. test({
  159. sanitizer: 'blacklist'
  160. , args: [ 'abc' ]
  161. , expect: {
  162. 'abcdef': 'def'
  163. , 'aaaaaaaaaabbbbbbbbbb': ''
  164. , 'a1b2c3': '123'
  165. , ' ': ' '
  166. }
  167. });
  168. });
  169. it('should normalize an email based on domain', function () {
  170. test({
  171. sanitizer: 'normalizeEmail'
  172. , expect: {
  173. 'some.name@gmail.com': 'somename@gmail.com'
  174. , 'some.name@googleMail.com': 'somename@googlemail.com'
  175. , 'some.name+extension@gmail.com': 'somename@gmail.com'
  176. , 'some.Name+extension@GoogleMail.com': 'somename@googlemail.com'
  177. , 'some.name.middleName+extension@gmail.com': 'somenamemiddlename@gmail.com'
  178. , 'some.name.middleName+extension@GoogleMail.com': 'somenamemiddlename@googlemail.com'
  179. , 'some.name.midd..leNa...me...+extension@gmail.com': 'somenamemiddlename@gmail.com'
  180. , 'some.name.midd..leNa...me...+extension@GoogleMail.com': 'somenamemiddlename@googlemail.com'
  181. , 'some.name+extension@unknown.com': 'some.name+extension@unknown.com'
  182. , 'an invalid email address': 'an invalid email address'
  183. , '': ''
  184. }
  185. });
  186. });
  187. });