Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use url instead of hostname when caching headers and cookies #123

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/entries/Background/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,18 +325,18 @@ export async function clearCookies(host: string) {
});
}

export async function getCookies(host: string, name: string) {
export async function getCookies(link: string, name: string) {
try {
const existing = await cookiesDb.sublevel(host).get(name);
const existing = await cookiesDb.sublevel(link).get(name);
return existing;
} catch (e) {
return null;
}
}

export async function getCookiesByHost(host: string) {
export async function getCookiesByHost(link: string) {
const ret: { [key: string]: string } = {};
for await (const [key, value] of cookiesDb.sublevel(host).iterator()) {
for await (const [key, value] of cookiesDb.sublevel(link).iterator()) {
ret[key] = value;
}
return ret;
Expand All @@ -359,10 +359,10 @@ export async function getConnection(origin: string) {
}
}

export async function setHeaders(host: string, name: string, value?: string) {
export async function setHeaders(link: string, name: string, value?: string) {
if (!value) return null;
return mutex.runExclusive(async () => {
await headersDb.sublevel(host).put(name, value);
await headersDb.sublevel(link).put(name, value);
return true;
});
}
Expand All @@ -382,9 +382,9 @@ export async function getHeaders(host: string, name: string) {
return null;
}
}
export async function getHeadersByHost(host: string) {
export async function getHeadersByHost(link: string) {
const ret: { [key: string]: string } = {};
for await (const [key, value] of headersDb.sublevel(host).iterator()) {
for await (const [key, value] of headersDb.sublevel(link).iterator()) {
ret[key] = value;
}
return ret;
Expand Down
12 changes: 7 additions & 5 deletions src/entries/Background/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import mutex from './mutex';
import browser from 'webextension-polyfill';
import { addRequest } from '../../reducers/requests';
import { urlify } from '../../utils/misc';
import { setCookies, setHeaders } from './db';
import { getHeadersByHost, setCookies, setHeaders } from './db';
export const onSendHeaders = (
details: browser.WebRequest.OnSendHeadersDetailsType,
) => {
Expand All @@ -14,20 +14,22 @@ export const onSendHeaders = (
if (method !== 'OPTIONS') {
const cache = getCacheByTabId(tabId);
const existing = cache.get<RequestLog>(requestId);
const { hostname } = urlify(details.url) || {};
const { origin, pathname } = urlify(details.url) || {};

if (hostname && details.requestHeaders) {
const link = [origin, pathname].join('');

if (link && details.requestHeaders) {
details.requestHeaders.forEach((header) => {
const { name, value } = header;
if (/^cookie$/i.test(name) && value) {
value
.split(';')
.map((v) => v.split('='))
.forEach((cookie) => {
setCookies(hostname, cookie[0].trim(), cookie[1]);
setCookies(link, cookie[0].trim(), cookie[1]);
});
} else {
setHeaders(hostname, name, value);
setHeaders(link, name, value);
}
});
}
Expand Down
16 changes: 8 additions & 8 deletions src/utils/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,20 +303,20 @@ export const makePlugin = async (
}

if (config?.cookies) {
const cookies: { [hostname: string]: { [key: string]: string } } = {};
for (const host of config.cookies) {
const cache = await getCookiesByHost(host);
cookies[host] = cache;
const cookies: { [link: string]: { [key: string]: string } } = {};
for (const link of config.cookies) {
const cache = await getCookiesByHost(link);
cookies[link] = cache;
}
// @ts-ignore
injectedConfig.cookies = JSON.stringify(cookies);
}

if (config?.headers) {
const headers: { [hostname: string]: { [key: string]: string } } = {};
for (const host of config.headers) {
const cache = await getHeadersByHost(host);
headers[host] = cache;
const headers: { [link: string]: { [key: string]: string } } = {};
for (const link of config.headers) {
const cache = await getHeadersByHost(link);
headers[link] = cache;
}
// @ts-ignore
injectedConfig.headers = JSON.stringify(headers);
Expand Down
Loading