Detect timing-unsafe secret comparisons via taint analysis (CWE-208)#576
Draft
Detect timing-unsafe secret comparisons via taint analysis (CWE-208)#576
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a new taint-analysis rule to detect timing-unsafe secret comparisons (CWE-208) by introducing taint sinks at ==/===/!=/!== and strcmp()/strcasecmp() usage, plus PHPT coverage to validate secret vs non-secret behavior.
Changes:
- Register a new
TimingUnsafeComparisonHandlerhook in the plugin. - Implement sink insertion for comparison operators and timing-unsafe string compare functions when
--taint-analysisis active. - Add 11 taint-analysis type tests covering user/system secret flows, operand position, indirect flow, and safe scenarios (
hash_equals, non-secret comparisons).
Reviewed changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Plugin.php | Registers the new rule handler with Psalm’s plugin hook system. |
| src/Handlers/Rules/TimingUnsafeComparisonHandler.php | Implements taint sink creation for timing-unsafe comparisons involving user_secret/system_secret. |
| tests/Type/tests/TaintAnalysis/TaintedUserSecretTimingComparison.phpt | Asserts == comparison against a user_secret triggers TaintedUserSecret. |
| tests/Type/tests/TaintAnalysis/TaintedUserSecretStrcasecmp.phpt | Asserts strcasecmp() against a user_secret triggers TaintedUserSecret. |
| tests/Type/tests/TaintAnalysis/TaintedUserSecretNotEqualComparison.phpt | Asserts != comparison against a user_secret triggers TaintedUserSecret. |
| tests/Type/tests/TaintAnalysis/TaintedUserSecretLeftOperand.phpt | Asserts detection works when the secret is on the left operand. |
| tests/Type/tests/TaintAnalysis/TaintedSystemSecretTimingComparison.phpt | Asserts === comparison against a system_secret triggers TaintedSystemSecret. |
| tests/Type/tests/TaintAnalysis/TaintedSystemSecretStrcmp.phpt | Asserts strcmp() against a system_secret triggers TaintedSystemSecret. |
| tests/Type/tests/TaintAnalysis/TaintedSystemSecretNotIdenticalComparison.phpt | Asserts !== comparison against a system_secret triggers TaintedSystemSecret. |
| tests/Type/tests/TaintAnalysis/TaintedSystemSecretIndirectComparison.phpt | Asserts secret taint propagates through variables before comparison. |
| tests/Type/tests/TaintAnalysis/SafeNonSecretStrcmp.phpt | Ensures strcmp() with non-secret input does not trigger secret issues. |
| tests/Type/tests/TaintAnalysis/SafeNonSecretComparison.phpt | Ensures === with non-secret input does not trigger secret issues. |
| tests/Type/tests/TaintAnalysis/SafeHashEqualsSecret.phpt | Ensures hash_equals() with a secret does not trigger timing-unsafe comparison issues. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Add TimingUnsafeComparisonHandler that creates taint sinks at ===, ==, !==, !=, strcmp(), and strcasecmp() for USER_SECRET and SYSTEM_SECRET taint types. When a secret-tainted value flows into a timing-unsafe comparison, Psalm emits TaintedUserSecret or TaintedSystemSecret. Closes #570
Reduces taint graph size and avoids duplicate reports when an operand has multiple parent nodes (unions/merged values). One sink is created per comparison side, with all parent nodes connected to it via addPath.
Better DX — the list is scannable and easy to extend.
29f278f to
6c0cf7f
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Blocked by vimeo/psalm#11762
What does this PR do?
Adds
TimingUnsafeComparisonHandlerthat detects timing-unsafe string comparisons involving secrets. When values tainted withuser_secretorsystem_secretflow into===,==,!==,!=,strcmp(), orstrcasecmp(), Psalm emitsTaintedUserSecretorTaintedSystemSecret.The handler works by adding taint sinks at comparison operators during expression analysis. Only activates during
--taint-analysisruns; near-zero overhead in normal analysis.Use
hash_equals()for constant-time comparison instead.Closes #570