2019-08-29 12:34:07 +03:00
|
|
|
package home
|
|
|
|
|
|
|
|
import (
|
2019-10-11 12:41:01 +03:00
|
|
|
"net/http"
|
2024-03-20 19:25:59 +03:00
|
|
|
"net/netip"
|
2021-04-06 14:31:20 +03:00
|
|
|
"net/textproto"
|
2019-10-11 12:41:01 +03:00
|
|
|
"net/url"
|
2019-10-02 17:02:16 +03:00
|
|
|
"path/filepath"
|
2019-08-29 12:34:07 +03:00
|
|
|
"testing"
|
|
|
|
|
2023-04-07 14:21:37 +03:00
|
|
|
"github.com/AdguardTeam/golibs/httphdr"
|
2021-10-22 11:58:18 +03:00
|
|
|
"github.com/AdguardTeam/golibs/testutil"
|
2019-08-29 12:34:07 +03:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-03-01 20:37:28 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-08-29 12:34:07 +03:00
|
|
|
)
|
|
|
|
|
2019-10-11 12:41:01 +03:00
|
|
|
// implements http.ResponseWriter
|
|
|
|
type testResponseWriter struct {
|
|
|
|
hdr http.Header
|
|
|
|
statusCode int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *testResponseWriter) Header() http.Header {
|
|
|
|
return w.hdr
|
|
|
|
}
|
2020-11-16 15:52:05 +03:00
|
|
|
|
2019-10-11 12:41:01 +03:00
|
|
|
func (w *testResponseWriter) Write([]byte) (int, error) {
|
|
|
|
return 0, nil
|
|
|
|
}
|
2020-11-16 15:52:05 +03:00
|
|
|
|
2019-10-11 12:41:01 +03:00
|
|
|
func (w *testResponseWriter) WriteHeader(statusCode int) {
|
|
|
|
w.statusCode = statusCode
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthHTTP(t *testing.T) {
|
2021-04-22 14:18:20 +03:00
|
|
|
dir := t.TempDir()
|
2019-10-11 12:41:01 +03:00
|
|
|
fn := filepath.Join(dir, "sessions.db")
|
|
|
|
|
2022-09-29 19:04:26 +03:00
|
|
|
users := []webUser{
|
2020-12-07 16:04:53 +03:00
|
|
|
{Name: "name", PasswordHash: "$2y$05$..vyzAECIhJPfaQiOK17IukcQnqEgKJHy0iETyYqxn3YXJl8yZuo2"},
|
2019-10-11 12:41:01 +03:00
|
|
|
}
|
2024-03-20 19:25:59 +03:00
|
|
|
Context.auth = InitAuth(fn, users, 60, nil, nil)
|
2019-10-11 12:41:01 +03:00
|
|
|
|
|
|
|
handlerCalled := false
|
2020-12-22 21:05:12 +03:00
|
|
|
handler := func(_ http.ResponseWriter, _ *http.Request) {
|
2019-10-11 12:41:01 +03:00
|
|
|
handlerCalled = true
|
|
|
|
}
|
|
|
|
handler2 := optionalAuth(handler)
|
|
|
|
w := testResponseWriter{}
|
|
|
|
w.hdr = make(http.Header)
|
|
|
|
r := http.Request{}
|
|
|
|
r.Header = make(http.Header)
|
2021-01-13 17:26:57 +03:00
|
|
|
r.Method = http.MethodGet
|
2019-10-11 12:41:01 +03:00
|
|
|
|
|
|
|
// get / - we're redirected to login page
|
|
|
|
r.URL = &url.URL{Path: "/"}
|
|
|
|
handlerCalled = false
|
|
|
|
handler2(&w, &r)
|
2021-01-13 16:56:05 +03:00
|
|
|
assert.Equal(t, http.StatusFound, w.statusCode)
|
2023-04-07 14:21:37 +03:00
|
|
|
assert.NotEmpty(t, w.hdr.Get(httphdr.Location))
|
2021-01-13 16:56:05 +03:00
|
|
|
assert.False(t, handlerCalled)
|
2019-10-11 12:41:01 +03:00
|
|
|
|
|
|
|
// go to login page
|
2023-04-07 14:21:37 +03:00
|
|
|
loginURL := w.hdr.Get(httphdr.Location)
|
2019-10-11 12:41:01 +03:00
|
|
|
r.URL = &url.URL{Path: loginURL}
|
|
|
|
handlerCalled = false
|
|
|
|
handler2(&w, &r)
|
|
|
|
assert.True(t, handlerCalled)
|
|
|
|
|
|
|
|
// perform login
|
2022-09-29 19:04:26 +03:00
|
|
|
cookie, err := Context.auth.newCookie(loginJSON{Name: "name", Password: "password"}, "")
|
2021-10-22 11:58:18 +03:00
|
|
|
require.NoError(t, err)
|
2022-09-29 19:04:26 +03:00
|
|
|
require.NotNil(t, cookie)
|
2019-10-11 12:41:01 +03:00
|
|
|
|
|
|
|
// get /
|
|
|
|
handler2 = optionalAuth(handler)
|
|
|
|
w.hdr = make(http.Header)
|
2023-04-07 14:21:37 +03:00
|
|
|
r.Header.Set(httphdr.Cookie, cookie.String())
|
2019-10-11 12:41:01 +03:00
|
|
|
r.URL = &url.URL{Path: "/"}
|
|
|
|
handlerCalled = false
|
|
|
|
handler2(&w, &r)
|
|
|
|
assert.True(t, handlerCalled)
|
2022-09-29 19:04:26 +03:00
|
|
|
|
2023-04-07 14:21:37 +03:00
|
|
|
r.Header.Del(httphdr.Cookie)
|
2019-10-11 12:41:01 +03:00
|
|
|
|
|
|
|
// get / with basic auth
|
|
|
|
handler2 = optionalAuth(handler)
|
|
|
|
w.hdr = make(http.Header)
|
|
|
|
r.URL = &url.URL{Path: "/"}
|
|
|
|
r.SetBasicAuth("name", "password")
|
|
|
|
handlerCalled = false
|
|
|
|
handler2(&w, &r)
|
|
|
|
assert.True(t, handlerCalled)
|
2023-04-07 14:21:37 +03:00
|
|
|
r.Header.Del(httphdr.Authorization)
|
2019-10-11 12:41:01 +03:00
|
|
|
|
|
|
|
// get login page with a valid cookie - we're redirected to /
|
|
|
|
handler2 = optionalAuth(handler)
|
|
|
|
w.hdr = make(http.Header)
|
2023-04-07 14:21:37 +03:00
|
|
|
r.Header.Set(httphdr.Cookie, cookie.String())
|
2019-10-11 12:41:01 +03:00
|
|
|
r.URL = &url.URL{Path: loginURL}
|
|
|
|
handlerCalled = false
|
|
|
|
handler2(&w, &r)
|
2023-04-07 14:21:37 +03:00
|
|
|
assert.NotEmpty(t, w.hdr.Get(httphdr.Location))
|
2021-01-13 16:56:05 +03:00
|
|
|
assert.False(t, handlerCalled)
|
2023-04-07 14:21:37 +03:00
|
|
|
r.Header.Del(httphdr.Cookie)
|
2019-10-11 12:41:01 +03:00
|
|
|
|
|
|
|
// get login page with an invalid cookie
|
|
|
|
handler2 = optionalAuth(handler)
|
|
|
|
w.hdr = make(http.Header)
|
2023-04-07 14:21:37 +03:00
|
|
|
r.Header.Set(httphdr.Cookie, "bad")
|
2019-10-11 12:41:01 +03:00
|
|
|
r.URL = &url.URL{Path: loginURL}
|
|
|
|
handlerCalled = false
|
|
|
|
handler2(&w, &r)
|
|
|
|
assert.True(t, handlerCalled)
|
2023-04-07 14:21:37 +03:00
|
|
|
r.Header.Del(httphdr.Cookie)
|
2019-10-11 12:41:01 +03:00
|
|
|
|
2020-02-13 18:42:07 +03:00
|
|
|
Context.auth.Close()
|
2019-10-11 12:41:01 +03:00
|
|
|
}
|
2021-04-06 14:31:20 +03:00
|
|
|
|
|
|
|
func TestRealIP(t *testing.T) {
|
|
|
|
const remoteAddr = "1.2.3.4:5678"
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
header http.Header
|
|
|
|
remoteAddr string
|
|
|
|
wantErrMsg string
|
2024-03-20 19:25:59 +03:00
|
|
|
wantIP netip.Addr
|
2021-04-06 14:31:20 +03:00
|
|
|
}{{
|
|
|
|
name: "success_no_proxy",
|
|
|
|
header: nil,
|
|
|
|
remoteAddr: remoteAddr,
|
|
|
|
wantErrMsg: "",
|
2024-03-20 19:25:59 +03:00
|
|
|
wantIP: netip.MustParseAddr("1.2.3.4"),
|
2021-04-06 14:31:20 +03:00
|
|
|
}, {
|
|
|
|
name: "success_proxy",
|
|
|
|
header: http.Header{
|
2023-04-07 14:21:37 +03:00
|
|
|
textproto.CanonicalMIMEHeaderKey(httphdr.XRealIP): []string{"1.2.3.5"},
|
2021-04-06 14:31:20 +03:00
|
|
|
},
|
|
|
|
remoteAddr: remoteAddr,
|
|
|
|
wantErrMsg: "",
|
2024-03-20 19:25:59 +03:00
|
|
|
wantIP: netip.MustParseAddr("1.2.3.5"),
|
2021-04-06 14:31:20 +03:00
|
|
|
}, {
|
|
|
|
name: "success_proxy_multiple",
|
|
|
|
header: http.Header{
|
2023-04-07 14:21:37 +03:00
|
|
|
textproto.CanonicalMIMEHeaderKey(httphdr.XForwardedFor): []string{
|
2021-04-06 14:31:20 +03:00
|
|
|
"1.2.3.6, 1.2.3.5",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
remoteAddr: remoteAddr,
|
|
|
|
wantErrMsg: "",
|
2024-03-20 19:25:59 +03:00
|
|
|
wantIP: netip.MustParseAddr("1.2.3.6"),
|
2021-04-06 14:31:20 +03:00
|
|
|
}, {
|
|
|
|
name: "error_no_proxy",
|
|
|
|
header: nil,
|
|
|
|
remoteAddr: "1:::2",
|
|
|
|
wantErrMsg: `getting ip from client addr: address 1:::2: ` +
|
|
|
|
`too many colons in address`,
|
2024-03-20 19:25:59 +03:00
|
|
|
wantIP: netip.Addr{},
|
2021-04-06 14:31:20 +03:00
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
r := &http.Request{
|
|
|
|
Header: tc.header,
|
|
|
|
RemoteAddr: tc.remoteAddr,
|
|
|
|
}
|
|
|
|
|
|
|
|
ip, err := realIP(r)
|
|
|
|
assert.Equal(t, tc.wantIP, ip)
|
|
|
|
|
2021-10-22 11:58:18 +03:00
|
|
|
testutil.AssertErrorMsg(t, tc.wantErrMsg, err)
|
2021-04-06 14:31:20 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|