common.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. 'use strict';
  2. // testing prerequisites
  3. global.assert = require('assert');
  4. global.jsc = require('jsverify');
  5. global.jsdom = require('jsdom-global');
  6. global.cleanup = global.jsdom();
  7. global.fs = require('fs');
  8. // application libraries to test
  9. global.$ = global.jQuery = require('./jquery-3.3.1');
  10. global.sjcl = require('./sjcl-1.0.7');
  11. global.Base64 = require('./base64-2.4.5').Base64;
  12. global.RawDeflate = require('./rawdeflate-0.5').RawDeflate;
  13. global.RawDeflate.inflate = require('./rawinflate-0.3').RawDeflate.inflate;
  14. require('./prettify');
  15. global.prettyPrint = window.PR.prettyPrint;
  16. global.prettyPrintOne = window.PR.prettyPrintOne;
  17. global.showdown = require('./showdown-1.8.6');
  18. global.DOMPurify = require('./purify-1.0.7');
  19. global.baseX = require('./base-x-3.0.5.1').baseX;
  20. require('./bootstrap-3.3.7');
  21. require('./privatebin');
  22. // internal variables
  23. var a2zString = ['a','b','c','d','e','f','g','h','i','j','k','l','m',
  24. 'n','o','p','q','r','s','t','u','v','w','x','y','z'],
  25. digitString = ['0','1','2','3','4','5','6','7','8','9'],
  26. alnumString = a2zString.concat(digitString),
  27. hexString = digitString.concat(['a','b','c','d','e','f']),
  28. queryString = alnumString.concat(['+','%','&','.','*','-','_']),
  29. hashString = queryString.concat(['!']),
  30. base64String = alnumString.concat(['+','/','=']).concat(
  31. a2zString.map(function(c) {
  32. return c.toUpperCase();
  33. })
  34. ),
  35. schemas = ['ftp','gopher','http','https','ws','wss'],
  36. supportedLanguages = ['de', 'es', 'fr', 'it', 'no', 'pl', 'pt', 'oc', 'ru', 'sl', 'zh'],
  37. mimeTypes = ['image/png', 'application/octet-stream'],
  38. formats = ['plaintext', 'markdown', 'syntaxhighlighting'],
  39. /**
  40. * character to HTML entity lookup table
  41. *
  42. * @see {@link https://github.com/janl/mustache.js/blob/master/mustache.js#L60}
  43. */
  44. entityMap = {
  45. '&': '&',
  46. '<': '&lt;',
  47. '>': '&gt;',
  48. '"': '&quot;',
  49. "'": '&#39;',
  50. '/': '&#x2F;',
  51. '`': '&#x60;',
  52. '=': '&#x3D;'
  53. },
  54. logFile = fs.createWriteStream('test.log'),
  55. mimeFile = fs.createReadStream('/etc/mime.types'),
  56. mimeLine = '';
  57. // redirect console messages to log file
  58. console.info = console.warn = console.error = function () {
  59. logFile.write(Array.prototype.slice.call(arguments).join('') + '\n');
  60. };
  61. // populate mime types from environment
  62. mimeFile.on('data', function(data) {
  63. mimeLine += data;
  64. var index = mimeLine.indexOf('\n');
  65. while (index > -1) {
  66. var line = mimeLine.substring(0, index);
  67. mimeLine = mimeLine.substring(index + 1);
  68. parseMime(line);
  69. index = mimeLine.indexOf('\n');
  70. }
  71. });
  72. mimeFile.on('end', function() {
  73. if (mimeLine.length > 0) {
  74. parseMime(mimeLine);
  75. }
  76. });
  77. function parseMime(line) {
  78. // ignore comments
  79. var index = line.indexOf('#');
  80. if (index > -1) {
  81. line = line.substring(0, index);
  82. }
  83. // ignore bits after tabs
  84. index = line.indexOf('\t');
  85. if (index > -1) {
  86. line = line.substring(0, index);
  87. }
  88. if (line.length > 0) {
  89. mimeTypes.push(line);
  90. }
  91. }
  92. // common testing helper functions
  93. /**
  94. * convert all applicable characters to HTML entities
  95. *
  96. * @see {@link https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet#RULE_.231_-_HTML_Escape_Before_Inserting_Untrusted_Data_into_HTML_Element_Content}
  97. * @name htmlEntities
  98. * @function
  99. * @param {string} str
  100. * @return {string} escaped HTML
  101. */
  102. exports.htmlEntities = function(str) {
  103. return String(str).replace(
  104. /[&<>"'`=\/]/g, function(s) {
  105. return entityMap[s];
  106. });
  107. };
  108. // provides random lowercase characters from a to z
  109. exports.jscA2zString = function() {
  110. return jsc.elements(a2zString);
  111. };
  112. // provides random lowercase alpha numeric characters (a to z and 0 to 9)
  113. exports.jscAlnumString = function() {
  114. return jsc.elements(alnumString);
  115. };
  116. //provides random characters allowed in hexadecimal notation
  117. exports.jscHexString = function() {
  118. return jsc.elements(hexString);
  119. };
  120. // provides random characters allowed in GET queries
  121. exports.jscQueryString = function() {
  122. return jsc.elements(queryString);
  123. };
  124. // provides random characters allowed in hash queries
  125. exports.jscHashString = function() {
  126. return jsc.elements(hashString);
  127. };
  128. // provides random characters allowed in base64 encoded strings
  129. exports.jscBase64String = function() {
  130. return jsc.elements(base64String);
  131. };
  132. // provides a random URL schema supported by the whatwg-url library
  133. exports.jscSchemas = function() {
  134. return jsc.elements(schemas);
  135. };
  136. // provides a random supported language string
  137. exports.jscSupportedLanguages = function() {
  138. return jsc.elements(supportedLanguages);
  139. };
  140. // provides a random mime type
  141. exports.jscMimeTypes = function() {
  142. return jsc.elements(mimeTypes);
  143. };
  144. // provides a random PrivateBin paste formatter
  145. exports.jscFormats = function() {
  146. return jsc.elements(formats);
  147. };