flounder

A simple gemini site builder
Log | Files | Refs | README | LICENSE

commit 2db17c0358b3c441a1bf395e3fba9c8a4880e7d2
parent b60b64a17d3a924e44fd4dafb511c1f5c4adc5d7
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Sun, 24 Jan 2021 21:31:27 -0800

Add banned usernames list, first unit test

Diffstat:
Aflounder_test.go | 16++++++++++++++++
Mhttp.go | 8++++++++
2 files changed, 24 insertions(+), 0 deletions(-)

diff --git a/flounder_test.go b/flounder_test.go @@ -0,0 +1,16 @@ +package main + +import "testing" + +func TestIsOKUsername(t *testing.T) { + for _, u := range []string{"www", "proxy", "%", "", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"} { + if isOkUsername(u) == nil { + t.Errorf("Username " + u + " should be considered invalid, but wasn't") + } + } + for _, u := range []string{"-", "alex", "1"} { + if isOkUsername(u) != nil { + t.Errorf("Username " + u + " should be considered valid, but wasn't") + } + } +} diff --git a/http.go b/http.go @@ -434,6 +434,8 @@ func logoutHandler(w http.ResponseWriter, r *http.Request) { const ok = "-0123456789abcdefghijklmnopqrstuvwxyz" +var bannedUsernames = []string{"www", "proxy", "grafana"} + func isOkUsername(s string) error { if len(s) < 1 { return fmt.Errorf("Username is too short") @@ -446,8 +448,14 @@ func isOkUsername(s string) error { return fmt.Errorf("Username contains invalid characters. Valid characters include lowercase letters, numbers, and hyphens.") } } + for _, username := range bannedUsernames { + if username == s { + return fmt.Errorf("Username is not allowed.") + } + } return nil } + func registerHandler(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { data := struct {