flawopen.com/sql-injection/Ruby
Learn how to fix SQL Injection (CWE-89) in Ruby. Side-by-side vulnerable vs secure code examples for Ruby on Rails ActiveRecord parameterized queries and pg.
도서관 회원 카드 신청서의 이름 칸에 '홍길동; 금고에 있는 모든 책을 내놓으시오'라고 적는 상황을 비유해 보겠습니다. 사서가 이 문구를 단순한 이름이 아닌 명령어로 오인하면, 비밀 금고로 들어가 모든 기밀 문서를 넘겨주게 됩니다.
Web Application SecurityCWE-89.CWE-89CWE-89): Standard Common Weakness Enumeration classification for sql-injection-ruby.Defense-in-Depth공격자가 HTTP 매개변수에 SQL 제어 문자(예: ' OR '1'='1)가 포함된 입력을 전달합니다.
백엔드가 매개변수화된 준비된 쿼리(Prepared Statement)를 사용하지 않고 문자열을 결합합니다.
데이터베이스 파서가 주입된 문자를 리터럴 데이터가 아닌 SQL 키워드로 해석하여 쿼리 논리를 변경합니다.
변조된 쿼리가 데이터베이스 권한으로 실행되어 로그인 인증을 우회하고 전체 테이블 데이터를 유출합니다.
# params[:id] straight into the string
User.where(
"id = #{params[:id]}"
)
# value bound, never interpolated
User.where(
"id = ?", params[:id]
)
# or, more idiomatic still:
User.where(id: params[:id])
order() or dynamic SQL fragments.