From 950a6988ec44b639d489bfe70331900f0733b766 Mon Sep 17 00:00:00 2001 From: Marija Gerasimovska Date: Fri, 9 May 2025 12:42:45 +0200 Subject: [PATCH 1/3] Fix the issue where reading the cookies from a different domains --- chrome/manifest.json | 2 +- chrome/src/background.ts | 157 +++++++++++++++++++++++---------------- 2 files changed, 96 insertions(+), 63 deletions(-) diff --git a/chrome/manifest.json b/chrome/manifest.json index 7e52c7e..282a3c6 100644 --- a/chrome/manifest.json +++ b/chrome/manifest.json @@ -2,7 +2,7 @@ "manifest_version": 3, "name": "Nordcraft", "description": "Browser extension for the Nordcraft editor.", - "version": "2.3.5", + "version": "2.3.6", "icons": { "32": "icons/nordcraft-32.png", "64": "icons/nordcraft-64.png", diff --git a/chrome/src/background.ts b/chrome/src/background.ts index 3fd1bed..6700799 100644 --- a/chrome/src/background.ts +++ b/chrome/src/background.ts @@ -75,72 +75,105 @@ chrome.webNavigation.onBeforeNavigate.addListener( chrome.webRequest.onHeadersReceived.addListener( (info) => { - if (info.responseHeaders) { - const setCookieHeaders = info.responseHeaders - .filter( - (h): h is RequireFields => - h.name.toLowerCase() === 'set-cookie' && - typeof h.value === 'string', - ) - .map((h) => h.value) - if (setCookieHeaders.length === 0) { - return - } - setCookies({ - setCookieHeaders, - requestUrl: info.url, - setCookie: async (cookie, domain) => { - await chrome.cookies.set(cookie) - - const domainCookies = await chrome.cookies.getAll({ - domain, - }) - - if (domainCookies.length > 0) { - await updateSessionRules({ domainCookies, RULE_ID }) - } - }, - removeCookie: async (cookie, domain) => { - await chrome.cookies.remove(cookie) - - const domainCookies = await chrome.cookies.getAll({ - domain, - }) + if (info.parentFrameId < 0) { + return + } - if (domainCookies.length > 0) { - await updateSessionRules({ domainCookies, RULE_ID }) - } else { - await chrome.declarativeNetRequest.updateSessionRules({ - removeRuleIds: [RULE_ID], - }) + // check the parent frame so we only override cookies if we are on nordcraft.com + chrome.webNavigation + .getFrame({ + documentId: info.parentDocumentId, + frameId: info.parentFrameId, + }) + .then((parentFrame) => { + if (!parentFrame) { + return undefined + } + + const parentUrl = new URL(parentFrame.url) + if ( + parentUrl.host.endsWith('toddle.dev') === false && + parentUrl.host.endsWith('nordcraft.com') === false + ) { + return undefined + } + + if (info.responseHeaders) { + const setCookieHeaders = info.responseHeaders + .filter( + ( + h, + ): h is RequireFields< + chrome.webRequest.HttpHeaders[0], + 'value' + > => + h.name.toLowerCase() === 'set-cookie' && + typeof h.value === 'string', + ) + .map((h) => h.value) + if (setCookieHeaders.length === 0) { + return } - }, - notifyUser: async (requestedUrl) => { - const url = new URL(info.url) - const domainCookies = await chrome.cookies.getAll({ - domain: url.host, - }) - - // Don't return the value for the http cookies and include the requested url - const cookies = domainCookies.map((c) => - c.httpOnly - ? { ...c, url: requestedUrl, value: undefined } - : { ...c, url: requestedUrl }, - ) - - const tab = chrome.tabs.query({ - active: true, - lastFocusedWindow: true, + setCookies({ + setCookieHeaders, + requestUrl: info.url, + setCookie: async (cookie, domain) => { + await chrome.cookies.set(cookie) + + const parsedUrl = new URL(cookie.url) + + const domainCookies = await chrome.cookies.getAll({ + domain: domain ?? parsedUrl.host, + }) + + if (domainCookies.length > 0) { + await updateSessionRules({ domainCookies, RULE_ID }) + } + }, + removeCookie: async (cookie, domain) => { + await chrome.cookies.remove(cookie) + + const domainCookies = await chrome.cookies.getAll({ + domain, + }) + + if (domainCookies.length > 0) { + await updateSessionRules({ domainCookies, RULE_ID }) + } else { + await chrome.declarativeNetRequest.updateSessionRules({ + removeRuleIds: [RULE_ID], + }) + } + }, + notifyUser: async (requestedUrl) => { + const url = new URL(info.url) + const domainCookies = await chrome.cookies.getAll({ + domain: url.host, + }) + + // Don't return the value for the http cookies and include the requested url + const cookies = domainCookies.map((c) => + c.httpOnly + ? { ...c, url: requestedUrl, value: undefined } + : { ...c, url: requestedUrl }, + ) + + const tab = chrome.tabs.query({ + active: true, + lastFocusedWindow: true, + }) + + tab.then(([t]) => { + if (t && t.id) { + chrome.tabs.sendMessage(t.id, cookies) + } + }) + }, }) - - tab.then(([t]) => { - if (t && t.id) { - chrome.tabs.sendMessage(t.id, cookies) - } - }) - }, + } + return undefined }) - } + return undefined }, { From 5c43c299556122cfece17b5ca954445ccf5c7953 Mon Sep 17 00:00:00 2001 From: Marija Gerasimovska Date: Fri, 9 May 2025 13:10:32 +0200 Subject: [PATCH 2/3] Use a function --- chrome/src/background.ts | 186 +++++++++++++++++---------------------- chrome/src/helpers.ts | 30 +++++++ 2 files changed, 111 insertions(+), 105 deletions(-) diff --git a/chrome/src/background.ts b/chrome/src/background.ts index 6700799..dc7446b 100644 --- a/chrome/src/background.ts +++ b/chrome/src/background.ts @@ -1,7 +1,7 @@ // The .js extension is necessary for Chrome to pickup the import correctly import { setCookies } from '../../shared/setCookies.js' import type { RequireFields } from '../../shared/setCookies.js' -import { updateSessionRules } from './helpers.js' +import { nordcraftIsParentFrame, updateSessionRules } from './helpers.js' console.log('Nordcraft extension loaded') @@ -14,24 +14,12 @@ chrome.webNavigation.onBeforeNavigate.addListener( removeRuleIds: [RULE_ID], }) - if (event.parentFrameId < 0) { - return - } - // check the parent frame so we only override cookies if we are on nordcraft.com - const parentFrame = await chrome.webNavigation.getFrame({ - documentId: event.parentDocumentId, - frameId: event.parentFrameId, + const isNordcraft = await nordcraftIsParentFrame({ + parentFrameId: event.parentFrameId, + parentDocumentId: event.parentDocumentId, }) - if (!parentFrame) { - return - } - - const parentUrl = new URL(parentFrame.url) - if ( - parentUrl.host.endsWith('toddle.dev') === false && - parentUrl.host.endsWith('nordcraft.com') === false - ) { + if (!isNordcraft) { return } @@ -80,99 +68,87 @@ chrome.webRequest.onHeadersReceived.addListener( } // check the parent frame so we only override cookies if we are on nordcraft.com - chrome.webNavigation - .getFrame({ - documentId: info.parentDocumentId, - frameId: info.parentFrameId, - }) - .then((parentFrame) => { - if (!parentFrame) { - return undefined - } - - const parentUrl = new URL(parentFrame.url) - if ( - parentUrl.host.endsWith('toddle.dev') === false && - parentUrl.host.endsWith('nordcraft.com') === false - ) { - return undefined + nordcraftIsParentFrame({ + parentFrameId: info.parentFrameId, + parentDocumentId: info.parentDocumentId, + }).then((isNordcraft) => { + if (!isNordcraft) { + return undefined + } + if (info.responseHeaders) { + const setCookieHeaders = info.responseHeaders + .filter( + ( + h, + ): h is RequireFields => + h.name.toLowerCase() === 'set-cookie' && + typeof h.value === 'string', + ) + .map((h) => h.value) + if (setCookieHeaders.length === 0) { + return } - - if (info.responseHeaders) { - const setCookieHeaders = info.responseHeaders - .filter( - ( - h, - ): h is RequireFields< - chrome.webRequest.HttpHeaders[0], - 'value' - > => - h.name.toLowerCase() === 'set-cookie' && - typeof h.value === 'string', - ) - .map((h) => h.value) - if (setCookieHeaders.length === 0) { - return - } - setCookies({ - setCookieHeaders, - requestUrl: info.url, - setCookie: async (cookie, domain) => { - await chrome.cookies.set(cookie) - - const parsedUrl = new URL(cookie.url) - - const domainCookies = await chrome.cookies.getAll({ - domain: domain ?? parsedUrl.host, + setCookies({ + setCookieHeaders, + requestUrl: info.url, + setCookie: async (cookie, domain) => { + await chrome.cookies.set(cookie) + + const parsedUrl = new URL(cookie.url) + + const domainCookies = await chrome.cookies.getAll({ + domain: domain ?? parsedUrl.host, + }) + + if (domainCookies.length > 0) { + await updateSessionRules({ domainCookies, RULE_ID }) + } + }, + removeCookie: async (cookie, domain) => { + await chrome.cookies.remove(cookie) + + const parsedUrl = new URL(cookie.url) + + const domainCookies = await chrome.cookies.getAll({ + domain: domain ?? parsedUrl.host, + }) + + if (domainCookies.length > 0) { + await updateSessionRules({ domainCookies, RULE_ID }) + } else { + await chrome.declarativeNetRequest.updateSessionRules({ + removeRuleIds: [RULE_ID], }) + } + }, + notifyUser: async (requestedUrl) => { + const url = new URL(info.url) + const domainCookies = await chrome.cookies.getAll({ + domain: url.host, + }) + + // Don't return the value for the http cookies and include the requested url + const cookies = domainCookies.map((c) => + c.httpOnly + ? { ...c, url: requestedUrl, value: undefined } + : { ...c, url: requestedUrl }, + ) - if (domainCookies.length > 0) { - await updateSessionRules({ domainCookies, RULE_ID }) - } - }, - removeCookie: async (cookie, domain) => { - await chrome.cookies.remove(cookie) - - const domainCookies = await chrome.cookies.getAll({ - domain, - }) + const tab = chrome.tabs.query({ + active: true, + lastFocusedWindow: true, + }) - if (domainCookies.length > 0) { - await updateSessionRules({ domainCookies, RULE_ID }) - } else { - await chrome.declarativeNetRequest.updateSessionRules({ - removeRuleIds: [RULE_ID], - }) + tab.then(([t]) => { + if (t && t.id) { + chrome.tabs.sendMessage(t.id, cookies) } - }, - notifyUser: async (requestedUrl) => { - const url = new URL(info.url) - const domainCookies = await chrome.cookies.getAll({ - domain: url.host, - }) - - // Don't return the value for the http cookies and include the requested url - const cookies = domainCookies.map((c) => - c.httpOnly - ? { ...c, url: requestedUrl, value: undefined } - : { ...c, url: requestedUrl }, - ) - - const tab = chrome.tabs.query({ - active: true, - lastFocusedWindow: true, - }) - - tab.then(([t]) => { - if (t && t.id) { - chrome.tabs.sendMessage(t.id, cookies) - } - }) - }, - }) - } - return undefined - }) + }) + }, + }) + } + return undefined + }) return undefined }, diff --git a/chrome/src/helpers.ts b/chrome/src/helpers.ts index ddf97d9..a5d969d 100644 --- a/chrome/src/helpers.ts +++ b/chrome/src/helpers.ts @@ -30,3 +30,33 @@ export async function updateSessionRules({ ], }) } + +export async function nordcraftIsParentFrame({ + parentFrameId, + parentDocumentId, +}: { + parentFrameId: number + parentDocumentId?: string +}) { + if (parentFrameId < 0) { + return false + } + // check the parent frame so we only override cookies if we are on nordcraft.com + const parentFrame = await chrome.webNavigation.getFrame({ + documentId: parentDocumentId, + frameId: parentFrameId, + }) + + if (!parentFrame) { + return false + } + + const parentUrl = new URL(parentFrame.url) + if ( + parentUrl.host.endsWith('toddle.dev') === false && + parentUrl.host.endsWith('nordcraft.com') === false + ) { + return false + } + return true +} From 5a39f8f6254a5ccfe8837d1be27b963fee422449 Mon Sep 17 00:00:00 2001 From: Marija Gerasimovska Date: Fri, 9 May 2025 14:58:54 +0200 Subject: [PATCH 3/3] small fix --- chrome/src/background.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/chrome/src/background.ts b/chrome/src/background.ts index dc7446b..8a529b1 100644 --- a/chrome/src/background.ts +++ b/chrome/src/background.ts @@ -63,10 +63,6 @@ chrome.webNavigation.onBeforeNavigate.addListener( chrome.webRequest.onHeadersReceived.addListener( (info) => { - if (info.parentFrameId < 0) { - return - } - // check the parent frame so we only override cookies if we are on nordcraft.com nordcraftIsParentFrame({ parentFrameId: info.parentFrameId, @@ -95,7 +91,6 @@ chrome.webRequest.onHeadersReceived.addListener( await chrome.cookies.set(cookie) const parsedUrl = new URL(cookie.url) - const domainCookies = await chrome.cookies.getAll({ domain: domain ?? parsedUrl.host, })