Static analysis (SAST) check · Go

Code injection from request input (Go)

A value read from the HTTP request is evaluated by an embedded interpreter or expression engine.

Why it matters

The caller can run code, or read anything the environment passed to the engine exposes.

How to fix it

Do not evaluate request data. If users must supply expressions, compile a server-held expression and pass the value as a variable, with an environment that exposes nothing else.

Example

The rule's own test fixtures, verbatim: the test suite requires the rule to report the first and not the second.

Vulnerable: reported (Go)
package calc

import (
	"net/http"

	"github.com/expr-lang/expr"
)

func Eval(w http.ResponseWriter, r *http.Request) {
	out, err := expr.Eval(r.FormValue("expr"), nil)
	_, _ = out, err
}
Fixed: not reported (Go)
package calc

import (
	"net/http"
	"strconv"

	"github.com/expr-lang/expr"
)

func Eval(w http.ResponseWriter, r *http.Request) {
	a, _ := strconv.Atoi(r.FormValue("a"))
	out, err := expr.Eval("a * 2", map[string]any{"a": a})
	_, _ = out, err
}

What TrustIvan reads

Runs on repository scans of Go source once static analysis is turned on (SAST_RULES and the separately built analyzer image); it is off by default.

Weakness classes and references

Terms: SAST (static application security testing), Taint analysis, CWE (Common Weakness Enumeration), OWASP Top 10

All static analysis (SAST) checks · The whole catalogue · How this works in TrustIvan · Documentation

See TrustIvan on your own estate.

Book thirty minutes with an engineer and bring an image, a repository or a cloud account, or start free on the hosted service.