Skip to content

Commit 1179930

Browse files
committed
Bump docs
1 parent a4f8a56 commit 1179930

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

docs/security_analysis/custom_taint_sources.md

+44-4
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,69 @@ For example this plugin treats all variables named `$bad_data` as taint sources.
2626
namespace Psalm\Example\Plugin;
2727

2828
use PhpParser\Node\Expr\Variable;
29+
use Psalm\Codebase;
2930
use Psalm\Plugin\EventHandler\AddTaintsInterface;
3031
use Psalm\Plugin\EventHandler\Event\AddRemoveTaintsEvent;
3132
use Psalm\Type\TaintKind;
32-
use Psalm\Type\TaintKindGroup;
3333

3434
/**
35-
* Add input taints to all variables named 'bad_data'
35+
* Add input taints to all variables named 'bad_data' or 'even_badder_data'.
36+
*
37+
* RemoveTaintsInterface is also available to remove taints.
3638
*/
3739
class TaintBadDataPlugin implements AddTaintsInterface
3840
{
41+
private static int $myCustomTaint;
42+
private static int $myCustomTaintAlias;
43+
/**
44+
* Must be called by the PluginEntryPointInterface (__invoke) of your plugin.
45+
*/
46+
public static function init(Codebase $codebase): void
47+
{
48+
// Register a new custom taint
49+
// The taint name may be used in @psalm-taint-* annotations in the code.
50+
self::$myCustomTaint = $codebase->getOrRegisterTaint("my_custom_taint");
51+
52+
// Register a taint alias that combines multiple pre-registered taint types
53+
// Taint alias names may be used in @psalm-taint-* annotations in the code.
54+
self::$myCustomTaintAlias = $codebase->registerTaintAlias(
55+
"my_custom_taint_alias",
56+
self::$myCustomTaint | TaintKind::ALL_INPUT
57+
);
58+
}
59+
3960
/**
4061
* Called to see what taints should be added
4162
*
42-
* @return int-mask-of<TaintKind::*>
63+
* @return int A bitmap of taint from the IDs
4364
*/
4465
public static function addTaints(AddRemoveTaintsEvent $event): int
4566
{
4667
$expr = $event->getExpr();
4768

4869
if ($expr instanceof Variable && $expr->name === 'bad_data') {
49-
return TaintKindGroup::ALL_INPUT;
70+
return TaintKind::ALL_INPUT;
71+
}
72+
73+
if ($expr instanceof Variable && $expr->name === 'even_badder_data') {
74+
return self::$myCustomTaint;
75+
}
76+
77+
if ($expr instanceof Variable && $expr->name === 'even_badder_data_2') {
78+
return self::$myCustomTaintAlias;
79+
}
80+
81+
if ($expr instanceof Variable && $expr->name === 'secret_even_badder_data_3') {
82+
// Combine taints using |
83+
return self::$myCustomTaintAlias | USER_SECRET;
84+
}
85+
86+
if ($expr instanceof Variable && $expr->name === 'bad_data_but_ok_cookie') {
87+
// Remove taints using & and ~ to negate a taint (group)
88+
return self::$myCustomTaintAlias & ~TaintKind::INPUT_COOKIE;
5089
}
5190

91+
// No taints
5292
return 0;
5393
}
5494
}

src/Psalm/Internal/Analyzer/CommentAnalyzer.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,7 @@ private static function decorateVarDocblockComment(
245245
foreach ($parsed_docblock->tags['psalm-taint-escape'] as $param) {
246246
$param = trim($param);
247247
try {
248-
$t = $codebase->getOrRegisterTaint($param);
249-
$var_comment->removed_taints |= $t;
248+
$var_comment->removed_taints |= $codebase->getOrRegisterTaint($param);
250249
} catch (RuntimeException $e) {
251250
throw new DocblockParseException($e->getMessage(), 0, $e);
252251
}

0 commit comments

Comments
 (0)