sanitize.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. "use strict";
  2. module.exports = {
  3. /**
  4. * sanitization of values : trim, escape characters
  5. *
  6. * @param {String} inputString - input
  7. * @param {String} escapeCharFor - escape for headers, body: raw, formdata etc
  8. * @param {Boolean} [inputTrim] - whether to trim the input
  9. * @returns {String}
  10. */
  11. sanitize: function (inputString, escapeCharFor, inputTrim) {
  12. if (typeof inputString !== 'string') {
  13. return '';
  14. }
  15. inputString = inputTrim && typeof inputTrim === 'boolean' ? inputString.trim() : inputString;
  16. if (escapeCharFor && typeof escapeCharFor === 'string') {
  17. switch (escapeCharFor) {
  18. case 'urlencoded':
  19. return encodeURIComponent(inputString).replace(/'/g, '\\\'');
  20. default:
  21. return inputString.replace(/\\/g, '\\\\').replace(/'/g, '\\\'');
  22. }
  23. }
  24. return inputString;
  25. },
  26. /**
  27. * sanitizes input options
  28. *
  29. * @param {Object} options - Options provided by the user
  30. * @param {Array} optionsArray - options array received from getOptions function
  31. *
  32. * @returns {Object} - Sanitized options object
  33. */
  34. sanitizeOptions: function (options, optionsArray) {
  35. var result = {}, defaultOptions = {}, id;
  36. optionsArray.forEach((option) => {
  37. defaultOptions[option.id] = {
  38. default: option.default,
  39. type: option.type
  40. };
  41. if (option.type === 'enum') {
  42. defaultOptions[option.id].availableOptions = option.availableOptions;
  43. }
  44. });
  45. for (id in options) {
  46. if (options.hasOwnProperty(id)) {
  47. if (defaultOptions[id] === undefined) {
  48. continue;
  49. }
  50. switch (defaultOptions[id].type) {
  51. case 'boolean':
  52. if (typeof options[id] !== 'boolean') {
  53. result[id] = defaultOptions[id].default;
  54. }
  55. else {
  56. result[id] = options[id];
  57. }
  58. break;
  59. case 'positiveInteger':
  60. if (typeof options[id] !== 'number' || options[id] < 0) {
  61. result[id] = defaultOptions[id].default;
  62. }
  63. else {
  64. result[id] = options[id];
  65. }
  66. break;
  67. case 'enum':
  68. if (!defaultOptions[id].availableOptions.includes(options[id])) {
  69. result[id] = defaultOptions[id].default;
  70. }
  71. else {
  72. result[id] = options[id];
  73. }
  74. break;
  75. default:
  76. result[id] = options[id];
  77. }
  78. }
  79. }
  80. for (id in defaultOptions) {
  81. if (defaultOptions.hasOwnProperty(id)) {
  82. if (result[id] === undefined) {
  83. result[id] = defaultOptions[id].default;
  84. }
  85. }
  86. }
  87. return result;
  88. },
  89. /**
  90. *
  91. * @param {Array} array - form data array
  92. * @param {String} key - key of form data param
  93. * @param {String} type - type of form data param(file/text)
  94. * @param {String} val - value/src property of form data param
  95. * @param {String} disabled - Boolean denoting whether the param is disabled or not
  96. * @param {String} contentType - content type header of the param
  97. *
  98. * Appends a single param to form data array
  99. */
  100. addFormParam: function (array, key, type, val, disabled, contentType) {
  101. if (type === 'file') {
  102. array.push({
  103. key: key,
  104. type: type,
  105. src: val,
  106. disabled: disabled,
  107. contentType: contentType
  108. });
  109. }
  110. else {
  111. array.push({
  112. key: key,
  113. type: type,
  114. value: val,
  115. disabled: disabled,
  116. contentType: contentType
  117. });
  118. }
  119. }
  120. };