common.js 4.9 KB

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