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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃敟Feature: add function to check if request came from localhost #1671

Merged
merged 2 commits into from Dec 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions ctx.go
Expand Up @@ -1289,3 +1289,23 @@ func (c *Ctx) IsProxyTrusted() bool {
_, trustProxy := c.app.config.trustedProxiesMap[c.fasthttp.RemoteIP().String()]
return trustProxy
}

// IsLocalHost will return true if address is a localhost address.
func (c *Ctx) isLocalHost(address string) bool {
localHosts := []string{"127.0.0.1", "0.0.0.0", "::1"}
for _, h := range localHosts {
if strings.Contains(address, h) {
return true
}
}
return false
}

// IsFromLocal will return true if request came from local.
func (c *Ctx) IsFromLocal() bool {
ips := c.IPs()
if len(ips) == 0 {
ips = append(ips, c.IP())
}
return c.isLocalHost(ips[0])
}
55 changes: 55 additions & 0 deletions ctx_test.go
Expand Up @@ -2751,3 +2751,58 @@ func Test_Ctx_GetRespHeader(t *testing.T) {
utils.AssertEqual(t, c.GetRespHeader("test"), "Hello, World 馃憢!")
utils.AssertEqual(t, c.GetRespHeader(HeaderContentType), "application/json")
}

// go test -run Test_Ctx_IsFromLocal
func Test_Ctx_IsFromLocal(t *testing.T) {
t.Parallel()
// Test "0.0.0.0", "127.0.0.1" and "::1".
{
app := New()
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)
utils.AssertEqual(t, true, c.IsFromLocal())
}
// This is a test for "0.0.0.0"
{
app := New()
c := app.AcquireCtx(&fasthttp.RequestCtx{})
c.Request().Header.Set(HeaderXForwardedFor, "0.0.0.0")
defer app.ReleaseCtx(c)
utils.AssertEqual(t, true, c.IsFromLocal())
}

// This is a test for "127.0.0.1"
{
app := New()
c := app.AcquireCtx(&fasthttp.RequestCtx{})
c.Request().Header.Set(HeaderXForwardedFor, "127.0.0.1")
defer app.ReleaseCtx(c)
utils.AssertEqual(t, true, c.IsFromLocal())
}

// This is a test for "localhost"
{
app := New()
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)
utils.AssertEqual(t, true, c.IsFromLocal())
}

// This is testing "::1", it is the compressed format IPV6 loopback address 0:0:0:0:0:0:0:1.
// It is the equivalent of the IPV4 address 127.0.0.1.
{
app := New()
c := app.AcquireCtx(&fasthttp.RequestCtx{})
c.Request().Header.Set(HeaderXForwardedFor, "::1")
defer app.ReleaseCtx(c)
utils.AssertEqual(t, true, c.IsFromLocal())
}

{
app := New()
c := app.AcquireCtx(&fasthttp.RequestCtx{})
c.Request().Header.Set(HeaderXForwardedFor, "93.46.8.90")
defer app.ReleaseCtx(c)
utils.AssertEqual(t, false, c.IsFromLocal())
}
}