-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathparental_parser.go
More file actions
249 lines (225 loc) · 6.8 KB
/
parental_parser.go
File metadata and controls
249 lines (225 loc) · 6.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
File: parental_parser.go
Version: 1.3.0
Updated: 11-May-2026 13:55 CEST
Description:
String and byte parsing for Parental Category domain lists.
Extracted from parental_categories.go to isolate formatting constraints.
Changes:
1.3.0 - [SECURITY/FIX] Addressed an IPv4-mapped IPv6 evasion vulnerability natively.
Enforced `.Unmap()` on all parsed `netip.Addr` structures strictly BEFORE
evaluating against `ipVersionSupport` bounds. This prevents clients from
bypassing explicit IPv4 limitations by obfuscating targets inside IPv6 translations.
Removed redundant dead-code branches tracking Adblock IPs to increase throughput.
1.2.0 - [SECURITY/FIX] Fortified blocklist parsers to safely absorb and bypass corrupted lines
exceeding 64KB. `parseSourceBody` now catches `bufio.Scanner` errors gracefully
and returns them to the orchestrator, preventing silent truncation of the remaining payload.
1.1.0 - [FEAT] Integrated `ipVersionSupport` check into `parseCategoryLine`
to selectively ignore IP addresses and subnets that do not match
the filtered IP version setting.
*/
package main
import (
"bufio"
"io"
"net/netip"
"os"
"strings"
)
// ---------------------------------------------------------------------------
// Service-label stripping
// ---------------------------------------------------------------------------
var serviceLabels = map[string]struct{}{
"www": {}, "www2": {}, "www3": {}, "web": {},
"cdn": {}, "static": {}, "assets": {}, "media": {}, "img": {}, "images": {},
"api": {}, "app": {}, "apps": {},
"mail": {}, "smtp": {}, "pop": {}, "pop3": {}, "imap": {}, "mx": {},
"ftp": {}, "sftp": {},
"vpn": {}, "proxy": {}, "gate": {},
"ns": {}, "ns1": {}, "ns2": {}, "ns3": {}, "ns4": {},
"download": {}, "downloads": {}, "update": {}, "updates": {},
"secure": {}, "ssl": {},
"dev": {}, "staging": {}, "stage": {}, "test": {}, "beta": {}, "alpha": {},
"blog": {}, "shop": {}, "store": {}, "portal": {},
}
func stripServiceLabel(d string) (string, bool) {
idx := strings.IndexByte(d, '.')
if idx < 0 {
return "", false
}
label := d[:idx]
rest := d[idx+1:]
if rest == "" {
return "", false
}
if isPublicSuffix(rest) {
return "", false
}
if len(label) == 1 {
return rest, true
}
if _, ok := serviceLabels[label]; ok {
return rest, true
}
return "", false
}
// ---------------------------------------------------------------------------
// Source format helpers
// ---------------------------------------------------------------------------
// isIPOrCIDR safely tests whether a string parses successfully as an IP or Subnet.
func isIPOrCIDR(s string) bool {
if _, err := netip.ParseAddr(s); err == nil {
return true
}
if _, err := netip.ParsePrefix(s); err == nil {
return true
}
return false
}
func parseCategoryLine(line string) []string {
line = strings.TrimSpace(line)
if line == "" || line[0] == '#' || line[0] == '!' {
return nil
}
pseudoHost := func(s string) bool {
switch s {
case "localhost", "local", "broadcasthost",
"ip6-localhost", "ip6-loopback", "ip6-localnet",
"ip6-mcastprefix", "ip6-allnodes", "ip6-allrouters",
"ip6-allhosts", "0.0.0.0", "::", "127.0.0.1", "::1":
return true
}
return false
}
switch {
case strings.HasPrefix(line, "@@||"):
return nil
case strings.HasPrefix(line, "||"):
d := line[2:]
if i := strings.IndexByte(d, '^'); i >= 0 {
d = d[:i]
}
d = strings.TrimPrefix(d, "*.")
if strings.ContainsAny(d, "/*?") {
return nil
}
d = strings.ToLower(strings.TrimSuffix(d, "."))
if d == "" {
return nil
}
// [SECURITY/PERF] Natively ignore IP and CIDR rules supplied in Adblock syntaxes
if _, err := netip.ParseAddr(d); err == nil {
return nil
}
if _, err := netip.ParsePrefix(d); err == nil {
return nil
}
return []string{d}
case len(line) > 0 && (line[0] >= '0' && line[0] <= '9' || line[0] == ':'):
cleanLine := line
if i := strings.IndexByte(cleanLine, '#'); i >= 0 {
cleanLine = strings.TrimSpace(cleanLine[:i])
}
if cleanLine == "" {
return nil
}
fields := strings.Fields(cleanLine)
if len(fields) == 0 {
return nil
}
if len(fields) == 1 {
d := strings.ToLower(strings.TrimSuffix(fields[0], "."))
if d == "" {
return nil
}
// [SECURITY/FIX] Ensure the payload is fully unmapped before evaluating IP version
// configurations to prevent IPv4-in-IPv6 evasion vulnerabilities.
if addr, err := netip.ParseAddr(d); err == nil {
addr = addr.Unmap()
if ipVersionSupport != "both" {
if ipVersionSupport == "ipv4" && !addr.Is4() { return nil }
if ipVersionSupport == "ipv6" && !addr.Is6() { return nil }
}
}
if prefix, err := netip.ParsePrefix(d); err == nil {
// Address underlying prefix address structures natively
prefixAddr := prefix.Addr().Unmap()
if ipVersionSupport != "both" {
if ipVersionSupport == "ipv4" && !prefixAddr.Is4() { return nil }
if ipVersionSupport == "ipv6" && !prefixAddr.Is6() { return nil }
}
}
return []string{d}
}
if addr, err := netip.ParseAddr(fields[0]); err == nil {
addr = addr.Unmap()
if ipVersionSupport != "both" {
if ipVersionSupport == "ipv4" && !addr.Is4() { return nil }
if ipVersionSupport == "ipv6" && !addr.Is6() { return nil }
}
var out []string
for _, f := range fields[1:] {
f = strings.ToLower(strings.TrimSuffix(f, "."))
if f == "" || pseudoHost(f) {
continue
}
if isIPOrCIDR(f) {
continue
}
out = append(out, f)
}
return out
}
var out []string
for _, f := range fields {
f = strings.ToLower(strings.TrimSuffix(f, "."))
if f == "" || pseudoHost(f) {
continue
}
out = append(out, f)
}
return out
default:
if i := strings.IndexByte(line, '#'); i >= 0 {
line = strings.TrimSpace(line[:i])
}
if line == "" || strings.ContainsAny(line, "[]") {
return nil
}
d := strings.ToLower(strings.TrimSuffix(line, "."))
if d == "" {
return nil
}
return []string{d}
}
}
func isSourceURL(s string) bool {
return strings.HasPrefix(s, "http://") || strings.HasPrefix(s, "https://")
}
func parseSourceBody(r io.Reader) ([]string, int, error) {
var lines []string
eTLDs := 0
sc := bufio.NewScanner(r)
// Expand scanner capacity to prevent 'bufio.Scanner: token too long' panics natively
// on heavily concatenated or corrupted external blocklists.
buf := make([]byte, 64*1024)
sc.Buffer(buf, 2*1024*1024)
for sc.Scan() {
domains := parseCategoryLine(sc.Text())
for _, d := range domains {
if !isIPOrCIDR(d) && isPublicSuffix(d) {
eTLDs++
}
lines = append(lines, d)
}
}
return lines, eTLDs, sc.Err()
}
func parseLocalFile(path string) ([]string, int, error) {
f, err := os.Open(path)
if err != nil {
return nil, 0, err
}
defer f.Close()
return parseSourceBody(f)
}