common.js 4.4 KB

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