Skip to content

Commit

Permalink
BDE-226 trying to capture cookies from document content
Browse files Browse the repository at this point in the history
  • Loading branch information
nxmatic committed Mar 26, 2024
1 parent 0711668 commit 0d1dc94
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
22 changes: 16 additions & 6 deletions src/content/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,20 @@ class ContentMessageHandler {
}
}

new ServiceWorkerBridge().bootstrap().then((worker) => {
const content = new Content(worker.browserStore);
const handler = new ContentMessageHandler(content);
new ServiceWorkerBridge()
.bootstrap({
name: 'content',
entrypoint: () => { console.warn('Loading content script'); }
})
.then((worker) => {
const content = new Content(worker.browserStore);
const handler = new ContentMessageHandler(content);

chrome.runtime.onMessage
.addListener((request, sender, sendResponse) => handler.handle(request, sender, sendResponse));
});
chrome.runtime.onMessage
.addListener((request, sender, sendResponse) => handler.handle(request, sender, sendResponse));

chrome.cookies.getAll({ domain: document.location.hostname }, (cookies) => {
const allCookies = cookies.map((cookie) => `${cookie.name}=${cookie.value}`).join(';');
worker.declarativeNetEngine.setCookieHeader({ domain: document.location.hostname, cookie: allCookies });
});
});
19 changes: 13 additions & 6 deletions src/main/declarative-net-engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ class BaseRule {
}

class CookieHeaderRule extends BaseRule {
constructor(rootUrl = 'https://connect.nuxeo.com', cookieHeader) {
constructor(domain = 'connect.nuxeo.com', cookieHeader) {
super();

// Set properties
this.cookieHeader = cookieHeader;
this.rootUrl = rootUrl;
this.domain = domain;

// Bind methods
Object.getOwnPropertyNames(Object.getPrototypeOf(this))
Expand All @@ -40,15 +40,15 @@ class CookieHeaderRule extends BaseRule {

// eslint-disable-next-line class-methods-use-this
key() {
return `cookieHeader-${this.rootUrl}`;
return `cookieHeader-${this.domain}`;
}

toJson(priority = 1) {
return {
id: this.hashCode(),
priority,
condition: {
urls: `${this.rootUrl}/*`,
urlFilter: `*://${this.domain}/*`
},
action: {
type: 'modifyHeaders',
Expand Down Expand Up @@ -137,9 +137,10 @@ class RedirectRule extends BaseRule {
type: 'redirect',
redirect: {
transform: {
scheme: 'https',
scheme: this.to.protocol.slice(0, -1), // Use the scheme from the `to` URL
host: this.to.host,
path: this.to.path,
port: (this.to.port && this.to.port !== '') ? this.to.port : '443', // Handle the case where the port is an empty string
path: this.to.pathname,
},
},
},
Expand Down Expand Up @@ -287,6 +288,12 @@ class DeclarativeNetEngine extends ServiceWorkerComponent {
console.error('Failed to remove dynamic rules:', error);
});
}

setCookieHeader({ domain, cookie }) {
return this
.push(new CookieHeaderRule(domain, cookie))
.then(() => this.flush());
}
}

export default {
Expand Down
10 changes: 10 additions & 0 deletions src/main/manifest-chrome.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,17 @@
"background": {
"service_worker": "main.js"
},
"content_scripts": [
{
"matches": ["http://localhost:8080/*", "https://nos-preprod-connect.nuxeocloud.com/*", "https://connect.nuxeo.com/*"],
"js": ["content/main/index.js"]
}
],
"content_security_policy": {
"extension_pages": "script-src 'self'; object-src 'self'"
},
"permissions": [
"cookies",
"storage",
"contextMenus",
"tabs",
Expand Down

0 comments on commit 0d1dc94

Please sign in to comment.