altcha-org / altcha

GDPR compliant, self-hosted CAPTCHA alternative with PoW mechanism and advanced anti-spam filter.
https://altcha.org
MIT License
483 stars 16 forks source link

Regression | Hidden input not created when dispatching "verified" event #70

Closed alexbcberio closed 2 weeks ago

alexbcberio commented 2 weeks ago

I have implemented a "auto-submit" feature on my site using the "verified" event of the widget but the server is not receiving any input.

On the past I opened a issue (#10) because the hidden input was not created when triggering the "verified" event. This issue has again been introduced.

If I submit the form on the next tick, inside a setTimeout with delay 0, the input is received on the server.


How to reproduce the issue, create a form and add the following altcha widget.

<altcha-widget
    challengeurl="<challengeUrl>"
    name="altcha"
    data-submit-verified
></altcha-widget>

Create a script (I'm using typescript) to initialize altcha.

import "altcha";

document.addEventListener("DOMContentLoaded", (): void => {
    const widgets = document.querySelectorAll<HTMLElement>("altcha-widget");

    for (let i = 0; i < widgets.length; i++) {
        const widget = widgets[i] as any;

        if (widget.hasAttribute("data-submit-verified")) {
            widget.addEventListener("verified", onWidgetVerified.bind(widget));
        }
    }
});

function onWidgetVerified(this: HTMLElement): void {
    const form = this.closest("form");

    if (!form) {
        return;
    }

    // submit at this point does not work
    form.submit();

    // submit inside setTimeout works
    /*setTimeout((): void => {
        form.submit();
    }, 0);*/
}

Open the html and complete the challenge, the page will reload but no "altcha" input will be sent on the request

ovx commented 2 weeks ago

Hi, thanks for reporting, this has been there for a while. Fixed in version 1.0.6 and E2E test added to cover this.