'Chrome extension having trouble with GitHub login when setting cookies

I created a Chrome extension that saves all cookies for a GitHub login and can retrieve it on demand. The goal is to switch between GitHub accounts with one click.

https://github.com/browsercapturesalt/chromeext

Already setting up the cookie profiles is a bit cumbersome. If I log out using GitHub UI after storing the cookies with the extension, then it does not work. I have to delete cookies in dev tools for it to work.

Even more stange, if I successfully log in by setting cookies with the extension and try to generate an access token with this login, then it asks for the password and when I enter it the browser crashes with fatal error which mentions I should try to disable all extensions.

So what dark forces are at work behind the scenes, is not only cookies all that a login needs?

enter image description here

Here is the worker

chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
  console.log(
    sender.tab
      ? "from a content script:" + sender.tab.url
      : "from the extension",
    request
  );

  if (request.kind === "storeCookies") {
    chrome.storage.local.get("profiles", (response) => {
      const profiles = response.profiles || {};
      chrome.cookies.getAll({}).then((cookies) => {
        cookies = cookies.filter((cookie) => cookie.domain.match(/github/));
        console.log({ cookies });
        profiles[request.profileName] = cookies;
        chrome.storage.local.set({ profiles });
        sendResponse(profiles);
      });
    });
  }

  if (request.kind === "retrieveCookies") {
    chrome.storage.local.get("profiles", (response) => {
      console.log("profiles", response);
      const cookies = response.profiles[request.profileName];
      console.log(cookies);
      sendResponse({ cookies });
      for (const cookie of cookies) {
        delete cookie["hostOnly"];
        delete cookie["session"];
        cookie["url"] =
          "https://" + cookie.domain.replace(/^\./, "") + cookie.path;
        if (cookie.expirationDate < Date.now()) {
          console.log("setting", cookie);
        } else {
          console.log("expired", cookie);
        }

        chrome.cookies.set(cookie);
      }

      chrome.tabs.update(undefined, { url: "https://github.com" });
    });
  }

  if (request.kind === "removeProfile") {
    chrome.storage.local.get("profiles", (response) => {
      const profiles = response.profiles || {};
      console.log("profiles", profiles);

      if (!request.profileName) {
        sendResponse(profiles);
        return true;
      }

      delete profiles[request.profileName];
      console.log("profiles modified", profiles);
      chrome.storage.local.set({ profiles });
      sendResponse(profiles);
    });
  }

  return true;
});

chrome.runtime.onInstalled.addListener(() => {
  console.log("Runtime initialized.");
});


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source