Skip to content

Commit 5683f53

Browse files
Update dependency adblock-rs to ^0.12.0 (#124)
* Update dependency adblock-rs to ^0.12.0 * fix path, dnf * fix path, allowScripts * renamed engine.serializeRaw * fix invalid hostnames * increase EphemeralStorage * await browser.close for datadir cleanup --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: mschfh <37435502+mschfh@users.noreply.github.com>
1 parent b723db1 commit 5683f53

8 files changed

Lines changed: 54 additions & 20 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ bundle:
4343
rm -rf $(TMP_WORKSPACE)/node_modules/core-js/client
4444
rm -rf $(TMP_WORKSPACE)/node_modules/core-js/stage
4545
rm -rf $(TMP_WORKSPACE)/node_modules/nan
46-
find $(TMP_WORKSPACE)/node_modules/adblock-rs/js/target/release/ -type f -not -name libadblock_rs.so -delete
46+
find $(TMP_WORKSPACE)/node_modules/adblock-rs/target/release/ -mindepth 1 -not -name index.js -not -name index.node -exec rm -rf {} +
4747
find $(TMP_WORKSPACE)/node_modules -type f -name "*.md" -delete
4848
find $(TMP_WORKSPACE)/node_modules -type d -name "test" | xargs rm -rf
4949
cd $(TMP_WORKSPACE)/ && zip -r $(FUNCTION_NAME).zip *

brave/adblock.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ const adblockRsLib = require('adblock-rs')
99

1010
const braveDebugLib = require('./debug')
1111

12+
// Non-network request URL schemes that adblock-rs can't parse a hostname from
13+
// (Request::new returns HostnameParseError). These are browser-internal or inline
14+
// resources, not real network requests, and show up routinely in crawl data. Drop
15+
// them so they don't abort the batch; anything else that fails to parse is
16+
// unexpected and should surface (with its value) so we can narrow it down.
17+
const NON_NETWORK_SCHEME_RE = /^(?:data|blob|about|javascript):/i
18+
1219
const serializeRules = rules => {
1320
braveDebugLib.verbose(`Serializing ${rules.length} rules`)
1421
const filterSet = new adblockRsLib.FilterSet(true)
@@ -17,7 +24,7 @@ const serializeRules = rules => {
1724
optimize: false
1825
}
1926
const adBlockClient = new adblockRsLib.Engine(filterSet, adBlockArgs)
20-
const adBlockDat = adBlockClient.serializeRaw()
27+
const adBlockDat = adBlockClient.serialize()
2128
const adBlockDatBuffer = Buffer.from(adBlockDat)
2229
braveDebugLib.verbose(`Successfully serialized rules into buffer of length ${adBlockDatBuffer.byteLength}`)
2330
return adBlockDatBuffer
@@ -40,7 +47,22 @@ const applyBlockingRules = (adblockClient, requests) => {
4047
const requestType = aReport[4]
4148
const requestUrl = aReport[5]
4249

43-
const matchResult = adblockClient.check(requestUrl, frameUrl, requestType, true)
50+
// Drop known non-network schemes up front: adblock-rs would throw
51+
// "hostname parsing failed" on these, and they aren't real requests to block.
52+
if (NON_NETWORK_SCHEME_RE.test(requestUrl)) {
53+
braveDebugLib.verbose(`Dropping non-network request ${requestUrl} in frame ${frameUrl}`)
54+
continue
55+
}
56+
57+
let matchResult
58+
try {
59+
matchResult = adblockClient.check(requestUrl, frameUrl, requestType, true)
60+
} catch (err) {
61+
// Not a known non-network scheme, so this parse failure is unexpected.
62+
// Re-throw with the offending values attached so Sentry shows what triggered
63+
// it (the bare adblock-rs message doesn't include the URL).
64+
throw new Error(`adblock check failed for requestUrl=${JSON.stringify(requestUrl)} frameUrl=${JSON.stringify(frameUrl)} requestType=${JSON.stringify(requestType)}: ${err.message}`, { cause: err })
65+
}
4466
if (matchResult.matched === false) {
4567
if (matchResult.exception) {
4668
braveDebugLib.verbose(`Would block ${requestUrl} in frame ${frameUrl} of type ${requestType} with rule ${matchResult.filter} but excepted by ${matchResult.exception}`)

brave/lambda_actions/assemble.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ const convertRules = (rules, format) => {
173173
const iosFilterSet = new FilterSet(true)
174174
iosFilterSet.addFilters(filtersUsed)
175175
const engine = new Engine(iosFilterSet, { optimize: false })
176-
const iosDat = engine.serializeRaw()
176+
const iosDat = engine.serialize()
177177
const datBuffer = Buffer.from(iosDat)
178178

179179
if (datBuffer.byteLength === 0) {

brave/lambda_actions/crawl.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -317,14 +317,16 @@ const start = async args => {
317317
}
318318

319319
braveDebugLib.verbose('Wrapping up and closing puppeteer.')
320+
// browser.close() can hang when Chromium wedges, so cap how long we block on it.
321+
// The timeout only stops us waiting; it does not cancel close(), which keeps
322+
// running in the background (along with puppeteer's own temp userDataDir cleanup),
323+
// so if the environment is reused the shutdown can still finish.
320324
try {
321-
browser.close()
325+
await Promise.race([
326+
browser.close(),
327+
new Promise(resolve => setTimeout(resolve, 15000))
328+
])
322329
} catch (_) {}
323-
324-
// We can't wait for the browser to always close cleanly, because it often
325-
// will hang indefinitely. So we just issue the request to close and wait
326-
// 10 sec.
327-
setTimeout(_ => {}, 10000)
328330
}
329331

330332
module.exports = {

build.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
#!/bin/bash
22

3+
set -euo pipefail
4+
35
# This script runs in the docker container, to set up the rust toolchain
46
# needed to build the adblock-rs node module.
57

6-
yum install -y openssl-devel nss;
8+
dnf install -y openssl-devel nss;
79
curl https://sh.rustup.rs -sSf > /tmp/sh.rustup.rs;
810
sh /tmp/sh.rustup.rs -y;
911
source ~/.cargo/env;

package-lock.json

Lines changed: 8 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"@aws-sdk/client-s3": "3.1071.0",
2323
"@aws-sdk/client-sqs": "3.1071.0",
2424
"@sparticuz/chromium": "^148.0.0",
25-
"adblock-rs": "^0.7.18",
25+
"adblock-rs": "^0.12.0",
2626
"fkill": "^6.2.0",
2727
"fs-extra": "^8.1.0",
2828
"glob": "^7.1.4",
@@ -36,5 +36,8 @@
3636
},
3737
"devDependencies": {
3838
"standard": "17.1.2"
39+
},
40+
"allowScripts": {
41+
"adblock-rs": true
3942
}
40-
}
43+
}

slim-list-lambdas.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,10 @@ Resources:
255255
CodeUri: ./build/slim-list-generator.zip
256256
Timeout: 600
257257
MemorySize: 2048
258+
# Chromium (via @sparticuz/chromium) is extracted into /tmp and puppeteer
259+
# creates a profile dir there too; the 512 MB default overflows (ENOSPC).
260+
EphemeralStorage:
261+
Size: 2048
258262
Layers:
259263
- !Sub "arn:aws:lambda:${AWS::Region}:${SentryNodeLayerArnTemplate}"
260264
Environment:

0 commit comments

Comments
 (0)