Skip to content

Commit abaa961

Browse files
committed
2 parents 0abc765 + 6521724 commit abaa961

275 files changed

Lines changed: 31708 additions & 4966 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.cargo/config.toml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
[build]
2+
# Uncomment to limit build jobs (0 means auto-detect, but not allowed in some contexts)
3+
# jobs = 4
4+
5+
[term]
6+
# Use colors in cargo output
7+
color = "auto"
8+
verbose = false
9+
10+
[net]
11+
# Use git CLI for git operations (more reliable in CI)
12+
git-fetch-with-cli = true
13+
14+
[target.'cfg(all())']
15+
# Use LLD linker on Linux for faster builds (if available)
16+
# rustflags = ["-C", "link-arg=-fuse-ld=lld"]
17+
18+
# Increase debug info for better profiling while keeping builds fast
19+
[profile.dev]
20+
debug = 1
21+
22+
[profile.release]
23+
# Generate debug symbols but strip them from the final binary
24+
debug = false
25+
26+
# Additional optimization settings for release builds
27+
[profile.release.build-override]
28+
opt-level = 0
29+
debug = false

.env.example

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,35 @@
66
# This file is tracked in git as an example. Your .env file is gitignored.
77
# =============================================================================
88

9-
# Debug mode (set to any value to enable)
9+
# =============================================================================
10+
# Debug Configuration
11+
# =============================================================================
12+
# Enable debug mode (set to 1, true, or any non-empty value to enable)
1013
DEBUG=
1114

1215
# =============================================================================
1316
# AdGuard DNS API Configuration
1417
# =============================================================================
15-
# Use ONE of the following formats (ADGUARD_AdGuard__ApiKey is recommended
16-
# for cross-platform compatibility across .NET, TypeScript, Rust, etc.):
17-
#
18-
# Recommended (cross-platform format - works with C#, TypeScript, Rust CLI, and others):
19-
ADGUARD_AdGuard__ApiKey=
20-
#
21-
# Alternative (TypeScript/JavaScript fallback for existing configs; new projects
22-
# should prefer ADGUARD_AdGuard__ApiKey, but the TypeScript client supports both):
23-
# ADGUARD_API_KEY=
24-
#
25-
# Legacy (Rust CLI only, deprecated):
26-
# ADGUARD_API_TOKEN=
18+
# API Key for AdGuard DNS (required for API access)
19+
ADGUARD_API_KEY=
20+
21+
# API Base URL (optional, defaults to https://api.adguard-dns.io)
22+
ADGUARD_API_BASE_URL=
23+
24+
# =============================================================================
25+
# AdGuard Webhook Configuration
26+
# =============================================================================
27+
# Webhook endpoint URL for notifications and updates
28+
ADGUARD_WEBHOOK_URL=
2729

2830
# =============================================================================
2931
# Linear Integration (for Linear import scripts in src/linear/)
3032
# =============================================================================
31-
LINEAR_API_KEY=
33+
# Linear API key for project management integration
34+
ADGUARD_LINEAR_API_KEY=
35+
36+
# Linear team ID (optional, defaults to first team)
37+
ADGUARD_LINEAR_TEAM_ID=
38+
39+
# Linear project name (optional)
40+
ADGUARD_LINEAR_PROJECT_NAME=

.envrc.example

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,35 @@
66
# This file is tracked in git as an example. Your .env file is gitignored.
77
# =============================================================================
88

9-
# Debug mode (set to any value to enable)
9+
# =============================================================================
10+
# Debug Configuration
11+
# =============================================================================
12+
# Enable debug mode (set to 1, true, or any non-empty value to enable)
1013
DEBUG=
1114

1215
# =============================================================================
1316
# AdGuard DNS API Configuration
1417
# =============================================================================
15-
# Use ONE of the following formats (ADGUARD_AdGuard__ApiKey is recommended
16-
# for cross-platform compatibility across .NET, TypeScript, Rust, etc.):
17-
#
18-
# Recommended (cross-platform format - works with C#, TypeScript, Rust CLI, and others):
19-
ADGUARD_AdGuard__ApiKey=
20-
#
21-
# Alternative (TypeScript/JavaScript fallback for existing configs; new projects
22-
# should prefer ADGUARD_AdGuard__ApiKey, but the TypeScript client supports both):
23-
# ADGUARD_API_KEY=
24-
#
25-
# Legacy (Rust CLI only, deprecated):
26-
# ADGUARD_API_TOKEN=
18+
# API Key for AdGuard DNS (required for API access)
19+
ADGUARD_API_KEY=
20+
21+
# API Base URL (optional, defaults to https://api.adguard-dns.io)
22+
ADGUARD_API_BASE_URL=
23+
24+
# =============================================================================
25+
# AdGuard Webhook Configuration
26+
# =============================================================================
27+
# Webhook endpoint URL for notifications and updates
28+
ADGUARD_WEBHOOK_URL=
2729

2830
# =============================================================================
2931
# Linear Integration (for Linear import scripts in src/linear/)
3032
# =============================================================================
31-
LINEAR_API_KEY=
33+
# Linear API key for project management integration
34+
ADGUARD_LINEAR_API_KEY=
35+
36+
# Linear team ID (optional, defaults to first team)
37+
ADGUARD_LINEAR_TEAM_ID=
38+
39+
# Linear project name (optional)
40+
ADGUARD_LINEAR_PROJECT_NAME=

.github/copilot-instructions.md

Lines changed: 76 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
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

57
Multi-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
1842
All 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
3661
ad-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)
60102
cd 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):
294343
1. `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:
343405
Supports 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
490552
npm 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

Comments
 (0)