Skip to content

Commit c14172e

Browse files
authored
Merge pull request #305 from itk-dev/feature/digital_signature_ips
Fix IP validation in digital signature file download (CIDR support)
2 parents 5fc32cb + d9b4a02 commit c14172e

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ before starting to add changes. Use example [placed in the end of the page](#exa
1111

1212
## [Unreleased]
1313

14+
- [PR-305](https://github.com/OS2Forms/os2forms/pull/305)
15+
Fix IP validation in digital signature file download (CIDR support)
1416
- [PR-317](https://github.com/OS2Forms/os2forms/pull/317)
1517
Updated code analysis script.
1618
- [PR-306](https://github.com/OS2Forms/os2forms/pull/306)

modules/os2forms_digital_signature/os2forms_digital_signature.module

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Drupal\Core\Form\FormStateInterface;
99
use Drupal\Core\StreamWrapper\StreamWrapperManager;
1010
use Drupal\os2forms_digital_signature\Form\SettingsForm;
11+
use Symfony\Component\HttpFoundation\IpUtils;
1112

1213
/**
1314
* Implements hook_cron().
@@ -57,18 +58,26 @@ function os2forms_digital_signature_file_download($uri) {
5758
$config = \Drupal::config(SettingsForm::$configName);
5859
$allowedIps = $config->get('os2forms_digital_signature_submission_allowed_ips');
5960

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();
6265

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)) {
6568
$basename = basename($uri);
6669
return [
6770
'Content-disposition' => 'attachment; filename="' . $basename . '"',
6871
];
6972
}
7073

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+
7281
return -1;
7382
}
7483

modules/os2forms_digital_signature/src/Form/SettingsForm.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,27 @@ public function buildForm(array $form, FormStateInterface $form_state) {
4040
$form['os2forms_digital_signature_remote_service_url'] = [
4141
'#type' => 'textfield',
4242
'#title' => $this->t('Signature server URL'),
43+
'#required' => TRUE,
4344
'#default_value' => $this->config(self::$configName)->get('os2forms_digital_signature_remote_service_url'),
4445
'#description' => $this->t('E.g. https://signering.bellcom.dk/sign.php?'),
4546
];
4647
$form['os2forms_digital_signature_sign_hash_salt'] = [
4748
'#type' => 'textfield',
4849
'#title' => $this->t('Hash Salt used for signature'),
50+
'#required' => TRUE,
4951
'#default_value' => $this->config(self::$configName)->get('os2forms_digital_signature_sign_hash_salt'),
5052
'#description' => $this->t('Must match hash salt on the signature server'),
5153
];
5254
$form['os2forms_digital_signature_submission_allowed_ips'] = [
5355
'#type' => 'textfield',
5456
'#title' => $this->t('List IPs which can download unsigned PDF submissions'),
5557
'#default_value' => $this->config(self::$configName)->get('os2forms_digital_signature_submission_allowed_ips'),
56-
'#description' => $this->t('Comma separated. e.g. 192.168.1.1,192.168.2.1'),
58+
'#description' => $this->t('Comma separated. e.g. 192.168.1.1,192.168.2.1 or 172.16.0.0/16. If left empty no restrictions will be applied.'),
5759
];
5860
$form['os2forms_digital_signature_submission_retention_period'] = [
5961
'#type' => 'textfield',
6062
'#title' => $this->t('Unsigned submission timespan (s)'),
63+
'#required' => TRUE,
6164
'#default_value' => ($this->config(self::$configName)->get('os2forms_digital_signature_submission_retention_period')) ?? 300,
6265
'#description' => $this->t('How many seconds can unsigned submission exist before being automatically deleted'),
6366
];

0 commit comments

Comments
 (0)