You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tooling.apache.org by as...@apache.org on 2026/05/05 20:59:36 UTC

(tooling-agents) branch main updated: ASVS audit: 15.4.3.md (redacted) [source: apache/steve/v3 @ d0aa7e9]

This is an automated email from the ASF dual-hosted git repository.

asf-tooling pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tooling-agents.git


The following commit(s) were added to refs/heads/main by this push:
     new bebd15c  ASVS audit: 15.4.3.md (redacted) [source: apache/steve/v3 @ d0aa7e9]
bebd15c is described below

commit bebd15c7ca4e7eb97de016ed67166933a1c782b2
Author: ASF Tooling Team <ro...@apache.org>
AuthorDate: Tue May 5 22:54:07 2026 +0200

    ASVS audit: 15.4.3.md (redacted) [source: apache/steve/v3 @ d0aa7e9]
---
 ASVS/reports/steve/v3/d0aa7e9/all/15.4.3.md | 230 ++++++++++++++++++++++++++++
 1 file changed, 230 insertions(+)

diff --git a/ASVS/reports/steve/v3/d0aa7e9/all/15.4.3.md b/ASVS/reports/steve/v3/d0aa7e9/all/15.4.3.md
new file mode 100644
index 0000000..85e8521
--- /dev/null
+++ b/ASVS/reports/steve/v3/d0aa7e9/all/15.4.3.md
@@ -0,0 +1,230 @@
+# Security Audit Report: ASVS 15.4.3
+
+## Executive Summary
+
+| Field | Value |
+|-------|-------|
+| **Repository** | apache/steve/v3 |
+| **Audit Date** | May 05, 2026 |
+| **Auditor** | Tooling Agents (bundled-pass mode) |
+| **ASVS Requirement** | ASVS 15.4.3 |
+| **Files Analyzed** | 3 relevant / 49 total |
+| **Files Skipped** | 12 |
+
+**Requirement description:** Verify that locks are used consistently to avoid threads getting stuck, whether by waiting on each other or retrying endlessly, and that locking logic stays within the code responsible for managing the resource to ensure locks cannot be inadvertently or maliciously modified by external classes or…
+
+### Findings Overview
+
+| Severity | Count |
+|----------|-------|
+| πŸ”΄ Critical | 0 |
+| 🟠 High | 0 |
+| 🟑 Medium | 2 |
+| 🟒 Low | 1 |
+
+---
+
+## ASVS-15.4.3: Lock Consistency and Deadlock Prevention
+
+### Findings for 15.4.3
+
+### [MEDIUM]
+**Finding ID:** ASVS-1543-MED-001
+
+**File:** `v3/steve/election.py`, method `delete`, lines 54-66; method `add_salts`, lines 121-133
+
+**Vulnerable code (delete):**
+```python
+def delete(self):
+    assert self.is_editable()
+    self.db.conn.execute('BEGIN TRANSACTION')
+    self.c_delete_mayvote.perform(self.eid)
+    self.c_delete_issues.perform(self.eid)
+    self.c_delete_election.perform(self.eid)
+    self.db.conn.execute('COMMIT')
+    self.db.conn.close()
+    self.db = None
+```
+
+**Vulnerable code (add_salts):**
+```python
+def add_salts(self):
+    assert self.is_editable()
+    self.db.conn.execute('BEGIN TRANSACTION')
+    self.q_all_issues.perform(self.eid)
+    for mayvote in self.q_all_issues.fetchall():
+        salt = crypto.gen_salt()
+        self.c_salt_mayvote.perform(salt, mayvote.rowid)
+    self.db.conn.execute('COMMIT')
+```
+
+**Issue:** Neither method has exception handling around the transaction. If any operation between `BEGIN TRANSACTION` and `COMMIT` raises an exception (e.g., `sqlite3.OperationalError`, `sqlite3.DatabaseError`, or even `AssertionError` from an internal check), the transaction remains open, holding a database lock.
+
+**Proof of concept:** 
+1. In `delete()`: If `c_delete_issues.perform()` fails (e.g., due to an unexpected foreign key constraint), the transaction is never committed or rolled back.
+2. The `db.conn.close()` in the finally-equivalent code would implicitly rollback, but only because it's the next statement. If an exception occurs mid-transaction without close, the lock persists until connection timeout or GC.
+
+**Impact:** An unhandled exception during a transaction leaves the database locked, causing all subsequent write operations to block or fail with `SQLITE_BUSY`. In a web application, this could effectively cause a denial of service until the connection is garbage collected.
+
+**Remediation:**
+```python
+def delete(self):
+    assert self.is_editable()
+    try:
+        self.db.conn.execute('BEGIN TRANSACTION')
+        self.c_delete_mayvote.perform(self.eid)
+        self.c_delete_issues.perform(self.eid)
+        self.c_delete_election.perform(self.eid)
+        self.db.conn.execute('COMMIT')
+    except Exception:
+        self.db.conn.execute('ROLLBACK')
+        raise
+    finally:
+        self.db.conn.close()
+        self.db = None
+```
+
+Or use a context manager:
+```python
+def delete(self):
+    assert self.is_editable()
+    with self.db.conn:  # auto-commit/rollback
+        self.c_delete_mayvote.perform(self.eid)
+        self.c_delete_issues.perform(self.eid)
+        self.c_delete_election.perform(self.eid)
+    self.db.conn.close()
+    self.db = None
+```
+
+---
+
+### [MEDIUM]
+**Finding ID:** ASVS-1543-MED-002
+
+**File:** `v3/steve/election.py`, method `open`, lines 70-79
+
+**Vulnerable code:**
+```python
+def open(self, pdb):
+    assert self.is_editable()
+    self.add_salts()  # Contains its own BEGIN TRANSACTION / COMMIT
+    
+    edata = self.gather_election_data(pdb)
+    salt = crypto.gen_salt()
+    opened_key = crypto.gen_opened_key(edata, salt)
+    self.c_open.perform(salt, opened_key, self.eid)
+```
+
+**Issue:** The `open()` method performs a multi-step operation that is not fully transactional. `add_salts()` commits its own transaction, then subsequent operations (`c_open.perform`) run in auto-commit mode. If `c_open.perform` fails after `add_salts()` has already committed, the election is left in an inconsistent state: salts are set but the election is never marked as open.
+
+**Data flow:** `add_salts()` COMMIT β†’ `gather_election_data()` β†’ `gen_opened_key()` β†’ `c_open.perform()` (potential failure point)
+
+**Impact:** Election left in a partially-opened state where salts are assigned but the `opened_key` is never stored. The election would appear editable (no `opened_key`) but would have non-null salts, violating `_compute_state()`'s assertions and potentially causing assertion errors on subsequent access.
+
+**Remediation:**
+```python
+def open(self, pdb):
+    assert self.is_editable()
+    
+    self.db.conn.execute('BEGIN IMMEDIATE')
+    try:
+        # Add salts within the same transaction
+        self.q_all_issues.perform(self.eid)
+        for mayvote in self.q_all_issues.fetchall():
+            salt = crypto.gen_salt()
+            self.c_salt_mayvote.perform(salt, mayvote.rowid)
+        
+        edata = self.gather_election_data(pdb)
+        salt = crypto.gen_salt()
+        opened_key = crypto.gen_opened_key(edata, salt)
+        self.c_open.perform(salt, opened_key, self.eid)
+        
+        self.db.conn.execute('COMMIT')
+    except Exception:
+        self.db.conn.execute('ROLLBACK')
+        raise
+```
+
+---
+
+### [LOW]
+**Finding ID:** ASVS-1543-LOW-001
+
+**File:** `v3/steve/election.py`, various methods
+
+**Observation:** The locking strategy is inconsistent across write operations. Some methods (`delete`, `add_salts`) use explicit transactions, while others (`close`, `add_vote`, `set_open_at`, `set_close_at`) rely on SQLite's auto-commit mode. This inconsistency makes it difficult to reason about concurrent behavior and increases the risk of subtle race conditions.
+
+**Impact:** Maintainability and correctness concern. Future modifications may introduce bugs due to the unclear locking contract.
+
+**Remediation:** Establish a consistent pattern β€” either all write operations use explicit transactions, or use SQLite's `isolation_level` configuration consistently. Document the concurrency contract.
+
+---
+
+### Security Controls Inventory for 15.4.3
+
+| Control | Location | Coverage Status |
+|---------|----------|-----------------|
+| Explicit transactions | `delete()`, `add_salts()` | ⚠️ Present but missing error handling |
+| Rollback on error | N/A | ❌ NOT present anywhere |
+| Context manager for transactions | N/A | ❌ NOT used |
+| Consistent locking pattern | Across Election class | ❌ Inconsistent β€” some methods transactional, others auto-commit |
+| Lock encapsulation (private) | `Election` class | βœ… DB connection is private to each instance |
+| Deadlock prevention | N/A | βœ… Single-database design prevents multi-resource deadlocks |
+
+### Positive Patterns for 15.4.3
+
+1. **Single-resource design**: All operations target a single SQLite database, eliminating the possibility of cross-resource deadlocks.
+2. **Instance-scoped connections**: Each `Election` instance owns its own connection, preventing lock sharing between unrelated operations.
+3. **Ordered deletions**: The `delete()` method explicitly orders operations by referential integrity (mayvote β†’ issues β†’ election), preventing constraint-related failures mid-transaction.
+4. **No external lock exposure**: The database connection is accessed only through the `Election` class methods (via `__getattr__` proxy), not exposed directly to callers.
+
+---
+
+
+
+## Cross-cutting Architecture Observations
+
+1. **Python + Async + SQLite Architecture**: The combination of Quart (async framework) with SQLite (file-based database) creates an inherent tension. SQLite's file-level locking doesn't align well with concurrent async request handling. While the single-threaded nature of asyncio mitigates threading issues, the lack of connection pooling and proper busy-timeout handling means production deployments could experience contention.
+
+2. **Assertion-based validation**: The codebase extensively uses `assert` statements for state validation (`assert self.is_editable()`). In production Python (`-O` flag), assertions are stripped entirely, removing all state guards. This is a systemic architectural risk that spans multiple ASVS requirements.
+
+3. **EasyDict usage throughout**: The pervasive use of `EasyDict` means type information is lost at runtime. While convenient, it makes it impossible to enforce schemas on data structures, creating a gap for type safety (15.3.5) and mass assignment concerns.
+
+4. **Separation of concerns**: The `Election` class handles both data access and business logic. The web layer (`pages.py`) directly instantiates `Election` objects rather than going through a service layer, which makes it harder to apply cross-cutting concerns like atomic transactions spanning business operations.
+
+5. **Authorization gap pattern**: The `### check authz` placeholders throughout `pages.py` indicate a systematic Type B gap β€” the authentication framework exists and is applied, but fine-grained authorization (ownership verification) is consistently missing.
+
+---
+
+## Cross-cutting Recommendations
+
+### Immediate (Critical/High Priority)
+
+1. **Replace assertions with proper validation**: Convert all `assert self.is_editable()` patterns to explicit checks that raise proper exceptions and cannot be disabled with `-O`:
+   ```python
+   if not self.is_editable():
+       raise ElectionBadState(self.eid, self.get_state(), self.S_EDITABLE)
+   ```
+
+2. **Add transaction error handling**: Wrap all `BEGIN TRANSACTION`/`COMMIT` blocks in try/except with explicit `ROLLBACK` on failure to prevent database lock leaks.
+
+3. **Make election state transitions atomic**: Combine state checks and state changes into single transactions using `BEGIN IMMEDIATE` to prevent TOCTOU races on open/close operations.
+
+### Short-term (Medium Priority)
+
+4. **Configure SQLite busy timeout**: Add `PRAGMA busy_timeout = 5000` on every new connection to handle concurrent access gracefully.
+
+5. **Add type validation for JSON inputs**: Verify that `get_json()` returns a dict and that expected fields have correct types before processing.
+
+6. **Implement duplicate parameter detection**: Check for multi-valued form parameters in vote submission and other form-processing endpoints.
+
+7. **Unify transaction patterns**: Adopt a consistent approach (e.g., context manager) for all database write operations.
+
+### Long-term (Architectural)
+
+8. **Consider database migration**: For production use with concurrent access, consider migrating from SQLite to PostgreSQL, which has proper row-level locking and better concurrency support.
+
+9. **Add application-level write serialization**: Implement an asyncio Lock for state-changing operations on the same election to prevent concurrent modification.
+
+10. **Implement a service layer**: Separate business logic from data access to centralize transaction management and make it easier to apply atomic operation patterns consistently.
+        
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@tooling.apache.org
For additional commands, e-mail: commits-help@tooling.apache.org