mirror of
https://github.com/sussy-code/browser-ext.git
synced 2026-04-26 19:12:52 +00:00
Fix response header modification
This commit is contained in:
parent
af263c1f70
commit
fa1e42eab1
2 changed files with 12 additions and 12 deletions
|
|
@ -3,7 +3,7 @@ import type { PlasmoMessaging } from '@plasmohq/messaging';
|
||||||
import type { BaseRequest } from '~types/request';
|
import type { BaseRequest } from '~types/request';
|
||||||
import type { BaseResponse } from '~types/response';
|
import type { BaseResponse } from '~types/response';
|
||||||
import { setDynamicRules } from '~utils/declarativeNetRequest';
|
import { setDynamicRules } from '~utils/declarativeNetRequest';
|
||||||
import { assertDomainWhitelist } from '~utils/storage';
|
import { assertDomainWhitelist, modifiableResponseHeaders } from '~utils/storage';
|
||||||
|
|
||||||
interface Request extends BaseRequest {
|
interface Request extends BaseRequest {
|
||||||
ruleId: number;
|
ruleId: number;
|
||||||
|
|
@ -18,6 +18,17 @@ const handler: PlasmoMessaging.MessageHandler<Request, BaseResponse> = async (re
|
||||||
if (!req.sender?.tab?.url) throw new Error('No tab URL found in the request.');
|
if (!req.sender?.tab?.url) throw new Error('No tab URL found in the request.');
|
||||||
if (!req.body) throw new Error('No request body found in the request.');
|
if (!req.body) throw new Error('No request body found in the request.');
|
||||||
|
|
||||||
|
// restrict what response headers can be modified
|
||||||
|
req.body.responseHeaders = Object.keys(req.body.responseHeaders ?? {})
|
||||||
|
.filter((key) => modifiableResponseHeaders.includes(key.toLowerCase()))
|
||||||
|
.reduce(
|
||||||
|
(obj, key) => {
|
||||||
|
obj[key] = (req.body?.responseHeaders ?? {})[key];
|
||||||
|
return obj;
|
||||||
|
},
|
||||||
|
{} as Record<string, string>,
|
||||||
|
);
|
||||||
|
|
||||||
await assertDomainWhitelist(req.sender.tab.url);
|
await assertDomainWhitelist(req.sender.tab.url);
|
||||||
await setDynamicRules(req.body);
|
await setDynamicRules(req.body);
|
||||||
res.send({
|
res.send({
|
||||||
|
|
|
||||||
|
|
@ -21,17 +21,6 @@ const mapHeadersToDeclarativeNetRequestHeaders = (
|
||||||
};
|
};
|
||||||
|
|
||||||
export const setDynamicRules = async (body: DynamicRule) => {
|
export const setDynamicRules = async (body: DynamicRule) => {
|
||||||
// restrict what response headers can be modified
|
|
||||||
body.responseHeaders = Object.keys(body.responseHeaders ?? {})
|
|
||||||
.filter((key) => modifiableResponseHeaders.includes(key.toLowerCase()))
|
|
||||||
.reduce(
|
|
||||||
(obj, key) => {
|
|
||||||
obj[key] = (body.responseHeaders ?? {})[key];
|
|
||||||
return obj;
|
|
||||||
},
|
|
||||||
{} as Record<string, string>,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (isChrome()) {
|
if (isChrome()) {
|
||||||
await chrome.declarativeNetRequest.updateDynamicRules({
|
await chrome.declarativeNetRequest.updateDynamicRules({
|
||||||
removeRuleIds: [body.ruleId],
|
removeRuleIds: [body.ruleId],
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue