🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)
🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)

🔧 Programmierung 🕛 kürzlich 4 Min Lesezeit SECURITY-FEED
0

Learning About Security: SQL Injection

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht




What is SQL Injection?



SQL Injection is an attack technique that manipulates SQL queries by inserting malicious code, allowing unauthorized operations on the database.






SQL Injection Attack Example



Using Bun's SQLite3 driver and Hono, we’ll set up two servers: one on localhost:3000 (target) and one on localhost:4000 (attacker) to demonstrate an SQL Injection attack.






Target Server



On the target server, we create a /user endpoint to retrieve data from a users table based on an id specified in the URL query parameter. We’ll populate this users table with a few records.




CODE
import { Hono } from "hono";
import { Database } from "bun:sqlite";

const app = new Hono();
const db = new Database(":memory:");

db.run(
"CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, username TEXT, password TEXT, role TEXT)"
);
db.run("INSERT INTO users (username, password, role) VALUES ('admin', 'adminpass', 'admin')");
db.run("INSERT INTO users (username, password, role) VALUES ('john_doe', 'john123', 'user')");
db.run("INSERT INTO users (username, password, role) VALUES ('jane_doe', 'jane123', 'user')");
db.run("INSERT INTO users (username, password, role) VALUES ('alice', 'alice123', 'user')");
db.run("INSERT INTO users (username, password, role) VALUES ('bob', 'bob123', 'user')");
db.run("INSERT INTO users (username, password, role) VALUES ('charlie', 'charlie123', 'user')");

app.get("/user", (c) => {
const id = c.req.query("id");
const query = db.query(`SELECT * FROM users WHERE id = ${id}`);
const user = query.all();
return c.json(user);
});

export default app;









Attacker Server



On the attacker server, we create an /attack endpoint that sends a request to the /user endpoint on the target server with a SQL Injection attempt.




CODE
import { Hono } from "hono";

const app = new Hono();

app.get("/attack", async (c) => {
const response = await fetch("http://localhost:3000/user?id=1 OR 1=1");
const data = await response.json();
return c.json(data);
});

export default {
port: 4000,
fetch: app.fetch,
};









Launching the Attack



By accessing http://localhost:4000/attack, the following JSON response is returned:




CODE
[
{"id":1,"username":"admin","password":"adminpass","role":"admin"},
{"id":2,"username":"john_doe","password":"john123","role":"user"},
{"id":3,"username":"jane_doe","password":"jane123","role":"user"},
{"id":4,"username":"alice","password":"alice123","role":"user"},
{"id":5,"username":"bob","password":"bob123","role":"user"},
{"id":6,"username":"charlie","password":"charlie123","role":"user"}
]






This occurs because the target server executes SELECT * FROM users WHERE id=1 OR 1=1. The 1=1 condition is always true, so it includes all rows in the query result, revealing all user data.






SQL Injection Mitigation



One way to prevent SQL Injection is to use parameterized queries. With parameterized queries, user input is treated as data, not executable SQL, nullifying injection attempts.






Fixing the Target Code



Here’s how we can modify the target server’s /user endpoint to prevent SQL Injection:




CODE
app.get("/user", (c) => {
const id = c.req.query("id");
const query = db.query(`SELECT * FROM users WHERE id = ?`);
const user = query.all(id);
return c.json(user);
});






With this change, an attack attempt will return an empty JSON.






Cases Where Parameterized Queries Are Ineffective



If table names or column names are dynamically modified based on user input, parameterized queries may not offer protection. This could still result in unauthorized data access.






Example with Dynamic Table Name



Sending a table=sqlite_master -- parameter retrieves database schema information, as sqlite_master holds metadata in SQLite.






Target Server Code






CODE
app.get("/select_table", (c) => {
const table = c.req.query("table");
const query = db.query(`SELECT * FROM ${table} WHERE role = ?`);
const results = query.all("user");

return c.json({
message: `Data from table ${table}`,
data: results,
});
});









Attacker Server Code






CODE
app.get("/attack_table", async (c) => {
const targetUrl = "http://localhost:3000/select_table?table=sqlite_master --";
const response = await fetch(targetUrl);
const result = await response.json();

return c.json(result);
});









Mitigation Using a Whitelist



Since table names cannot be parameterized, validating them with a whitelist is necessary to prevent SQL Injection.




CODE
app.get("/select_table", (c) => {
const table = c.req.query("table");

// Validate table name using a whitelist
const allowedTables = ["users"];
if (!allowedTables.includes(table)) {
return c.json({ message: "Invalid table name" }, 400);
}

const query = db.query(`SELECT id, username, role FROM ${table} WHERE role = ?`);
const results = query.all("user");

return c.json({
message: `Data from table ${table}`,
data: results,
});
});






By enforcing a whitelist, we ensure only authorized tables are queried, providing a safeguard against SQL Injection in cases where parameterization isn’t possible.

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:
Community Threat-Level Barometer
Live Votum

Wie stufst du das Risiko dieser Schwachstelle / Bedrohung für dein Unternehmen ein?

Noch keine Stimmen — schätze das Risiko als Erster ein.

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
3 Quellen
GPT-6 Astra Release Today? OpenAI’s Next Major AI Model Is Almost Here
1 Quelle
Apple accuses OpenAI of destroying evidence as trade-secrets fight intensifies
1 Quelle
Major AI platforms go down in unprecedented simultaneous outage
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Learning About Security: SQL Injection

Thematisch verwandte Begriffe: Learning, About, Security, Injection · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...