Addressing SQL Injection Vulnerabilities with Raw Methods. #47257
Unanswered
craigfrancis
asked this question in
Ideas
Replies: 2 comments 2 replies
-
Huh, that would solve some issues - last week I found Have you found any issues with this approach? maybe false-positives? |
Beta Was this translation helpful? Give feedback.
1 reply
-
That's a good approach! It will make potential SQL injection vulnerabilities a lot less likely if teams use PHPStan to analyze their code. And with my different PRs for query expressions, we can now even get rid of those raw expressions entirely and replace them with safe escaped generated sql constructs. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
While the documentation notes "Laravel can not guarantee that any query using raw expressions is protected against SQL injection vulnerabilities", unfortunately not everyone will read/understand/remember this; and even the best developers make mistakes.
While Injection Vulnerabilities are becoming rarer (thanks to database abstractions like yours), they have not been solved, and these mistakes are still found in penetration tests (as reflected in the OWASP Top 10). They also tend to go un-reported, as it's kinda embarrassing, especially for developers who think they are more "experienced" and believe they should use the "powerful" methods for everything (sigh).
A simple example - I found someone had used
$sort = $request->input('sort')
with->orderBy($sort)
, all good, fairly normal. But a junior developer had been asked to split thename
into first/last fields, so$sort
was set toname_first,name_last
; this caused an SQL error (good), but instead of updating their code to call->orderBy()
for each field, they simply changed it to->orderByRaw($sort)
, it "worked", and was uploaded to the live site.And then you have these (abbreviated) classics, would you say these mistakes are obvious?
I believe Laravel should provide a way to check unsafe user input is not used with these sensitive parameters... i.e. these parameters should expect a "developer defined string", known as the
literal-string
type in Static Analysis tools PHPStan and Psalm (also supported by PhpStorm 2022.3).This is used by Nette, Propel, RedBean; and it does find mistakes.
As an aside, Pradeep Srinivasan and Graham Bleaney (Facebook/Meta) introduced the LiteralString type in Python 3.11; and this technique can be used in other programming languages.
I can put together a PR, but want to check that you (the Laravel developers) want to address this issue, and are happy with this approach, e.g.
I'd prefer to start with a few of the simpler methods (to get some feedback), before we look at methods like
DB::raw()
, andDB::select()
.Beta Was this translation helpful? Give feedback.
All reactions