-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add utility functions in proxy to be used in the near future i…
…n the request/response middleware(s)
- Loading branch information
1 parent
eb819e1
commit d6ee900
Showing
5 changed files
with
203 additions
and
1 deletion.
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
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,30 @@ | ||
import { urlOriginsMatch, urlSameSiteMatch } from '@packages/network/lib/cors' | ||
import type { HttpMiddlewareThis } from '../index' | ||
import type { SecFetchSite } from '../../types' | ||
|
||
export const doesTopNeedToBeSimulated = <T>(ctx: HttpMiddlewareThis<T>): boolean => { | ||
const currentAUTUrl = ctx.getAUTUrl() | ||
|
||
// if the AUT url is undefined for whatever reason, return false as we do not want to complicate top simulation | ||
if (!currentAUTUrl) { | ||
return false | ||
} | ||
|
||
// only simulate top if the AUT is NOT the primary origin, meaning that we should treat the AUT as top | ||
const doesTopNeedToSimulating = !ctx.remoteStates.isPrimaryOrigin(currentAUTUrl) | ||
|
||
return doesTopNeedToSimulating | ||
} | ||
|
||
// @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-Fetch-Site | ||
export const calculateSiteContext = (url1: string, url2: string, isUserInteraction = false): SecFetchSite => { | ||
if (urlOriginsMatch(url1, url2)) { | ||
return 'same-origin' | ||
} | ||
|
||
if (urlSameSiteMatch(url1, url2)) { | ||
return 'same-site' | ||
} | ||
|
||
return isUserInteraction ? 'none' : 'cross-site' | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { expect } from 'chai' | ||
import sinon from 'sinon' | ||
import { HttpMiddlewareThis } from '../../../../lib/http' | ||
import { calculateSiteContext, doesTopNeedToBeSimulated } from '../../../../lib/http/util/top-simulation' | ||
|
||
context('.doesTopNeedToBeSimulated', () => { | ||
const autUrl = 'http://localhost:8080' | ||
|
||
it('returns false when URL matches the AUT Url origin policy and the AUT Url exists', () => { | ||
const mockCtx: HttpMiddlewareThis<any> = { | ||
getAUTUrl: sinon.stub().returns(autUrl), | ||
remoteStates: { | ||
isPrimaryOrigin: sinon.stub().returns(true), | ||
}, | ||
} | ||
|
||
expect(doesTopNeedToBeSimulated(mockCtx)).to.be.false | ||
}) | ||
|
||
it('returns false when AUT Url is not defined, regardless of primary origin stack', () => { | ||
const mockCtx: HttpMiddlewareThis<any> = { | ||
getAUTUrl: sinon.stub().returns(undefined), | ||
} | ||
|
||
expect(doesTopNeedToBeSimulated(mockCtx)).to.be.false | ||
}) | ||
|
||
it('returns true when AUT Url is defined but AUT Url no longer matches the primary origin', () => { | ||
const mockCtx: HttpMiddlewareThis<any> = { | ||
getAUTUrl: sinon.stub().returns(autUrl), | ||
remoteStates: { | ||
isPrimaryOrigin: sinon.stub().returns(false), | ||
}, | ||
} | ||
|
||
expect(doesTopNeedToBeSimulated(mockCtx)).to.be.true | ||
}) | ||
}) | ||
|
||
context('.calculateSiteContext', () => { | ||
const autUrl = 'https://staging.google.com' | ||
|
||
it('calculates same-origin correctly for same-origin / same-site urls', () => { | ||
expect(calculateSiteContext(autUrl, 'https://staging.google.com')).to.equal('same-origin') | ||
}) | ||
|
||
it('calculates same-site correctly for cross-origin / same-site urls', () => { | ||
expect(calculateSiteContext(autUrl, 'https://app.google.com')).to.equal('same-site') | ||
}) | ||
|
||
it('calculates cross-site correctly for cross-origin / cross-site urls', () => { | ||
expect(calculateSiteContext(autUrl, 'https://staging.google2.com')).to.equal('cross-site') | ||
}) | ||
|
||
it('returns "none" if the interaction is triggered by the user, regardless of other properties', () => { | ||
expect(calculateSiteContext(autUrl, 'https://staging.google2.com', true)).to.equal('none') | ||
}) | ||
}) |