Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extension hook to validate()? #78

Open
micschk opened this issue Oct 11, 2022 · 0 comments
Open

Add extension hook to validate()? #78

micschk opened this issue Oct 11, 2022 · 0 comments

Comments

@micschk
Copy link

micschk commented Oct 11, 2022

If an extension call would be added to validate, this would devs to tweak the validation result without having to duplicate/replace the existing validation logic.

Use case is a subsites setup (with reCaptcha v3) to which new hostnames may be added by the client. These hostnames should then also be added to the reCaptcha key in order to work. Instead, the reCaptcha key is marked as 'do not check origin' and we are required to check the hostname field ourselves.

Screenshot 2022-10-11 at 12 41 59

Currently we override the field with our own subclass (via Injector) but an extension hook would be better imo, eg just before return true in validate():
$this->extend('extendValidation', $validator);
Note: get the response in an extension via $this->owner->getVerifyResponse(), no need to include that as argument.

Logic for hostname validation:

class NocaptchaField_ValidateSubsiteHostname
extends NocaptchaField {

    public function validate($validator)
    {
        $isValid = parent::validate($validator);

        // Extra check: validate hostname the recaptcha was solved on to be a valid subsite domain
        $response = $this->getVerifyResponse();
        if( isset($response['hostname']) && Subsite::getSubsiteIDForDomain($response['hostname']) !== SubsiteState::singleton()->getSubsiteId() ) {
            $validator->validationError($this->name, _t(__CLASS__.'.SPAM', 'Invalid hostname in validation response: SPAM'));

            return false;
        }

        return $isValid;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants