-
Notifications
You must be signed in to change notification settings - Fork 7
Feature/8316 improve callback verification #26
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dc60258
Immplement method signature verification
jf-cbd b44c139
Fix error message
jf-cbd 99da1c4
Add tests
jf-cbd 76213ab
Refactor Callback functions in a service and adap tests
jf-cbd d220fa4
Use ::class
jf-cbd e6b786b
Fix CI test + import Hipska's change
jf-cbd 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
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,104 @@ | ||
| <?php | ||
|
|
||
| namespace Combodo\iTop\Service; | ||
|
|
||
| use Exception; | ||
| use IssueLog; | ||
| use ReflectionClass; | ||
| use ReflectionException; | ||
| use SecurityException; | ||
|
|
||
| /** | ||
| * A service that will perform checks on the callback signature of a webhook action. | ||
| */ | ||
| class CallbackService | ||
| { | ||
| private string $sCallBackDefinition; | ||
| private string $sCallBackClassName; | ||
| private string $sCallBackMethodName; | ||
| private bool $bIsStatic; | ||
|
|
||
| /** | ||
| * @throws Exception | ||
| */ | ||
| public function __construct(string $sCallBackDefinition) | ||
| { | ||
| $this->sCallBackDefinition = $sCallBackDefinition; | ||
| if (stripos($sCallBackDefinition, '$this->') !== false) { | ||
| $this->sCallBackMethodName = str_ireplace('$this->', '', $sCallBackDefinition); | ||
| $this->bIsStatic = false; | ||
| $this->sCallBackClassName = ''; // we don't need it for non-static methods | ||
| } else { | ||
| if (!is_callable($sCallBackDefinition)) { | ||
| $sMessageError = "The callback '$sCallBackDefinition' is not valid."; | ||
| IssueLog::Error($sMessageError, 'console'); | ||
| throw new Exception($sMessageError); | ||
| } | ||
| $iPos = strrpos($sCallBackDefinition, '::'); | ||
| if ($iPos !== false && $iPos > 0) { | ||
| $this->sCallBackClassName = substr($sCallBackDefinition, 0, $iPos); | ||
| $this->sCallBackMethodName = substr($sCallBackDefinition, $iPos + 2); | ||
| $this->bIsStatic = true; | ||
| } else { | ||
| $sMessageError = "The callback '$sCallBackDefinition' is not a valid static method."; | ||
| IssueLog::Error($sMessageError, 'console'); | ||
| throw new Exception($sMessageError); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public function IsStatic(): bool | ||
| { | ||
| return $this->bIsStatic; | ||
| } | ||
| /** | ||
| * @throws ReflectionException | ||
| * @throws SecurityException | ||
| */ | ||
| public function CheckCallbackSignature(string $sTriggeringObjectType, array $aParamsType): void | ||
| { | ||
| $iParamCount = 0; | ||
| if ($this->bIsStatic) { // If the callback is static, we expect the first parameter to be the triggering object type | ||
| array_unshift($aParamsType, $sTriggeringObjectType); | ||
| } else { | ||
| $this->sCallBackClassName = $sTriggeringObjectType; | ||
| } | ||
| $oReflector = new ReflectionClass($this->sCallBackClassName); | ||
| $aCallbackParameters = $oReflector->getMethod($this->sCallBackMethodName)->getParameters(); | ||
| $iRequiredNumberOfParams = count($aParamsType); | ||
| if (count($aCallbackParameters) !== $iRequiredNumberOfParams) { | ||
| $sErrorMessage = "The callback method '$this->sCallBackMethodName' of class '$this->sCallBackClassName' must have exactly $iRequiredNumberOfParams parameters."; | ||
| IssueLog::Error($sErrorMessage); | ||
| throw new SecurityException($sErrorMessage); | ||
| } | ||
| foreach ($aCallbackParameters as $oParam) { | ||
| if ($oParam->getType() === null) { | ||
| $sErrorMessage = "The callback method '$this->sCallBackMethodName' of class '$this->sCallBackClassName' must have type-hinted parameters, but parameter {$oParam->getName()} is not type-hinted."; | ||
| IssueLog::Error($sErrorMessage); | ||
| throw new SecurityException($sErrorMessage); | ||
| } | ||
| if ($oParam->getType()->getName() !== $aParamsType[$iParamCount]) { | ||
| $sErrorMessage = "The callback method '$this->sCallBackMethodName' of class '$this->sCallBackClassName' must have a first parameter of type '$aParamsType[$iParamCount]', but it has {$oParam->getType()->getName()} instead."; | ||
| IssueLog::Error($sErrorMessage); | ||
| throw new SecurityException($sErrorMessage); | ||
| } | ||
| $iParamCount++; | ||
| } | ||
jf-cbd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * @param $oTriggeringObject | ||
| * @param array $aParams | ||
| * | ||
| * @return mixed | ||
| */ | ||
| public function Invoke($oTriggeringObject, array $aParams): mixed | ||
| { | ||
| if ($this->bIsStatic) { | ||
| return call_user_func_array([$this->sCallBackClassName, $this->sCallBackMethodName], array_merge([$oTriggeringObject], $aParams)); | ||
|
|
||
| } else { | ||
| return call_user_func_array([$oTriggeringObject, $this->sCallBackMethodName], $aParams); | ||
| } | ||
| } | ||
| } | ||
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
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.