r/PHPhelp • u/aliosayle • Feb 08 '25
Security issue with script to fetch data dynamically
Hi,
I'm working on a PHP script to serve as a unified script for all frontend table pages. It functions in a way that the frontend sends the table name and column names, and the backend script then retrieves the data. This approach avoids loading all the data at once, which can be time-consuming for large datasets. The script also supports search and conditional queries.
I posted this on r/php for assistance, but I was informed that the script has several security vulnerabilities. The post request can be intercepted, allowing users to query any table they desire. I'm hoping someone can help me address these issues.
Here's the GitHub repository for the project: https://github.com/aliosayle/php-datatable-with-backed-processing.git
5
u/colshrapnel Feb 08 '25
This one is simple.
However, you must understand that it is not just "any table they desire" but a full-fledged SQL injection. So it's not only tabe names but column names as well.
For the table, you can just define an array with table names allowed to be browsed, and then simply check the input against this array.
The same goes for the columns, though listing them for all tables manually could be tedious, hence I would suggest to run a SHOW COLUMNS FROM table query (after sanitizing the table name of course) to get the column names for that table. And then something like this would do
3
u/No_Astronomer9508 Feb 08 '25
1
u/colshrapnel Feb 08 '25
I would also suggest some minor improvements
In the error reporting section, another option has to be added and its value should be changed depends on the environment, 0 in production and 1 in development
The following code code makes no sense and could be safely removed.
log('debug', $searchBuilder);instead offile_put_contents(__DIR__ . '/search_builder.log', print_r($searchBuilder, return: true));. Also you can check a DEBUG constant inside this function to disable the output.1
u/eurosat7 Feb 08 '25
You might want to do at least one:
1
u/Aggressive_Ad_5454 Feb 08 '25
This is going to be really hard to make secure against determined cybercreeps. See this cartoon.
https://imgs.xkcd.com/comics/exploits_of_a_mom.png
Seriously. Please please don't do this. If you have other peoples' money or private data in that database, with respect, doing this is irresponsible.
If you must make this app available on the public web, do the following things.
Or you'll get the dreaded phone call from Brian Krebs of https://krebsonsecurity.com/
1
u/cursingcucumber Feb 08 '25
Have you checked out GraphQL? 👀
1
u/tekagami Feb 08 '25
Wouldn’t it be easier just to have right of expected table names, and columns?
If the request deviates from the expected, just exit.
0
u/liamsorsby Feb 08 '25
Use an allow list of tables that can be accessed and change your mysqli statements to prepared statements https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php
Don't just execute sql with a variable that is essentially user controlled data. If you don't this is a SQL injection vulnerability.
0
u/[deleted] Feb 08 '25
[deleted] — view deleted comment
-4
u/[deleted] Feb 08 '25
[removed] — view removed comment