isActiveSuperuser.tsx 1.0 KB

123456789101112131415161718192021222324252627282930
  1. import Cookies from 'js-cookie';
  2. import ConfigStore from 'sentry/stores/configStore';
  3. const SUPERUSER_COOKIE_NAME = window.superUserCookieName ?? 'su';
  4. /**
  5. * Checking for just isSuperuser on a config object may not be enough as backend often checks for *active* superuser.
  6. * We therefore check both isSuperuser flag AND superuser session cookie.
  7. */
  8. export function isActiveSuperuser() {
  9. const {isSuperuser} = ConfigStore.get('user') || {};
  10. if (isSuperuser) {
  11. const superUserCookieName =
  12. ConfigStore.get('superUserCookieName') || SUPERUSER_COOKIE_NAME;
  13. /**
  14. * Superuser cookie cannot be checked for existence as it is HttpOnly.
  15. * As a workaround, we try to change it to something else and if that fails we can assume that it's being present.
  16. * There may be an edgecase where it's present and expired but for current usage it's not a big deal.
  17. */
  18. Cookies.set(superUserCookieName, 'test');
  19. if (Cookies.get(superUserCookieName) === undefined) {
  20. return true;
  21. }
  22. }
  23. return false;
  24. }