mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
gosec severity=medium passes, all severity=low errors are from unhandled errors, we have 107 of them. tbh it doesn't look worth it to me, but maybe there are a few assholes even itchier than mine out there. medium has some good stuff in it, and of course high makes sense if we're gonna do this at all. this adds some nosec annotations for some things like sql sprintfs where we know it's clean (we're constructing the strings with variables in them). fixed up other spots where we were sprinting without need. some stuff like filepath.Clean when opening a file from a variable, and file permissions, easy stuff... I can't get the CI build to shut up, but I can locally get it to be pretty quiet about imports and it just outputs the gosec output. fortunately, it still works as expected even when it's noisy. I got it to shut up by unsetting some of the go mod flags locally, but that doesn't seem to quite do it in circle, printed the env out and don't see them, so idk... i give up, this works closes #1303
74 lines
1.4 KiB
Go
74 lines
1.4 KiB
Go
package sqlite
|
|
|
|
import (
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/fnproject/fn/api/datastore/sql/dbhelper"
|
|
"github.com/jmoiron/sqlx"
|
|
"github.com/mattn/go-sqlite3"
|
|
)
|
|
|
|
type sqliteHelper int
|
|
|
|
func (sqliteHelper) Supports(scheme string) bool {
|
|
switch scheme {
|
|
case "sqlite3", "sqlite":
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (sqliteHelper) PreConnect(url *url.URL) (string, error) {
|
|
// make all the dirs so we can make the file..
|
|
dir := filepath.Dir(url.Path)
|
|
err := os.MkdirAll(dir, 0750)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return strings.TrimPrefix(url.String(), url.Scheme+"://"), nil
|
|
}
|
|
|
|
func (sqliteHelper) PostCreate(db *sqlx.DB) (*sqlx.DB, error) {
|
|
db.SetMaxOpenConns(1)
|
|
return db, nil
|
|
}
|
|
|
|
func (sqliteHelper) CheckTableExists(tx *sqlx.Tx, table string) (bool, error) {
|
|
query := tx.Rebind(`SELECT count(*)
|
|
FROM sqlite_master
|
|
WHERE name = ?`)
|
|
|
|
row := tx.QueryRow(query, table)
|
|
|
|
var count int
|
|
err := row.Scan(&count)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
exists := count > 0
|
|
return exists, nil
|
|
}
|
|
|
|
func (sqliteHelper) String() string {
|
|
return "sqlite"
|
|
}
|
|
|
|
func (sqliteHelper) IsDuplicateKeyError(err error) bool {
|
|
sqliteErr, ok := err.(sqlite3.Error)
|
|
if ok {
|
|
if sqliteErr.ExtendedCode == sqlite3.ErrConstraintUnique || sqliteErr.ExtendedCode == sqlite3.ErrConstraintPrimaryKey {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func init() {
|
|
dbhelper.Register(sqliteHelper(0))
|
|
}
|