-
Notifications
You must be signed in to change notification settings - Fork 75
Detect timing-unsafe secret comparisons via taint analysis (CWE-208) #576
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
alies-dev
wants to merge
4
commits into
master
Choose a base branch
from
worktree-570-timing-unsafe-comparison
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8b92508
Detect timing-unsafe secret comparisons via taint analysis (CWE-208)
alies-dev f07b80f
Use single sink per operand instead of per parent node
alies-dev 6c0cf7f
Extract timing-unsafe function names to a const array
alies-dev 2ed7e3c
style: auto-fix (rector + php-cs-fixer)
actions-user File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Psalm\LaravelPlugin\Handlers\Rules; | ||
|
|
||
| use PhpParser\Node\Arg; | ||
| use PhpParser\Node\Expr; | ||
| use PhpParser\Node\Expr\BinaryOp; | ||
| use PhpParser\Node\Expr\FuncCall; | ||
| use PhpParser\Node\Name; | ||
| use Psalm\CodeLocation; | ||
| use Psalm\Internal\Analyzer\StatementsAnalyzer; | ||
| use Psalm\Internal\Codebase\TaintFlowGraph; | ||
| use Psalm\Internal\DataFlow\DataFlowNode; | ||
| use Psalm\Plugin\EventHandler\AfterExpressionAnalysisInterface; | ||
| use Psalm\Plugin\EventHandler\Event\AfterExpressionAnalysisEvent; | ||
| use Psalm\Type\TaintKind; | ||
| use Psalm\Type\Union; | ||
|
|
||
| /** | ||
| * Detects timing-unsafe string comparisons involving secrets (CWE-208). | ||
| * | ||
| * When secrets (values tainted with user_secret or system_secret) are compared | ||
| * using ===, ==, !==, !=, strcmp(), or strcasecmp(), an attacker can determine | ||
| * the correct value character-by-character by measuring response time differences. | ||
| * Use hash_equals() for constant-time comparison instead. | ||
| * | ||
| * This handler adds taint sinks at comparison operators and timing-unsafe | ||
| * functions. When a secret-tainted value flows into these sinks, Psalm emits | ||
| * TaintedUserSecret or TaintedSystemSecret. | ||
| * | ||
| * @see https://cwe.mitre.org/data/definitions/208.html | ||
| */ | ||
| final class TimingUnsafeComparisonHandler implements AfterExpressionAnalysisInterface | ||
| { | ||
| /** Taint mask for secrets that require constant-time comparison */ | ||
| private const SECRET_TAINTS = TaintKind::USER_SECRET | TaintKind::SYSTEM_SECRET; | ||
|
|
||
| /** Functions that compare strings in a timing-unsafe manner */ | ||
| private const TIMING_UNSAFE_FUNCTIONS = ['strcmp', 'strcasecmp']; | ||
|
|
||
| /** @inheritDoc */ | ||
| #[\Override] | ||
| public static function afterExpressionAnalysis(AfterExpressionAnalysisEvent $event): ?bool | ||
| { | ||
| $expr = $event->getExpr(); | ||
| $source = $event->getStatementsSource(); | ||
|
|
||
| if (!$source instanceof StatementsAnalyzer) { | ||
| return null; | ||
| } | ||
|
|
||
| $taintFlowGraph = $source->taint_flow_graph; | ||
|
|
||
| if (!$taintFlowGraph instanceof \Psalm\Internal\Codebase\TaintFlowGraph) { | ||
| return null; | ||
| } | ||
|
|
||
| if ($expr instanceof BinaryOp\Identical | ||
| || $expr instanceof BinaryOp\Equal | ||
| || $expr instanceof BinaryOp\NotIdentical | ||
| || $expr instanceof BinaryOp\NotEqual | ||
| ) { | ||
| self::addSinksForOperands( | ||
| $source, | ||
| $taintFlowGraph, | ||
| $expr, | ||
| $source->getNodeTypeProvider()->getType($expr->left), | ||
| $source->getNodeTypeProvider()->getType($expr->right), | ||
| ); | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| // strcmp() and strcasecmp() are also timing-unsafe — they compare | ||
| // character-by-character and the return value reveals partial ordering | ||
| if ($expr instanceof FuncCall | ||
| && $expr->name instanceof Name | ||
| && \in_array($expr->name->toLowerString(), self::TIMING_UNSAFE_FUNCTIONS, true) | ||
| && \count($expr->args) >= 2 | ||
| && $expr->args[0] instanceof Arg | ||
| && $expr->args[1] instanceof Arg | ||
| ) { | ||
| self::addSinksForOperands( | ||
| $source, | ||
| $taintFlowGraph, | ||
| $expr, | ||
| $source->getNodeTypeProvider()->getType($expr->args[0]->value), | ||
| $source->getNodeTypeProvider()->getType($expr->args[1]->value), | ||
| ); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Add taint sinks for both operands of a timing-unsafe comparison. | ||
| * | ||
| * Each operand's data flow parent nodes are connected to a new sink node | ||
| * that matches user_secret and system_secret taints. If either operand | ||
| * carries secret taint, Psalm's taint resolution will report the issue. | ||
| * | ||
| * @psalm-external-mutation-free | ||
| */ | ||
| private static function addSinksForOperands( | ||
| StatementsAnalyzer $source, | ||
| TaintFlowGraph $graph, | ||
| Expr $comparisonExpr, | ||
| ?Union $leftType, | ||
| ?Union $rightType, | ||
| ): void { | ||
| $codeLocation = new CodeLocation($source, $comparisonExpr); | ||
| $locationId = \strtolower($codeLocation->file_name) | ||
| . ':' . $codeLocation->raw_file_start | ||
| . '-' . $codeLocation->raw_file_end; | ||
|
|
||
| self::addSinkForType($graph, $leftType, 'timing-comparison-left', $locationId, $codeLocation); | ||
| self::addSinkForType($graph, $rightType, 'timing-comparison-right', $locationId, $codeLocation); | ||
| } | ||
alies-dev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Create a single taint sink for an operand and connect all its data flow | ||
| * parent nodes to it. One sink per operand side avoids duplicate reports | ||
| * and keeps the taint graph compact. | ||
| * | ||
| * The sink matches USER_SECRET | SYSTEM_SECRET, so only secret-tainted | ||
| * data triggers an issue — ordinary input taint is not affected. | ||
| * | ||
| * @psalm-external-mutation-free | ||
| */ | ||
alies-dev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| private static function addSinkForType( | ||
| TaintFlowGraph $graph, | ||
| ?Union $type, | ||
| string $sinkLabel, | ||
| string $locationId, | ||
| CodeLocation $codeLocation, | ||
| ): void { | ||
| if (!$type instanceof \Psalm\Type\Union || $type->parent_nodes === []) { | ||
| return; | ||
| } | ||
|
|
||
| $sinkId = $sinkLabel . '-' . $locationId; | ||
|
|
||
| $sink = DataFlowNode::make( | ||
| $sinkId, | ||
| $sinkLabel, | ||
| $codeLocation, | ||
| null, | ||
| self::SECRET_TAINTS, | ||
| ); | ||
|
|
||
| $graph->addSink($sink); | ||
|
|
||
| foreach ($type->parent_nodes as $parentNode) { | ||
| $graph->addPath($parentNode, $sink, 'timing-comparison'); | ||
| } | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| --ARGS-- | ||
| --no-progress --no-diff --config=./tests/Type/psalm.xml --taint-analysis | ||
| --FILE-- | ||
| <?php declare(strict_types=1); | ||
|
|
||
| /** | ||
| * hash_equals() provides constant-time comparison — no timing attack. | ||
| * | ||
| * @psalm-taint-source system_secret | ||
| */ | ||
| function getExpectedToken(): string { | ||
| return 'secret-token'; | ||
| } | ||
|
|
||
| function verifyToken(string $userInput): bool { | ||
| return hash_equals(getExpectedToken(), $userInput); | ||
| } | ||
| ?> | ||
| --EXPECTF-- | ||
|
|
15 changes: 15 additions & 0 deletions
15
tests/Type/tests/TaintAnalysis/SafeNonSecretComparison.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| --ARGS-- | ||
| --no-progress --no-diff --config=./tests/Type/psalm.xml --taint-analysis | ||
| --FILE-- | ||
| <?php declare(strict_types=1); | ||
|
|
||
| /** | ||
| * Comparing non-secret input with === is fine — no timing attack risk | ||
| * because the input taint type does not include user_secret or system_secret. | ||
| */ | ||
| function checkRole(\Illuminate\Http\Request $request): bool { | ||
| return $request->input('role') === 'admin'; | ||
| } | ||
| ?> | ||
| --EXPECTF-- | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| --ARGS-- | ||
| --no-progress --no-diff --config=./tests/Type/psalm.xml --taint-analysis | ||
| --FILE-- | ||
| <?php declare(strict_types=1); | ||
|
|
||
| /** | ||
| * strcmp() with non-secret input should not trigger a timing issue — | ||
| * only user_secret and system_secret taint types are flagged. | ||
| */ | ||
| function compareRole(\Illuminate\Http\Request $request): bool { | ||
| /** @var string $role */ | ||
| $role = $request->input('role'); | ||
| return strcmp($role, 'admin') === 0; | ||
| } | ||
| ?> | ||
| --EXPECTF-- | ||
|
|
22 changes: 22 additions & 0 deletions
22
tests/Type/tests/TaintAnalysis/TaintedSystemSecretIndirectComparison.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| --ARGS-- | ||
| --no-progress --no-diff --config=./tests/Type/psalm.xml --taint-analysis | ||
| --FILE-- | ||
| <?php declare(strict_types=1); | ||
|
|
||
| /** | ||
| * Indirect flow: secret is assigned to a variable before comparison. | ||
| * The taint should still propagate through the variable. | ||
| * | ||
| * @psalm-taint-source system_secret | ||
| */ | ||
| function getApiKey(): string { | ||
| return 'secret-api-key'; | ||
| } | ||
|
|
||
| function verifyApiKey(string $input): bool { | ||
| $expected = getApiKey(); | ||
| return $input === $expected; | ||
| } | ||
| ?> | ||
| --EXPECTF-- | ||
| %ATaintedSystemSecret on line %d: Detected tainted system secret leaking |
23 changes: 23 additions & 0 deletions
23
tests/Type/tests/TaintAnalysis/TaintedSystemSecretNotIdenticalComparison.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| --ARGS-- | ||
| --no-progress --no-diff --config=./tests/Type/psalm.xml --taint-analysis | ||
| --FILE-- | ||
| <?php declare(strict_types=1); | ||
|
|
||
| /** | ||
| * !== has the same timing characteristics as === — the negation happens | ||
| * after the byte-by-byte comparison, so it leaks the same timing info. | ||
| * | ||
| * @psalm-taint-source system_secret | ||
| */ | ||
| function getExpectedToken(): string { | ||
| return 'secret-token'; | ||
| } | ||
|
|
||
| function rejectInvalidToken(string $userInput): void { | ||
| if ($userInput !== getExpectedToken()) { | ||
| throw new \RuntimeException('Invalid token'); | ||
| } | ||
| } | ||
| ?> | ||
| --EXPECTF-- | ||
| %ATaintedSystemSecret on line %d: Detected tainted system secret leaking |
21 changes: 21 additions & 0 deletions
21
tests/Type/tests/TaintAnalysis/TaintedSystemSecretStrcmp.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| --ARGS-- | ||
| --no-progress --no-diff --config=./tests/Type/psalm.xml --taint-analysis | ||
| --FILE-- | ||
| <?php declare(strict_types=1); | ||
|
|
||
| /** | ||
| * strcmp() is also timing-unsafe — it compares character-by-character. | ||
| * Use hash_equals() instead. | ||
| * | ||
| * @psalm-taint-source system_secret | ||
| */ | ||
| function getApiKey(): string { | ||
| return 'secret-api-key'; | ||
| } | ||
|
|
||
| function verifyApiKey(string $input): bool { | ||
| return strcmp($input, getApiKey()) === 0; | ||
| } | ||
| ?> | ||
| --EXPECTF-- | ||
| %ATaintedSystemSecret on line %d: Detected tainted system secret leaking |
21 changes: 21 additions & 0 deletions
21
tests/Type/tests/TaintAnalysis/TaintedSystemSecretTimingComparison.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| --ARGS-- | ||
| --no-progress --no-diff --config=./tests/Type/psalm.xml --taint-analysis | ||
| --FILE-- | ||
| <?php declare(strict_types=1); | ||
|
|
||
| /** | ||
| * Comparing a system secret with === is vulnerable to timing attacks. | ||
| * Use hash_equals() for constant-time comparison. | ||
| * | ||
| * @psalm-taint-source system_secret | ||
| */ | ||
| function getExpectedToken(): string { | ||
| return 'secret-token'; | ||
| } | ||
|
|
||
| function verifyToken(string $userInput): bool { | ||
| return $userInput === getExpectedToken(); | ||
| } | ||
| ?> | ||
| --EXPECTF-- | ||
| %ATaintedSystemSecret on line %d: Detected tainted system secret leaking |
21 changes: 21 additions & 0 deletions
21
tests/Type/tests/TaintAnalysis/TaintedUserSecretLeftOperand.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| --ARGS-- | ||
| --no-progress --no-diff --config=./tests/Type/psalm.xml --taint-analysis | ||
| --FILE-- | ||
| <?php declare(strict_types=1); | ||
|
|
||
| /** | ||
| * The secret can be on either side of the comparison — both operands | ||
| * are checked for secret taint. | ||
| * | ||
| * @psalm-taint-source user_secret | ||
| */ | ||
| function getUserApiKey(): string { | ||
| return 'secret-key'; | ||
| } | ||
|
|
||
| function verifyKey(string $input): bool { | ||
| return getUserApiKey() === $input; | ||
| } | ||
| ?> | ||
| --EXPECTF-- | ||
| %ATaintedUserSecret on line %d: Detected tainted user secret leaking |
22 changes: 22 additions & 0 deletions
22
tests/Type/tests/TaintAnalysis/TaintedUserSecretNotEqualComparison.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| --ARGS-- | ||
| --no-progress --no-diff --config=./tests/Type/psalm.xml --taint-analysis | ||
| --FILE-- | ||
| <?php declare(strict_types=1); | ||
|
|
||
| /** | ||
| * != has the same timing characteristics as == — both are timing-unsafe. | ||
| * | ||
| * @psalm-taint-source user_secret | ||
| */ | ||
| function getUserPassword(): string { | ||
| return 'password'; | ||
| } | ||
|
|
||
| function rejectWrongPassword(string $input): void { | ||
| if ($input != getUserPassword()) { | ||
| throw new \RuntimeException('Wrong password'); | ||
| } | ||
| } | ||
| ?> | ||
| --EXPECTF-- | ||
| %ATaintedUserSecret on line %d: Detected tainted user secret leaking |
20 changes: 20 additions & 0 deletions
20
tests/Type/tests/TaintAnalysis/TaintedUserSecretStrcasecmp.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| --ARGS-- | ||
| --no-progress --no-diff --config=./tests/Type/psalm.xml --taint-analysis | ||
| --FILE-- | ||
| <?php declare(strict_types=1); | ||
|
|
||
| /** | ||
| * strcasecmp() is also timing-unsafe. | ||
| * | ||
| * @psalm-taint-source user_secret | ||
| */ | ||
| function getSecret(): string { | ||
| return 'secret'; | ||
| } | ||
|
|
||
| function verify(string $input): bool { | ||
| return strcasecmp($input, getSecret()) === 0; | ||
| } | ||
| ?> | ||
| --EXPECTF-- | ||
| %ATaintedUserSecret on line %d: Detected tainted user secret leaking |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.