Static analysis (SAST) check · Go

Llm output to sink from request input (Go)

Text generated by a language model reaches a shell command, a SQL statement or template.HTML.

Why it matters

Model output follows instructions planted in its input (prompt injection), so treat it as untrusted user input.

How to fix it

Never run model output as a command. Parse it into a fixed structure (JSON into a struct, an allow-list of actions), pass values to SQL as query arguments, and let html/template escape it instead of converting it to template.HTML.

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 fixture

import (
	"context"
	"os/exec"

	"github.com/openai/openai-go"
)

func runSuggested(ctx context.Context, client openai.Client, task string) error {
	resp, err := client.Chat.Completions.New(ctx, openai.ChatCompletionNewParams{
		Model:    openai.ChatModelGPT4oMini,
		Messages: []openai.ChatCompletionMessageParamUnion{openai.UserMessage(task)},
	})
	if err != nil {
		return err
	}
	return exec.Command("sh", "-c", resp.Choices[0].Message.Content).Run()
}
Fixed: not reported (Go)
package fixture

import (
	"context"
	"os/exec"

	"github.com/openai/openai-go"
)

var allowed = map[string][]string{"status": {"git", "status"}}

func runSuggested(ctx context.Context, client openai.Client, task string) error {
	resp, err := client.Chat.Completions.New(ctx, openai.ChatCompletionNewParams{
		Model:    openai.ChatModelGPT4oMini,
		Messages: []openai.ChatCompletionMessageParamUnion{openai.UserMessage(task)},
	})
	if err != nil {
		return err
	}
	_, ok := allowed[resp.Choices[0].Message.Content]
	if !ok {
		return nil
	}
	return exec.Command("git", "status").Run()
}

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.