diff --git a/package.json b/package.json index 3a7a8958e..7752f154e 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "testcafe-hammerhead", "description": "A powerful web-proxy used as a core for the TestCafe testing framework (https://github.com/DevExpress/testcafe).", - "version": "24.5.9", + "version": "24.5.10", "homepage": "https://github.com/DevExpress/testcafe-hammerhead", "bugs": { "url": "https://github.com/DevExpress/testcafe-hammerhead/issues" diff --git a/src/client/sandbox/node/document/index.ts b/src/client/sandbox/node/document/index.ts index 8333ae217..f8a4dd59a 100644 --- a/src/client/sandbox/node/document/index.ts +++ b/src/client/sandbox/node/document/index.ts @@ -2,7 +2,6 @@ import SandboxBase from '../../base'; import IframeSandbox from '../../iframe'; import nativeMethods from '../../native-methods'; import domProcessor from '../../../dom-processor'; -import * as urlUtils from '../../../utils/url'; import settings from '../../../settings'; import { isIE } from '../../../utils/browser'; import { isIframeWithoutSrc, getFrameElement, isImgElement, isShadowUIElement } from '../../../utils/dom'; @@ -12,7 +11,8 @@ import INTERNAL_PROPS from '../../../../processing/dom/internal-properties'; import LocationAccessorsInstrumentation from '../../code-instrumentation/location'; import { overrideDescriptor, createOverriddenDescriptor, overrideFunction } from '../../../utils/overriding'; import NodeSandbox from '../index'; -import { getDestinationUrl, isSpecialPage } from '../../../utils/url'; +import { getDestinationUrl, isSpecialPage, convertToProxyUrl, getCrossDomainProxyOrigin } from '../../../utils/url'; +import { getReferrer } from '../../../utils/destination-location'; import DocumentTitleStorageInitializer from './title-storage-initializer'; import CookieSandbox from '../../cookie'; @@ -204,7 +204,7 @@ export default class DocumentSandbox extends SandboxBase { const el = nativeMethods.createElement.apply(this, args); DocumentSandbox.forceProxySrcForImageIfNecessary(el); - domProcessor.processElement(el, urlUtils.convertToProxyUrl); + domProcessor.processElement(el, convertToProxyUrl); documentSandbox._nodeSandbox.processNodes(el); return el; @@ -214,7 +214,7 @@ export default class DocumentSandbox extends SandboxBase { const el = nativeMethods.createElementNS.apply(this, args); DocumentSandbox.forceProxySrcForImageIfNecessary(el); - domProcessor.processElement(el, urlUtils.convertToProxyUrl); + domProcessor.processElement(el, convertToProxyUrl); documentSandbox._nodeSandbox.processNodes(el); return el; @@ -243,6 +243,9 @@ export default class DocumentSandbox extends SandboxBase { getter: function () { const referrer = getDestinationUrl(nativeMethods.documentReferrerGetter.call(this)); + if (referrer === getCrossDomainProxyOrigin() + '/') + return getReferrer(); + return isSpecialPage(referrer) ? '' : referrer; } }); diff --git a/src/client/utils/destination-location.ts b/src/client/utils/destination-location.ts index 24ef5c358..e9d638410 100644 --- a/src/client/utils/destination-location.ts +++ b/src/client/utils/destination-location.ts @@ -65,6 +65,13 @@ export let get = function (): string { return parsedProxyUrl ? parsedProxyUrl.destUrl : location; } +export function getReferrer () { + const location = getLocation(); + const parsedProxyUrl = sharedUrlUtils.parseProxyUrl(location); + + return parsedProxyUrl?.reqOrigin ? parsedProxyUrl.reqOrigin + '/' : ''; +} + export function overrideGet (func: typeof get) { get = func; } diff --git a/src/client/utils/url.ts b/src/client/utils/url.ts index eba2300b9..a35adf1aa 100644 --- a/src/client/utils/url.ts +++ b/src/client/utils/url.ts @@ -134,6 +134,9 @@ export let getProxyUrl = function (url: string, opts?): string { reqOrigin = reqOrigin || destLocation.getOriginHeader(); } + if (parsedResourceType.isIframe && proxyPort === settings.get().crossDomainProxyPort) + reqOrigin = reqOrigin || destLocation.getOriginHeader(); + return sharedUrlUtils.getProxyUrl(resolvedUrl, { proxyProtocol, proxyHostname, @@ -248,6 +251,14 @@ export let convertToProxyUrl = function (url: string, resourceType, charset, isC }); } +export function getCrossDomainProxyOrigin () { + return sharedUrlUtils.getDomain({ + protocol: location.protocol, // eslint-disable-line no-restricted-properties + hostname: location.hostname, // eslint-disable-line no-restricted-properties + port: settings.get().crossDomainProxyPort, + }); +} + export function overrideConvertToProxyUrl (func: typeof convertToProxyUrl): void { convertToProxyUrl = func; } diff --git a/src/request-pipeline/context.ts b/src/request-pipeline/context.ts index 939c6705b..56c674bd7 100644 --- a/src/request-pipeline/context.ts +++ b/src/request-pipeline/context.ts @@ -436,6 +436,9 @@ export default class RequestPipelineContext { const sessionId = this.session.id; const windowId = this.windowId; + if (isCrossDomain) + reqOrigin = this.dest.domain; + return urlUtils.getProxyUrl(url, { proxyHostname, proxyProtocol, diff --git a/src/utils/url.ts b/src/utils/url.ts index 8bd592857..f2bfc5ed8 100644 --- a/src/utils/url.ts +++ b/src/utils/url.ts @@ -206,6 +206,8 @@ function parseRequestDescriptor (desc: string): RequestDescriptor | null { parsedDesc.charset = resourceData[0]; else if (parsedResourceType.isWebSocket) parsedDesc.reqOrigin = decodeURIComponent(restoreShortOrigin(resourceData[0])); + else if (parsedResourceType.isIframe && resourceData[0]) + parsedDesc.reqOrigin = decodeURIComponent(restoreShortOrigin(resourceData[0])); else if (parsedResourceType.isAjax) { parsedDesc.credentials = parseInt(resourceData[0]); diff --git a/test/client/before-test.js b/test/client/before-test.js index cd5e7dd7b..1ab2429cc 100644 --- a/test/client/before-test.js +++ b/test/client/before-test.js @@ -209,7 +209,7 @@ }; window.waitForMessage = function (receiver) { - return new Promise(function (resolve) { + return new hammerhead.Promise(function (resolve) { receiver.onmessage = function (e) { receiver.onmessage = void 0; diff --git a/test/client/data/cross-domain/get-referrer.html b/test/client/data/cross-domain/get-referrer.html new file mode 100644 index 000000000..bc1c6edce --- /dev/null +++ b/test/client/data/cross-domain/get-referrer.html @@ -0,0 +1,20 @@ + + +
+