# 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: ## Escaping tainted input Some operations remove taints from data – for example, wrapping `$_GET['name']` in an `htmlentities` call prevents cross-site-scripting attacks in that `$_GET` call. Psalm allows you to remove taints via a `@psalm-taint-escape ` annotation: ```php '], '', $str); echo $str; } echoVar($_GET["text"]); ``` ## Conditionally escaping tainted input A slightly modified version of the previous example is using a condition to determine whether the return value is considered secure. Only in case function argument `$escape` is true, the corresponding annotation `@psalm-taint-escape` is applied for taint type `html` . ```php '], '', $str); } return $str; } echo processVar($_GET['text'], false); // detects tainted HTML echo processVar($_GET['text'], true); // considered secure ``` ## Sanitizing HTML user input Whenever possible, applications should be designed to accept & store user input as discrete text fields, rather than blocks of HTML. This allows user input to be fully escaped via `htmlspecialchars` or `htmlentities`. In cases where HTML user input is required (e.g. rich text editors like [TinyMCE](https://www.tiny.cloud/)), a library designed specifically to filter out risky HTML is highly recommended. For example, [HTML Purifier](http://htmlpurifier.org/docs) could be used as follows: ```php purify($html); } ``` ## Specializing taints in functions For functions, methods and classes you can use the `@psalm-taint-specialize` annotation. ```php 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 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 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 ```