Skip to content

Commit

Permalink
fix: use link instead of hostname when caching headers and cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
0xtsukino committed Dec 11, 2024
1 parent ca382f3 commit 5ef4116
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
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

0 comments on commit 5ef4116

Please sign in to comment.