Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Safesql: args are not spread out when calling wrapped database/sql functions #227

Closed
mattiasgrenfeldt opened this issue Dec 2, 2020 · 2 comments
Labels
bug Something isn't working

Comments

@mattiasgrenfeldt
Copy link
Contributor

Hello friends! I tried using safesql and it didn't work out that well.

database/sql is not wrapped properly in safesql/sqlwrap.go. The args are not spread out in the call to the underlying functions. Here is Exec as an example:

// Exec is a tiny wrapper for https://pkg.go.dev/sql#DB.Exec
func (db DB) Exec(query TrustedSQLString, args ...interface{}) (Result, error) {
	return db.db.Exec(query.s, args) // <-- Should be args...
}

This seems to be the case for all functions taking an args ...interface{} in sqlwrap.go.
Here is a small POC:

package main

import (
	"fmt"
	"log"

	"github.com/google/go-safeweb/safesql"
	_ "github.com/jackc/pgx/v4/stdlib"
)

func main() {
	db, err := safesql.Open("pgx", "postgres://postgres:password@localhost:5432/")
	if err != nil {
		log.Fatal(err)
	}
	if _, err := db.Exec(safesql.New("CREATE TABLE blah (id int)")); err != nil {
		log.Fatalf("db.Exec got error: %v", err)
	}
	fmt.Println("It worked")
}

Running this gives:

2020/12/02 23:32:45 db.Exec got error: expected 0 arguments, got 1
exit status 1

But if i run the same code using database/sql it works.

@mattiasgrenfeldt mattiasgrenfeldt added the bug Something isn't working label Dec 2, 2020
@kele
Copy link
Collaborator

kele commented Dec 7, 2020

Huh, this is an interesting bug around ... interface{}! I wonder if a tool like semgrep could help with that (tagged dgryski at twitter who AFAIU owns Go's semgrep): https://twitter.com/kele_codes/status/1335995071557218305

@empijei empijei closed this as completed Dec 16, 2020
@empijei
Copy link
Contributor

empijei commented Dec 16, 2020

Thanks for reporting this. This is now fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants