{"id":48977651,"url":"https://github.com/antoineburet/sql-injection-prevention-demo","last_synced_at":"2026-04-18T10:10:25.722Z","repository":{"id":324499604,"uuid":"1097438452","full_name":"antoineburet/sql-injection-prevention-demo","owner":"antoineburet","description":"A Node.js demo of SQL injection, plain text password storage, and how to fix it with parameterized queries and hashing.","archived":false,"fork":false,"pushed_at":"2025-11-16T08:29:10.000Z","size":23,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-11-16T09:09:17.867Z","etag":null,"topics":["authentication","mysql","nodejs","security","security-demo","sql-injection"],"latest_commit_sha":null,"homepage":"","language":"HTML","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/antoineburet.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-11-16T07:08:05.000Z","updated_at":"2025-11-16T08:29:14.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/antoineburet/sql-injection-prevention-demo","commit_stats":null,"previous_names":["antoineburet/sql-injection-prevention-demo"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/antoineburet/sql-injection-prevention-demo","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antoineburet%2Fsql-injection-prevention-demo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antoineburet%2Fsql-injection-prevention-demo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antoineburet%2Fsql-injection-prevention-demo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antoineburet%2Fsql-injection-prevention-demo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/antoineburet","download_url":"https://codeload.github.com/antoineburet/sql-injection-prevention-demo/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antoineburet%2Fsql-injection-prevention-demo/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31964669,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-18T00:39:45.007Z","status":"online","status_checked_at":"2026-04-18T02:00:07.018Z","response_time":103,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["authentication","mysql","nodejs","security","security-demo","sql-injection"],"created_at":"2026-04-18T10:10:24.933Z","updated_at":"2026-04-18T10:10:25.714Z","avatar_url":"https://github.com/antoineburet.png","language":"HTML","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cimg width=\"1586\" height=\"340\" alt=\"sqli-demo-screenshot\" src=\"https://github.com/user-attachments/assets/b5383400-e0b9-4fac-ac66-b77ad9601b95\" /\u003e\n\n# SQL Injection Prevention Demo\n\nThis project is a simple Node.js, Express, and MySQL application built to demonstrate **two major security flaws** and, more importantly, how to fix them.\n\nIt provides a hands-on comparison of a dangerously vulnerable login system versus a modern, secure one.\n\n## 🚀 Project Purpose\n\nThis app features three routes to illustrate key security concepts:\n\n1.  **`/register` (Flawed but functional):**\n    * Securely validates input (e.g., password length).\n    * Securely hashes the password using **`bcrypt`**.\n    * **🚨 Major Flaw:** It *also* saves the plain text password in a `password_clear` column, completely defeating the purpose of hashing.\n\n2.  **`/login` (Extremely Vulnerable):**\n    * **🚨 Flaw 1 (SQL Injection):** This route builds its query by concatenating strings, making it vulnerable to a classic `' OR 1 = 1 #` SQL injection attack.\n    * **🚨 Flaw 2 (Plain Text):** It works by checking against the `password_clear` column, demonstrating an insecure authentication method.\n\n3.  **`/login-secure` (The Correct Way):**\n    * **✅ Secure (Parameterized Queries):** This route uses prepared statements (`?` placeholders), which makes SQL injection impossible.\n    * **✅ Secure (Hashing):** It *only* reads the `bcrypt` hash and uses the `bcrypt.compare()` function to safely verify the user's password.\n\n\n\n---\n\n## ⚙️ How to Run\n\n### 1. Prerequisites\n* Node.js\n* MySQL (Workbench or server)\n\n### 2. Setup\n1.  **Clone the repository:**\n    ```sh\n    git clone https://github.com/antoineburet/sql-injection-prevention-demo.git\n    cd sql-injection-prevention-demo\n    ```\n2.  **Install dependencies:**\n    ```sh\n    npm install\n    ```\n3.  **Set up the database:**\n    * Open MySQL Workbench and connect to your server.\n    * Create a database named `accounts`.\n    * Create the `credentials` table using the following SQL. (This schema is *intentionally* insecure.)\n        ```sql\n        USE accounts;\n        CREATE TABLE credentials (\n          id INT NOT NULL AUTO_INCREMENT,\n          user VARCHAR(45) NOT NULL,\n          password VARCHAR(255) NOT NULL, -- For the bcrypt hash\n          password_clear VARCHAR(45) NULL, -- INTENTIONALLY INSECURE COLUMN\n          PRIMARY KEY (id),\n          UNIQUE INDEX user_UNIQUE (user ASC)\n        );\n        ```\n4.  **Create your environment file:**\n    * Create a file named `.env` in the root of the project.\n    * Add your database credentials:\n        ```\n        DB_HOST=localhost\n        DB_USER=root\n        DB_PASSWORD=your_mysql_password\n        DB_NAME=accounts\n        ```\n\n### 3. Run the Server\n```sh\nnode server.js\n````\n\nThe server will be running on `http://localhost:3000`.\n\n-----\n\n## 🛡️ How to Test the Vulnerabilities\n\n1.  **Create an Account:**\n\n      * Use the \"Create Account\" form. (e.g., `user: admin`, `pass: password123`)\n      * *Observe:* The `credentials` table now contains both a `bcrypt` hash and the plain text \"password123\".\n\n2.  **Test the Vulnerable Login:**\n\n      * **Test 1 (Normal Login):** Use `user: admin`, `pass: password123`. It will **work** because it reads the plain text column.\n      * **Test 2 (SQL Injection):** Use `user: ' OR 1 = 1 #`, `pass: (anything)`. It will **work** and log you in as the first user in the database.\n\n3.  **Test the Secure Login:**\n\n      * **Test 1 (Normal Login):** Use `user: admin`, `pass: password123`. It will **work** because it correctly uses `bcrypt.compare()`.\n      * **Test 2 (SQL Injection):** Use `user: ' OR 1 = 1 #`, `pass: (anything)`. It will **FAIL** (as it should). The parameterized query treats the entire string as a username, and no user exists with that literal name.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fantoineburet%2Fsql-injection-prevention-demo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fantoineburet%2Fsql-injection-prevention-demo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fantoineburet%2Fsql-injection-prevention-demo/lists"}