Skip to content

[codeql-go]: Add CWE-79: HTML template escaping passthrough #306

Closed

Description

@gagliardetto

Query

This query is going to be merged into the codeql-go repository through this PR: github/codeql-go#493

CVE ID(s)

No CVEs, yet.

Report

Short summary

This query finds cases where an (html) template is executed using user-provided values that were converted to types that allow completely avoiding the escaping that (normally) values would undergo in the HTML template execution.

Longer summary

Go has a package from the standard library for safely creating and handling HTML templates: html/template

Normally, any value provided to a template from the html/template package would be sensibly escaped to avoid any possible XSS.

Example:

package main

import (
	"html/template"
	"os"
)

func main() {
	tmpl, _ := template.New("test").Parse(`<div>Hello, <b>{{.}}</b></div>`)

	{ // This will be correctly escaped:
		var escaped = `<img src="example.gif" onload="alert(document.cookie)" width="100" height="132">`
		tmpl.Execute(os.Stdout, escaped)
	}
}

will render to

<div>Hello, <b>&lt;img src=&#34;example.gif&#34; onload=&#34;alert(document.cookie)&#34; width=&#34;100&#34; height=&#34;132&#34;&gt;</b></div>

However, there are a few special types (HTML, HTMLAttr, JS, JSStr, CSS, Srcset, and URL) that allow values to avoid being escaped.

Example:

package main

import (
	"html/template"
	"os"
)

func main() {
	tmpl, _ := template.New("test").Parse(`<div>Hello, <b>{{.}}</b></div>`)

	{
		// This will render any provided HTML.
		var passthrough = template.HTML(`<img src="example.gif" onload="alert(document.cookie)" width="100" height="132">`)
		tmpl.Execute(os.Stdout, passthrough)
	}
}

will render to

<div>Hello, <b><img src="example.gif" onload="alert(document.cookie)" width="100" height="132"></b></div>

In essence, converting anything that is user-provided to one of the above types makes an application potentially vulnerable to XSS.

  • Are you planning to discuss this vulnerability submission publicly? (Blog Post, social networks, etc). We would love to have you spread the word about the good work you are doing

No, I'm not planning on discussing this vulnerability submission publicly.

Result(s)

Provide at least one useful result found by your query, on some revision of a real project.

Activity

  1. ghsecuritylab commented on Mar 19, 2021

    @ghsecuritylab
    Collaborator

    Your submission is now in status SecLab review.

    For information, the evaluation workflow is the following:
    CodeQL initial assessment > SecLab review > CodeQL review > SecLab finalize > Pay > Closed

  2. m-y-mo commented on Mar 22, 2021

    @m-y-mo
    Contributor

    @gagliardetto We've reviewed the results from the query. I'd suggest adding a sanitiser that checks when the data becomes numeric type because sometimes the data that goes into a template is a number and that wouldn't cause issues. Thanks.

  3. gagliardetto commented on Mar 22, 2021

    @gagliardetto
    ContributorAuthor

    @m-y-mo Thanks for the review. Can you please send me privately a link to an example of such case?

  4. gagliardetto commented on Mar 22, 2021

    @gagliardetto
    ContributorAuthor

    You can find me in the securitylab slack @Slavomir.

  5. m-y-mo commented on Mar 23, 2021

    @m-y-mo
    Contributor

    @gagliardetto If you run the query on this project and inspect the paths, you'll see that all the results are tainted by the userId which is an integer:

    https://lgtm.com/projects/g/democratic-coin/dcoin-go

    Thanks.

  6. gagliardetto commented on Mar 25, 2021

    @gagliardetto
    ContributorAuthor

    I added a sanitizer for NumericType (github/codeql-go@fdfca3b), which should avoid the issue with numbers. As for the few somewhat confusing taint path explanations in the results, I opened github/codeql#9308

  7. ghsecuritylab commented on Apr 1, 2021

    @ghsecuritylab
    Collaborator

    Your submission is now in status CodeQL review.

    For information, the evaluation workflow is the following:
    CodeQL initial assessment > SecLab review > CodeQL review > SecLab finalize > Pay > Closed

  8. gagliardetto commented on Apr 9, 2021

    @gagliardetto
    ContributorAuthor

    PR merged.

  9. ghsecuritylab commented on Apr 9, 2021

    @ghsecuritylab
    Collaborator

    Your submission is now in status SecLab finalize.

    For information, the evaluation workflow is the following:
    CodeQL initial assessment > SecLab review > CodeQL review > SecLab finalize > Pay > Closed

  10. xcorail commented on Apr 12, 2021

    @xcorail
    Contributor

    Created Hackerone report 1161806 for bounty 294551 : [306] [codeql-go]: Add CWE-79: HTML template escaping passthrough

  11. ghsecuritylab commented on Apr 12, 2021

    @ghsecuritylab
    Collaborator

    Your submission is now in status Closed.

    For information, the evaluation workflow is the following:
    CodeQL initial assessment > SecLab review > CodeQL review > SecLab finalize > Pay > Closed

  12. gagliardetto commented on Apr 12, 2021

    @gagliardetto
    ContributorAuthor

    🎉 Thank you @xcorail ! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    All For OneSubmissions to the All for One, One for All bounty

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions