Skip to content

Commit c353dbb

Browse files
J0Joel Lee
andauthored
fix: add configurable hcaptcha timeout (#441)
* initial commit * feat: minor fixes * fix: directly use env var * fix: run gofmt * chore: remove context import * Update test.env * Update example.env Co-authored-by: Joel Lee <[email protected]>
1 parent 8ef6798 commit c353dbb

File tree

6 files changed

+19
-4
lines changed

6 files changed

+19
-4
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,8 @@ Whether captcha middleware is enabled
509509

510510
for now the only option supported is: `hcaptcha`
511511

512-
`SECURITY_CAPTCHA_SECRET` - `string`
512+
- `SECURITY_CAPTCHA_SECRET` - `string`
513+
- `SECURITY_CAPTCHA_TIMEOUT` - `string`
513514

514515
Retrieve from hcaptcha account
515516

api/middleware.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ func (a *API) verifyCaptcha(w http.ResponseWriter, req *http.Request) (context.C
234234
if secret == "" {
235235
return nil, internalServerError("server misconfigured")
236236
}
237+
237238
verificationResult, err := security.VerifyRequest(req, secret)
238239
if err != nil {
239240
logrus.WithField("err", err).Infof("failed to validate result")

api/phone.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func (a *API) validatePhone(phone string) (string, error) {
3030
return phone, nil
3131
}
3232

33-
// validateE165Format checks if phone number follows the E.164 format
33+
// validateE164Format checks if phone number follows the E.164 format
3434
func (a *API) validateE164Format(phone string) bool {
3535
// match should never fail as long as regexp is valid
3636
matched, _ := regexp.Match(e164Format, []byte(phone))

example.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ GOTRUE_SMS_VONAGE_FROM=""
178178
GOTRUE_SECURITY_CAPTCHA_ENABLED="false"
179179
GOTRUE_SECURITY_CAPTCHA_PROVIDER="hcaptcha"
180180
GOTRUE_SECURITY_CAPTCHA_SECRET="0x0000000000000000000000000000000000000000"
181+
GOTRUE_SECURITY_CAPTCHA_TIMEOUT="10s"
181182
GOTRUE_SESSION_KEY=""
182183

183184
# SAML config

hack/test.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,4 @@ GOTRUE_TRACING_TAGS="env:test"
9494
GOTRUE_SECURITY_CAPTCHA_ENABLED="false"
9595
GOTRUE_SECURITY_CAPTCHA_PROVIDER="hcaptcha"
9696
GOTRUE_SECURITY_CAPTCHA_SECRET="0x0000000000000000000000000000000000000000"
97+
GOTRUE_SECURITY_CAPTCHA_TIMEOUT="10s"

security/hcaptcha.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import (
55
"encoding/json"
66
"fmt"
77
"io/ioutil"
8+
"log"
89
"net/http"
910
"net/url"
11+
"os"
1012
"strconv"
1113
"strings"
1214
"time"
@@ -40,8 +42,17 @@ const (
4042
var Client *http.Client
4143

4244
func init() {
43-
// TODO (darora): make timeout configurable
44-
Client = &http.Client{Timeout: 10 * time.Second}
45+
var defaultTimeout time.Duration = time.Second * 10
46+
timeoutStr := os.Getenv("GOTRUE_SECURITY_CAPTCHA_TIMEOUT")
47+
if timeoutStr != "" {
48+
if timeout, err := time.ParseDuration(timeoutStr); err != nil {
49+
log.Fatalf("error loading GOTRUE_SECURITY_CAPTCHA_TIMEOUT: %v", err.Error())
50+
} else if timeout != 0 {
51+
defaultTimeout = timeout
52+
}
53+
}
54+
55+
Client = &http.Client{Timeout: defaultTimeout}
4556
}
4657

4758
func VerifyRequest(r *http.Request, secretKey string) (VerificationResult, error) {

0 commit comments

Comments
 (0)