Skip to content

Commit

Permalink
Sanitize some trace logs that might contain user input. #24
Browse files Browse the repository at this point in the history
  • Loading branch information
roblillack committed Apr 29, 2022
1 parent b8b7f93 commit fa3955d
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 8 deletions.
5 changes: 0 additions & 5 deletions csrf.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@ func isSafeMethod(c *Controller) bool {

func findCSRFToken(c *Controller) string {
if h := c.Request.Header.Get(csrfHeaderName); h != "" {
TRACE.Printf("Have header CSRF token: %s\n", h)
return h
}

if f := c.Params.Get(csrfFieldName); f != "" {
TRACE.Printf("Have form field CSRF token: %s\n", f)
return f
}

Expand Down Expand Up @@ -108,9 +106,6 @@ func CSRFFilter(c *Controller, fc []Filter) {
if len(csrfToken) != 22 {
csrfToken = generateRandomToken()
c.Session[csrfCookieKey] = csrfToken
TRACE.Printf("Created session token: %s\n", csrfToken)
} else {
TRACE.Printf("Browser sent session token: %s\n", csrfToken)
}

c.SetCookie(&http.Cookie{
Expand Down
2 changes: 1 addition & 1 deletion http.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func ResolveAcceptLanguage(req *http.Request) AcceptLanguages {
if qualifiedRange := strings.Split(languageRange, ";q="); len(qualifiedRange) == 2 {
quality, error := strconv.ParseFloat(qualifiedRange[1], 32)
if error != nil {
WARN.Printf("Detected malformed Accept-Language header quality in '%s', assuming quality is 1", languageRange)
WARN.Printf("Detected malformed Accept-Language header quality in '%s', assuming quality is 1", removeLineBreaks(languageRange))
acceptLanguages[i] = AcceptLanguage{qualifiedRange[0], 1}
} else {
acceptLanguages[i] = AcceptLanguage{qualifiedRange[0], float32(quality)}
Expand Down
4 changes: 2 additions & 2 deletions i18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func setCurrentLocaleControllerArguments(c *Controller, locale string) {
// quality first in the slice.
func hasAcceptLanguageHeader(request *Request) (bool, string) {
if request.AcceptLanguages != nil && len(request.AcceptLanguages) > 0 {
return true, request.AcceptLanguages[0].Language
return true, removeAllWhitespace(request.AcceptLanguages[0].Language)
}

return false, ""
Expand All @@ -239,7 +239,7 @@ func hasLocaleCookie(request *Request) (bool, string) {
if request != nil && request.Cookies() != nil {
name := Config.StringDefault(localeCookieConfigKey, CookiePrefix+"_LANG")
if cookie, error := request.Cookie(name); error == nil {
return true, cookie.Value
return true, removeAllWhitespace(cookie.Value)
} else {
TRACE.Printf("Unable to read locale cookie with name '%s': %s", name, error.Error())
}
Expand Down
13 changes: 13 additions & 0 deletions sanitize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package mars

import "regexp"

var lineBreakPattern = regexp.MustCompile(`[\r\n]+`)

func removeLineBreaks(s string) string {
return lineBreakPattern.ReplaceAllString(s, " ")
}

func removeAllWhitespace(s string) string {
return whiteSpacePattern.ReplaceAllString(s, "")
}
33 changes: 33 additions & 0 deletions sanitize_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package mars

import (
"testing"
)

func TestRemovingLineBreaks(t *testing.T) {
for i, exp := range map[string]string{
"This is a test.": "This is a test.",
"This is\n a test.": "This is a test.",
"This is\r a test.": "This is a test.",
"This is\r\n a test.": "This is a test.",
"\n\n\n\n\nThis is\r a test.": " This is a test.",
} {
if res := removeLineBreaks(i); res != exp {
t.Errorf("Unexpected result '%s' when removing line breaks from '%s'.\n", res, i)
}
}
}

func TestRemovingAllWhitespace(t *testing.T) {
for i, exp := range map[string]string{
"This is a test.": "Thisisatest.",
"This is\n a test.": "Thisisatest.",
"This is\r a test.": "Thisisatest.",
"This is\r\n a test.": "Thisisatest.",
"\n\n\n\n\nThis is\r a test.": "Thisisatest.",
} {
if res := removeAllWhitespace(i); res != exp {
t.Errorf("Unexpected result '%s' when removing all whitespace from '%s'.\n", res, i)
}
}
}

0 comments on commit fa3955d

Please sign in to comment.