IntlTaintingTests.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. function throwFunction() { throw new Error(); }
  6. function verifyPropertyTainting(property, func, attributes) {
  7. try {
  8. attributes.configurable = true;
  9. Object.defineProperty(Object.prototype, property, attributes);
  10. var result = func();
  11. delete Object.prototype[property];
  12. return result;
  13. }
  14. catch (e) {
  15. delete Object.prototype[property];
  16. throw e;
  17. }
  18. }
  19. //Pass
  20. verifyPropertyTainting("bob", function () { return Intl.Collator.supportedLocalesOf(); }, { value: throwFunction })
  21. //Actual tests, much pass (Bug 362896)
  22. verifyPropertyTainting("enumerable", function () { return Intl.Collator.supportedLocalesOf(); }, { value: throwFunction })
  23. verifyPropertyTainting("get", function () { return Intl.Collator.supportedLocalesOf(); }, { value: throwFunction })
  24. verifyPropertyTainting("set", function () { return Intl.Collator.supportedLocalesOf(); }, { value: throwFunction })
  25. //Testing to make sure regex doesn't change.
  26. "a".match(/(a)/);
  27. var before = {};
  28. Object.getOwnPropertyNames(RegExp).forEach(function (key) { before[key] = RegExp[key]; });
  29. new Intl.NumberFormat("en-US", { style: "currency", currency: "USD" }).format(5);
  30. new Intl.Collator().compare("a", "b");
  31. new Intl.DateTimeFormat().format(new Date());
  32. Object.getOwnPropertyNames(RegExp).forEach(function (key) { if (RegExp[key] !== before[key]) WScript.Echo("Built-In regex implementation overwrote the global constructor's value."); });
  33. try {
  34. var failed = false;
  35. function getErrorFunction(global) {
  36. return function () {
  37. failed = true;
  38. WScript.Echo("Error when tainting '" + global + "'!");
  39. }
  40. }
  41. function generalTainting() {
  42. failed = false;
  43. Date = getErrorFunction("Date");
  44. Object = getErrorFunction("Object");
  45. Number = getErrorFunction("Number");
  46. RegExp = getErrorFunction("RegExp");
  47. String = getErrorFunction("String");
  48. Boolean = getErrorFunction("Boolean");
  49. Error = getErrorFunction("Error");
  50. TypeError = getErrorFunction("TypeError");
  51. RangeError = getErrorFunction("RangeError");
  52. Map = getErrorFunction("Map");
  53. Math = {
  54. abs: getErrorFunction("Math.abs"),
  55. floor: getErrorFunction("Math.floor"),
  56. max: getErrorFunction("Math.max"),
  57. pow: getErrorFunction("Math.pow")
  58. };
  59. isFinite = getErrorFunction("isFinite");
  60. isNaN = getErrorFunction("isNaN");
  61. new Intl.NumberFormat().format(5);
  62. new Intl.DateTimeFormat().format(5);
  63. new Intl.Collator().compare(null, "");
  64. if (failed === false) {
  65. WScript.Echo("Passed general tainting!");
  66. }
  67. }
  68. function objectTainting() {
  69. failed = false;
  70. Object.create = getErrorFunction("Object.create");
  71. Object.defineProperty = getErrorFunction("Object.defineProperty");
  72. Object.getPrototypeOf = getErrorFunction("Object.getPrototypeOf");
  73. Object.isExtensible = getErrorFunction("Object.isExtensible");
  74. Object.getOwnPropertyNames = getErrorFunction("Object.getOwnPropertyNames");
  75. Object.prototype.hasOwnProperty = getErrorFunction("Object.prototype.hasOwnProperty");
  76. new Intl.NumberFormat().format(5);
  77. new Intl.DateTimeFormat().format(5);
  78. new Intl.Collator().compare(null, "");
  79. if (failed === false) {
  80. WScript.Echo("Passed object prototype tainting!");
  81. }
  82. }
  83. function arrayTainting() {
  84. failed = false;
  85. Array.prototype.forEach = getErrorFunction("Array.prototype.forEach");
  86. Array.prototype.indexOf = getErrorFunction("Array.prototype.indexOf");
  87. Array.prototype.push = getErrorFunction("Array.prototype.push");
  88. Array.prototype.join = getErrorFunction("Array.prototype.join");
  89. new Intl.NumberFormat().format(5);
  90. new Intl.DateTimeFormat("en", { month: "short" }).format(5);
  91. new Intl.Collator().compare("en", "");
  92. if (failed === false) {
  93. WScript.Echo("Passed array prototype tainting!");
  94. }
  95. }
  96. function stringTainting() {
  97. failed = false;
  98. String.prototype.match = getErrorFunction("String.prototype.match");
  99. String.prototype.replace = getErrorFunction("String.prototype.replace");
  100. String.prototype.toLowerCase = getErrorFunction("String.prototype.toLowerCase");
  101. String.prototype.toUpperCase = getErrorFunction("String.prototype.toUpperCase");
  102. new Intl.NumberFormat().format(5);
  103. new Intl.DateTimeFormat("en", { month: "short" }).format(5);
  104. new Intl.Collator().compare("en", "");
  105. if (failed === false) {
  106. WScript.Echo("Passed string prototype tainting!");
  107. }
  108. }
  109. function otherProtototypeTainting() {
  110. failed = false;
  111. Function.prototype.bind = getErrorFunction("Function.prototype.bind");
  112. Date.prototype.getDate = getErrorFunction("Date.prototype.getDate");
  113. RegExp.prototype.test = getErrorFunction("RegExp.prototype.test");
  114. new Intl.NumberFormat().format(5);
  115. new Intl.DateTimeFormat("en", { month: "short" }).format(5);
  116. new Intl.Collator().compare("en", "");
  117. if (failed === false) {
  118. WScript.Echo("Passed other tainting!");
  119. }
  120. }
  121. objectTainting();
  122. arrayTainting();
  123. stringTainting();
  124. otherProtototypeTainting();
  125. generalTainting();
  126. } catch (e) {
  127. WScript.Echo(e);
  128. }
  129. Intl.NumberFormat = undefined;
  130. (0.0).toLocaleString();