• Stars
    star
    489
  • Rank 89,990 (Top 2 %)
  • Language
    Go
  • License
    MIT License
  • Created almost 5 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Go fearless SQL. Sqlvet performs static analysis on raw SQL queries in your Go code base.

Sqlvet

goreportcard codecov build-status

Sqlvet performs static analysis on raw SQL queries in your Go code base to surface potential runtime errors at build time.

Feature highlights:

  • Check for SQL syntax error
  • Identify unsafe queries that could potentially lead to SQL injections
  • For INSERT statements, make sure column count matches value count
  • Validate table names
  • Validate column names

TODO:

  • Validate query function argument count and types
  • Support MySQL syntax
  • Type check value list in UPDATE query
  • Trace wrapper function call

Usage

Installation

Go less than 1.18:

$ go get github.com/houqp/sqlvet

Go greater or equal 1.18:

$ go install github.com/houqp/sqlvet@latest

Zero conf

SqlVet should work out of the box for any Go project using go modules:

$ sqlvet .
[!] No schema specified, will run without table and column validation.
Checked 10 SQL queries.
🎉 Everything is awesome!

Note: unreachable code will be skipped.

Schema validation

To enable more in-depth analysis, create a sqlvet.toml config file at the root of your project and specify the path to a database schema file:

$ cat ./sqlvet.toml
schema_path = "schema/full_schema.sql"

$ sqlvet .
Loaded DB schema from schema/full_schema.sql
        table alembic_version with 1 columns
        table incident with 13 columns
        table usr with 4 columns
Exec @ ./pkg/incident.go:75:19
        UPDATE incident SET oops = $1 WHERE id = $2

        ERROR: column `oops` is not defined in table `incident`

Checked 10 SQL queries.
Identified 1 errors.

Customer query functions and libraries

By default, sqlvet checks all calls to query function in database/sql, github.com/jmoiron/sqlx, github.com/jinzhu/gorm and go-gorp/gorp libraries. You can however configure it to white-list arbitrary query functions like below:

[[sqlfunc_matchers]]
  pkg_path = "github.com/mattermost/gorp"
  [[sqlfunc_matchers.rules]]
    query_arg_name = "query"
    query_arg_pos  = 0
  [[sqlfunc_matchers.rules]]
    query_arg_name = "sql"
    query_arg_pos  = 0

The above config tells sqlvet to analyze any function/method from github.com/mattermost/gorp package that has the first parameter named either query or sql.

You can also match query functions by names:

[[sqlfunc_matchers]]
  pkg_path = "github.com/jmoiron/sqlx"
  [[sqlfunc_matchers.rules]]
    func_name = "NamedExecContext"
    query_arg_pos  = 1

The above config tells sqlvet to analyze the second parameter of any function/method named NamedExecContext in github.com/jmoiron/sqlx package.

Ignore false positives

To skip a false positive, annotate the relevant line with sqlvet: ignore comment:

func foo() {
    Db.Query(fmt.Sprintf("SELECT %s", "1")) // sqlvet: ignore
}

Acknowledgements

Sqlvet was inspired by safesql and sqlc.

More Repositories

1

leptess

Productive and safe Rust binding for leptonica and tesseract
Rust
258
star
2

vpcstudio

VPC design made easy
TypeScript
128
star
3

shell.py

shell power for python
Python
96
star
4

asciidoc-deckjs

A deck.js backend for asciidoc
CSS
39
star
5

gtest

Go test utility library inspired by pytest
Go
31
star
6

libue

A self-contained minimal library for interacting with Linux hot-plug events
C
25
star
7

pyh3c

Client and Sever for h3c 802.1x authentication
Python
20
star
8

terraform-provider-airflow

Go
20
star
9

iris-python-client

python client for Iris REST api
Python
8
star
10

vimrc-houqp

my vim configuration file
Vim Script
7
star
11

wbwa

一份简陋的AsciiDoc教程
Shell
6
star
12

DebugOnline

an online debuger
PHP
4
star
13

pex_uwsgi

build script for uwsgi with pex support
Python
4
star
14

mindwavesocket

web interface for mindwave metrics
JavaScript
4
star
15

ownPrey

Prey control panel app for ownCloud
PHP
3
star
16

download-release-assets-action

GitHub action to download release assets
Shell
2
star
17

linux.vim

vim plugin for kernel hacking
Vim Script
2
star
18

myNasmTemplate

code template for NASM
Assembly
2
star
19

znc.docker

ZNC docker image
Shell
2
star
20

NoMoreRestriction

A very very very SIMPLE chrome extension that aims to disable annoying scripts.
JavaScript
1
star
21

longkey

My fork of longkey for linux, the original projects seems not in maintain anymore, I am still trying to contact the author...
Perl
1
star
22

bibdb

Translates BibTeX bibliographic entries to DocBook XML. Author: Sylvain Schmitz
Shell
1
star
23

markdownpage

A simple script that generates html page with template file and markdown file as input
Python
1
star
24

map

deprecated, project moved to https://github.com/owncloud/maps
JavaScript
1
star