-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ENGG-1696] MV3: support for rq_request_initiator_origin() predefined…
… function (#1677) * MV3: Expose onBeforeAjaxRequest hook for service worker to register new DNR rules * added request processor * fix: tabService cleanup * fix: cache the rules * added todo comment * update timeout * cleanup AJAX interception * cleanup request processor * remove logs * fix: excluded * fix: excluded domains * added comment * moved to psmh * refactor: methods * remove session rules on extension status toggle * do no create dnr for predefined function * added predefined function session rule * fix:type * null safe * refactor: request processor * fix: headers match * added filters check * fix: request type * fix: regex substitution * fix: moved to file * fix: initiator domain function * fix: parseHeaders rule * fix: matchedRule info * fixed match rule getter * fix: destructure * fix: matched rule * fix: import changes * remove log * fix: functions --------- Co-authored-by: Vaibhav Nigam <[email protected]>
- Loading branch information
Showing
5 changed files
with
135 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
...tension/mv3/src/service-worker/services/requestProcessor/handleInitiatorDomainFunction.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { Rule } from "common/types"; | ||
import { findMatchingRule } from "../ruleMatcher"; | ||
import { updateRequestSpecificRules } from "../rulesManager"; | ||
import { AJAXRequestDetails, SessionRuleType } from "./types"; | ||
|
||
const INITIATOR_DOMAIN_FUNCTION = "rq_request_initiator_origin()"; | ||
|
||
export const handleInitiatorDomainFunction = async ( | ||
tabId: number, | ||
requestDetails: AJAXRequestDetails, | ||
rules: Rule[] | ||
) => { | ||
const { isApplied, matchedPair } = findMatchingRule(rules, requestDetails) ?? {}; | ||
|
||
if (!isApplied) { | ||
return; | ||
} | ||
|
||
const headerKeyValueMap: Record<"Response" | "Request", Record<string, string>> = { | ||
Request: {}, | ||
Response: {}, | ||
}; | ||
|
||
if (matchedPair.modifications?.Request?.length) { | ||
matchedPair.modifications.Request.forEach((header: { header: string; type: string; value: string }) => { | ||
if (header.value === INITIATOR_DOMAIN_FUNCTION) { | ||
headerKeyValueMap.Request[header.header] = requestDetails.initiatorDomain; | ||
} | ||
}); | ||
} | ||
|
||
if (matchedPair.modifications?.Response?.length) { | ||
matchedPair.modifications.Response.forEach((header: { header: string; type: string; value: string }) => { | ||
if (header.value === INITIATOR_DOMAIN_FUNCTION) { | ||
headerKeyValueMap.Response[header.header] = requestDetails.initiatorDomain; | ||
} | ||
}); | ||
} | ||
|
||
const ruleAction: { | ||
requestHeaders?: chrome.declarativeNetRequest.RuleAction["requestHeaders"]; | ||
responseHeaders?: chrome.declarativeNetRequest.RuleAction["responseHeaders"]; | ||
} = {}; | ||
|
||
if (Object.keys(headerKeyValueMap.Request).length) { | ||
ruleAction.requestHeaders = Object.entries(headerKeyValueMap.Request).map(([header, value]) => ({ | ||
header, | ||
value, | ||
operation: chrome.declarativeNetRequest.HeaderOperation.SET, | ||
})); | ||
} | ||
|
||
if (Object.keys(headerKeyValueMap.Response).length) { | ||
ruleAction.responseHeaders = Object.entries(headerKeyValueMap.Response).map(([header, value]) => ({ | ||
header, | ||
value, | ||
operation: chrome.declarativeNetRequest.HeaderOperation.SET, | ||
})); | ||
} | ||
|
||
if (!Object.keys(ruleAction).length) { | ||
return; | ||
} | ||
|
||
await updateRequestSpecificRules( | ||
tabId, | ||
requestDetails.url, | ||
{ | ||
action: { | ||
...ruleAction, | ||
type: chrome.declarativeNetRequest.RuleActionType.MODIFY_HEADERS, | ||
}, | ||
condition: { | ||
urlFilter: `|${requestDetails.url}|`, | ||
resourceTypes: [chrome.declarativeNetRequest.ResourceType.XMLHTTPREQUEST], | ||
tabIds: [tabId], | ||
requestMethods: [requestDetails.method.toLowerCase() as chrome.declarativeNetRequest.RequestMethod], | ||
excludedInitiatorDomains: ["requestly.io", "requestly.com"], | ||
}, | ||
}, | ||
SessionRuleType.INITIATOR_DOMAIN | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters