feat: add SQL/SQLite, AppSec, Capstone incident-tracker, Gitea Actions CI and spaced repetition
Test and Lint / quality (push) Successful in 1m50s

This commit is contained in:
soda64 committed 2026-12-02 19:03:16 +01:00
1 parent 015eae08c3
commit 9130af822e
19 files changed
+722 -7

No files matched your search

@@ -0,0 +1,7 @@
[package]
name = "eco05-sql-sqlite"
version = "0.1.0"
edition = "2021"
[dependencies]
rusqlite = { version = "0.32", features = ["bundled"] }
@@ -0,0 +1,14 @@
# 05 — SQL i SQLite (`rusqlite`, parametryzowane zapytania, ochrona przed SQLi)
Teoria: [notes/06-sql-and-databases.md](../../../notes/06-sql-and-databases.md)
## Zadania (`src/lib.rs`)
Zaimplementuj `ServiceRegistry` na `rusqlite`:
1. `init_schema(&self) -> rusqlite::Result<()>` — `CREATE TABLE IF NOT EXISTS services(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT UNIQUE NOT NULL, owner TEXT NOT NULL)`.
2. `register(&self, name: &str, owner: &str) -> rusqlite::Result<i64>` — parametryzowany `INSERT` (`params![name, owner]`) i `self.conn.last_insert_rowid()`.
3. `find_by_owner(&self, owner: &str) -> rusqlite::Result<Vec<String>>` — posortowana lista nazw bez podatności na SQL Injection.
## Uruchomienie testów
```bash
make test PKG=eco05-sql-sqlite
```
@@ -0,0 +1,63 @@
#![allow(unused_variables, dead_code)]
use rusqlite::{params, Connection};
use std::path::Path;
pub struct ServiceRegistry {
conn: Connection,
}
impl ServiceRegistry {
pub fn open(path: &Path) -> rusqlite::Result<Self> {
Ok(Self {
conn: Connection::open(path)?,
})
}
pub fn init_schema(&self) -> rusqlite::Result<()> {
todo!("Utwórz tabelę services przez self.conn.execute_batch(...)")
}
pub fn register(&self, name: &str, owner: &str) -> rusqlite::Result<i64> {
let _ = params![name, owner];
todo!("Wstaw rekord parametryzowanym INSERT i zwróć self.conn.last_insert_rowid()")
}
pub fn find_by_owner(&self, owner: &str) -> rusqlite::Result<Vec<String>> {
todo!("Parametryzowany SELECT name FROM services WHERE owner = ?1 ORDER BY name ASC")
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
use std::time::{SystemTime, UNIX_EPOCH};
#[test]
fn sqlite_crud_and_sqli_resistance() {
let nanos = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_nanos();
let db = std::env::temp_dir().join(format!("rust-learning-reg-{nanos}.db"));
let reg = ServiceRegistry::open(&db).unwrap();
reg.init_schema().unwrap();
let id1 = reg.register("billing-api", "team-core").unwrap();
let id2 = reg.register("auth-api", "team-core").unwrap();
reg.register("metrics-proxy", "team-sre").unwrap();
assert!(id1 > 0 && id2 > id1);
assert_eq!(
reg.find_by_owner("team-core").unwrap(),
vec![String::from("auth-api"), String::from("billing-api")]
);
// Negative security assertion: SQLi payload must NOT return all rows
assert!(reg.find_by_owner("' OR '1'='1").unwrap().is_empty());
drop(reg);
let _ = fs::remove_file(&db);
}
}