IntlTaintingPreInitTests.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. try {
  6. var failed = false;
  7. function getErrorFunction(global) {
  8. return function () {
  9. failed = true;
  10. WScript.Echo("Error when tainting '" + global + "'!");
  11. }
  12. }
  13. function generalTainting() {
  14. // tainting built-in object constructors and functions
  15. Date = getErrorFunction("Date");
  16. Object = getErrorFunction("Object");
  17. Number = getErrorFunction("Number");
  18. RegExp = getErrorFunction("RegExp");
  19. String = getErrorFunction("String");
  20. Boolean = getErrorFunction("Boolean");
  21. Error = getErrorFunction("Error");
  22. TypeError = getErrorFunction("TypeError");
  23. RangeError = getErrorFunction("RangeError");
  24. Map = getErrorFunction("Map");
  25. Math = {
  26. abs: getErrorFunction("Math.abs"),
  27. floor: getErrorFunction("Math.floor"),
  28. max: getErrorFunction("Math.max"),
  29. pow: getErrorFunction("Math.pow")
  30. };
  31. isFinite = getErrorFunction("isFinite");
  32. isNaN = getErrorFunction("isNaN");
  33. }
  34. function objectTainting() {
  35. Object.create = getErrorFunction("Object.create");
  36. Object.defineProperty = getErrorFunction("Object.defineProperty");
  37. Object.getPrototypeOf = getErrorFunction("Object.getPrototypeOf");
  38. Object.isExtensible = getErrorFunction("Object.isExtensible");
  39. Object.getOwnPropertyNames = getErrorFunction("Object.getOwnPropertyNames");
  40. Object.prototype.hasOwnProperty = getErrorFunction("Object.prototype.hasOwnProperty");
  41. }
  42. function arrayTainting() {
  43. Array.prototype.forEach = getErrorFunction("Array.prototype.forEach");
  44. Array.prototype.indexOf = getErrorFunction("Array.prototype.indexOf");
  45. Array.prototype.push = getErrorFunction("Array.prototype.push");
  46. Array.prototype.join = getErrorFunction("Array.prototype.join");
  47. }
  48. function stringTainting() {
  49. String.prototype.match = getErrorFunction("String.prototype.match");
  50. String.prototype.replace = getErrorFunction("String.prototype.replace");
  51. String.prototype.toLowerCase = getErrorFunction("String.prototype.toLowerCase");
  52. String.prototype.toUpperCase = getErrorFunction("String.prototype.toUpperCase");
  53. }
  54. function otherProtototypeTainting() {
  55. Function.prototype.bind = getErrorFunction("Function.prototype.bind");
  56. Date.prototype.getDate = getErrorFunction("Date.prototype.getDate");
  57. RegExp.prototype.test = getErrorFunction("RegExp.prototype.test");
  58. }
  59. function runTests() {
  60. failed = false;
  61. new Intl.NumberFormat().format(5);
  62. new Intl.DateTimeFormat().format(5);
  63. new Intl.Collator().compare(null, "");
  64. new Intl.NumberFormat().format(5);
  65. new Intl.DateTimeFormat().format(5);
  66. new Intl.Collator().compare(null, "");
  67. new Intl.NumberFormat().format(5);
  68. new Intl.DateTimeFormat("en", { month: "short" }).format(5);
  69. new Intl.Collator().compare("en", "");
  70. new Intl.NumberFormat().format(5);
  71. new Intl.DateTimeFormat("en", { month: "short" }).format(5);
  72. new Intl.Collator().compare("en", "");
  73. new Intl.NumberFormat().format(5);
  74. new Intl.DateTimeFormat("en", { month: "short" }).format(5);
  75. new Intl.Collator().compare("en", "");
  76. if (failed === false) {
  77. WScript.Echo("Passed pre-init tainting!");
  78. }
  79. }
  80. objectTainting();
  81. arrayTainting();
  82. stringTainting();
  83. otherProtototypeTainting();
  84. generalTainting();
  85. runTests();
  86. } catch (e) {
  87. WScript.Echo(e);
  88. }