Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .jules/sentinel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## 2024-06-04 - SQLite Dynamic Column Name Injection
**Vulnerability:** SQL injection vulnerability in dynamic column name interpolation (`format!("SELECT {field_name} FROM books ...")`) in `get_book_field`, `get_book_i64_field`, and `get_book_field_value_for_lock`.
**Learning:** Rust's `format!` macro for SQL queries allows injection if the interpolated variable (`field_name`) isn't validated. Column names can't be parameterized in standard SQLite, leading to developers occasionally interpolating them directly.
**Prevention:** Implement strict string validation (e.g., `field_name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')`) before injecting dynamic values like column names or table names into raw SQL strings.
9 changes: 9 additions & 0 deletions src-tauri/src/library/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2196,6 +2196,9 @@ impl Repository {
}

fn get_book_field(&self, book_id: &str, field_name: &str) -> anyhow::Result<Option<String>> {
if !field_name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') {
return Err(anyhow!("Invalid field name"));
}
let conn = self.conn()?;
let query = format!("SELECT {field_name} FROM books WHERE id = ?1");
Ok(
Expand All @@ -2207,6 +2210,9 @@ impl Repository {
}

fn get_book_i64_field(&self, book_id: &str, field_name: &str) -> anyhow::Result<Option<i64>> {
if !field_name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') {
return Err(anyhow!("Invalid field name"));
}
let conn = self.conn()?;
let query = format!("SELECT {field_name} FROM books WHERE id = ?1");
Ok(
Expand Down Expand Up @@ -2315,6 +2321,9 @@ fn get_book_field_value_for_lock(
book_id: &str,
field_name: &str,
) -> anyhow::Result<Option<String>> {
if !field_name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') {
return Err(anyhow!("Invalid field name"));
}
let query = format!("SELECT {field_name} FROM books WHERE id = ?1 LIMIT 1");
if matches!(field_name, "page_count" | "series_index") {
let numeric = tx
Expand Down