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

RequestCtx.IsTLS() doesn't recognizeperIPConn when TLS is used #1062

Closed
tysg opened this issue Aug 2, 2021 · 3 comments · Fixed by #1064
Closed

RequestCtx.IsTLS() doesn't recognizeperIPConn when TLS is used #1062

tysg opened this issue Aug 2, 2021 · 3 comments · Fixed by #1064

Comments

@tysg
Copy link
Contributor

tysg commented Aug 2, 2021

Steps To Reproduce: See gofiber/fiber#1456

Description:

When Server.MaxConnsPerIP is set to a non-zero value, the server wraps every incoming connection to a perIPConn in acquirePerIPConn. perIPConn has the struct definition:

type perIPConn struct {
	net.Conn

	ip               uint32
	perIPConnCounter *perIPConnCounter
}

When a tls.Conn comes in, acquirePerIPConn assigns the tls.Conn to the field Conn in the return value, which restricts the tls.Conn to net.Conn.

fasthttp/peripconn.go

Lines 48 to 59 in 9466cd7

func acquirePerIPConn(conn net.Conn, ip uint32, counter *perIPConnCounter) *perIPConn {
v := counter.pool.Get()
if v == nil {
v = &perIPConn{
perIPConnCounter: counter,
}
}
c := v.(*perIPConn)
c.Conn = conn
c.ip = ip
return c
}

Therefore, the IsTLS() check on perIPConn will fail, as it doesn't implement the connTLSer any more.

fasthttp/server.go

Lines 709 to 720 in 9466cd7

func (ctx *RequestCtx) IsTLS() bool {
// cast to (connTLSer) instead of (*tls.Conn), since it catches
// cases with overridden tls.Conn such as:
//
// type customConn struct {
// *tls.Conn
//
// // other custom fields here
// }
_, ok := ctx.c.(connTLSer)
return ok
}

Furthermore, adding the following failing test in server_test.go confirmed the discovery:

func TestRequestCtxIsTLS(t *testing.T) {
	t.Parallel()

	var ctx RequestCtx

        // ... omitted

	ctx.c = &struct {
		net.Conn
		fooBar bool
	}{Conn: &tls.Conn{}}
	if !ctx.IsTLS() {
		t.Fatal("IsTLS must return true") // fails
	}
}
@tysg
Copy link
Contributor Author

tysg commented Aug 2, 2021

I would love to work on this if the maintainers can point me the correct direction :)

@erikdubbelboer
Copy link
Collaborator

@tysg the simplest way is probably for RequestCtx.IsTLS to check for perIPConn like this example: https://play.golang.org/p/p6zHsqpKhyC
If you can make a pull request for this including the test from above that would be great! 🙏

@tysg
Copy link
Contributor Author

tysg commented Aug 4, 2021

@erikdubbelboer Thanks for the code snippet! I submitted the PR under #1064

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

Successfully merging a pull request may close this issue.

2 participants