Skip to content

Commit da12890

Browse files
committed
correct ASN/UA/IP blocks comments; fix blank UA blocking; ensure global blocks occurr before any other action is taken;
1 parent 4d2faed commit da12890

3 files changed

Lines changed: 227 additions & 139 deletions

File tree

internal/handlers/security/handler.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -615,10 +615,22 @@ func importSecurityCSV(r *http.Request) ([][]string, error) {
615615
return records, nil
616616
}
617617

618+
// stripInlineComment removes a trailing "#"-style comment from a rule line and
619+
// trims the remainder. The marker only counts at the start of the line or when
620+
// preceded by whitespace, so a "#" embedded mid-token is left alone.
621+
func stripInlineComment(line string) string {
622+
for i := 0; i < len(line); i++ {
623+
if line[i] == '#' && (i == 0 || line[i-1] == ' ' || line[i-1] == '\t') {
624+
return strings.TrimSpace(line[:i])
625+
}
626+
}
627+
return line
628+
}
629+
618630
func parseIPRules(raw string, listType int) []db.IPRule {
619631
var out []db.IPRule
620632
for _, line := range strings.Split(raw, "\n") {
621-
line = strings.TrimSpace(line)
633+
line = stripInlineComment(strings.TrimSpace(line))
622634
if line == "" || strings.HasPrefix(line, "#") {
623635
continue
624636
}
@@ -630,7 +642,7 @@ func parseIPRules(raw string, listType int) []db.IPRule {
630642
func parseUARules(raw string, listType int) []db.UARule {
631643
var out []db.UARule
632644
for _, line := range strings.Split(raw, "\n") {
633-
line = strings.TrimSpace(line)
645+
line = stripInlineComment(strings.TrimSpace(line))
634646
if line == "" || strings.HasPrefix(line, "#") {
635647
continue
636648
}
@@ -645,7 +657,7 @@ func parseUARules(raw string, listType int) []db.UARule {
645657
func parseCountryRules(raw string, listType int) []db.CountryRule {
646658
var out []db.CountryRule
647659
for _, line := range strings.Split(raw, "\n") {
648-
line = strings.ToUpper(strings.TrimSpace(line))
660+
line = strings.ToUpper(stripInlineComment(strings.TrimSpace(line)))
649661
if line == "" || strings.HasPrefix(line, "#") {
650662
continue
651663
}
@@ -752,7 +764,7 @@ func (h *Handler) saveASNRules(w http.ResponseWriter, r *http.Request, siteID *i
752764
func parseASNRules(raw string, listType int) []db.ASNRule {
753765
var out []db.ASNRule
754766
for _, line := range strings.Split(raw, "\n") {
755-
line = strings.ToUpper(strings.TrimSpace(line))
767+
line = strings.ToUpper(stripInlineComment(strings.TrimSpace(line)))
756768
if line == "" || strings.HasPrefix(line, "#") {
757769
continue
758770
}

0 commit comments

Comments
 (0)