Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
QuiteAFancyEmerald
GitHub Repository: QuiteAFancyEmerald/Holy-Unblocker
Path: blob/master/lib/rammerhead/src/util/addUrlShuffling.js
6530 views
1
const RequestPipelineContext = require('testcafe-hammerhead/lib/request-pipeline/context');
2
const StrShuffler = require('./StrShuffler');
3
const getSessionId = require('./getSessionId');
4
5
const replaceUrl = (url, replacer) => {
6
// regex: https://google.com/ sessionid/ url
7
return (url || '').replace(/^((?:[a-z0-9]+:\/\/[^/]+)?(?:\/[^/]+\/))([^]+)/i, function (_, g1, g2) {
8
return g1 + replacer(g2);
9
});
10
};
11
12
function patch(url) {
13
// url = _rhsEPrcb://bqhQko.tHR/
14
// remove slash
15
return url.replace(/(^.*?:\/)\//, '$1');
16
}
17
18
function unpatch(url) {
19
// url = _rhsEPrcb:/bqhQko.tHR/
20
// restore slash
21
return url.replace(/^.*?:\/(?!\/)/, '$&/');
22
}
23
24
// unshuffle incoming url //
25
const BUILTIN_HEADERS = require('testcafe-hammerhead/lib/request-pipeline/builtin-header-names');
26
const _dispatch = RequestPipelineContext.prototype.dispatch;
27
RequestPipelineContext.prototype.dispatch = function (openSessions) {
28
let sessionId = getSessionId(this.req.url);
29
let session = sessionId && openSessions.get(sessionId);
30
if (!session) {
31
sessionId = getSessionId(this.req.headers[BUILTIN_HEADERS.referer]);
32
session = sessionId && openSessions.get(sessionId);
33
}
34
if (session && session.shuffleDict) {
35
const shuffler = new StrShuffler(session.shuffleDict);
36
this.req.url = replaceUrl(this.req.url, (url) => shuffler.unshuffle(unpatch(url)));
37
if (getSessionId(this.req.headers[BUILTIN_HEADERS.referer]) === sessionId) {
38
this.req.headers[BUILTIN_HEADERS.referer] = replaceUrl(this.req.headers[BUILTIN_HEADERS.referer], (url) =>
39
shuffler.unshuffle(unpatch(url))
40
);
41
}
42
}
43
44
return _dispatch.call(this, openSessions);
45
};
46
47
// shuffle rewritten proxy urls //
48
let disableShuffling = false; // for later use
49
const _toProxyUrl = RequestPipelineContext.prototype.toProxyUrl;
50
RequestPipelineContext.prototype.toProxyUrl = function (...args) {
51
const proxyUrl = _toProxyUrl.apply(this, args);
52
53
if (!this.session.shuffleDict || disableShuffling) return proxyUrl;
54
55
const shuffler = new StrShuffler(this.session.shuffleDict);
56
return replaceUrl(proxyUrl, (url) => patch(shuffler.shuffle(url)));
57
};
58
59
// unshuffle task.js referer header
60
const Proxy = require('testcafe-hammerhead/lib/proxy/index');
61
const __onTaskScriptRequest = Proxy.prototype._onTaskScriptRequest;
62
Proxy.prototype._onTaskScriptRequest = async function _onTaskScriptRequest(req, ...args) {
63
const referer = req.headers[BUILTIN_HEADERS.referer];
64
65
const sessionId = getSessionId(referer);
66
const session = sessionId && this.openSessions.get(sessionId);
67
if (session && session.shuffleDict) {
68
const shuffler = new StrShuffler(session.shuffleDict);
69
req.headers[BUILTIN_HEADERS.referer] = replaceUrl(req.headers[BUILTIN_HEADERS.referer], (url) =>
70
shuffler.unshuffle(unpatch(url))
71
);
72
}
73
return __onTaskScriptRequest.call(this, req, ...args);
74
};
75
76
// don't shuffle action urls (because we don't get to control the rewriting when the user submits the form)
77
const DomProcessor = require('testcafe-hammerhead/lib/processing/dom/index');
78
const __processUrlAttrs = DomProcessor.prototype._processUrlAttrs;
79
DomProcessor.prototype._processUrlAttrs = function _processUrlAttrs(el, urlReplacer, pattern) {
80
try {
81
disableShuffling = pattern.urlAttr?.toLowerCase() === 'action';
82
__processUrlAttrs.call(this, el, urlReplacer, pattern);
83
disableShuffling = false;
84
} catch (e) {
85
disableShuffling = false;
86
throw e;
87
}
88
};
89
90