Add whitelist functionality

- Implement whitelist package for filtering URLs
- Support pattern matching for allowed URLs
- Add URL validation against whitelist patterns
- Include test cases for whitelist functionality
This commit is contained in:
2025-05-22 12:46:28 +03:00
committed by antanst
parent dc6eb610a2
commit 57f5c0e865
2 changed files with 161 additions and 0 deletions

View File

@@ -0,0 +1,74 @@
package whiteList
import (
"fmt"
"os"
"regexp"
"strings"
"gemini-grc/config"
"gemini-grc/logging"
"git.antanst.com/antanst/xerrors"
)
var whitelist []regexp.Regexp //nolint:gochecknoglobals
func Initialize() error {
var err error
// Initialize whitelist
if config.CONFIG.WhitelistPath != "" {
if err = loadWhitelist(config.CONFIG.WhitelistPath); err != nil {
return err
}
}
return nil
}
func loadWhitelist(filePath string) error {
if whitelist != nil {
return nil
}
data, err := os.ReadFile(filePath)
if err != nil {
whitelist = []regexp.Regexp{}
return xerrors.NewError(fmt.Errorf("could not load whitelist file: %w", err), 0, "", true)
}
lines := strings.Split(string(data), "\n")
whitelist = []regexp.Regexp{}
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" || strings.HasPrefix(line, "#") {
continue
}
regex, err := regexp.Compile(line)
if err != nil {
return xerrors.NewError(fmt.Errorf("could not compile whitelist line %s: %w", line, err), 0, "", true)
}
whitelist = append(whitelist, *regex)
}
if len(whitelist) > 0 {
logging.LogInfo("Loaded %d whitelist entries", len(whitelist))
}
return nil
}
func Shutdown() error {
return nil
}
// IsWhitelisted checks if the URL matches any whitelist pattern
func IsWhitelisted(u string) bool {
for _, v := range whitelist {
if v.MatchString(u) {
return true
}
}
return false
}

View File

@@ -0,0 +1,87 @@
package whiteList
import (
"os"
"regexp"
"testing"
"gemini-grc/config"
)
func TestIsWhitelisted(t *testing.T) {
// Set up a test whitelist
whitelist = []regexp.Regexp{
*regexp.MustCompile(`^gemini://example\.com`),
*regexp.MustCompile(`^gemini://test\.org/path`),
}
testCases := []struct {
url string
expected bool
}{
{"gemini://example.com", true},
{"gemini://example.com/path", true},
{"gemini://test.org", false},
{"gemini://test.org/path", true},
{"gemini://test.org/path/subpath", true},
{"gemini://other.site", false},
}
for _, tc := range testCases {
result := IsWhitelisted(tc.url)
if result != tc.expected {
t.Errorf("IsWhitelisted(%s) = %v, want %v", tc.url, result, tc.expected)
}
}
}
func TestLoadWhitelist(t *testing.T) {
// Create a temporary whitelist file
content := `# This is a test whitelist
^gemini://example\.com
^gemini://test\.org/path
`
tmpfile, err := os.CreateTemp("", "whitelist")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name())
if _, err := tmpfile.Write([]byte(content)); err != nil {
t.Fatal(err)
}
if err := tmpfile.Close(); err != nil {
t.Fatal(err)
}
// Reset whitelist
whitelist = nil
// Set up configuration to use the temporary file
oldPath := config.CONFIG.WhitelistPath
config.CONFIG.WhitelistPath = tmpfile.Name()
defer func() {
config.CONFIG.WhitelistPath = oldPath
}()
// Load whitelist from the file
err = loadWhitelist(tmpfile.Name())
if err != nil {
t.Fatalf("loadWhitelist() error = %v", err)
}
// Check if whitelist was loaded correctly
if len(whitelist) != 2 {
t.Errorf("loadWhitelist() loaded %d entries, want 2", len(whitelist))
}
// Test a whitelisted URL
if !IsWhitelisted("gemini://example.com") {
t.Error("IsWhitelisted(\"gemini://example.com\") = false, want true")
}
// Test a URL in a whitelisted path
if !IsWhitelisted("gemini://test.org/path/subpage.gmi") {
t.Error("IsWhitelisted(\"gemini://test.org/path/subpage.gmi\") = false, want true")
}
}