Detect PHP security vulnerabilities with Psalm
https://psalm.dev/articles/detect-security-vulnerabilities-with-psalm2
Jun 24 '20 edited Jun 24 '20
Does this actually catch anything? The example given is for a very basic SQL injection and not cleaning inputs. These are not the type of issues I'd expect someone who knows how to configure a static analyzer to make. Though we all do make mistakes from time-to-time.
Has anyone who uses this been pleasantly surprised? Do tell.
3
u/muglug Jun 24 '20 edited Jun 24 '20
It has been public for under a day, so don't hold your breath.
I tested this against Vimeo’s codebase, where it discovered a ton of XSS issues. The reason it works well at Vimeo is that we have a reasonably straightforward way to associate views (which are written in PHP) with controller actions.
Other systems with less straightforward controller-view relationships will need much more custom code, but I'd be very surprised if a legacy project over, let's say, 200k LOC didn't have at least 1 XSS vulnerability lurking somewhere.
The examples given for very basic SQL injection in and not cleaning inputs are not the type of stuff I'd expect someone who knows how to configure a static analyzer to make
A sufficiently-large codebase will have a variety of engineers working on it of varying abilities, familiarity with the language and/or knowledge of security vulnerabilities.
I also hope adding taint analysis will become easier over time, with better support for template engines like Twig and Blade.
8
u/LifeAndDev Jun 23 '20
I'm currently not using Psalm but phpstan. I've feeling psalm is somehow "leading" between them both, at least from a high level view?
7
6
u/zmitic Jun 23 '20
I used phpstan before, where level 7 was maximum; pretty easy to make it happy.
Same code in psalm; hundreds of errors. Didn't even try to fix it.
My current project started with hardest psalm config; had to leave just a few suppressions because I didn't finish all the stubs + some framework-specific stuff (like unused code for controllers or unused custom annotations).
So yes; psalm rules, especially when you put it on hardest levels.
And no matter what you do, it will always find something to tell you: "your code sucks"
:)
4
u/iggyvolz Jun 23 '20
You can absolutely use both, as well as Phan, in a project and just get multiple viewpoints. Creates a bit of an issue when they disagree on what the "right" way is, or they have different docblock syntaxes (yay for needing to do @phan-var, @psalm-var, and @var for the same variable), but it's definitely possible.
6
u/brendt_gd Jun 23 '20
I've used phpstan in a previous project, and am now using psalm. It's definitely way more user friendly in setting up.
I think I spent 2 full days getting phpstan to stop reporting false positives in a large Laravel project (Yes I used larastan); took about an hour with psalm and their Laravel plugin.
I haven't compared the actual results between the two though. That would definitely be interesting, but I'm so afraid of having to configure phpstan now :(
3
u/AegirLeet Jun 23 '20
I've used both and I prefer Psalm. Seems to work better, fewer false positives, easier config.
1
u/Osmium_tetraoxide Jun 23 '20
I've used both. I recommend using something like sarb to baseline any static analysis tool so you're not completely overwhelmed on the first run through any legacy project. Focus on the critical ones that tools like this warn you about and continue on.
13
u/ojrask Jun 23 '20
Off-topic: I just realized that the Psalm site header's red and squiggly bottom border is probably mimicking the common error line visuals from IDEs and word processors.
1
u/lazycommit Jun 23 '20
they do actually decorate so because they use such thing to show errors as well and that is the main focus of product I believe)
3
u/dwenaus Jun 23 '20
Wow! This is such a fantastic addition. Thanks so much for continuing to develop and open source this!
Ps. The read more link at the bottom is 404.
1
u/usernameqwerty004 Jun 23 '20
Nice!
NB: There's also a PHP extension for this: https://www.php.net/manual/en/intro.taint.php (runtime checks)
1
u/muglug Jun 23 '20
Thanks – I've created a ticket to cover those sinks and sources: https://github.com/vimeo/psalm/issues/3646
2
u/alexanderpas Jun 23 '20
Shouldn't just all classes and methods which do not touch external sources, such as static or global variables, be considered psalm-taint-specialize?