← PHP / Laravel 목차

PHP-003 / 중급

orderBy에 사용자 입력 컬럼명 전달

A03:2021 Injection · CWE-89 SQL Injection

왜 문제가 될까요?

식별자(컬럼명)는 바인딩 파라미터로 처리할 수 없어 화이트리스트가 필요하다. 서브쿼리 주입도 가능.

이렇게 수정하세요

php
$allowed = ['created_at', 'title'];
$sort = in_array($request->sort, $allowed, true) ? $request->sort : 'created_at';
$posts = Post::orderBy($sort, 'desc')->get();

더 읽기