picatz / taint

🚰 Static taint analysis for Go programs.
https://picatz.github.io/#blog/taint
Mozilla Public License 2.0
57 stars 1 forks source link

Handle pathway found when using `json.NewDecoder` pattern #1

Closed picatz closed 1 year ago

picatz commented 1 year ago

At this time, this would be a false negative:

mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    var input map[string]any ←────────╮         ↑
                    ╭─────────────────│─────────╯
                    ↓                 ↓
    json.NewDecoder(r.Body).Decode(&input) ←────╮
                                            │
    func() {                        ↓
        userValue := fmt.Sprintf("%s", input["query"]) ←────────╮
        business(db, func() *string {               │
            return &userValue ←─────────────────────────────────╯
        }())
     }()
})
  1. userValue comes from the parent scope's input map object (query key).
  2. The input map object comes from it's parent's scope. It's used by (*json.Decoder).Decode.
  3. The decoder uses r.Body (user input) as its source to populate the input map.

This relationship needs to be properly traversed, analyzed, or captured to find this SQL injection.

userValue:string → input:map["query"] → input:map → (*json.Decoder).Decode → json.NewDecoder → (*http.Request).Body → *http.Request

Related test:

https://github.com/picatz/taint/blob/c158d0fd952cad97687c5337360bf64a4dae7ad1/sql/injection/injection_test.go#L39-L42