# Go Security > Security of Go code, binaries and builds. The runtime is memory-safe by default, so real Go bugs concentrate in error-handling semantics, panics and concurrency, stdlib API footguns, deliberate escapes from the type system (`unsafe`, cgo), and how binaries are built and shipped. ## How it works Go removes most C-class memory bugs with bounds-checked slices, garbage collection and typed pointers. Trail of Bits' assessment of Go projects (after their Kubernetes audit) found that the exploitable problems sit elsewhere: scoping, goroutines, error handling and dependency management. Most of these aren't runtime bugs. The recurring root causes: - **Errors are values, not control flow.** An ignored or overwritten `err` lets execution continue on bad state. Examples are `:=` redeclaration silently discarding an error, and the Kubelet `ionice` bug where a logged-but-unreturned read error caused an index-out-of-range panic. - **Panics are recoverable, state isn't.** `net/http` recovers per-request panics, but a panic inside a critical section leaves mutexes locked or invariants half-applied. The result is a persistent DoS from one request. - **Convenience APIs with surprising semantics.** These include `text/template` vs `html/template`, `encoding/json` float64 coercion and zero-value defaults, `net/url` decoded paths, `http.DefaultClient` with no timeout, and `fmt.Sprintf`-built SQL. - **Deliberate escapes.** `unsafe.Pointer`, `go:linkname` and cgo opt out of the guarantees. - **Build output.** Binaries were historically non-PIE, ship full symbol and `.gopclntab` metadata unless stripped, and dynamic linking appears silently via `net`/`os/user`. Contrast notes a tooling gap: Go's static typing plus interface-heavy code breaks many data-flow SAST tools, and Go APIs often front NoSQL stores. So the classic SQLi-centric rule packs under-fit. ## Map of the topic ### Code-level bugs - [[Go Secure Coding Pitfalls]]: shadowed `err`, integer width (TOB-K8S-015), template package choice, JSON decoding quirks, `net/url` semantics, `Sprintf` SQL, HTTP client timeouts, timing-safe compares and bcrypt cost. - [[Go Panics and Concurrency Bugs]]: recover-in-handler deadlocks, inconsistent state after recovery, `recover()` scoping, loop-variable capture, unsynchronized maps, closed-channel and nil-map panics, and `log.Fatal` exits. - [[Go unsafe and cgo]]: `unsafe.Pointer` arithmetic, writing unexported fields, zero-copy string and slice aliasing, `go:linkname`, cgo trust and `CGO_ENABLED=0`. - [[Go Template SSTI]]: method-call gadgets in `text/template` and `html/template` (ssti topic). ### Assessment and tooling - [[Go Security Testing Tools]]: gosec, go vet, staticcheck and ineffassign, custom `go/analysis` and SSA checkers, go-fuzz and gopter, KRF fault injection, the race detector and on-edge, and IAST. ### Build, supply chain and binaries - [[Go Build and Deployment Hardening]]: `go.mod`/`go.sum`, dependency scanning, static linking (`netgo`, `osusergo`), `-trimpath -ldflags="-s -w"`, PIE and ASLR, scratch images pinned by digest, and gops agent exposure. - [[Go Binary Reverse Engineering]]: `.gopclntab` symbol recovery, obfuscators (gobfuscate, GoMang), stdlib signature matching, and why offensive tooling is written in Go. ## Review quick start 1. Run `gosec ./...`, `go vet ./...`, `staticcheck ./...` and `ineffassign ./...` together. Each misses cases the others catch. 2. Grep for `unsafe.`, `//go:linkname`, `import "C"`, `text/template`, `fmt.Sprintf(` near `Query`/`Exec`, `exec.Command`, `http.Get(`/`http.DefaultClient`, `recover()`, and `strconv.Atoi` followed by a narrowing cast. 3. For each `mu.Lock()` without `defer mu.Unlock()`, ask what happens if the critical section panics. 4. Run tests with `-race`, and build for 32-bit as well as 64-bit targets. 5. Fuzz parsers and decoders. Inject syscall faults to exercise error paths. 6. Check that the build output is stripped, PIE, `CGO_ENABLED=0` where possible, nonroot and from a digest-pinned scratch image. ## Related - [[Software Supply Chain Security]] · [[Malicious Packages]] · [[SBOM and Provenance]] - [[Python Security]] · [[Python SAST Tools]] (comparable tooling map for Python) - [[Server-Side Template Injection]] · [[Cross-Site Scripting]] · [[SQL Injection]] · [[Command Injection]] - [[URL Parser Differentials]] · [[SSRF in Kubernetes and Containers]] ## Sources - [[raw/blog.trailofbits.com/security-assessment-techniques-for-go-projects-trail-of-bits-blog-1213|Security assessment techniques for Go projects]] - [[raw/snyk.io/go-security-cheatsheet-snyk-blog-1225|Go Security cheatsheet]] - [[raw/contrastsecurity.com/secure-coding-with-go-1224|Secure Coding with Go]] - [[raw/iximiuz.com/go-http-handlers-panic-and-deadlocks-1304|Go, HTTP handlers, panic, and deadlocks]] - [[raw/devs.cloudimmunity.com/50-shades-of-go-traps-gotchas-and-common-mistakes-for-new-golang-devs-1102|50 Shades of Go: Traps, Gotchas, and Common Mistakes]] - [[raw/link.medium.com/the-top-10-most-common-mistakes-i-ve-seen-in-go-projects-1366|The Top 10 Most Common Mistakes I've Seen in Go Projects]] - [[raw/pixelstech.net/understand-unsafe-in-golang-pixelstech-net-1204|Understand unsafe in GoLang]] - [[raw/rain-1.github.io/golang-aslr-rain-1-github-io-1098|Golang ASLR]] - [[raw/arp242.net/statically-compiling-go-programs-1195|Statically compiling Go programs]] - [[raw/link.medium.com/create-the-smallest-and-secured-golang-docker-image-based-on-scratch-1184|Create the smallest and secured golang docker image based on scratch]] - [[raw/medium.com/de-ofuscating-golang-functions-1368|De-Obfuscating GoLang Functions]] - [[raw/about.houqp.me/write-your-own-go-static-analysis-tool-houqp-1145|Write your own Go static analysis tool]] - [[raw/golang.org/url-package-net-url-go-packages-1345|url package - net/url]] - [[raw/link.medium.com/building-and-testing-a-rest-api-in-golang-using-gorilla-mux-and-mysql-1168|Building and testing a REST API in golang using gorilla mux and MySQL]]