flawopen.com/Ruby में SQL Injection/ActiveRecord where()
क्या Rails where() में string interpolation सुरक्षित है?
SQL को ऐसे फ़ॉर्म की तरह समझें जिसमें निर्देश तय हों और मान भरने के लिए खाली खाने हों। Bound parameter केवल खाना भरता है; Ruby interpolation, Rails के दोनों हिस्सों को अलग करने से पहले request text को निर्देशों में जोड़ देता है। SQL संरचना स्थिर रखें और अविश्वसनीय मान parameters के रूप में दें।
User.where(
"id = #{params[:id]}"
)
User.where(id: params[:id])
# or, for more complex conditions:
User.where("id = ?", params[:id])
Interpolation query को क्यों बदलता है
Active Record को condition मिलने से पहले Ruby #{...} का मूल्यांकन करता है। उदाहरण के लिए, where("id = #{params[:id]}") Rails को पहले से जुड़ी SQL string देता है; request input predicate बदल सकता है। Active Record इस तरह string बनाने को असुरक्षित बताता है। समर्थित equality, range या subset conditions के लिए hash इस्तेमाल करें; अन्य predicates के लिए ? placeholders लगाएँ। Placeholders मान bind करते हैं, identifiers या SQL keywords नहीं; query structure स्थिर रखें या सख्त allowlist से identifiers चुनें।
कोडबेस की जाँच कैसे करें
rg -n 'where\s*\(' app/अक्सर पूछे जाने वाले प्रश्न
नहीं। Rails hash conditions equality, range और subset (IN) जाँच का समर्थन करती हैं। अन्य predicates के लिए bound values वाली स्थिर SQL condition लिखें, जैसे where("price > ?", amount)। Binding मानों को सुरक्षित करता है; dynamic table/column names या SQL fragments को नहीं।