Path: blob/master/lib/rammerhead/src/util/addUrlShuffling.js
6530 views
const RequestPipelineContext = require('testcafe-hammerhead/lib/request-pipeline/context');1const StrShuffler = require('./StrShuffler');2const getSessionId = require('./getSessionId');34const replaceUrl = (url, replacer) => {5// regex: https://google.com/ sessionid/ url6return (url || '').replace(/^((?:[a-z0-9]+:\/\/[^/]+)?(?:\/[^/]+\/))([^]+)/i, function (_, g1, g2) {7return g1 + replacer(g2);8});9};1011function patch(url) {12// url = _rhsEPrcb://bqhQko.tHR/13// remove slash14return url.replace(/(^.*?:\/)\//, '$1');15}1617function unpatch(url) {18// url = _rhsEPrcb:/bqhQko.tHR/19// restore slash20return url.replace(/^.*?:\/(?!\/)/, '$&/');21}2223// unshuffle incoming url //24const BUILTIN_HEADERS = require('testcafe-hammerhead/lib/request-pipeline/builtin-header-names');25const _dispatch = RequestPipelineContext.prototype.dispatch;26RequestPipelineContext.prototype.dispatch = function (openSessions) {27let sessionId = getSessionId(this.req.url);28let session = sessionId && openSessions.get(sessionId);29if (!session) {30sessionId = getSessionId(this.req.headers[BUILTIN_HEADERS.referer]);31session = sessionId && openSessions.get(sessionId);32}33if (session && session.shuffleDict) {34const shuffler = new StrShuffler(session.shuffleDict);35this.req.url = replaceUrl(this.req.url, (url) => shuffler.unshuffle(unpatch(url)));36if (getSessionId(this.req.headers[BUILTIN_HEADERS.referer]) === sessionId) {37this.req.headers[BUILTIN_HEADERS.referer] = replaceUrl(this.req.headers[BUILTIN_HEADERS.referer], (url) =>38shuffler.unshuffle(unpatch(url))39);40}41}4243return _dispatch.call(this, openSessions);44};4546// shuffle rewritten proxy urls //47let disableShuffling = false; // for later use48const _toProxyUrl = RequestPipelineContext.prototype.toProxyUrl;49RequestPipelineContext.prototype.toProxyUrl = function (...args) {50const proxyUrl = _toProxyUrl.apply(this, args);5152if (!this.session.shuffleDict || disableShuffling) return proxyUrl;5354const shuffler = new StrShuffler(this.session.shuffleDict);55return replaceUrl(proxyUrl, (url) => patch(shuffler.shuffle(url)));56};5758// unshuffle task.js referer header59const Proxy = require('testcafe-hammerhead/lib/proxy/index');60const __onTaskScriptRequest = Proxy.prototype._onTaskScriptRequest;61Proxy.prototype._onTaskScriptRequest = async function _onTaskScriptRequest(req, ...args) {62const referer = req.headers[BUILTIN_HEADERS.referer];6364const sessionId = getSessionId(referer);65const session = sessionId && this.openSessions.get(sessionId);66if (session && session.shuffleDict) {67const shuffler = new StrShuffler(session.shuffleDict);68req.headers[BUILTIN_HEADERS.referer] = replaceUrl(req.headers[BUILTIN_HEADERS.referer], (url) =>69shuffler.unshuffle(unpatch(url))70);71}72return __onTaskScriptRequest.call(this, req, ...args);73};7475// don't shuffle action urls (because we don't get to control the rewriting when the user submits the form)76const DomProcessor = require('testcafe-hammerhead/lib/processing/dom/index');77const __processUrlAttrs = DomProcessor.prototype._processUrlAttrs;78DomProcessor.prototype._processUrlAttrs = function _processUrlAttrs(el, urlReplacer, pattern) {79try {80disableShuffling = pattern.urlAttr?.toLowerCase() === 'action';81__processUrlAttrs.call(this, el, urlReplacer, pattern);82disableShuffling = false;83} catch (e) {84disableShuffling = false;85throw e;86}87};888990