Skip to content

refactor: harden D1 database operations and optimize memory management (#263)#265

Open
ojaswa072 wants to merge 2 commits intoOWASP-BLT:mainfrom
ojaswa072:fix/database-hardening-mem-safety
Open

refactor: harden D1 database operations and optimize memory management (#263)#265
ojaswa072 wants to merge 2 commits intoOWASP-BLT:mainfrom
ojaswa072:fix/database-hardening-mem-safety

Conversation

@ojaswa072
Copy link
Copy Markdown
Contributor

Summary

Resolves #263. This PR implements a comprehensive hardening of the database layer. While the initial audit was focused on SQL injection, the refactor addresses deeper architectural risks related to the Pyodide/D1 bridge and Worker environment stability.

Key Changes

  • Standardized JsProxy lifecycle: All D1 results (.first(), .all(), and .run()) now consistently call .to_py() followed by .destroy(). This ensures that JavaScript proxy objects are explicitly garbage-collected, preventing memory bloat in the Worker isolate.
  • Unified Chaining Pattern: Refactored functions to use the .prepare().bind().run() inline chaining pattern. This ensures the statement context is preserved across the bridge boundary and improves code readability.
  • Execution Reliability: upsert_pr and other write operations now capture and handle the .run() result, ensuring that silent D1 failures (like constraint violations) can be properly tracked.
  • Security Hardening: Removed the dir(env) debug log from get_db. This prevents the potential exposure of sensitive binding names and secret keys in production logs.

Verification Results

  • Verified local D1 operations with wrangler dev
  • Confirmed JsProxy destruction logic via memory usage monitoring
  • Validated that all queries still use parameter binding (?) exclusively

@github-actions github-actions bot added the files-changed: 4 PR changes 4 files label Feb 28, 2026
@github-actions
Copy link
Copy Markdown

🍃 PR Readiness Check

Check the readiness of this PR on Leaf:
👉 Open on Leaf

Leaf reviews pull requests for operational readiness, security risks, and production-impacting changes before they ship.

@ojaswa072 ojaswa072 force-pushed the fix/database-hardening-mem-safety branch from 4b808c4 to c332edc Compare February 28, 2026 13:42
@github-actions github-actions bot added files-changed: 1 PR changes 1 file and removed files-changed: 4 PR changes 4 files labels Feb 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

files-changed: 1 PR changes 1 file

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Security: SQL Injection Risk in src/database.py via String Interpolation

1 participant