11# Copilot Instructions for Ad-Blocking Repository
22
3+ > 🔒 ** Security First** : All filter compilation includes mandatory validation. See [ Why Validation Matters] ( ../docs/WHY_VALIDATION_MATTERS.md ) for user-friendly explanation.
4+
35## Repository Overview
46
57Multi-language toolkit for ad-blocking and AdGuard DNS management with ** identical output** across all compilers. Key principle: ** Four languages, one schema, same SHA-384 hash** .
68
79** Components** :
8- - ** Filter Rules** : AdGuard filter lists (` rules/adguard_user_filter.txt ` )
10+ - ** Filter Rules** : AdGuard filter lists with input/output separation
11+ - ** Input** : ` data/input/ ` - Local rules and internet source references with hash verification
12+ - ** Output** : ` data/output/adguard_user_filter.txt ` - Final compiled list in adblock format
913- ** API Client** : C# SDK for AdGuard DNS API v1.11 with Polly resilience
1014- ** Rules Compilers** : TypeScript, C#, Python, Rust - all produce identical output
1115- ** ConsoleUI** : Spectre.Console menu-driven interface with DI architecture
@@ -14,12 +18,33 @@ Multi-language toolkit for ad-blocking and AdGuard DNS management with **identic
1418
1519## Architecture Principles
1620
21+ ### Centralized Validation Layer
22+ ** NEW** : Rust-based validation library (` src/adguard-validation/ ` ) provides unified security across all compilers:
23+ - ** At-rest hash verification** : SHA-384 for local files (` data/input/.hashes.json ` database)
24+ - ** In-flight hash verification** : SHA-384 for downloads (prevents MITM)
25+ - ** URL security** : HTTPS enforcement, domain validation, content verification
26+ - ** Syntax validation** : Adblock and hosts format linting
27+ - ** File conflict handling** : Rename, overwrite, or error strategies
28+ - ** Archiving** : Timestamped with manifest tracking
29+
30+ ** Integration Options** :
31+ - ** Rust compiler** : Direct native library usage (zero overhead)
32+ - ** .NET compiler** : P/Invoke to native library or WASM via Wasmtime
33+ - ** Python compiler** : PyO3 bindings for native Rust integration
34+ - ** TypeScript compiler** : WebAssembly module (no Node.js runtime required)
35+
36+ ** Build outputs** :
37+ - ` libadguard_validation.so/.dll/.dylib ` (native libraries)
38+ - ` adguard_validation.wasm ` (WebAssembly module)
39+ - ` adguard-validate ` (CLI tool)
40+
1741### Compiler Equivalence
1842All four compilers (TypeScript, .NET, Python, Rust) wrap ` @adguard/hostlist-compiler ` and ** must** :
1943- Support JSON, YAML, TOML config formats (except PowerShell: JSON only)
2044- Count rules identically (exclude empty lines and ` ! ` /` # ` comments)
2145- Compute SHA-384 hash of output (96 hex chars)
2246- Return same result structure: ` { success, ruleCount, hash, elapsedMs, outputPath } `
47+ - ** Use centralized validation library** for all security checks
2348
2449### ConsoleUI DI Pattern
2550` src/adguard-api-dotnet/src/AdGuard.ConsoleUI/ ` uses service-oriented architecture:
@@ -36,7 +61,10 @@ Program.cs → ConsoleApplication → [DeviceMenu, DnsServerMenu, StatisticsMenu
3661ad-blocking/
3762├── .github/ # GitHub configuration and workflows
3863├── docs/ # Documentation (API docs, guides)
39- ├── rules/ # Filter rules and compilation configs
64+ ├── data/ # Filter rules and compilation data
65+ │ ├── input/ # Source filter lists (local & remote refs)
66+ │ ├── output/ # Compiled filter output
67+ │ └── archive/ # Archived input files (timestamped)
4068├── src/
4169│ ├── adguard-api-dotnet/ # C# AdGuard DNS API client
4270│ ├── rules-compiler-typescript/ # TypeScript rules compiler
@@ -53,16 +81,37 @@ ad-blocking/
5381## Critical Workflows
5482
5583### Compiling Filter Rules (Core Workflow)
56- ** Goal** : Generate ` rules/adguard_user_filter.txt ` with verified output
57-
84+ ** Goal** : Generate ` data/output/adguard_user_filter.txt ` with verified output in adblock format
85+
86+ ** Input Processing** :
87+ 1 . Scan ` data/input/ ` for local filter files (` .txt ` , ` .hosts ` )
88+ 2 . Parse ` internet-sources.txt ` for remote list URLs
89+ 3 . ** Validate URLs for security** :
90+ - Enforce HTTPS only (reject HTTP)
91+ - Validate domain via DNS
92+ - Verify Content-Type is text/plain
93+ - Scan first 1KB for valid filter syntax
94+ - Optional: verify SHA-384 hash (URL format: ` https://...#sha384=hash ` )
95+ 4 . Validate syntax of all sources (adblock/hosts format)
96+ 5 . Compute SHA-384 hashes for integrity verification
97+ 6 . Detect tampering via hash comparison
98+
99+ ** Compilation** :
58100``` bash
59101# TypeScript (primary method)
60102cd src/rules-compiler-typescript && npm run compile
61103
62- # Validates against compiler-config.json , writes output, computes SHA-384
104+ # Validates inputs, fetches remote sources , writes output, computes SHA-384
63105# CI: .github/workflows/typescript.yml runs this with type-checking
64106```
65107
108+ ** Output Guarantees** :
109+ - ✅ Always in adblock syntax (not hosts format)
110+ - ✅ SHA-384 hash computed and verified
111+ - ✅ Rule count validation (excludes comments/empty lines)
112+ - ✅ Deduplication and validation applied
113+ - ✅ URL security verified (HTTPS, domain validation, content checks)
114+
66115** Why four compilers?** Provides language flexibility while ensuring identical output. Use ` docs/compiler-comparison.md ` to choose based on runtime requirements.
67116
68117### API Client Development Pattern
@@ -287,8 +336,8 @@ pwsh -Command "Invoke-ScriptAnalyzer -Path . -Recurse"
287336** Environment variables** :
288337- ` ADGUARD_WEBHOOK_URL ` - Webhook endpoint for notifications
289338- ` ADGUARD_API_KEY ` - AdGuard DNS API authentication
290- - ` LINEAR_API_KEY ` - Linear integration (src/linear/)
291- - ` SECRET_KEY ` - Encryption keys
339+ - ` ADGUARD_LINEAR_API_KEY ` - Linear integration (src/linear/)
340+ - ` DEBUG ` - Enable debug mode (common cross-platform variable)
292341
293342** Configuration priority** (ConsoleUI):
2943431 . ` appsettings.json ` (checked into repo, no secrets)
@@ -302,11 +351,24 @@ pwsh -Command "Invoke-ScriptAnalyzer -Path . -Recurse"
302351- API client uses ** Polly** for rate limiting and retry strategies
303352
304353### Filter Rules Security
305- - Validate sources before adding to ` sources[] ` in config
306- - Test rule changes locally before committing to ` rules/adguard_user_filter.txt `
354+ - ** Input validation** : All files in ` data/input/ ` undergo syntax validation and linting
355+ - ** Hash verification** : SHA-384 hashes computed for all input files to detect tampering
356+ - ** Format enforcement** : Output is always adblock syntax, regardless of input formats
357+ - ** Source tracking** : Maintains provenance of rules from local and internet sources
358+ - ** Syntax validation** : Rules validated before compilation; errors reported with line numbers
359+ - ** Remote source verification** : Internet lists verified via hashes after download
360+ - ** URL security validation** : Comprehensive security checks for internet sources
361+ - HTTPS enforcement (HTTP rejected)
362+ - Domain validation via DNS
363+ - Content-Type verification (text/plain required)
364+ - Content scanning for valid filter syntax
365+ - Optional SHA-384 hash verification (add ` #sha384=hash ` to URL)
366+ - File size limits to prevent abuse
367+ - Test rule changes locally before committing to ` data/output/adguard_user_filter.txt `
307368- Rules deployed to AdGuard DNS affect real traffic filtering
308369- Be cautious when adding rules from untrusted sources
309370- Validate and test new filter rules before deployment
371+ - Only use trusted, well-known sources for internet lists
310372
311373## Rules Compilation Standards
312374
@@ -343,7 +405,7 @@ All compilers return/output:
343405Supports 3 formats (JSON/YAML/TOML), mirrors ` @adguard/hostlist-compiler ` :
344406``` json
345407{
346- "output" : " rules /adguard_user_filter.txt" ,
408+ "output" : " data/output /adguard_user_filter.txt" ,
347409 "sources" : [
348410 { "url" : " https://example.com/list.txt" , "transformations" : [" RemoveComments" ] }
349411 ],
@@ -450,7 +512,7 @@ cd src/rules-compiler-rust && cargo test
450512
451513| File/Folder | Purpose | When to Modify |
452514| -------------| ---------| ----------------|
453- | ` rules /adguard_user_filter.txt` | ** Production filter list** | After successful compilation and testing |
515+ | ` data/output /adguard_user_filter.txt` | ** Production filter list** | After successful compilation and testing |
454516| ` src/rules-compiler-typescript/compiler-config.json ` | ** Primary config** for rule compilation | To change filter sources or transformations |
455517| ` api/openapi.yaml ` | AdGuard DNS API spec (v1.11) | Never (upstream dependency) |
456518| ` src/adguard-api-dotnet/src/AdGuard.ApiClient/ ` | ** Auto-generated** API client | Never (regenerate from spec instead) |
@@ -490,9 +552,9 @@ npm install
490552npm run build
491553
492554# Configure .env (never commit this!)
493- LINEAR_API_KEY =lin_api_your_key_here
494- LINEAR_TEAM_ID =optional_team_id
495- LINEAR_PROJECT_NAME =Ad-Blocking Documentation
555+ ADGUARD_LINEAR_API_KEY =lin_api_your_key_here
556+ ADGUARD_LINEAR_TEAM_ID =optional_team_id
557+ ADGUARD_LINEAR_PROJECT_NAME =Ad-Blocking Documentation
496558```
497559
498560### Usage
0 commit comments