top | item 33232072

Semgrep: Writing quick rules to verify ideas

62 points| adrianomartins | 3 years ago |blog.deesee.xyz

19 comments

order

craigds|3 years ago

I use semgrep for semantic search (and replace, sometimes).

Their docs and website try very hard to suggest you should use it for some kind of CI process, but so far I haven't found any need to do so. I can maybe see it being useful in a pre-commit hook.

It's VERY handy for semantic searches though - in situations where ripgrep would be useless due to multi-line matches.

I set up this alias to make it a bit less verbose for Python patterns:

    pygrep () {
        pat="$1"
        shift
        filez="$*"
        bash -xc "semgrep --lang=python --pattern '$pat' $filez"
    }
Usage is something like:

    pygrep 'myfunc(..., needle_arg=..., ...)'

burntsushi|3 years ago

Note that ripgrep can do multi-line searches with the -U flag.

Not that this detracts from your main point. Semgrep is much smarter than ripgrep and goes well beyond multi line searches.

I just wanted to clarify the small thing.

underyx|3 years ago

Heya, Semgrep maintainer here. Just wanted to ask you about an idea I had before, how would you feel about specifying the language parameter in the binary name, making the invocation look like this?

    semgrep.py search 'myfunc(..., needle_arg=..., ...)'
And then the other subcommands would remain

    semgrep scan --config auto
to scan with all recommended rules and

    semgrep ci
to scan in CI jobs.

iib|3 years ago

Don't you have to shift the arguments, so that `$1` does not also end in `filez`?

koyanisqatsi|3 years ago

I was looking for something like this the other day but then ended up just using RubyVM::AbstractSyntaxTree.parse_file and then rolled my own visitor on top of the AST. It's cool what they can do here but I think any language that exposes its AST is amenable to this kind of analysis, you just have to write some code to do it. The main bottleneck in my experience is just being familiar with the AST structure and how it maps to source syntax. It's cool that they have abstracted a lot of the commonality among several languages, definitely gonna look into this next time I need semantic code search.

renewiltord|3 years ago

Very cool. Thank you for writing this!