Skip to content

Commit c8beac0

Browse files
committed
Sanitize strings
1 parent 9dfb2b7 commit c8beac0

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

helper/sanitizer.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package helper
2+
3+
import "strings"
4+
5+
func Sanitize(s string) string {
6+
return strings.Replace(s, "\n", "", -1)
7+
}

helper/sanitizer_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package helper
2+
3+
import "testing"
4+
5+
func TestSanitize(t *testing.T) {
6+
ours := "abc"
7+
theirs := Sanitize("abc\n\n\n")
8+
9+
if ours != theirs {
10+
t.Errorf("Got %s but wanted %s", theirs, ours)
11+
}
12+
}
13+
14+
func TestSanitizeWithNoNewLines(t *testing.T) {
15+
ours := "abc"
16+
theirs := Sanitize("abc")
17+
18+
if ours != theirs {
19+
t.Errorf("Got %s but wanted %s", theirs, ours)
20+
}
21+
}

server/handlers.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ func AuthenticationHandler(next http.Handler) http.Handler {
3535
user, err := sql.GetUserByUsername(signRequest.Username)
3636

3737
if err != nil {
38-
authorisationFailed(w, "No such user %s", signRequest.Username)
38+
authorisationFailed(w, "No such user %s", helper.Sanitize(signRequest.Username))
3939
}
4040

4141
hasValidAPIKey, err := crypto.Validate(signRequest.APIKey, user.APIKey.Key)
4242

4343
if !hasValidAPIKey {
44-
authorisationFailed(w, "Invalid API key for user %s", signRequest.Username)
44+
authorisationFailed(w, "Invalid API key for user %s", helper.Sanitize(signRequest.Username))
4545
}
4646

4747
hasValidPrincipals := CheckPrincipals(user.Principals, signRequest.Principals)
@@ -50,7 +50,7 @@ func AuthenticationHandler(next http.Handler) http.Handler {
5050
authorisationFailed(w, "One or more unauthorised principals requested %v", signRequest.Principals)
5151
}
5252

53-
log.Infof("User %s is authenticated", signRequest.Username)
53+
log.Infof("User %s is authenticated", helper.Sanitize(signRequest.Username))
5454

5555
next.ServeHTTP(w, r)
5656
}

0 commit comments

Comments
 (0)