Skip to content

Commit

Permalink
Fix forwarding non-ok Options request (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
vokomarov committed Apr 7, 2024
2 parents 4c037ee + af4c54b commit 29fafe6
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
4 changes: 4 additions & 0 deletions captcha/google_recaptcha.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ func (p *GoogleReCaptchaProvider) Verify(ctx *fasthttp.RequestCtx) (bool, error)
return true, nil
}

if string(ctx.Request.Header.Method()) == fasthttp.MethodOptions {
return true, nil
}

challenge := ctx.Request.Header.Peek(headers.XCtCaptchaChallenge)
if challenge == nil || string(challenge) == "" {
log.Printf("[%s] captcha challenge empty", clientIp)
Expand Down
21 changes: 21 additions & 0 deletions captcha/google_recaptcha_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,27 @@ func TestVerifyEmptySecret(t *testing.T) {
assert.NoError(t, err)
}

func TestVerifyOptions(t *testing.T) {
ctrl := gomock.NewController(t)
c := mocks.NewHttpClientMock(ctrl)

ctx := fasthttp.RequestCtx{}
ctx.SetRemoteAddr(&net.TCPAddr{IP: []byte{0xA, 0x0, 0x0, 0x1}})
ctx.Request.Header.SetMethod(fasthttp.MethodOptions)
ctx.Request.Header.Set(headers.XCtCaptchaChallenge, "captcha_challenge_2")

c.EXPECT().WithReadTimeout(gomock.Eq(googleApiReadTimeout))
c.EXPECT().WithWriteTimeout(gomock.Eq(googleApiWriteTimeout))

p := NewGoogleReCaptchaProvider(c, config.Config{
CaptchaSecret: "captcha_secret_1",
})
state, err := p.Verify(&ctx)

assert.True(t, state)
assert.NoError(t, err)
}

func TestVerifyEmptyChallenge(t *testing.T) {
ctrl := gomock.NewController(t)
c := mocks.NewHttpClientMock(ctrl)
Expand Down
4 changes: 4 additions & 0 deletions headers/cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,8 @@ func writeCorsAllowedHeaders(ctx *fasthttp.RequestCtx) {
ctx.Response.Header.Set(AccessControlAllowMethods, strings.Join(CorsAllowedMethods, ","))
ctx.Response.Header.Set(AccessControlAllowHeaders, strings.Join(CorsAllowedHeaders, ","))
ctx.Response.Header.Set(AccessControlAllowCredentials, "true")

if string(ctx.Request.Header.Method()) == fasthttp.MethodOptions {
ctx.Response.Header.SetStatusCode(fasthttp.StatusOK)
}
}
12 changes: 12 additions & 0 deletions headers/cors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ func TestCorsHandler(t *testing.T) {
assert.Equal(t, "test.com", string(ctx.Response.Header.Peek(AccessControlAllowOrigin)))
})

t.Run("AllowOptionsStatusAlwaysOk", func(t *testing.T) {
ctx := fasthttp.RequestCtx{}
ctx.Request.Header.SetMethod(fasthttp.MethodOptions)
ctx.Request.Header.Set(Origin, "Test.Com")
ctx.Request.Header.Set(XForwardedFor, "127.0.0.1")

handler := CorsHandler(func(ctx *fasthttp.RequestCtx) {})
handler(&ctx)

assert.Equal(t, fasthttp.StatusOK, ctx.Response.StatusCode())
})

t.Run("RejectIgnorePath", func(t *testing.T) {
ctx := fasthttp.RequestCtx{}
ctx.Request.Header.Set(Origin, "a.Test.Com")
Expand Down

0 comments on commit 29fafe6

Please sign in to comment.