prevent-fetch.js (7338B) - View raw
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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221/******************************************************************************* uBlock Origin - a comprehensive, efficient content blocker Copyright (C) 2019-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 */ import { generateContentFn } from './utils.js'; import { proxyApplyFn } from './proxy-apply.js'; import { registerScriptlet } from './base.js'; import { safeSelf } from './safe-self.js'; /******************************************************************************/ function preventFetchFn( trusted = false, propsToMatch = '', responseBody = '', responseType = '' ) { const safe = safeSelf(); const setTimeout = self.setTimeout; const scriptletName = `${trusted ? 'trusted-' : ''}prevent-fetch`; const logPrefix = safe.makeLogPrefix( scriptletName, propsToMatch, responseBody, responseType ); const extraArgs = safe.getExtraArgs(Array.from(arguments), 4); const needles = []; for ( const condition of safe.String_split.call(propsToMatch, /\s+/) ) { if ( condition === '' ) { continue; } const pos = condition.indexOf(':'); let key, value; if ( pos !== -1 ) { key = condition.slice(0, pos); value = condition.slice(pos + 1); } else { key = 'url'; value = condition; } needles.push({ key, pattern: safe.initPattern(value, { canNegate: true }) }); } const validResponseProps = { ok: [ false, true ], statusText: [ '', 'Not Found' ], type: [ 'basic', 'cors', 'default', 'error', 'opaque' ], }; const responseProps = { statusText: { value: 'OK' }, }; if ( /^\{.*\}$/.test(responseType) ) { try { Object.entries(JSON.parse(responseType)).forEach(([ p, v ]) => { if ( validResponseProps[p] === undefined ) { return; } if ( validResponseProps[p].includes(v) === false ) { return; } responseProps[p] = { value: v }; }); } catch { } } else if ( responseType !== '' ) { if ( validResponseProps.type.includes(responseType) ) { responseProps.type = { value: responseType }; } } proxyApplyFn('fetch', function fetch(context) { const { callArgs } = context; const details = callArgs[0] instanceof self.Request ? callArgs[0] : Object.assign({ url: callArgs[0] }, callArgs[1]); let proceed = true; try { const props = new Map(); for ( const prop in details ) { let v = details[prop]; if ( typeof v !== 'string' ) { try { v = safe.JSON_stringify(v); } catch { } } if ( typeof v !== 'string' ) { continue; } props.set(prop, v); } if ( safe.logLevel > 1 || propsToMatch === '' && responseBody === '' ) { const out = Array.from(props).map(a => `${a[0]}:${a[1]}`); safe.uboLog(logPrefix, `Called: ${out.join('\n')}`); } if ( propsToMatch === '' && responseBody === '' ) { return context.reflect(); } proceed = needles.length === 0; for ( const { key, pattern } of needles ) { if ( pattern.expect && props.has(key) === false || safe.testPattern(pattern, props.get(key)) === false ) { proceed = true; break; } } } catch { } if ( proceed ) { return context.reflect(); } return Promise.resolve(generateContentFn(trusted, responseBody)).then(text => { safe.uboLog(logPrefix, `Prevented with response "${text}"`); const response = new Response(text, { headers: { 'Content-Length': text.length, } }); const props = Object.assign( { url: { value: details.url } }, responseProps ); safe.Object_defineProperties(response, props); if ( extraArgs.throttle ) { return new Promise(resolve => { setTimeout(( ) => { resolve(response); }, extraArgs.throttle); }); } return response; }); }); } registerScriptlet(preventFetchFn, { name: 'prevent-fetch.fn', dependencies: [ generateContentFn, proxyApplyFn, safeSelf, ], }); /******************************************************************************/ /** * @scriptlet prevent-fetch * * @description * Prevent a fetch() call from making a network request to a remote server. * * @param propsToMatch * The fetch arguments to match for the prevention to be triggered. The * untrusted flavor limits the realm of response to return to safe values. * * @param [responseBody] * Optional. The reponse to return when the prevention occurs. * * @param [responseType] * Optional. The response type to use when emitting a dummy response as a * result of the prevention. * * @param [...varargs] * ["throttle", n]: the time to wait in ms before returning a result. * * */ function preventFetch(...args) { preventFetchFn(false, ...args); } registerScriptlet(preventFetch, { name: 'prevent-fetch.js', aliases: [ 'no-fetch-if.js', ], dependencies: [ preventFetchFn, ], }); /******************************************************************************/ /** * @scriptlet trusted-prevent-fetch * * @description * Prevent a fetch() call from making a network request to a remote server. * * @param propsToMatch * The fetch arguments to match for the prevention to be triggered. * * @param [responseBody] * Optional. The reponse to return when the prevention occurs. The trusted * flavor allows to return any response. * * @param [responseType] * Optional. The response type to use when emitting a dummy response as a * result of the prevention. * * @param [...varargs] * ["throttle", n]: the time to wait in ms before returning a result. * * */ function trustedPreventFetch(...args) { preventFetchFn(true, ...args); } registerScriptlet(trustedPreventFetch, { name: 'trusted-prevent-fetch.js', requiresTrust: true, dependencies: [ preventFetchFn, ], }); /******************************************************************************/