This repository has been archived by the owner on Sep 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
Add support for sqlx #8
Open
richo
wants to merge
1
commit into
master
Choose a base branch
from
richo-sqlx
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,13 @@ import ( | |
"golang.org/x/tools/go/ssa/ssautil" | ||
) | ||
|
||
type Config struct { | ||
Sqlx bool | ||
DatabaseSql bool | ||
} | ||
|
||
const SQLX string = "github.com/jmoiron/sqlx" | ||
|
||
func main() { | ||
var verbose, quiet bool | ||
flag.BoolVar(&verbose, "v", false, "Verbose mode") | ||
|
@@ -28,6 +35,11 @@ func main() { | |
flag.PrintDefaults() | ||
} | ||
|
||
config := Config{ | ||
Sqlx: false, | ||
DatabaseSql: false, | ||
} | ||
|
||
flag.Parse() | ||
pkgs := flag.Args() | ||
if len(pkgs) == 0 { | ||
|
@@ -38,21 +50,51 @@ func main() { | |
c := loader.Config{ | ||
FindPackage: FindPackage, | ||
} | ||
c.Import("database/sql") | ||
for _, pkg := range pkgs { | ||
c.Import(pkg) | ||
} | ||
p, err := c.Load() | ||
|
||
imports := GetImports(p) | ||
|
||
if _, exist := imports["database/sql"]; exist { | ||
if verbose { | ||
fmt.Println("Enabling support for database/sql") | ||
} | ||
config.DatabaseSql = true | ||
} | ||
|
||
if _, exist := imports["github.com/jmoiron/sqlx"]; exist { | ||
if verbose { | ||
fmt.Println("Enabling support for sqlx") | ||
} | ||
config.Sqlx = true | ||
} | ||
|
||
if !(config.Sqlx || config.DatabaseSql) { | ||
fmt.Printf("No packages in %v include a supported database driver", pkgs) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be honest, I'd probably just skip the whole import checking business. If a package doesn't use |
||
os.Exit(2) | ||
} | ||
|
||
if err != nil { | ||
fmt.Printf("error loading packages %v: %v\n", pkgs, err) | ||
os.Exit(2) | ||
} | ||
|
||
s := ssautil.CreateProgram(p, 0) | ||
s.Build() | ||
|
||
qms := FindQueryMethods(p.Package("database/sql").Pkg, s) | ||
qms := make([]*QueryMethod, 0) | ||
|
||
if config.DatabaseSql { | ||
qms = append(qms, FindQueryMethods(p.Package("database/sql").Pkg, s)...) | ||
} | ||
if config.Sqlx { | ||
qms = append(qms, FindQueryMethods(p.Package(SQLX).Pkg, s)...) | ||
} | ||
|
||
if verbose { | ||
fmt.Println("database/sql functions that accept queries:") | ||
fmt.Println("database driver functions that accept queries:") | ||
for _, m := range qms { | ||
fmt.Printf("- %s (param %d)\n", m.Func, m.Param) | ||
} | ||
|
@@ -75,6 +117,7 @@ func main() { | |
} | ||
|
||
bad := FindNonConstCalls(res.CallGraph, qms) | ||
|
||
if len(bad) == 0 { | ||
if !quiet { | ||
fmt.Println(`You're safe from SQL injection! Yay \o/`) | ||
|
@@ -164,6 +207,17 @@ func FindMains(p *loader.Program, s *ssa.Program) []*ssa.Package { | |
return mains | ||
} | ||
|
||
func GetImports(p *loader.Program) map[string]interface{} { | ||
packages := make(map[string]interface{}) | ||
for _, info := range p.AllPackages { | ||
// Invert the map so we can do lookups more easily | ||
if info.Importable { | ||
packages[info.Pkg.Path()] = nil | ||
} | ||
} | ||
return packages | ||
} | ||
|
||
// FindNonConstCalls returns the set of callsites of the given set of methods | ||
// for which the "query" parameter is not a compile-time constant. | ||
func FindNonConstCalls(cg *callgraph.Graph, qms []*QueryMethod) []ssa.CallInstruction { | ||
|
@@ -196,6 +250,13 @@ func FindNonConstCalls(cg *callgraph.Graph, qms []*QueryMethod) []ssa.CallInstru | |
} | ||
v := args[m.Param] | ||
if _, ok := v.(*ssa.Const); !ok { | ||
// This is super lurky, but sqlx wants to hand query objects about under | ||
// the hood. We could do clever taint analysis, but it's easier | ||
// to just bless the innards of sqlx internally, and rely on it | ||
// to do Reasonable Things under the hood. | ||
if edge.Caller.Func.Pkg.Pkg.Path() == SQLX { | ||
continue | ||
} | ||
bad = append(bad, edge.Site) | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You use this quite inconsistently
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh right, I think I missed a couple