feat: add SQL/SQLite, AppSec, Capstone incident-tracker, Gitea Actions CI and spaced repetition
Test and Lint / quality (push) Successful in 2m13s
Test and Lint / quality (push) Successful in 2m13s
This commit is contained in:
1 parent
011f23e637
commit
33082598fc
19 files changed
+496
-6
No files matched your search
@@ -0,0 +1,14 @@
|
||||
# 05 — SQL i SQLite (`database/sql` + `modernc.org/sqlite`, ochrona przed SQLi)
|
||||
|
||||
Teoria: [notes/06-sql-and-databases.md](../../../notes/06-sql-and-databases.md)
|
||||
|
||||
## Zadania (`sqlite_store.go`)
|
||||
Zaimplementuj `ServiceRegistry` na `database/sql` ze sterownikiem pure-Go `modernc.org/sqlite` (`sql.Open("sqlite", dbPath)`):
|
||||
1. `InitSchema() error` — `CREATE TABLE IF NOT EXISTS services(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT UNIQUE NOT NULL, owner TEXT NOT NULL)`.
|
||||
2. `Register(name, owner string) (int64, error)` — parametryzowany `INSERT` (`?`) i `res.LastInsertId()`.
|
||||
3. `FindByOwner(owner string) ([]string, error)` — posortowana lista nazw serwisów bez podatności na SQL Injection.
|
||||
|
||||
## Uruchomienie testów
|
||||
```bash
|
||||
make test PKG=./exercises/03-ecosystems/05-sql-sqlite
|
||||
```
|
||||
@@ -0,0 +1,36 @@
|
||||
package sqlsqlite
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
|
||||
type ServiceRegistry struct {
|
||||
//lint:ignore U1000 używane po zaimplementowaniu metod
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
func OpenRegistry(dbPath string) (*ServiceRegistry, error) {
|
||||
db, err := sql.Open("sqlite", dbPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &ServiceRegistry{db: db}, nil
|
||||
}
|
||||
|
||||
func (r *ServiceRegistry) Close() error {
|
||||
return r.db.Close()
|
||||
}
|
||||
|
||||
func (r *ServiceRegistry) InitSchema() error {
|
||||
panic("TODO: CREATE TABLE IF NOT EXISTS services (...)")
|
||||
}
|
||||
|
||||
func (r *ServiceRegistry) Register(name, owner string) (int64, error) {
|
||||
panic("TODO: parametryzowany INSERT INTO services(name, owner) VALUES(?, ?)")
|
||||
}
|
||||
|
||||
func (r *ServiceRegistry) FindByOwner(owner string) ([]string, error) {
|
||||
panic("TODO: parametryzowany SELECT name FROM services WHERE owner = ? ORDER BY name ASC")
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package sqlsqlite
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSQLiteCRUDAndSQLiResistance(t *testing.T) {
|
||||
reg, err := OpenRegistry(filepath.Join(t.TempDir(), "registry.db"))
|
||||
if err != nil {
|
||||
t.Fatalf("open failed: %v", err)
|
||||
}
|
||||
defer reg.Close()
|
||||
|
||||
if err := reg.InitSchema(); err != nil {
|
||||
t.Fatalf("init schema failed: %v", err)
|
||||
}
|
||||
id1, err := reg.Register("billing-api", "team-core")
|
||||
if err != nil || id1 <= 0 {
|
||||
t.Fatalf("register failed: id=%d err=%v", id1, err)
|
||||
}
|
||||
_, _ = reg.Register("auth-api", "team-core")
|
||||
_, _ = reg.Register("metrics-proxy", "team-sre")
|
||||
|
||||
got, err := reg.FindByOwner("team-core")
|
||||
if err != nil {
|
||||
t.Fatalf("find failed: %v", err)
|
||||
}
|
||||
want := []string{"auth-api", "billing-api"}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("got %v, want %v", got, want)
|
||||
}
|
||||
|
||||
// Negative security assertion: SQL Injection payload must NOT return all rows
|
||||
sqli, err := reg.FindByOwner("' OR '1'='1")
|
||||
if err != nil || len(sqli) != 0 {
|
||||
t.Fatalf("SQLi resistance failed: got=%v err=%v", sqli, err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user