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

[ENGG-1689] MV3: Expose onBeforeAjaxRequest hook to forward auth header in cross origin redirects #1664

Merged
merged 27 commits into from May 12, 2024

Conversation

lazyvab
Copy link
Contributor

@lazyvab lazyvab commented May 5, 2024

Closes issue:

πŸ“œ Summary of changes:

βœ… Checklist:

  • Make sure linting and unit tests pass.
  • No install/build warnings introduced.
  • Verified UI in browser.
  • For UI changes, added/updated analytics events (if applicable).
  • For changes in extension's code, manually tested in Chrome and Firefox.
  • For changes in extension's code, both MV2 and MV3 are covered.
  • Added/updated unit tests for this change.
  • Raised pull request to update corresponding documentation (if already exists).

πŸ§ͺ Test instructions:

πŸ”— Other references:

Copy link

deepsource-io bot commented May 5, 2024

Here's the code health analysis summary for commits 027228f..81f6546. View details on DeepSourceΒ β†—.

Analysis Summary

AnalyzerStatusSummaryLink
DeepSource Shell LogoShellβœ…Β SuccessView CheckΒ β†—
DeepSource JavaScript LogoJavaScript❌ Failure
❗ 24 occurences introduced
🎯 3 occurences resolved
View CheckΒ β†—

πŸ’‘ If you’re a repository administrator, you can configure the quality gates from the settings.

@nafees87n nafees87n changed the title MV3: Expose onBeforeAjaxRequest hook for service worker to register new DNR rules [ENGG-1689] MV3: Expose onBeforeAjaxRequest hook to forward auth header in cross origin redirects May 6, 2024
Comment on lines 125 to 129
if (Object.values(this.cachedRules).every((rules) => rules.length === 0)) {
return;
}

await this.forwardIgnoredHeadersOnRedirect(tabId, requestDetails);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should move this in a separate method handleRedirectAuthorizationHeader. We can add cases too here only and keep other logic separate from each other

Comment on lines 31 to 74
private forwardIgnoredHeadersOnRedirect = async (tabId: number, requestDetails: AJAXRequestDetails) => {
if (
!IGNORED_HEADERS_ON_REDIRECT.some(
(header) => requestDetails.requestHeaders[header] || requestDetails.requestHeaders[header.toLowerCase()]
)
) {
return;
}

const matchedRule = this.findMatchingRule(
[...this.cachedRules.redirectRules, ...this.cachedRules.replaceRules],
requestDetails.url
);

if (!matchedRule) {
return;
}

const ignoredHeaderValues = IGNORED_HEADERS_ON_REDIRECT.map((header) => ({
name: header,
value: requestDetails.requestHeaders[header] || requestDetails.requestHeaders[header.toLowerCase()],
}));

const redirectedUrl = this.getRedirectedUrl(requestDetails.url, matchedRule);

return this.updateRequestSpecificRules(tabId, requestDetails.url, {
action: {
requestHeaders: ignoredHeaderValues.map((header: { name: string; value: string }) => ({
header: header.name,
value: header.value,
operation: chrome.declarativeNetRequest.HeaderOperation.SET,
})),
type: chrome.declarativeNetRequest.RuleActionType.MODIFY_HEADERS,
},
condition: {
// Exact URL is not used because for replace rules, a marker query param is add which changes the URL so exact URL doesn't match
urlFilter: `|${redirectedUrl}`,
resourceTypes: [chrome.declarativeNetRequest.ResourceType.XMLHTTPREQUEST],
tabIds: [tabId],
requestMethods: [requestDetails.method.toLowerCase() as chrome.declarativeNetRequest.RequestMethod],
excludedInitiatorDomains: ["requestly.io", "requestly.com"],
},
});
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we will need this logic more large number of cases in future. Lets create a folder for this as separate out logics

  • requestProcessor
    • index.ts
    • redirectAuthHeader
    • initiatorDomainHeader

private forwardIgnoredHeadersOnRedirect = async (tabId: number, requestDetails: AJAXRequestDetails) => {
if (
!IGNORED_HEADERS_ON_REDIRECT.some(
(header) => requestDetails.requestHeaders[header] || requestDetails.requestHeaders[header.toLowerCase()]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compare both headers in lowercase.
There might be case when we stored header in lowercase in IGNORED_HEADERS_ON_REDIRECT and the actual header is in uppercase. This logic will fail then


const ignoredHeaderValues = IGNORED_HEADERS_ON_REDIRECT.map((header) => ({
name: header,
value: requestDetails.requestHeaders[header] || requestDetails.requestHeaders[header.toLowerCase()],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use both lowercase header keys

Comment on lines 49 to 52
const ignoredHeaderValues = IGNORED_HEADERS_ON_REDIRECT.map((header) => ({
name: header,
value: requestDetails.requestHeaders[header] || requestDetails.requestHeaders[header.toLowerCase()],
}));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should do this for only existing headers. Right now it generates for header values which are not present in the request

Comment on lines 82 to 85
const redirectedUrl = pair.destination || requestUrl.replace(pair.from, pair.to);
if (redirectedUrl === requestUrl) {
return null;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this work in case of wildcard and regex subsitutions?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Substitutions are not supported in case of replace rules. We need to handle only redirect rule


tabService.setData(tabId, TAB_SERVICE_DATA.SESSION_RULES_MAP, {
...sessionRulesMap,
[requestUrl]: ruleId,
Copy link
Member

@wrongsahil wrongsahil May 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't ruleIds be an array or map of rulesIds because there might be case when initiator_domain and auth case applies on same URL

How about this?

{
    [requestUrl]: {
        auth_header: ruleId,
        initiator_domain: ruleId,
    }
}

@nafees87n nafees87n requested a review from wrongsahil May 9, 2024 18:36
@wrongsahil wrongsahil merged commit 7ff78a0 into master May 12, 2024
2 of 3 checks passed
@wrongsahil wrongsahil deleted the mv3-onBeforeAjaxRequest branch May 12, 2024 21:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants