Principals.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. * vim: set ts=8 sts=4 et sw=4 tw=99:
  3. * This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6. /* JSPrincipals and related interfaces. */
  7. #ifndef js_Principals_h
  8. #define js_Principals_h
  9. #include "mozilla/Atomics.h"
  10. #include <stdint.h>
  11. #include "jspubtd.h"
  12. #include "js/StructuredClone.h"
  13. namespace js {
  14. struct PerformanceGroup;
  15. } // namespace js
  16. struct JSPrincipals {
  17. /* Don't call "destroy"; use reference counting macros below. */
  18. mozilla::Atomic<int32_t> refcount;
  19. #ifdef JS_DEBUG
  20. /* A helper to facilitate principals debugging. */
  21. uint32_t debugToken;
  22. #endif
  23. JSPrincipals() : refcount(0) {}
  24. void setDebugToken(uint32_t token) {
  25. # ifdef JS_DEBUG
  26. debugToken = token;
  27. # endif
  28. }
  29. /*
  30. * Write the principals with the given |writer|. Return false on failure,
  31. * true on success.
  32. */
  33. virtual bool write(JSContext* cx, JSStructuredCloneWriter* writer) = 0;
  34. /*
  35. * This is not defined by the JS engine but should be provided by the
  36. * embedding.
  37. */
  38. JS_PUBLIC_API(void) dump();
  39. };
  40. extern JS_PUBLIC_API(void)
  41. JS_HoldPrincipals(JSPrincipals* principals);
  42. extern JS_PUBLIC_API(void)
  43. JS_DropPrincipals(JSContext* cx, JSPrincipals* principals);
  44. // Return whether the first principal subsumes the second. The exact meaning of
  45. // 'subsumes' is left up to the browser. Subsumption is checked inside the JS
  46. // engine when determining, e.g., which stack frames to display in a backtrace.
  47. typedef bool
  48. (* JSSubsumesOp)(JSPrincipals* first, JSPrincipals* second);
  49. /*
  50. * Used to check if a CSP instance wants to disable eval() and friends.
  51. * See js_CheckCSPPermitsJSAction() in jsobj.
  52. */
  53. typedef bool
  54. (* JSCSPEvalChecker)(JSContext* cx);
  55. struct JSSecurityCallbacks {
  56. JSCSPEvalChecker contentSecurityPolicyAllows;
  57. JSSubsumesOp subsumes;
  58. };
  59. extern JS_PUBLIC_API(void)
  60. JS_SetSecurityCallbacks(JSContext* cx, const JSSecurityCallbacks* callbacks);
  61. extern JS_PUBLIC_API(const JSSecurityCallbacks*)
  62. JS_GetSecurityCallbacks(JSContext* cx);
  63. /*
  64. * Code running with "trusted" principals will be given a deeper stack
  65. * allocation than ordinary scripts. This allows trusted script to run after
  66. * untrusted script has exhausted the stack. This function sets the
  67. * runtime-wide trusted principal.
  68. *
  69. * This principals is not held (via JS_HoldPrincipals/JS_DropPrincipals).
  70. * Instead, the caller must ensure that the given principals stays valid for as
  71. * long as 'cx' may point to it. If the principals would be destroyed before
  72. * 'cx', JS_SetTrustedPrincipals must be called again, passing nullptr for
  73. * 'prin'.
  74. */
  75. extern JS_PUBLIC_API(void)
  76. JS_SetTrustedPrincipals(JSContext* cx, JSPrincipals* prin);
  77. typedef void
  78. (* JSDestroyPrincipalsOp)(JSPrincipals* principals);
  79. /*
  80. * Initialize the callback that is called to destroy JSPrincipals instance
  81. * when its reference counter drops to zero. The initialization can be done
  82. * only once per JS runtime.
  83. */
  84. extern JS_PUBLIC_API(void)
  85. JS_InitDestroyPrincipalsCallback(JSContext* cx, JSDestroyPrincipalsOp destroyPrincipals);
  86. /*
  87. * Read a JSPrincipals instance from the given |reader| and initialize the out
  88. * paratemer |outPrincipals| to the JSPrincipals instance read.
  89. *
  90. * Return false on failure, true on success. The |outPrincipals| parameter
  91. * should not be modified if false is returned.
  92. *
  93. * The caller is not responsible for calling JS_HoldPrincipals on the resulting
  94. * JSPrincipals instance, the JSReadPrincipalsOp must increment the refcount of
  95. * the resulting JSPrincipals on behalf of the caller.
  96. */
  97. using JSReadPrincipalsOp = bool (*)(JSContext* cx, JSStructuredCloneReader* reader,
  98. JSPrincipals** outPrincipals);
  99. /*
  100. * Initialize the callback that is called to read JSPrincipals instances from a
  101. * buffer. The initialization can be done only once per JS runtime.
  102. */
  103. extern JS_PUBLIC_API(void)
  104. JS_InitReadPrincipalsCallback(JSContext* cx, JSReadPrincipalsOp read);
  105. #endif /* js_Principals_h */