Skip to content

Commit 8960453

Browse files
committed
Add sanitizer to remove http.Error sink
1 parent 89aa86a commit 8960453

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ module SharedXss {
108108
)
109109
}
110110
}
111+
/**
112+
* A http.Error function returns with the ContentType of text/plain, and is not a valid XSS sink
113+
*/
114+
class ErrorSanitizer extends Sanitizer{
115+
ErrorSanitizer() {
116+
exists(Function f, DataFlow::CallNode call | f = call.getCall().getTarget() | f.hasQualifiedName("net/http", "Error")
117+
and call.getArgument(1) = this)
118+
}
119+
}
111120

112121
/**
113122
* A regexp replacement involving an HTML meta-character, or a call to an escape

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)