{"id":37123502,"url":"https://github.com/jlauinger/go-safer","last_synced_at":"2026-01-14T14:15:37.126Z","repository":{"id":40389184,"uuid":"266775172","full_name":"jlauinger/go-safer","owner":"jlauinger","description":"Go Vet-style linter to find incorrect uses of reflect.SliceHeader and reflect.StringHeader, and unsafe casts between structs with architecture-sized fields","archived":false,"fork":false,"pushed_at":"2022-11-28T21:44:39.000Z","size":55,"stargazers_count":40,"open_issues_count":1,"forks_count":4,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-06-19T00:38:46.773Z","etag":null,"topics":["go","golang","linter","static-analysis","unsafe-code"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jlauinger.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-05-25T12:37:13.000Z","updated_at":"2024-06-19T00:38:46.774Z","dependencies_parsed_at":"2023-01-22T15:31:08.736Z","dependency_job_id":null,"html_url":"https://github.com/jlauinger/go-safer","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/jlauinger/go-safer","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jlauinger%2Fgo-safer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jlauinger%2Fgo-safer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jlauinger%2Fgo-safer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jlauinger%2Fgo-safer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jlauinger","download_url":"https://codeload.github.com/jlauinger/go-safer/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jlauinger%2Fgo-safer/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28422444,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T13:30:50.153Z","status":"ssl_error","status_checked_at":"2026-01-14T13:29:08.907Z","response_time":107,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["go","golang","linter","static-analysis","unsafe-code"],"created_at":"2026-01-14T14:15:36.351Z","updated_at":"2026-01-14T14:15:37.102Z","avatar_url":"https://github.com/jlauinger.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# go-safer\n\n[![build](https://github.com/jlauinger/go-safer/workflows/build/badge.svg)](https://github.com/jlauinger/go-safer/actions/)\n[![Go Report Card](https://goreportcard.com/badge/github.com/jlauinger/go-safer)](https://goreportcard.com/report/github.com/jlauinger/go-safer)\n[![go-recipes](https://raw.githubusercontent.com/nikolaydubina/go-recipes/main/badge.svg?raw=true)](https://github.com/nikolaydubina/go-recipes)\n\nGo linter in the style of `go vet` to find incorrect uses of `reflect.SliceHeader` and `reflect.StringHeader`, and\nunsafe casts between structs with architecture-sized fields.\n\n\n## Output example\n\n![go-safer output example](https://user-images.githubusercontent.com/1872086/88237459-928f5000-cc7f-11ea-9b78-33f28d480610.png)\n\n\n## Incorrect usage patterns that are reported\n\n`go-safer` reports the following usage patterns:\n\n 1. There is a composite literal of underlying type `reflect.SliceHeader` or `reflect.StringHeader`,\n 2. There is an assignment to an instance of type `reflect.SliceHeader` or `reflect.StringHeader` that was not created\n    by casting an actual slice or `string`, and\n 3. There is a cast between struct types, where the structs contain a different number of fields with the architecture-dependently sized types `int`, `uint`, or `uintptr`\n\nPattern 1 identifies code that looks like this:\n\n```go\nfunc unsafeFunction(s string) []byte {\n    sH := (*reflect.StringHeader)(unsafe.Pointer(\u0026s))\n    bH := \u0026reflect.SliceHeader{\n        Data: sH.Data,\n        Len:  sH.Len,\n        Cap:  sH.Len,\n    }\n    return *(*[]byte)(unsafe.Pointer(bH))\n}\n```\n\nIt will also catch cases where `reflect.SliceHeader` has been renamed, like in `type MysteryType reflect.SliceHeader`.\n\nPattern 2 identifies code such as the following:\n\n```go\nfunc unsafeFunction(s string) []byte {\n    strH := (*reflect.StringHeader)(unsafe.Pointer(\u0026str))\n    sH := (*reflect.SliceHeader)(unsafe.Pointer(nil))\n    sH.Len = strH.Len\n    sH.Cap = strH.Len\n    sH.Data = strH.Data\n    return\n}\n```\n\n`safer-go` will catch the assignments to an object of type `reflect.SliceHeader`. Using the control flow graph of the\nfunction, it can see that `sH` was not derived by casting a real slice (here it's `nil` instead).\n\nPattern 3 identified casts as the following:\n\n```go\ntype A struct {\n  x int\n}\ntype B struct {\n  y int64\n}\nfunc unsafeFunction(a A) B {\n  return *(*B)(unsafe.Pointer(\u0026a))\n}\n```\n\nThere are more examples on incorrect (reported) and safe code in the test cases in the `passes/*/testdata/src`\ndirectories.\n\n\n## Why are these patterns insecure?\n\nIf `reflect.SliceHeader` or `reflect.StringHeader` is not created by casting a real slice or `string`, then the Go runtime\nwill not treat the `Data` field within these types as a reference to the underlying data array. Therefore, if the garbage\ncollector runs just before the final cast from the literal header instance to a real slice or `string`, it may collect\nthe original slice or `string`. This can lead to an information leak vulnerability.\n\nFor more details, such as a Proof-of-Concept exploit and a suggestion for a fixed version of these unsafe patterns, read\nthis blog post: [Golang Slice Header GC-Based Data Confusion on Real-World Code](https://dev.to/jlauinger/sliceheader-literals-in-go-create-a-gc-race-and-flawed-escape-analysis-exploitation-with-unsafe-pointer-on-real-world-code-4mh7)\n\n\n## Install\n\nTo install `go-safer`, use the following command:\n\n```\ngo get github.com/jlauinger/go-safer\n```\n\nThis will install `go-safer` to `$GOPATH/bin`, so make sure that it is included in your `$PATH` environment variable.\n\n\n## Usage\n\nRun go-safer on a package like this:\n\n```\n$ go-safer example/cmd\n```\n\nOr supply multiple packages, separated by spaces:\n\n```\n$ go-safer example/cmd example/util strings\n```\n\nTo check a package and, recursively, all its imports, use `./...`:\n\n```\n$ go-safer example/cmd/...\n```\n\nFinally, to check the package in the current directory you can use `.`:\n\n```\n$ go-safer .\n```\n\n`go-safer` accepts the same flags as `go vet`:\n\n```\nFlags:\n  -V\tprint version and exit\n  -all\n    \tno effect (deprecated)\n  -c int\n    \tdisplay offending line with this many lines of context (default -1)\n  -cpuprofile string\n    \twrite CPU profile to this file\n  -debug string\n    \tdebug flags, any subset of \"fpstv\"\n  -fix\n    \tapply all suggested fixes\n  -flags\n    \tprint analyzer flags in JSON\n  -json\n    \temit JSON output\n  -memprofile string\n    \twrite memory profile to this file\n  -source\n    \tno effect (deprecated)\n  -tags string\n    \tno effect (deprecated)\n  -trace string\n    \twrite trace log to this file\n  -v\tno effect (deprecated)\n```\n\nSupplying the `-help` flag prints the usage information for `go-safer`:\n\n```\n$ go-safer -help\n```\n\n## Dependency Management\n\nIf your project uses Go modules and a `go.mod` file, `go-safer` will fetch all dependencies automatically before it\nanalyzes them. It behaves exactly like `go build` would.\n\nIf you use a different form of dependency management, e.g. manual `go get`, `go mod vendor` or anything else, you need\nto run your dependency management before running `go-safer` in order to have all dependencies up to date before\nanalysis.\n\n\n## Development\n\nTo get the source code and compile the binary, run this:\n\n```\n$ git clone https://github.com/jlauinger/go-safer\n$ cd go-safer\n$ go build\n```\n\nTo run the test cases use the following command:\n\n```\n$ go test ./...\n```\n\n`go-safer` uses the testing infrastructure from `golang.org/x/tools/go/analysis/analysistest`. To add a test case, create\na new package within the `bad` or `good` directories in `passes/sliceheader/testdata/src`. Add as many Go files to the\npackage as needed.\n\nThen, register the new package in the `sliceheader_test.go` file by specifying the package path.\n\nIn the test case source files, add annotation comments to the lines that should be reported (or not). The comments must\nlook like this:\n\n```\nsH.Len = strH.Len            // want \"assigning to reflect header object\" \"assigning to reflect header object\"\nfmt.Println(\"hello world\")   // ok\n```\n\nAnnotations that indicate a line that should be reported must begin with `want` and then have the desired message twice.\nFor some reason, the testing infrastructure will cause `go-safer` to output the annotation twice, therefore it has to be\nexpected twice as well to pass the test.\n\nTest cases for the `structcast` pass can be added similarly.\n\nSince `go-safer` is built upon the Go Vet standard infrastructure, you can import the passes into you own Go Vet-based\nlinter.\n\n\n## License\n\nLicensed under the MIT License (the \"License\"). You may not use this project except in compliance with the License. You\nmay obtain a copy of the License [here](https://opensource.org/licenses/MIT).\n\nCopyright 2020 Johannes Lauinger\n\nThis tool has been developed as part of my Master's thesis at the\n[Software Technology Group](https://www.stg.tu-darmstadt.de/stg/homepage.en.jsp) at TU Darmstadt.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjlauinger%2Fgo-safer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjlauinger%2Fgo-safer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjlauinger%2Fgo-safer/lists"}