feat: add SQL/SQLite, AppSec, Capstone incident-tracker, Gitea Actions CI and spaced repetition
Test and Lint / quality (push) Successful in 34s
Test and Lint / quality (push) Successful in 34s
This commit is contained in:
1 parent
0ac80d60d4
commit
9430a194c3
18 files changed
+422
-7
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.py`)
|
||||
1. `safe_join(base_dir: Path, relative_path: str) -> Path` — bezpiecznie połącz `base_dir` z `relative_path`. Jeśli wynikowa ścieżka (po `resolve()`) ucieka poza `base_dir.resolve()` (np. `../../etc/passwd` lub ścieżka absolutna `/etc/passwd`), rzuć `PathTraversalError`.
|
||||
2. `redact_secrets(log_line: str) -> str` — zamaskuj wartości po `token=`, `password=` lub `Bearer ` na `***`, aby sekrety nigdy nie trafiały do logów.
|
||||
|
||||
## Uruchomienie testów
|
||||
```bash
|
||||
make test MOD=02-engineering-2026/06-security-appsec
|
||||
```
|
||||
@@ -0,0 +1,17 @@
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
class PathTraversalError(ValueError):
|
||||
"""Próba wyjścia poza dozwolony katalog bazowy."""
|
||||
|
||||
|
||||
def safe_join(base_dir: Path, relative_path: str) -> Path:
|
||||
raise NotImplementedError(
|
||||
"Rozwiąż (base_dir / relative_path).resolve() i sprawdź is_relative_to(base_dir.resolve())"
|
||||
)
|
||||
|
||||
|
||||
def redact_secrets(log_line: str) -> str:
|
||||
_ = re
|
||||
raise NotImplementedError("Zamień wartości po Bearer , token= oraz password= na '***'")
|
||||
@@ -0,0 +1,24 @@
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from appsec import PathTraversalError, redact_secrets, safe_join
|
||||
|
||||
|
||||
def test_safe_join_allows_nested_and_blocks_traversal(tmp_path: Path) -> None:
|
||||
ok = safe_join(tmp_path, "configs/app.yaml")
|
||||
assert ok == (tmp_path / "configs" / "app.yaml").resolve()
|
||||
|
||||
with pytest.raises(PathTraversalError):
|
||||
safe_join(tmp_path, "../../etc/passwd")
|
||||
|
||||
with pytest.raises(PathTraversalError):
|
||||
safe_join(tmp_path, "/etc/passwd")
|
||||
|
||||
|
||||
def test_redact_secrets_masks_credentials() -> None:
|
||||
line = "auth failed header=Bearer s3cr3tTok3n user=admin password=hunter2 token=abc_123"
|
||||
cleaned = redact_secrets(line)
|
||||
assert "s3cr3tTok3n" not in cleaned
|
||||
assert "hunter2" not in cleaned
|
||||
assert "abc_123" not in cleaned
|
||||
assert "user=admin" in cleaned
|
||||
@@ -0,0 +1,14 @@
|
||||
# 05 — SQL i SQLite (`sqlite3`, parametryzowane zapytania, ochrona przed SQLi)
|
||||
|
||||
Teoria: [notes/06-sql-and-databases.md](../../../notes/06-sql-and-databases.md)
|
||||
|
||||
## Zadania (`sql_sqlite.py`)
|
||||
Zaimplementuj `ServiceRegistry` na wbudowanym `sqlite3`:
|
||||
1. `init_schema()` — utwórz tabelę `services(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT UNIQUE NOT NULL, owner TEXT NOT NULL)`.
|
||||
2. `register(name: str, owner: str) -> int` — wstaw rekord **parametryzowanym zapytaniem** (`?`) i zwróć `lastrowid`.
|
||||
3. `find_by_owner(owner: str) -> list[str]` — zwróć posortowaną listę nazw serwisów danego właściciela (bez podatności na SQL Injection!).
|
||||
|
||||
## Uruchomienie testów
|
||||
```bash
|
||||
make test MOD=03-ecosystems/05-sql-sqlite
|
||||
```
|
||||
@@ -0,0 +1,17 @@
|
||||
import sqlite3
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
class ServiceRegistry:
|
||||
def __init__(self, db_path: Path) -> None:
|
||||
self._db_path = db_path
|
||||
|
||||
def init_schema(self) -> None:
|
||||
_ = sqlite3
|
||||
raise NotImplementedError("Utwórz tabelę services (CREATE TABLE IF NOT EXISTS ...)")
|
||||
|
||||
def register(self, name: str, owner: str) -> int:
|
||||
raise NotImplementedError("Wstaw (name, owner) parametryzowanym INSERT i zwróć id")
|
||||
|
||||
def find_by_owner(self, owner: str) -> list[str]:
|
||||
raise NotImplementedError("Zwróć posortowane nazwy serwisów dla owner (parametryzowane ?)")
|
||||
@@ -0,0 +1,21 @@
|
||||
from pathlib import Path
|
||||
|
||||
from sql_sqlite import ServiceRegistry
|
||||
|
||||
|
||||
def test_sqlite_crud_and_sqli_resistance(tmp_path: Path) -> None:
|
||||
db = tmp_path / "registry.db"
|
||||
reg = ServiceRegistry(db)
|
||||
reg.init_schema()
|
||||
|
||||
id1 = reg.register("billing-api", "team-core")
|
||||
id2 = reg.register("auth-api", "team-core")
|
||||
reg.register("metrics-proxy", "team-sre")
|
||||
|
||||
assert id1 > 0
|
||||
assert id2 > id1
|
||||
assert reg.find_by_owner("team-core") == ["auth-api", "billing-api"]
|
||||
|
||||
# Negative security assertion: SQL Injection payload must NOT return all rows
|
||||
sqli_payload = "' OR '1'='1"
|
||||
assert reg.find_by_owner(sqli_payload) == []
|
||||
Reference in new issue
Block a user