securejson.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /****************************************************************
  2. ** Licensed Materials - Property of IBM
  3. **
  4. ** IBM Cognos Products: drill
  5. **
  6. ** (C) Copyright IBM Corp. 2001, 2010
  7. **
  8. ** US Government Users Restricted Rights - Use, duplication or
  9. ** disclosure restricted by GSA ADP Schedule Contract with
  10. ** IBM Corp.
  11. *****************************************************************/
  12. // Copyright (C) 2006 Cognos Incorporated. All rights reserved.
  13. // Cognos (R) is a trademark of Cognos Incorporated.
  14. /**
  15. * securejson.js extends json.js to allow one add a POISONTOKEN to
  16. * the JSONString and parse a SecureJSONString (POISONTOKEN + JSONString)
  17. * <p>
  18. * By adding a POISONTOKEN like while(1);/* as a prefix to the JSONString
  19. * we prevent a possible eavsdropper to use an <script> tag and run the code
  20. * <p>
  21. * The class also enable one to recieve a SecureJSONString and parse it in a proper manner.
  22. * @author Alireza Pourshahid
  23. */
  24. /**
  25. * POISONTOKEN is defined as a global static variable that is used by all the method
  26. * this is the string that is added as a prefix to JSONString to make it secure.
  27. */
  28. var POISONTOKEN = "while(1);/*";
  29. /**
  30. *
  31. * addPoisonToken returns a SecureJSONString by adding a POISONTOKEN to a JSONString
  32. * This method should be used when user orginaly create a String and not a JSONObject
  33. * @return string SecureJSONString (POISONTOKEN + JSONSTRING)
  34. * @author Alireza Pourshahid
  35. */
  36. Object.prototype.addPoisonToken = function () {
  37. return POISONTOKEN + this;
  38. };
  39. /**
  40. *
  41. * addPoisonToken returns a SecureJSONString by adding a POISONTOKEN to a JSONString
  42. * This method should be used when user create a JSONObject and not a String
  43. * @return string SecureJSONString (POISONTOKEN + JSONSTRING)
  44. * @author Alireza Pourshahid
  45. */
  46. Object.prototype.toSecureJSONString = function () {
  47. if (typeof JSON != 'undefined') {
  48. return POISONTOKEN + JSON.stringify(this);
  49. } else {
  50. return POISONTOKEN + this.toJSONString();
  51. }
  52. };
  53. /**
  54. * returns a JSONObject after parsing a SecureJSONString
  55. * This method removes the POISONTOKEN and parse the remaned
  56. * String as a JSONString using parseJSON method from JSON
  57. * orginal distirbution.
  58. *
  59. * @return j a JSONObject
  60. * @author Alireza Pourshahid
  61. */
  62. (function (s) {
  63. s.parseSecureJSON = function (filter) {
  64. var j;
  65. j = this.substr(POISONTOKEN.length,this.length);
  66. if (typeof JSON != 'undefined') {
  67. return JSON.parse(j);
  68. } else {
  69. return j.parseJSON();
  70. }
  71. };
  72. })(String.prototype);