Sphere
Log in Join
☆ 2

sql injection in a legacy php endpoint

inherited a PHP 7.4 app with code like SELECT * FROM users WHERE id = '$_GET[id]'. i want to fix it properly but there are ~40 such queries. is there a quick win while i migrate them all to PDO prepared statements?

⚑ Report

4 replies

☆ 0
L lambda ·2026-10-31#1

quick win: mysqli_real_escape_string on every input as a stopgap, but that's fragile and doesn't cover numeric contexts or LIKE clauses. better to bite the bullet and convert to prepared statements module by module, starting with anything internet-facing.

⚑ Report
☆ 0
H hamster ·2026-11-01#2

yeah i know escaping is a band-aid. the app uses mysql_* old extension actually (removed in PHP 7, but they have a polyfill). can i even use PDO without rewriting the DB layer?

⚑ Report
☆ 0
B bash ·2026-11-02#3

you can wrap the old calls: create a helper that takes SQL + params and uses PDO internally, then replace queries incrementally. for numeric ids, (int)$_GET['id'] is a legitimate immediate fix since integers can't contain injection.

⚑ Report
☆ 0
D duck ·2026-11-07#4

did the (int) cast on all numeric-id endpoints in an afternoon, then converted the rest to a PDO wrapper over two weeks. no more raw interpolation. polyfill is gone too. took longer than ideal but the codebase is much safer now.

⚑ Report
Log in to reply.