|
8 | 8 | use Drupal\Core\Form\FormStateInterface; |
9 | 9 | use Drupal\Core\StreamWrapper\StreamWrapperManager; |
10 | 10 | use Drupal\os2forms_digital_signature\Form\SettingsForm; |
| 11 | +use Symfony\Component\HttpFoundation\IpUtils; |
11 | 12 |
|
12 | 13 | /** |
13 | 14 | * Implements hook_cron(). |
@@ -57,18 +58,26 @@ function os2forms_digital_signature_file_download($uri) { |
57 | 58 | $config = \Drupal::config(SettingsForm::$configName); |
58 | 59 | $allowedIps = $config->get('os2forms_digital_signature_submission_allowed_ips'); |
59 | 60 |
|
60 | | - $allowedIpsArr = explode(',', $allowedIps); |
61 | | - $remoteIp = Drupal::request()->getClientIp(); |
| 61 | + $allowedIpsArr = array_map('trim', explode(',', $allowedIps)); |
| 62 | + // Remove empty entries (e.g. from trailing comma or empty config). |
| 63 | + $allowedIpsArr = array_filter($allowedIpsArr); |
| 64 | + $remoteIp = \Drupal::request()->getClientIp(); |
62 | 65 |
|
63 | | - // IP list is empty, or request IP is allowed. |
64 | | - if (empty($allowedIpsArr) || in_array($remoteIp, $allowedIpsArr)) { |
| 66 | + // Check if remote IP matches any allowed IP or CIDR range. |
| 67 | + if (empty($allowedIpsArr) || IpUtils::checkIp($remoteIp, $allowedIpsArr)) { |
65 | 68 | $basename = basename($uri); |
66 | 69 | return [ |
67 | 70 | 'Content-disposition' => 'attachment; filename="' . $basename . '"', |
68 | 71 | ]; |
69 | 72 | } |
70 | 73 |
|
71 | | - // Otherwise - Deny access. |
| 74 | + // Deny access and log warning. |
| 75 | + \Drupal::logger('os2forms_digital_signature')->warning('File download denied for IP @ip on URI @uri. Allowed IPs: @allowed', [ |
| 76 | + '@ip' => $remoteIp, |
| 77 | + '@uri' => $uri, |
| 78 | + '@allowed' => $allowedIps, |
| 79 | + ]); |
| 80 | + |
72 | 81 | return -1; |
73 | 82 | } |
74 | 83 |
|
|
0 commit comments