2020-06-19 17:56:04 +02:00
# Avoiding false-positives
When you run Psalm’ s taint analysis for the first time you may see a bunch of false-positives.
Nobody likes false-positives!
There are a number of ways you can prevent them:
2020-06-22 06:18:15 +02:00
## Escaping tainted input
2020-06-19 17:57:34 +02:00
2020-06-19 18:12:28 +02:00
Some operations remove taints from data – for example, wrapping `$_GET['name']` in an `htmlentities` call prevents cross-site-scripting attacks in that `$_GET` call.
2020-06-19 17:57:34 +02:00
2020-06-21 17:43:08 +02:00
Psalm allows you to remove taints via a `@psalm-taint-escape <taint-type>` annotation:
2020-06-19 17:57:34 +02:00
```php
2020-06-22 18:45:41 +02:00
< ?php
2020-06-19 17:57:34 +02:00
function echoVar(string $str) : void {
/**
2020-06-21 17:43:08 +02:00
* @psalm -taint-escape html
2020-06-19 17:57:34 +02:00
*/
2020-06-22 06:18:15 +02:00
$str = str_replace(['< ', '>'], '', $str);
2020-06-19 17:57:34 +02:00
echo $str;
}
echoVar($_GET["text"]);
```
2020-06-19 17:56:04 +02:00
## Specializing taints in functions
For functions, methods and classes you can use the `@psalm-taint-specialize` annotation.
```php
2020-06-22 18:45:41 +02:00
< ?php
2020-06-19 17:56:04 +02:00
function takesInput(string $s) : string {
return $s;
}
echo htmlentities(takesInput($_GET["name"]));
echo takesInput("hello"); // Psalm detects tainted HTML here
```
Adding a `@psalm-taint-specialize` annotation solves the problem, by telling Psalm that each invocation of the function should be treated separately.
```php
2020-06-22 18:45:41 +02:00
< ?php
2020-06-19 17:56:04 +02:00
/**
* @psalm -taint-specialize
*/
function takesInput(string $s) : string {
return $s;
}
echo htmlentities(takesInput($_GET["name"]));
echo takesInput("hello"); // No error
```
A specialized function or method will still track tainted input:
```php
2020-06-22 18:45:41 +02:00
< ?php
2020-06-19 17:56:04 +02:00
/**
* @psalm -taint-specialize
*/
function takesInput(string $s) : string {
return $s;
}
echo takesInput($_GET["name"]); // Psalm detects tainted input
echo takesInput("hello"); // No error
```
2020-08-08 14:09:41 +02:00
Here we’ re telling Psalm that a function’ s taintedness is wholly dependent on the input to the function.
2020-06-19 17:56:04 +02:00
If you're familiar with [immutability in Psalm ](https://psalm.dev/articles/immutability-and-beyond ) then this general idea should be familiar, since a pure function is one where the output is wholly dependent on its input. Unsurprisingly, all functions marked `@psalm-pure` _also_ specialize the taintedness of their output based on input:
```php
2020-06-22 18:45:41 +02:00
< ?php
2020-06-19 17:56:04 +02:00
/**
* @psalm -pure
*/
function takesInput(string $s) : string {
return $s;
}
echo htmlentities(takesInput($_GET["name"]));
echo takesInput("hello"); // No error
```
## Specializing taints in classes
Just as taints can be specialized in function calls, tainted properties can also be specialized to a given class.
```php
2020-06-22 18:45:41 +02:00
< ?php
2020-06-19 17:56:04 +02:00
class User {
public string $name;
public function __construct(string $name) {
$this->name = $name;
}
}
/**
* @psalm -taint-specialize
*/
function echoUserName(User $user) {
echo $user->name; // Error, detected tainted input
}
$user1 = new User("Keith");
$user2 = new User($_GET["name"]);
echoUserName($user1);
```
Adding `@psalm-taint-specialize` to the class fixes the issue.
```php
2020-06-22 18:45:41 +02:00
< ?php
2020-06-19 17:56:04 +02:00
/**
* @psalm -taint-specialize
*/
class User {
public string $name;
public function __construct(string $name) {
$this->name = $name;
}
}
/**
* @psalm -taint-specialize
*/
function echoUserName(User $user) {
echo $user->name; // No error
}
$user1 = new User("Keith");
$user2 = new User($_GET["name"]);
echoUserName($user1);
```
And, because it’ s form of purity enforcement, `@psalm-immutable` can also be used:
```php
2020-06-22 18:45:41 +02:00
< ?php
2020-06-19 17:56:04 +02:00
/**
* @psalm -immutable
*/
class User {
public string $name;
public function __construct(string $name) {
$this->name = $name;
}
}
/**
* @psalm -taint-specialize
*/
function echoUserName(User $user) {
echo $user->name; // No error
}
$user1 = new User("Keith");
$user2 = new User($_GET["name"]);
echoUserName($user1);
```
## Avoiding files in taint paths
You can also tell Psalm that you’ re not interested in any taint paths that flow through certain files or directories by specifying them in your Psalm config:
```xml
< taintAnalysis >
< ignoreFiles >
< directory name = "tests" / >
< / ignoreFiles >
< / taintAnalysis >
```