Skip to content

Commit 77369a0

Browse files
authored
Merge pull request github#13872 from Kwstubbs/Kevin_error_sanitizer
Go: Add sanitizer to remove paths passing through http.Error
2 parents 03ad04b + 84d52b9 commit 77369a0

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* Added [http.Error](https://pkg.go.dev/net/http#Error) to XSS sanitzers.

go/ql/lib/semmle/go/security/Xss.qll

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,18 @@ module SharedXss {
109109
}
110110
}
111111

112+
/**
113+
* A http.Error function returns with the ContentType of text/plain, and is not a valid XSS sink
114+
*/
115+
class ErrorSanitizer extends Sanitizer {
116+
ErrorSanitizer() {
117+
exists(Function f, DataFlow::CallNode call | call = f.getACall() |
118+
f.hasQualifiedName("net/http", "Error") and
119+
call.getArgument(1) = this
120+
)
121+
}
122+
}
123+
112124
/**
113125
* A regexp replacement involving an HTML meta-character, or a call to an escape
114126
* function, viewed as a sanitizer for XSS vulnerabilities.

go/ql/test/query-tests/Security/CWE-079/reflectedxsstest.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ func ServeJsonDirect(w http.ResponseWriter, r http.Request) {
2525

2626
func ErrTest(w http.ResponseWriter, r http.Request) {
2727
cookie, err := r.Cookie("somecookie")
28-
w.Write([]byte(fmt.Sprintf("Cookie result: %v", cookie))) // BAD: Cookie's value is user-controlled
29-
w.Write([]byte(fmt.Sprintf("Cookie check error: %v", err))) // GOOD: Cookie's err return is harmless
30-
28+
w.Write([]byte(fmt.Sprintf("Cookie result: %v", cookie))) // BAD: Cookie's value is user-controlled
29+
w.Write([]byte(fmt.Sprintf("Cookie check error: %v", err))) // GOOD: Cookie's err return is harmless
30+
http.Error(w, fmt.Sprintf("Cookie result: %v", cookie), 500) // Good: only plain text is written.
3131
file, header, err := r.FormFile("someFile")
3232
content, err2 := ioutil.ReadAll(file)
3333
w.Write([]byte(fmt.Sprintf("File content: %v", content))) // BAD: file content is user-controlled

0 commit comments

Comments
 (0)