semgrep-go

Go rules for semgrep and go-ruleguard

  • Owner: dgryski/semgrep-go
  • Platform:
  • License:: MIT License
  • Category::
  • Topic:
  • Like:
    0
      Compare:

Github stars Tracking Chart

semgrep-go

This repo holds patterns for finding odd Go code.

The rules engines currently supported:

I'll accept comby patterns if you can't get them to work with either semgrep or ruleguard.

To run a single semgrep rule:

$ semgrep -f path/to/semgrep-go/rule.yml .

To run all semgrep rules:

$ semgrep -f path/to/semgrep-go/ .

To run all the ruleguard rules:

$ ruleguard -c=0 -rules path/to/semgrep-go/rules.go

Semgrep checks:

  • badexponentiation: check for 2^x and 10^x which look like exponentiation
  • badnilguard: check for poorly formed nil guards
  • errtodo: check for TODOs in error handling code
  • hmac-bytes: check for using bytes.Equal() with HMACs
  • hostport: check for using fmt.Sprintf() instead of net.JoinHostPort()
  • mathbits: check for places you should use math/bits instead
  • mail-address: check for using fmt.Sprintf() instead of net/mail.Address.String()
  • oddbitwise: check for odd bit-wise expressions
  • oddcompare: check for odd comparisons
  • oddcompound: check for odd compound += or -= expressions
  • oddifsequence: check for an odd sequence of ifs
  • oddmathbits: check for odd uses of math/bits
  • parseint-downcast: check for places a parsed 64-bit int is downcast to 32-bits
  • returnnil: check for odd nil-returns
  • sprinterr: check for fmt.Sprint(err) instead of err.Error()
  • joinpath: check for using strings.Join() to construct paths
  • readfull: check for extra length check for io.ReadFull()
  • nilerr: returning a nil err instead of a nil value
  • errclosed: check for call strings.Contains() to detect net.ErrClosed
  • hmac-hash: check for bad hash.New passed to hmac.New()
  • readeof: check for ignoring io.EOF as a successful read
  • writestring: check for using io.WriteString(w, string(b))
  • wronglock: find incorrect lock/unlock pairs for rwmutex

Ruleguard checks are in ruleguard.rules.go.

  • unconvert: check for unnecessary conversions
  • timeeq: check for using == and != with time.Time values
  • wrongerr: check for potentially checking the wrong error value
  • errnoterror: check for variables called err which are not the error type
  • ifbodythenbody: check for if statements with identical if and else bodies
  • subtractnoteq: check for x-y==0 instead of x==y
  • selfassign: check for variable self-assignments
  • oddnestedif: check for odd patterns of nested-ifs.
  • oddbitwise: check for odd bitwise expressions
  • ifreturn: check for off if/return sequences
  • oddifsequence: check for if sequences
  • nestedifsequence: check for odd nested if sequences
  • identicalassignments: check for x = y ; y = x pairs.
  • oddcompoundop: check for odd compound operations
  • constswitch: check for switch statements with expressions
  • oddcomparisons: check for odd comparisons
  • oddmathbits: check for odd uses of math/bits
  • floateq: check for exact comparisons of floating point values
  • badexponent: check for 2^x and 10^x , which look like exponentiation
  • floatloop: check for using floats as loop counters
  • urlredacted: check for logging urls without calling url.Redacted()
  • sprinterr: check for calling fmt.Sprint(err) instead of err.Error()
  • largeloopcopy: check for large value copies in loops
  • joinpath: check for using strings.Join() to construct paths
  • readfull: check for extra length check for io.ReadFull()
  • nilerr: returning an nil error instead of a nil value
  • errnetclosed: check for call strings.Contains() to detect net.ErrClosed
  • httpheaderadd: check for use of http.Header.Add method instead of Set
  • hmac-hash: check for bad hash.New passed to hmac.New()
  • readeof: check for ignoring io.EOF as a successful read
  • writestring: check for using io.WriteString(w, string(b)) when b is []byte
  • badlock: find incorrect lock/unlock pairs for rwmutex
    _

Find this useful? Buy me a coffee!

Main metrics

Overview
Name With Ownerdgryski/semgrep-go
Primary LanguageGo
Program languageGo (Language Count: 2)
Platform
License:MIT License
所有者活动
Created At2020-07-09 22:00:24
Pushed At2024-11-17 21:30:56
Last Commit At2024-11-17 13:30:34
Release Count0
用户参与
Stargazers Count469
Watchers Count20
Fork Count39
Commits Count213
Has Issues Enabled
Issues Count29
Issue Open Count16
Pull Requests Count34
Pull Requests Open Count4
Pull Requests Close Count2
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private