Skip to content

Commit

Permalink
fix: captcha preview panic when clientId or clientSecret is empty (#824)
Browse files Browse the repository at this point in the history
* fix: captcha preview panic when clientId or clientSecret is empty

* return original errors from captcha
  • Loading branch information
Resulte committed Jun 26, 2022
1 parent 339c6c2 commit 477d386
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
9 changes: 8 additions & 1 deletion captcha/hcaptcha.go
Expand Up @@ -16,9 +16,11 @@ package captcha

import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/url"
"strings"
)

const HCaptchaVerifyUrl = "https://hcaptcha.com/siteverify"
Expand Down Expand Up @@ -48,13 +50,18 @@ func (captcha *HCaptchaProvider) VerifyCaptcha(token, clientSecret string) (bool
}

type captchaResponse struct {
Success bool `json:"success"`
Success bool `json:"success"`
ErrorCodes []string `json:"error-codes"`
}
captchaResp := &captchaResponse{}
err = json.Unmarshal(body, captchaResp)
if err != nil {
return false, err
}

if len(captchaResp.ErrorCodes) > 0 {
return false, errors.New(strings.Join(captchaResp.ErrorCodes, ","))
}

return captchaResp.Success, nil
}
9 changes: 8 additions & 1 deletion captcha/recaptcha.go
Expand Up @@ -16,9 +16,11 @@ package captcha

import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/url"
"strings"
)

const ReCaptchaVerifyUrl = "https://recaptcha.net/recaptcha/api/siteverify"
Expand Down Expand Up @@ -48,13 +50,18 @@ func (captcha *ReCaptchaProvider) VerifyCaptcha(token, clientSecret string) (boo
}

type captchaResponse struct {
Success bool `json:"success"`
Success bool `json:"success"`
ErrorCodes []string `json:"error-codes"`
}
captchaResp := &captchaResponse{}
err = json.Unmarshal(body, captchaResp)
if err != nil {
return false, err
}

if len(captchaResp.ErrorCodes) > 0 {
return false, errors.New(strings.Join(captchaResp.ErrorCodes, ","))
}

return captchaResp.Success, nil
}
6 changes: 3 additions & 3 deletions controllers/verification.go
Expand Up @@ -63,10 +63,10 @@ func (c *ApiController) SendVerificationCode() {
}
isHuman, err := captchaProvider.VerifyCaptcha(checkKey, checkId)
if err != nil {
c.ResponseError("Failed to verify captcha: %v", err)
c.ResponseError(err.Error())
return
}

if !isHuman {
c.ResponseError("Turing test failed.")
return
Expand Down Expand Up @@ -209,7 +209,7 @@ func (c *ApiController) VerifyCaptcha() {

isValid, err := provider.VerifyCaptcha(captchaToken, clientSecret)
if err != nil {
c.ResponseError("Failed to verify captcha: %v", err)
c.ResponseError(err.Error())
return
}

Expand Down
2 changes: 1 addition & 1 deletion web/src/common/CaptchaPreview.js
Expand Up @@ -117,7 +117,7 @@ export const CaptchaPreview = ({ provider, providerName, clientSecret, captchaTy

return (
<React.Fragment>
<Button style={{ fontSize: 14 }} type={"primary"} onClick={clickPreview}>
<Button style={{ fontSize: 14 }} type={"primary"} onClick={clickPreview} disabled={captchaType !== "Default" && (!clientId || !clientSecret)}>
{i18next.t("general:Preview")}
</Button>
<Modal
Expand Down

0 comments on commit 477d386

Please sign in to comment.