Skip to content

Commit

Permalink
Updating blocklist functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
leppert committed Mar 24, 2023
1 parent 6106b34 commit 8bbc43b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 46 deletions.
2 changes: 1 addition & 1 deletion Scoop.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export class Scoop {
* osName: ?string,
* osVersion: ?string,
* cpuArchitecture: ?string,
* blockedRequests: Array.<{url: string, ip: string, rule: string}>,
* blockedRequests: Array.<{match: string, rule: string}>,
* noArchiveUrls: string[],
* certificates: Array.<{host: string, pem: string}>,
* ytDlpHash: string,
Expand Down
6 changes: 2 additions & 4 deletions assets/templates/provenance-summary.njk
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,14 @@
<table>
<thead>
<tr>
<th>URL</th>
<th>IP</th>
<th>Match</th>
<th>Blocklist Rule</th>
</tr>
</thead>
<tbody>
{% for block in blockedRequests %}
<tr>
<td>{{ block.url }}</td>
<td>{{ block.ip }}</td>
<td>{{ block.match }}</td>
<td>{{ block.rule }}</td>
</tr>
{% endfor %}
Expand Down
51 changes: 10 additions & 41 deletions intercepters/ScoopProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,19 @@ export class ScoopProxy extends ScoopIntercepter {
}

onRequest (request) {
if (this.recordExchanges && !this.urlFoundInBlocklist(request)) {
const exchange = new ScoopProxyExchange({ requestParsed: request })
this.exchanges.push(exchange)
const url = request.url.startsWith('/')
? `https://${request.headers.host}${request.url}`
: request.url
if (this.recordExchanges && !this.foundInBlocklist(url, request)) {
this.exchanges.push(new ScoopProxyExchange({ requestParsed: request }))
this.cacheBody(request)
}
}

onResponse (response, request) {
// there will not be an exchange with this request if we're, for instance, not recording
const exchange = this.exchanges.find(ex => ex.requestParsed === request)
if (exchange && !this.ipFoundInBlocklist(request)) {
if (exchange && !this.foundInBlocklist(response.socket.remoteAddress, request)) {
exchange.responseParsed = response
response.on('end', () => this.checkExchangeForNoArchive(exchange))
this.cacheBody(response)
Expand Down Expand Up @@ -97,57 +99,24 @@ export class ScoopProxy extends ScoopIntercepter {
* @param {ScoopProxyExchange} exchange
* @returns {boolean} - `true` if request was interrupted
*/
urlFoundInBlocklist (request) {
return false
const url = request.url.startsWith('/')
? `https://${request.headers.host}${request.url}`
: request.url

foundInBlocklist (toMatch, request) {
// Search for a blocklist match:
// Use the index to pull the original un-parsed rule from options so that the printing matches user expectations
const ruleIndex = this.capture.blocklist.findIndex(searchBlocklistFor(url))
const ruleIndex = this.capture.blocklist.findIndex(searchBlocklistFor(toMatch))

if (ruleIndex === -1) {
return false
}

const rule = this.capture.options.blocklist[ruleIndex]
this.capture.log.warn(`Blocking ${url} matching rule ${rule}`)
this.capture.provenanceInfo.blockedRequests.push({ url, rule })
this.capture.log.warn(`Blocking ${toMatch} matching rule ${rule}`)
this.capture.provenanceInfo.blockedRequests.push({ match: toMatch, rule })

request.socket.destroy()

return true
}

/**
* Checks an outgoing request against the blocklist. Interrupts the request it needed.
* Keeps trace of blocked requests in `Scoop.provenanceInfo`.
*
* @param {ScoopProxyExchange} exchange
* @returns {boolean} - `true` if request was interrupted
*/
ipFoundInBlocklist (response) {
return false
const ip = response.socket.remoteAddress

// Search for a blocklist match:
// Use the index to pull the original un-parsed rule from options so that the printing matches user expectations
const ruleIndex = this.capture.blocklist.findIndex(searchBlocklistFor(ip))

if (ruleIndex === -1) {
return false
}

const rule = this.capture.options.blocklist[ruleIndex]
this.capture.log.warn(`Blocking IP ${ip} matching rule ${rule}`)
this.capture.provenanceInfo.blockedRequests.push({ ip, rule })

response.socket.destroy()

return true
}

requestTransformer (request) {
return new Transform({
transform: (chunk, _encoding, callback) => {
Expand Down

0 comments on commit 8bbc43b

Please sign in to comment.