diff --git a/Securitytxt/Model/Config/Backend/SecureUrl.php b/Securitytxt/Model/Config/Backend/SecureUrl.php new file mode 100644 index 00000000..50e6bd71 --- /dev/null +++ b/Securitytxt/Model/Config/Backend/SecureUrl.php @@ -0,0 +1,36 @@ +getValue(); + // phpcs:ignore Magento2.Functions.DiscouragedFunction + $isValid = parse_url($url, PHP_URL_SCHEME) === 'https'; + if (!$isValid && $url !== '') { + throw new ValidatorException( + __('URL should be in correct format and must start with HTTPS.') + ); + } + return $this; + } +} diff --git a/Securitytxt/Model/Config/Backend/Validate.php b/Securitytxt/Model/Config/Backend/Validate.php deleted file mode 100644 index 30e78bcf..00000000 --- a/Securitytxt/Model/Config/Backend/Validate.php +++ /dev/null @@ -1,126 +0,0 @@ -getPath()); - $sectionName = reset($sectionPathString); - - if ($sectionName !== Config::XML_SECURITYTXT_MODULE || $this->getData('group_id') !== 'contact_information') { - return parent::validateBeforeSave(); - } - - $dataGroup = $this->getData()['groups']; - $contactInformationFields = $dataGroup['contact_information']['fields']; - $otherInformationFields = $dataGroup['other_information']['fields']; - $isExtensionEnabled = (bool)$dataGroup['general']['fields']['enabled']['value']; - $contactEmail = $contactInformationFields['email']['value']; - $contactPhone = $contactInformationFields['phone']['value']; - $contactWebPage = $contactInformationFields['contact_page']['value']; - - if ($isExtensionEnabled) { - if ($contactEmail === '' && $contactPhone === '' && $contactWebPage === '') { - throw new ValidatorException(__('At least one contact information is required.')); - } - } else { - return parent::validateBeforeSave(); - } - - $this->validateContactEmail($contactEmail); - $this->validateContactWebpageUrl($contactWebPage); - $this->validateUrlField("Contact Page URL", $contactWebPage); - $this->validateUrlField("Encryption URL", $otherInformationFields['encryption']['value']); - $this->validateUrlField("Acknowledgements URL", $otherInformationFields['acknowledgements']['value']); - $this->validateUrlField("Hiring URL", $otherInformationFields['hiring']['value']); - $this->validateUrlField("Policy URL", $otherInformationFields['policy']['value']); - - return parent::validateBeforeSave(); - } - - /** - * Validate url value to be secure. - * - * @param string $url - * @return bool - */ - private function validateSecureUrl(string $url): bool - { - $url = filter_var($url, FILTER_SANITIZE_STRING); - // phpcs:ignore Magento2.Functions.DiscouragedFunction - if (parse_url($url, PHP_URL_SCHEME) === 'https' && filter_var($url, FILTER_VALIDATE_URL)) { - return true; - } - - return false; - } - - /** - * Validate contact email configuration field. - * - * @param string $contactEmail - * @return void - * @throws ValidatorException - */ - private function validateContactEmail(string $contactEmail): void - { - if ($contactEmail !== '' && !filter_var($contactEmail, FILTER_VALIDATE_EMAIL)) { - throw new ValidatorException( - __('Contact Information: Email validation failed. Please enter in correct format.') - ); - } - } - - /** - * Validate contact web page configuration field. - * - * @param string $contactWebpage - * @return void - * @throws ValidatorException - */ - private function validateContactWebpageUrl(string $contactWebpage): void - { - if ($contactWebpage !== '' && !$this->validateSecureUrl($contactWebpage)) { - throw new ValidatorException( - __('Contact Information: Contact Page URL should be in correct format and must start with HTTPS.') - ); - } - } - - /** - * Validate Security.txt configuration field containing url. - * - * @param string $fieldName - * @param string $fieldValue - * @throws ValidatorException - */ - private function validateUrlField(string $fieldName, string $fieldValue): void - { - if ($fieldValue !== '' && !$this->validateSecureUrl($fieldValue)) { - throw new ValidatorException( - __('Other Information: %1 should be in correct format and must start with HTTPS.', $fieldName) - ); - } - } -} diff --git a/Securitytxt/etc/adminhtml/system.xml b/Securitytxt/etc/adminhtml/system.xml index c636be88..2ccb332a 100644 --- a/Securitytxt/etc/adminhtml/system.xml +++ b/Securitytxt/etc/adminhtml/system.xml @@ -12,82 +12,92 @@
+ showInWebsite="1" showInStore="0"> separator-top security Magento_Securitytxt::config + showInStore="0"> + showInStore="0"> Magento\Config\Model\Config\Source\Yesno + showInStore="0"> + showInWebsite="1" showInStore="0"> validate-email - Magento\Securitytxt\Model\Config\Backend\Validate + showInWebsite="1" showInStore="0"> + showInWebsite="1" showInStore="0"> validate-url validate-no-html-tags + Magento\Securitytxt\Model\Config\Backend\SecureUrl Example: https://example.com/security-contact.html + + 1 + + showInStore="0"> + showInWebsite="1" showInStore="0" > validate-url validate-no-html-tags Example: https://example.com/pgp-key.txt + Magento\Securitytxt\Model\Config\Backend\SecureUrl + showInWebsite="1" showInStore="0"> validate-url validate-no-html-tags Example: https://example.com/hall-of-fame.html + Magento\Securitytxt\Model\Config\Backend\SecureUrl + showInWebsite="1" showInStore="0"> validate-text validate-no-html-tags Example: en, es, hi, de, fr + showInWebsite="1" showInStore="0"> validate-url validate-no-html-tags Example: https://example.com/jobs.html + Magento\Securitytxt\Model\Config\Backend\SecureUrl + showInWebsite="1" showInStore="0"> validate-url validate-no-html-tags Example: https://example.com/security-policy.html + Magento\Securitytxt\Model\Config\Backend\SecureUrl + showInWebsite="1" showInStore="0"> validate-no-html-tags + + 1 +
- \ No newline at end of file +