1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
/*******************************************************************************
uBlock Origin - a comprehensive, efficient content blocker
Copyright (C) 2022-present Raymond Hill
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see {http://www.gnu.org/licenses/}.
Home: https://github.com/gorhill/uBlock
*/
/**
* @trustedOption urlskip
*
* @description
* Extract a URL from another URL according to one or more transformation steps,
* thereby skipping over intermediate network request(s) to remote servers.
* Requires a trusted source.
*
* @param steps
* A serie of space-separated directives representing the transformation steps
* to perform to extract the final URL to which a network request should be
* redirected.
*
* Supported directives:
*
* `?name`: extract the value of parameter `name` as the current string.
*
* `&i`: extract the name of the parameter at position `i` as the current
* string. The position is 1-based.
*
* `#`: extract the hash as the current string.
*
* `/.../`: extract the first capture group of a regex as the current string.
*
* `+https`: prepend the current string with `https://`.
*
* `-base64`: decode the current string as a base64-encoded string.
*
* `-safebase64`: decode the current string as a safe base64-encoded string.
*
* `-uricomponent`: decode the current string as a URI encoded string.
*
* `-blocked`: allow the redirection of blocked requests. By default, blocked
* requests can't by urlskip'ed.
*
* At any given step, the currently extracted string may not necessarily be
* a valid URL, and more transformation steps may be needed to obtain a valid
* URL once all the steps are applied.
*
* An unsupported step or a failed step will abort the transformation and no
* redirection will be performed.
*
* The final step is expected to yield a valid URL. If the result is not a
* valid URL, no redirection will be performed.
*
* @example
* ||example.com/path/to/tracker$urlskip=?url
* ||example.com/path/to/tracker$urlskip=?url ?to
* ||pixiv.net/jump.php?$urlskip=&1
* ||podtrac.com/pts/redirect.mp3/$urlskip=/\/redirect\.mp3\/(.*?\.mp3\b)/ +https
*
* */
export function urlSkip(url, blocked, steps, directive = {}) {
try {
let redirectBlocked = false;
let urlout = url;
for ( const step of steps ) {
const urlin = urlout;
const c0 = step.charCodeAt(0);
// Extract from hash
if ( c0 === 0x23 && step === '#' ) { // #
const pos = urlin.indexOf('#');
urlout = pos !== -1 ? urlin.slice(pos+1) : '';
continue;
}
// Extract from URL parameter name at position i
if ( c0 === 0x26 ) { // &
const i = (parseInt(step.slice(1)) || 0) - 1;
if ( i < 0 ) { return; }
const url = new URL(urlin);
if ( i >= url.searchParams.size ) { return; }
const params = Array.from(url.searchParams.keys());
urlout = decodeURIComponent(params[i]);
continue;
}
// Enforce https
if ( c0 === 0x2B && step === '+https' ) { // +
const s = urlin.replace(/^https?:\/\//, '');
if ( /^[\w-]:\/\//.test(s) ) { return; }
urlout = `https://${s}`;
continue;
}
// Decode
if ( c0 === 0x2D ) { // -
// Base64
if ( step === '-base64' ) {
urlout = self.atob(urlin);
continue;
}
// Safe Base64
if ( step === '-safebase64' ) {
if ( urlSkip.safeBase64Replacer === undefined ) {
urlSkip.safeBase64Map = { '-': '+', '_': '/' };
urlSkip.safeBase64Replacer = s => urlSkip.safeBase64Map[s];
}
urlout = urlin.replace(/[-_]/g, urlSkip.safeBase64Replacer);
urlout = self.atob(urlout);
continue;
}
// URI component
if ( step === '-uricomponent' ) {
urlout = decodeURIComponent(urlin);
continue;
}
// Enable skip of blocked requests
if ( step === '-blocked' ) {
redirectBlocked = true;
continue;
}
}
// Regex extraction from first capture group
if ( c0 === 0x2F ) { // /
const re = directive.cache ?? new RegExp(step.slice(1, -1));
if ( directive.cache === null ) {
directive.cache = re;
}
const match = re.exec(urlin);
if ( match === null ) { return; }
if ( match.length <= 1 ) { return; }
urlout = match[1];
continue;
}
// Extract from URL parameter
if ( c0 === 0x3F ) { // ?
urlout = (new URL(urlin)).searchParams.get(step.slice(1));
if ( urlout === null ) { return; }
if ( urlout.includes(' ') ) {
urlout = urlout.replace(/ /g, '%20');
}
continue;
}
// Unknown directive
return;
}
const urlfinal = new URL(urlout);
if ( urlfinal.protocol !== 'https:' ) {
if ( urlfinal.protocol !== 'http:' ) { return; }
}
if ( blocked && redirectBlocked !== true ) { return; }
return urlout;
} catch {
}
}
|