|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +namespace In2code\Powermail\Domain\Validator\SpamShield; |
| 5 | + |
| 6 | +use In2code\Powermail\Finisher\RateLimitFinisher; |
| 7 | +use In2code\Powermail\Storage\RateLimitStorage; |
| 8 | +use Symfony\Component\RateLimiter\RateLimiterFactory; |
| 9 | +use TYPO3\CMS\Core\Utility\GeneralUtility; |
| 10 | + |
| 11 | +/** |
| 12 | + * Limit the number of submissions in a given time frame. |
| 13 | + * |
| 14 | + * Marks the submission as spam if the rate limit has been reached. |
| 15 | + * Counting a submission against the rate limit is done in RateLimitFinisher. |
| 16 | + * |
| 17 | + * Exclusion of IP addresses is possible with a powermail breaker configuration. |
| 18 | + */ |
| 19 | +class RateLimitMethod extends AbstractMethod |
| 20 | +{ |
| 21 | + /** |
| 22 | + * Check if this form submission is limited or shall be allowed. |
| 23 | + * |
| 24 | + * @return bool true if spam recognized |
| 25 | + */ |
| 26 | + public function spamCheck(): bool |
| 27 | + { |
| 28 | + $config = [ |
| 29 | + 'id' => 'powermail-ratelimit', |
| 30 | + 'policy' => 'sliding_window', |
| 31 | + 'limit' => $this->getLimit(), |
| 32 | + 'interval' => $this->getInterval(), |
| 33 | + ]; |
| 34 | + |
| 35 | + $storage = GeneralUtility::makeInstance(RateLimitStorage::class); |
| 36 | + |
| 37 | + $factory = new RateLimiterFactory($config, $storage); |
| 38 | + |
| 39 | + $keyParts = $this->getRestrictionValues($this->getRestrictions()); |
| 40 | + $key = implode('-', $keyParts); |
| 41 | + |
| 42 | + $limiter = $factory->create($key); |
| 43 | + RateLimitFinisher::markForConsumption($limiter); |
| 44 | + |
| 45 | + if ($limiter->consume(0)->getRemainingTokens() > 0) { |
| 46 | + return false; |
| 47 | + } |
| 48 | + |
| 49 | + //spam |
| 50 | + return true; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Replace the restriction variables with their values |
| 55 | + * |
| 56 | + * @param string[] $restrictions |
| 57 | + * |
| 58 | + * @return string[] |
| 59 | + */ |
| 60 | + protected function getRestrictionValues(array $restrictions): array |
| 61 | + { |
| 62 | + $answers = $this->mail->getAnswersByFieldMarker(); |
| 63 | + |
| 64 | + $values = []; |
| 65 | + foreach ($restrictions as $restriction) { |
| 66 | + if ($restriction === '__ipAddress') { |
| 67 | + $values[$restriction] = GeneralUtility::getIndpEnv('REMOTE_ADDR'); |
| 68 | + } elseif ($restriction === '__formIdentifier') { |
| 69 | + $values[$restriction] = $this->mail->getForm()->getUid(); |
| 70 | + } elseif ($restriction[0] === '{') { |
| 71 | + //form field |
| 72 | + $fieldName = substr($restriction, 1, -1); |
| 73 | + if (!isset($answers[$fieldName])) { |
| 74 | + throw new \InvalidArgumentException('Form has no field with variable name ' . $fieldName, 1763046923); |
| 75 | + } |
| 76 | + $values[$restriction] = $answers[$fieldName]->getValue(); |
| 77 | + } else { |
| 78 | + //hard-coded value |
| 79 | + $values[$restriction] = $restriction; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return $values; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Get the configured time interval in which the limit has to be adhered to |
| 88 | + */ |
| 89 | + protected function getInterval(): string |
| 90 | + { |
| 91 | + $interval = $this->configuration['interval']; |
| 92 | + |
| 93 | + if ($interval === null) { |
| 94 | + throw new \InvalidArgumentException('Interval must be set!', 1671448702); |
| 95 | + } |
| 96 | + if (! \is_string($interval)) { |
| 97 | + throw new \InvalidArgumentException('Interval must be a string!', 1671448703); |
| 98 | + } |
| 99 | + |
| 100 | + if (@\DateInterval::createFromDateString($interval) === false) { |
| 101 | + // @todo Remove check and exception when compatibility of PHP >= 8.3 |
| 102 | + // @see https://www.php.net/manual/de/class.datemalformedintervalstringexception.php |
| 103 | + throw new \InvalidArgumentException( |
| 104 | + \sprintf( |
| 105 | + 'Interval is not valid, "%s" given!', |
| 106 | + $interval, |
| 107 | + ), |
| 108 | + 1671448704, |
| 109 | + ); |
| 110 | + } |
| 111 | + |
| 112 | + return $interval; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Get how many form submissions are allowed within the time interval |
| 117 | + */ |
| 118 | + protected function getLimit(): int |
| 119 | + { |
| 120 | + $limit = $this->configuration['limit']; |
| 121 | + |
| 122 | + if ($limit === null) { |
| 123 | + throw new \InvalidArgumentException('Limit must be set!', 1671449026); |
| 124 | + } |
| 125 | + |
| 126 | + if (! \is_numeric($limit)) { |
| 127 | + throw new \InvalidArgumentException('Limit must be numeric!', 1671449027); |
| 128 | + } |
| 129 | + |
| 130 | + $limit = (int)$limit; |
| 131 | + if ($limit < 1) { |
| 132 | + throw new \InvalidArgumentException('Limit must be greater than 0!', 1671449028); |
| 133 | + } |
| 134 | + |
| 135 | + return $limit; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Get the list of properties that are used to identify the form |
| 140 | + * |
| 141 | + * Supported values: |
| 142 | + * - __ipAddress |
| 143 | + * - __formIdentifier |
| 144 | + * - {email} - Form field names |
| 145 | + * - foo - Hard-coded values |
| 146 | + * |
| 147 | + * @return string[] |
| 148 | + */ |
| 149 | + protected function getRestrictions(): array |
| 150 | + { |
| 151 | + $restrictions = $this->configuration['restrictions']; |
| 152 | + |
| 153 | + if ($restrictions === null) { |
| 154 | + throw new \InvalidArgumentException('Restrictions must be set!', 1671727527); |
| 155 | + } |
| 156 | + |
| 157 | + if (! \is_array($restrictions)) { |
| 158 | + throw new \InvalidArgumentException('Restrictions must be an array!', 1671727528); |
| 159 | + } |
| 160 | + |
| 161 | + if ($restrictions === []) { |
| 162 | + throw new \InvalidArgumentException('Restrictions must not be an empty array!', 1671727529); |
| 163 | + } |
| 164 | + |
| 165 | + foreach ($restrictions as $restriction) { |
| 166 | + if (! \is_string($restriction)) { |
| 167 | + throw new \InvalidArgumentException('A single restrictions must be a string!', 1671727530); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + return \array_values($restrictions); |
| 172 | + } |
| 173 | +} |
0 commit comments