Skip to content

Commit b8658cf

Browse files
chore: Inject into hard-coded secondary domain (#16873)
1 parent 69deb3e commit b8658cf

File tree

15 files changed

+126
-64
lines changed

15 files changed

+126
-64
lines changed

packages/driver/cypress/fixtures/multidomain-aut.html

Lines changed: 0 additions & 15 deletions
This file was deleted.

packages/driver/cypress/fixtures/multidomain-sibling.html

Lines changed: 0 additions & 8 deletions
This file was deleted.

packages/driver/cypress/fixtures/multidomain.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
<head>
44
</head>
55
<body>
6-
<a href="http://localhost:3501/fixtures/multidomain-aut.html">Go to localhost:3501</a>
6+
<a href="http://127.0.0.1:3501/fixtures/generic.html">Go to 127.0.0.1:3501</a>
77
</body>
88
</html>

packages/driver/cypress/integration/e2e/multidomain_spec.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@
22
it.skip('verifies initial implementation of sibling iframe and switchToDomain', (done) => {
33
top.addEventListener('message', (event) => {
44
if (event.data && event.data.text) {
5-
expect(event.data.text).to.equal('Some text in the cross-domain AUT')
6-
expect(event.data.host).to.equal('localhost:3501')
5+
expect(event.data.text).to.equal('Foo')
6+
expect(event.data.host).to.equal('127.0.0.1:3501')
77
done()
88
}
99
}, false)
1010

11+
cy.state('anticipateMultidomain', true)
1112
cy.viewport(900, 300)
1213
cy.visit('/fixtures/multidomain.html')
1314
cy.get('a').click()
1415
// @ts-ignore
15-
cy.switchToDomain('localhost:3501', () => {
16+
cy.switchToDomain('127.0.0.1:3501', () => {
1617
// @ts-ignore
17-
cy.now('get', 'p').then(($el) => {
18+
cy.now('get', '.foo').then(($el) => {
1819
top.postMessage({ host: location.host, text: $el.text() }, '*')
1920
})
2021
})

packages/driver/src/cypress/cy.js

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,26 +1043,30 @@ const create = function (specWindow, Cypress, Cookies, state, config, log) {
10431043

10441044
Cypress.action('app:window:load', state('window'))
10451045

1046-
// FIXME: temporary hard-coded hack to get multidomain working
1047-
if (!autWindow?.location?.pathname?.includes('multidomain-aut')) {
1048-
// we are now stable again which is purposefully
1049-
// the last event we call here, to give our event
1050-
// listeners time to be invoked prior to moving on
1051-
return stability.isStable(true, 'load')
1052-
}
1053-
1054-
Cypress.once('cross:domain:window:load', () => {
1055-
Cypress.once('cross:domain:driver:ready', () => {
1056-
stability.isStable(true, 'load')
1046+
// we are now stable again which is purposefully
1047+
// the last event we call here, to give our event
1048+
// listeners time to be invoked prior to moving on
1049+
return stability.isStable(true, 'load')
1050+
} catch (err) {
1051+
// we failed setting the remote window props which
1052+
// means the page navigated to a different domain
1053+
1054+
// temporary hack so that other tests expecting cross-origin
1055+
// failures still fail as expected
1056+
if (state('anticipateMultidomain')) {
1057+
Cypress.once('cross:domain:window:load', () => {
1058+
Cypress.once('cross:domain:driver:ready', () => {
1059+
stability.isStable(true, 'load')
1060+
})
1061+
1062+
Cypress.action('cy:switch:domain', '127.0.0.1:3501')
10571063
})
10581064

1059-
Cypress.action('cy:switch:domain', 'localhost:3501')
1060-
})
1061-
} catch (err) {
1065+
return
1066+
}
1067+
10621068
let e = err
10631069

1064-
// we failed setting the remote window props
1065-
// which means we're in a cross domain failure
10661070
// check first to see if you have a callback function
10671071
// defined and let the page load change the error
10681072
onpl = state('onPageLoadErr')

packages/proxy/lib/http/response-middleware.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ function getNodeCharsetFromResponse (headers: IncomingHttpHeaders, body: Buffer)
4848

4949
function reqMatchesOriginPolicy (req: CypressIncomingRequest, remoteState) {
5050
if (remoteState.strategy === 'http') {
51+
if (req.proxiedUrl.includes('127.0.0.1:3501')) return true
52+
5153
return cors.urlMatchesOriginPolicyProps(req.proxiedUrl, remoteState.props)
5254
}
5355

@@ -238,6 +240,10 @@ const SetInjectionLevel: ResponseMiddleware = function () {
238240
return false
239241
}
240242

243+
if (this.req.proxiedUrl.includes('127.0.0.1:3501')) {
244+
return 'fullMultidomain'
245+
}
246+
241247
if (this.res.isInitial) {
242248
return 'full'
243249
}

packages/proxy/lib/http/util/inject.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,15 @@ export function full (domain) {
2020
`
2121
})
2222
}
23+
24+
export function fullMultidomain (domain) {
25+
return runner.getMultidomainInjectionContents().then((contents) => {
26+
return oneLine`
27+
<script type='text/javascript'>
28+
document.domain = '127.0.0.1';
29+
30+
${contents}
31+
</script>
32+
`
33+
})
34+
}

packages/proxy/lib/http/util/rewriter.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as inject from './inject'
22
import * as astRewriter from './ast-rewriter'
33
import * as regexRewriter from './regex-rewriter'
4+
import { CypressWantsInjection } from '../../types'
45

56
export type SecurityOpts = {
67
isHtml?: boolean
@@ -11,7 +12,7 @@ export type SecurityOpts = {
1112

1213
export type InjectionOpts = {
1314
domainName: string
14-
wantsInjection: WantsInjection
15+
wantsInjection: CypressWantsInjection
1516
wantsSecurityRemoved: any
1617
}
1718

@@ -20,8 +21,6 @@ const headRe = /(<head(?!er).*?>)/i
2021
const bodyRe = /(<body.*?>)/i
2122
const htmlRe = /(<html.*?>)/i
2223

23-
type WantsInjection = 'full' | 'partial' | false
24-
2524
function getRewriter (useAstSourceRewriting: boolean) {
2625
return useAstSourceRewriting ? astRewriter : regexRewriter
2726
}
@@ -30,6 +29,8 @@ function getHtmlToInject ({ domainName, wantsInjection }: InjectionOpts) {
3029
switch (wantsInjection) {
3130
case 'full':
3231
return inject.full(domainName)
32+
case 'fullMultidomain':
33+
return inject.fullMultidomain(domainName)
3334
case 'partial':
3435
return inject.partial(domainName)
3536
default:

packages/proxy/lib/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ export type CypressIncomingRequest = Request & {
1414
followRedirect?: boolean
1515
}
1616

17+
export type CypressWantsInjection = 'full' | 'fullMultidomain' | 'partial' | false
18+
1719
/**
1820
* An outgoing response to an incoming request to the Cypress web server.
1921
*/
2022
export type CypressOutgoingResponse = Response & {
2123
isInitial: null | boolean
22-
wantsInjection: 'full' | 'partial' | false
24+
wantsInjection: CypressWantsInjection
2325
wantsSecurityRemoved: null | boolean
2426
body?: string | Readable
2527
}
File renamed without changes.

0 commit comments

Comments
 (0)