Skip to content

Commit

Permalink
CA/Cert descr validation fixes. Fixes #13387
Browse files Browse the repository at this point in the history
Validate description on save when editing and in other situations that
were not yet covered.

While here, ensure that errors when editing a cert leave the user on the
cert edit screen properly, but successful cases return to the cert list.

Also encode some output just in case a bad value was already present
before the validation was fixed.
  • Loading branch information
jim-p committed Aug 1, 2022
1 parent 2884bd1 commit 2fe0e0f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
6 changes: 5 additions & 1 deletion src/usr/local/www/system_camanager.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@
$reqdfieldsn = array(
gettext("Descriptive name"),
gettext("Certificate data"));
/* Make sure we do not have invalid characters in the fields for the certificate */
if (preg_match("/[\?\>\<\&\/\\\"\']/", $_POST['descr'])) {
array_push($input_errors, gettext("The field 'Descriptive Name' contains invalid characters."));
}
if ($_POST['cert'] && (!strstr($_POST['cert'], "BEGIN CERTIFICATE") || !strstr($_POST['cert'], "END CERTIFICATE"))) {
$input_errors[] = gettext("This certificate does not appear to be valid.");
}
Expand Down Expand Up @@ -439,7 +443,7 @@

$issuer_ca = lookup_ca($ca['caref']);
if ($issuer_ca) {
$issuer_name = $issuer_ca['descr'];
$issuer_name = htmlspecialchars($issuer_ca['descr']);
}

foreach ($a_cert as $cert) {
Expand Down
34 changes: 26 additions & 8 deletions src/usr/local/www/system_certmanager.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@
break;
case 'edit':
case 'import':
/* Make sure we do not have invalid characters in the fields for the certificate */
if (preg_match("/[\?\>\<\&\/\\\"\']/", $_POST['descr'])) {
$input_errors[] = gettext("The field 'Descriptive Name' contains invalid characters.");
}
$pkcs12_data = '';
if ($_POST['import_type'] == 'x509') {
$reqdfields = explode(" ",
Expand Down Expand Up @@ -448,7 +452,7 @@
$ucert = lookup_cert($pconfig['certref']);
if ($ucert && $a_user) {
$a_user[$userid]['cert'][] = $ucert['refid'];
$savemsg = sprintf(gettext("Added certificate %s to user %s"), $ucert['descr'], $a_user[$userid]['name']);
$savemsg = sprintf(gettext("Added certificate %s to user %s"), htmlspecialchars($ucert['descr']), $a_user[$userid]['name']);
}
unset($cert);
break;
Expand Down Expand Up @@ -484,13 +488,15 @@
}
// Add it to the config file
$config['cert'][] = $newcert;
$savemsg = sprintf(gettext("Signed certificate %s"), $newcert['descr']);
$savemsg = sprintf(gettext("Signed certificate %s"), htmlspecialchars($newcert['descr']));
unset($act);
}
unset($cert);
break;
case 'edit':
cert_import($cert, $pconfig['cert'], $pconfig['key']);
$savemsg = sprintf(gettext("Edited certificate %s"), $cert['descr']);
$savemsg = sprintf(gettext("Edited certificate %s"), htmlspecialchars($cert['descr']));
unset($act);
break;
case 'import':
/* Import an external certificate+key */
Expand All @@ -510,7 +516,8 @@
}
}
cert_import($cert, $pconfig['cert'], $pconfig['key']);
$savemsg = sprintf(gettext("Imported certificate %s"), $cert['descr']);
$savemsg = sprintf(gettext("Imported certificate %s"), htmlspecialchars($cert['descr']));
unset($act);
break;
case 'internal':
/* Create an internal certificate */
Expand Down Expand Up @@ -554,7 +561,8 @@
}
}
}
$savemsg = sprintf(gettext("Created internal certificate %s"), $cert['descr']);
$savemsg = sprintf(gettext("Created internal certificate %s"), htmlspecialchars($cert['descr']));
unset($act);
break;
case 'external':
/* Create a certificate signing request */
Expand Down Expand Up @@ -598,7 +606,8 @@
}
}
}
$savemsg = sprintf(gettext("Created certificate signing request %s"), $cert['descr']);
$savemsg = sprintf(gettext("Created certificate signing request %s"), htmlspecialchars($cert['descr']));
unset($act);
break;
default:
break;
Expand Down Expand Up @@ -656,7 +665,7 @@
$cert['descr'] = $pconfig['descr'];
csr_complete($cert, $pconfig['cert']);
$thiscert = $cert;
$savemsg = sprintf(gettext("Updated certificate signing request %s"), $pconfig['descr']);
$savemsg = sprintf(gettext("Updated certificate signing request %s"), htmlspecialchars($pconfig['descr']));
write_config($savemsg);
pfSenseHeader("system_certmanager.php");
}
Expand Down Expand Up @@ -708,6 +717,15 @@
));
}

if ($act) {
$form->addGlobal(new Form_Input(
'act',
null,
'hidden',
$act
));
}

switch ($act) {
case 'edit':
$maintitle = gettext('Edit an Existing Certificate');
Expand Down Expand Up @@ -1402,7 +1420,7 @@ function list_csrs() {

$ca = lookup_ca($cert['caref']);
if ($ca) {
$caname = $ca['descr'];
$caname = htmlspecialchars($ca['descr']);
}
?>
<tr>
Expand Down

0 comments on commit 2fe0e0f

Please sign in to comment.