flawopen.com/SQL इंजेक्शन/Java

SQL इंजेक्शन in Java

गंभीर CWE-89 Draft — pending review
Language: English Português (Brasil) Español Français Deutsch Русский 简体中文 日本語 हिन्दी 한국어 Bahasa Indonesia
सरल व्याख्या (ELI5)

सोचिए एक टिकट काउंटर जहां केवल टिकट नंबर दर्ज करना है। यदि कोई चालाक कोड टाइप करके सिस्टम से सभी टिकट दिखाने को कह दे, तो यह SQL इंजेक्शन है।

इस पृष्ठ पर मुख्य शर्तें
output encoding
Converting special markup characters into safe entities so browsers display them as text rather than script.

क्या हो रहा है

SQL इंजेक्शन in Java occurs when unvalidated strings are concatenated into JDBC Statement or HQL/JPQL queries.

वास्तविक दुनिया का प्रभाव

In 2005, the Samy XSS worm infected over 1 million user profiles on MySpace in under 20 hours, forcing the platform offline.

Documented historical AppSec case study.

असुरक्षित बनाम सुरक्षित कोड

VULNERABLE
// userId comes straight from the request
String query = "SELECT * FROM users "
    + "WHERE id = " + userId;
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(query);
FIXED
// value is bound, never concatenated
String query = "SELECT * FROM users WHERE id = ?";
PreparedStatement stmt =
    connection.prepareStatement(query);
stmt.setString(1, userId);
ResultSet rs = stmt.executeQuery();

समाधान कैसे काम करता है

PreparedStatement precompiles the query structure in the database engine, binding parameter values separately.

भाषा-विशिष्ट सावधानियां

MyBatis: ${} vs #{}

#{} uses safe parameterized binding, while ${} performs raw string substitution.

Hibernate HQL/JPQL

Concatenating HQL queries is just as dangerous as raw SQL. Always use setParameter().

आम गलतफहमियां

"Stored procedures prevent injection"

Only if the stored procedure itself uses parameters rather than dynamic EXECUTE strings.

प्रभावित होने की जांच कैसे करें

grep -rn "createStatement().*executeQuery(.*+" --include="*.java" .
Run SpotBugs with the FindSecBugs plugin in CI to catch SQL_INJECTION_JDBC automatically.

रोकथाम चेकलिस्ट

अक्सर पूछे जाने वाले प्रश्न

Is Spring Data JPA safe?

Derived queries (@Query) are safe if method parameters are bound with :param or ?1.

संदर्भ

इस भाषा में देखें: Java Python JavaScript Java Go PHP C# Rust
यह भी देखें: Cross-Site Scripting Command InjectionPath Traversal