github / github/codeql

False positive: email content injection

Open
#16,699 1 comment 0 reactions 0 assignees View on GitHub
false-positive
Dominant language
CodeQL
Stars
10.1k
Forks
2.1k
Avg merge
2d 15h
Merged PRs (30d)
141

Description

**Description of the false positive**

A simple, valid e-mail address inside an e-mail message triggers https://codeql.github.com/codeql-query-help/go/go-email-injection/

The rule also triggers on valid html, although the [security model](https://pkg.go.dev/html/template#hdr-Security_Model) in `html/template` should make it safe.

**Code samples**
```go
func SendEmailHandler(w http.Response, r http.Request) {
user := r.URL.Query().Get("user")
if m, _ := regexp.MatchString("[a-z]{3}", user); !m {
return
}
msg := fmt.Sprintf("To: %s@example.com\r\n\r\nHello!", user)
smtp.SendMail("", nil, "", nil, []byte(msg))
}
```

Another example where `html/template` takes care of escaping the html content.

```go
var htmlTempl = template.Must(template.New("t").Parse("Message:

{{.}}
"))

func SendEmailHandler(w http.Response, r http.Request) {
msg := r.URL.Query().Get("message")
buf := bytes.NewBuffer([]byte(`MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n`))
htmlTempl.Execute(buf, msg)
smtp.SendMail("", nil, "", nil, buf.Bytes())
}```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.