common.js 4.8 KB

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