common.js 4.6 KB

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