-
-
Notifications
You must be signed in to change notification settings - Fork 166
feat(safari): strip DNR rules WebKit cannot compile #3280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
79e73e1
feat(safari): strip DNR rules WebKit cannot compile
chrmod 68b845e
refactor(safari): use import.meta.dirname in dnr validator scripts
chrmod f90a02c
refactor(safari): align dnr validator scripts with repo patterns
chrmod c088bc3
refactor(safari): download dnr validator on demand, drop postinstall …
chrmod File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /** | ||
| * Ghostery Browser Extension | ||
| * https://www.ghostery.com/ | ||
| * | ||
| * Copyright 2017-present Ghostery GmbH. All rights reserved. | ||
| * | ||
| * This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0 | ||
| */ | ||
|
|
||
| import { existsSync, mkdirSync, writeFileSync, chmodSync } from 'node:fs'; | ||
| import { resolve } from 'node:path'; | ||
|
|
||
| const PLATFORM_MAP = { | ||
| 'darwin-arm64': 'macos-arm64', | ||
| 'darwin-x64': 'macos-arm64', | ||
| 'linux-x64': 'linux-x64', | ||
| }; | ||
|
|
||
| const binDir = resolve(import.meta.dirname, 'bin'); | ||
| const binPath = resolve(binDir, 'validate-dnr-rules'); | ||
|
|
||
| const key = `${process.platform}-${process.arch}`; | ||
| const suffix = PLATFORM_MAP[key]; | ||
|
|
||
| if (!suffix) { | ||
| console.log(`[validate-dnr-rules] Skipping download: no binary available for ${key}.`); | ||
| process.exit(0); | ||
| } | ||
|
|
||
| if (existsSync(binPath)) { | ||
| process.exit(0); | ||
| } | ||
|
|
||
| const url = `https://github.com/ghostery/WebKit/releases/latest/download/validate-dnr-rules-${suffix}`; | ||
|
|
||
| console.log(`[validate-dnr-rules] Downloading ${url}`); | ||
|
|
||
| try { | ||
| const res = await fetch(url); | ||
| if (!res.ok) { | ||
| throw new Error(`HTTP ${res.status} ${res.statusText}`); | ||
| } | ||
| mkdirSync(binDir, { recursive: true }); | ||
| writeFileSync(binPath, new Uint8Array(await res.arrayBuffer())); | ||
| chmodSync(binPath, 0o755); | ||
| console.log(`[validate-dnr-rules] Saved to ${binPath}`); | ||
| } catch (err) { | ||
| console.warn( | ||
| `[validate-dnr-rules] Download failed: ${err.message}. Safari builds will not filter invalid DNR rules until this succeeds.`, | ||
| ); | ||
| process.exit(0); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /** | ||
| * Ghostery Browser Extension | ||
| * https://www.ghostery.com/ | ||
| * | ||
| * Copyright 2017-present Ghostery GmbH. All rights reserved. | ||
| * | ||
| * This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0 | ||
| */ | ||
|
|
||
| import { existsSync, readdirSync, readFileSync, writeFileSync } from 'node:fs'; | ||
| import { join, relative, resolve } from 'node:path'; | ||
| import { spawnSync } from 'node:child_process'; | ||
|
|
||
| const binPath = resolve(import.meta.dirname, 'bin', 'validate-dnr-rules'); | ||
| const rulesDir = resolve(process.cwd(), 'dist', 'rule_resources'); | ||
|
|
||
| if (!existsSync(binPath)) { | ||
| console.error( | ||
| `[filter-invalid-dnr-rules] Validator binary not found at ${binPath}.\n` + | ||
| `Run 'npm install' to download it from https://github.com/ghostery/WebKit/releases.`, | ||
| ); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| if (!existsSync(rulesDir)) { | ||
| console.error(`[filter-invalid-dnr-rules] Rules directory not found: ${rulesDir}`); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| const files = readdirSync(rulesDir) | ||
| .filter((f) => f.startsWith('dnr-') && f.endsWith('.json') && !f.endsWith('.metadata.json')) | ||
| .map((f) => join(rulesDir, f)); | ||
|
|
||
| const ERROR_RE = /^\s*ERROR: Rule (-?\d+)/gm; | ||
|
|
||
| let totalRemoved = 0; | ||
|
|
||
| for (const file of files) { | ||
| const result = spawnSync(binPath, [file], { | ||
| encoding: 'utf8', | ||
| maxBuffer: 256 * 1024 * 1024, | ||
| }); | ||
|
|
||
| if (result.error) { | ||
| console.error(`[filter-invalid-dnr-rules] Failed to spawn validator: ${result.error.message}`); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| if (result.status === 0) { | ||
| continue; | ||
| } | ||
|
|
||
| const invalidIds = new Set(); | ||
| for (const m of result.stdout.matchAll(ERROR_RE)) { | ||
| invalidIds.add(Number(m[1])); | ||
| } | ||
|
|
||
| if (invalidIds.size === 0) { | ||
| console.error( | ||
| `[filter-invalid-dnr-rules] Validator failed for ${file} but produced no parseable errors:\n${result.stdout}${result.stderr}`, | ||
| ); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| const rules = JSON.parse(readFileSync(file, 'utf8')); | ||
| const filtered = rules.filter((r) => !invalidIds.has(r.id)); | ||
| const removed = rules.length - filtered.length; | ||
|
|
||
| writeFileSync(file, JSON.stringify(filtered)); | ||
| totalRemoved += removed; | ||
|
|
||
| console.log( | ||
| `[filter-invalid-dnr-rules] ${relative(process.cwd(), file)}: removed ${removed}/${rules.length} invalid rule(s)`, | ||
| ); | ||
| } | ||
|
|
||
| console.log( | ||
| `[filter-invalid-dnr-rules] Removed ${totalRemoved} rule(s) across ${files.length} ruleset(s).`, | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.