Skip to content

checker: go_html_req_template_injection#79

Merged
sourya-deepsource merged 2 commits intoDeepSourceCorp:masterfrom
Thiru-moorthi:go_html_req_template_injection
Mar 7, 2025
Merged

checker: go_html_req_template_injection#79
sourya-deepsource merged 2 commits intoDeepSourceCorp:masterfrom
Thiru-moorthi:go_html_req_template_injection

Conversation

@Thiru-moorthi
Copy link
Contributor

@Thiru-moorthi Thiru-moorthi commented Feb 24, 2025

Description

This PR adds a new Go checker to detect security risks associated with rendering user-controlled data using html/template without proper sanitization. Improper handling of user inputs in templates can lead to Server-Side Template Injection (SSTI), allowing attackers to execute arbitrary code, leak sensitive data, or launch Cross-Site Scripting (XSS) attacks.

Detection Logic

This checker flags instances where:

  • User-controlled input (from r.Cookie, r.URL.Query().Get, or r.FormValue) is directly mapped to a template.
  • The template executes user input without proper sanitization.

Recommended Alternatives

To mitigate security risks, always sanitize user inputs and ensure context-aware encoding before rendering:

Insecure Example:

http.HandleFunc("/unsafe", func(w http.ResponseWriter, r *http.Request) {
    r.ParseForm()
    data := map[string]string{"Username": r.FormValue("username")}
    tmpl, _ := template.New("example").Parse("<h1>Hello, {{.Username}}</h1>")
    tmpl.Execute(w, data) // Vulnerable: renders unsanitized user input
})

Secure Example:

http.HandleFunc("/safe", func(w http.ResponseWriter, r *http.Request) {
    r.ParseForm()
    userInput := template.HTMLEscapeString(r.FormValue("username"))
    data := map[string]string{"Username": userInput}
    tmpl, _ := template.New("example").Parse("<h1>Hello, {{.Username}}</h1>")
    tmpl.Execute(w, data) // Safe: sanitized input prevents injection
})

Exclusions

To reduce noise, this checker does not flag occurrences in:

  • Test files (test/**, *_test.go, tests/**, __tests__/**)

References

@vercel
Copy link

vercel bot commented Feb 24, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
globstar ⬜️ Ignored (Inspect) Visit Preview Mar 7, 2025 0:14am

@Thiru-moorthi Thiru-moorthi force-pushed the go_html_req_template_injection branch from 6df8c71 to 213d983 Compare February 25, 2025 15:22
@Thiru-moorthi Thiru-moorthi force-pushed the go_html_req_template_injection branch from 213d983 to 97efb0e Compare February 25, 2025 15:23
Signed-off-by: Sourya Vatsyayan <sourya@deepsource.io>
@sourya-deepsource sourya-deepsource merged commit 8e0a1cd into DeepSourceCorp:master Mar 7, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants