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,12 @@
|
||||
# 06 — Bezpieczeństwo Kodu (AppSec: Path Traversal, Redakcja Sekretów)
|
||||
|
||||
Teoria: [notes/07-appsec-and-secure-coding.md](../../../notes/07-appsec-and-secure-coding.md)
|
||||
|
||||
## Zadania (`appsec.go`)
|
||||
1. `SafeJoin(baseDir, relPath string) (string, error)` — bezpiecznie połącz `baseDir` z `relPath`. Odrzuć ścieżki absolutne i próby wyjścia poza `baseDir` (`../../etc/passwd`) błędem `ErrPathTraversal`.
|
||||
2. `RedactSecrets(logLine string) string` — zamaskuj wartości po `Bearer `, `token=` i `password=` na `***`.
|
||||
|
||||
## Uruchomienie testów
|
||||
```bash
|
||||
make test PKG=./exercises/02-engineering-2026/06-security-appsec
|
||||
```
|
||||
@@ -0,0 +1,13 @@
|
||||
package appsec
|
||||
|
||||
import "errors"
|
||||
|
||||
var ErrPathTraversal = errors.New("path escapes base directory")
|
||||
|
||||
func SafeJoin(baseDir, relPath string) (string, error) {
|
||||
panic("TODO: odrzuć filepath.IsAbs(relPath) oraz ścieżki wychodzące poza baseDir (filepath.Rel -> '..')")
|
||||
}
|
||||
|
||||
func RedactSecrets(logLine string) string {
|
||||
panic("TODO: zamień wartości po 'Bearer ', 'token=' i 'password=' na '***'")
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package appsec
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSafeJoinBlocksTraversal(t *testing.T) {
|
||||
base := t.TempDir()
|
||||
got, err := SafeJoin(base, filepath.Join("configs", "app.yaml"))
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
want := filepath.Join(base, "configs", "app.yaml")
|
||||
if got != want {
|
||||
t.Fatalf("got %q, want %q", got, want)
|
||||
}
|
||||
|
||||
if _, err := SafeJoin(base, "../../etc/passwd"); !errors.Is(err, ErrPathTraversal) {
|
||||
t.Fatalf("expected ErrPathTraversal, got %v", err)
|
||||
}
|
||||
if _, err := SafeJoin(base, "/etc/passwd"); !errors.Is(err, ErrPathTraversal) {
|
||||
t.Fatalf("expected ErrPathTraversal for abs path, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRedactSecrets(t *testing.T) {
|
||||
line := "auth failed header=Bearer s3cr3tTok3n user=admin password=hunter2 token=abc_123"
|
||||
cleaned := RedactSecrets(line)
|
||||
for _, leaked := range []string{"s3cr3tTok3n", "hunter2", "abc_123"} {
|
||||
if strings.Contains(cleaned, leaked) {
|
||||
t.Errorf("secret %q leaked in %q", leaked, cleaned)
|
||||
}
|
||||
}
|
||||
if !strings.Contains(cleaned, "user=admin") {
|
||||
t.Errorf("non-secret field lost in %q", cleaned)
|
||||
}
|
||||
}
|
||||
@@ -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