2019-08-29 02:34:07 -07:00
|
|
|
package home
|
|
|
|
|
|
|
|
import (
|
2021-03-01 10:37:28 -07:00
|
|
|
"bytes"
|
|
|
|
"crypto/rand"
|
2019-08-29 02:34:07 -07:00
|
|
|
"encoding/hex"
|
2021-04-06 04:31:20 -07:00
|
|
|
"net"
|
2019-10-11 02:41:01 -07:00
|
|
|
"net/http"
|
2021-04-06 04:31:20 -07:00
|
|
|
"net/textproto"
|
2019-10-11 02:41:01 -07:00
|
|
|
"net/url"
|
2019-10-02 07:02:16 -07:00
|
|
|
"path/filepath"
|
2019-08-29 02:34:07 -07:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-02-04 10:35:13 -07:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghtest"
|
2019-08-29 02:34:07 -07:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-03-01 10:37:28 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-08-29 02:34:07 -07:00
|
|
|
)
|
|
|
|
|
2020-11-16 05:52:05 -07:00
|
|
|
func TestMain(m *testing.M) {
|
2021-02-04 10:35:13 -07:00
|
|
|
aghtest.DiscardLogOutput(m)
|
2020-11-16 05:52:05 -07:00
|
|
|
}
|
|
|
|
|
2021-03-01 10:37:28 -07:00
|
|
|
func TestNewSessionToken(t *testing.T) {
|
|
|
|
// Successful case.
|
|
|
|
token, err := newSessionToken()
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Len(t, token, sessionTokenSize)
|
|
|
|
|
|
|
|
// Break the rand.Reader.
|
|
|
|
prevReader := rand.Reader
|
|
|
|
t.Cleanup(func() {
|
|
|
|
rand.Reader = prevReader
|
|
|
|
})
|
|
|
|
rand.Reader = &bytes.Buffer{}
|
|
|
|
|
|
|
|
// Unsuccessful case.
|
|
|
|
token, err = newSessionToken()
|
|
|
|
require.NotNil(t, err)
|
|
|
|
assert.Empty(t, token)
|
|
|
|
}
|
|
|
|
|
2019-10-11 02:41:01 -07:00
|
|
|
func TestAuth(t *testing.T) {
|
2021-04-22 04:18:20 -07:00
|
|
|
dir := t.TempDir()
|
2019-10-11 02:41:01 -07:00
|
|
|
fn := filepath.Join(dir, "sessions.db")
|
2019-10-02 07:02:16 -07:00
|
|
|
|
2021-03-01 10:37:28 -07:00
|
|
|
users := []User{{
|
|
|
|
Name: "name",
|
|
|
|
PasswordHash: "$2y$05$..vyzAECIhJPfaQiOK17IukcQnqEgKJHy0iETyYqxn3YXJl8yZuo2",
|
|
|
|
}}
|
2021-04-27 08:56:32 -07:00
|
|
|
a := InitAuth(fn, nil, 60, nil)
|
2019-10-21 07:44:07 -07:00
|
|
|
s := session{}
|
2019-08-29 02:34:07 -07:00
|
|
|
|
2019-10-11 02:41:01 -07:00
|
|
|
user := User{Name: "name"}
|
|
|
|
a.UserAdd(&user, "password")
|
2019-08-29 02:34:07 -07:00
|
|
|
|
2020-12-22 11:05:12 -07:00
|
|
|
assert.Equal(t, checkSessionNotFound, a.checkSession("notfound"))
|
2019-08-29 02:34:07 -07:00
|
|
|
a.RemoveSession("notfound")
|
|
|
|
|
2021-03-01 10:37:28 -07:00
|
|
|
sess, err := newSessionToken()
|
2020-11-20 07:32:41 -07:00
|
|
|
assert.Nil(t, err)
|
2019-08-29 02:34:07 -07:00
|
|
|
sessStr := hex.EncodeToString(sess)
|
|
|
|
|
2019-10-21 07:44:07 -07:00
|
|
|
now := time.Now().UTC().Unix()
|
2019-08-29 02:34:07 -07:00
|
|
|
// check expiration
|
2019-10-21 07:44:07 -07:00
|
|
|
s.expire = uint32(now)
|
|
|
|
a.addSession(sess, &s)
|
2020-12-22 11:05:12 -07:00
|
|
|
assert.Equal(t, checkSessionExpired, a.checkSession(sessStr))
|
2019-08-29 02:34:07 -07:00
|
|
|
|
|
|
|
// add session with TTL = 2 sec
|
2019-10-21 07:44:07 -07:00
|
|
|
s = session{}
|
2020-04-24 04:04:36 -07:00
|
|
|
s.expire = uint32(time.Now().UTC().Unix() + 2)
|
2019-10-21 07:44:07 -07:00
|
|
|
a.addSession(sess, &s)
|
2020-12-22 11:05:12 -07:00
|
|
|
assert.Equal(t, checkSessionOK, a.checkSession(sessStr))
|
2019-08-29 02:34:07 -07:00
|
|
|
|
|
|
|
a.Close()
|
|
|
|
|
|
|
|
// load saved session
|
2021-04-27 08:56:32 -07:00
|
|
|
a = InitAuth(fn, users, 60, nil)
|
2019-08-29 02:34:07 -07:00
|
|
|
|
|
|
|
// the session is still alive
|
2020-12-22 11:05:12 -07:00
|
|
|
assert.Equal(t, checkSessionOK, a.checkSession(sessStr))
|
|
|
|
// reset our expiration time because checkSession() has just updated it
|
2020-04-24 04:04:36 -07:00
|
|
|
s.expire = uint32(time.Now().UTC().Unix() + 2)
|
2019-10-21 07:44:07 -07:00
|
|
|
a.storeSession(sess, &s)
|
2019-08-29 02:34:07 -07:00
|
|
|
a.Close()
|
|
|
|
|
|
|
|
u := a.UserFind("name", "password")
|
2021-01-13 06:56:05 -07:00
|
|
|
assert.NotEmpty(t, u.Name)
|
2019-08-29 02:34:07 -07:00
|
|
|
|
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
|
|
|
|
// load and remove expired sessions
|
2021-04-27 08:56:32 -07:00
|
|
|
a = InitAuth(fn, users, 60, nil)
|
2020-12-22 11:05:12 -07:00
|
|
|
assert.Equal(t, checkSessionNotFound, a.checkSession(sessStr))
|
2019-08-29 02:34:07 -07:00
|
|
|
|
|
|
|
a.Close()
|
|
|
|
}
|
2019-10-11 02:41:01 -07:00
|
|
|
|
|
|
|
// implements http.ResponseWriter
|
|
|
|
type testResponseWriter struct {
|
|
|
|
hdr http.Header
|
|
|
|
statusCode int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *testResponseWriter) Header() http.Header {
|
|
|
|
return w.hdr
|
|
|
|
}
|
2020-11-16 05:52:05 -07:00
|
|
|
|
2019-10-11 02:41:01 -07:00
|
|
|
func (w *testResponseWriter) Write([]byte) (int, error) {
|
|
|
|
return 0, nil
|
|
|
|
}
|
2020-11-16 05:52:05 -07:00
|
|
|
|
2019-10-11 02:41:01 -07:00
|
|
|
func (w *testResponseWriter) WriteHeader(statusCode int) {
|
|
|
|
w.statusCode = statusCode
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthHTTP(t *testing.T) {
|
2021-04-22 04:18:20 -07:00
|
|
|
dir := t.TempDir()
|
2019-10-11 02:41:01 -07:00
|
|
|
fn := filepath.Join(dir, "sessions.db")
|
|
|
|
|
|
|
|
users := []User{
|
2020-12-07 06:04:53 -07:00
|
|
|
{Name: "name", PasswordHash: "$2y$05$..vyzAECIhJPfaQiOK17IukcQnqEgKJHy0iETyYqxn3YXJl8yZuo2"},
|
2019-10-11 02:41:01 -07:00
|
|
|
}
|
2021-04-27 08:56:32 -07:00
|
|
|
Context.auth = InitAuth(fn, users, 60, nil)
|
2019-10-11 02:41:01 -07:00
|
|
|
|
|
|
|
handlerCalled := false
|
2020-12-22 11:05:12 -07:00
|
|
|
handler := func(_ http.ResponseWriter, _ *http.Request) {
|
2019-10-11 02:41:01 -07:00
|
|
|
handlerCalled = true
|
|
|
|
}
|
|
|
|
handler2 := optionalAuth(handler)
|
|
|
|
w := testResponseWriter{}
|
|
|
|
w.hdr = make(http.Header)
|
|
|
|
r := http.Request{}
|
|
|
|
r.Header = make(http.Header)
|
2021-01-13 07:26:57 -07:00
|
|
|
r.Method = http.MethodGet
|
2019-10-11 02:41:01 -07:00
|
|
|
|
|
|
|
// get / - we're redirected to login page
|
|
|
|
r.URL = &url.URL{Path: "/"}
|
|
|
|
handlerCalled = false
|
|
|
|
handler2(&w, &r)
|
2021-01-13 06:56:05 -07:00
|
|
|
assert.Equal(t, http.StatusFound, w.statusCode)
|
|
|
|
assert.NotEmpty(t, w.hdr.Get("Location"))
|
|
|
|
assert.False(t, handlerCalled)
|
2019-10-11 02:41:01 -07:00
|
|
|
|
|
|
|
// go to login page
|
|
|
|
loginURL := w.hdr.Get("Location")
|
|
|
|
r.URL = &url.URL{Path: loginURL}
|
|
|
|
handlerCalled = false
|
|
|
|
handler2(&w, &r)
|
|
|
|
assert.True(t, handlerCalled)
|
|
|
|
|
|
|
|
// perform login
|
2021-04-27 08:56:32 -07:00
|
|
|
cookie, err := Context.auth.httpCookie(loginJSON{Name: "name", Password: "password"}, "")
|
2020-11-20 07:32:41 -07:00
|
|
|
assert.Nil(t, err)
|
2021-01-13 06:56:05 -07:00
|
|
|
assert.NotEmpty(t, cookie)
|
2019-10-11 02:41:01 -07:00
|
|
|
|
|
|
|
// get /
|
|
|
|
handler2 = optionalAuth(handler)
|
|
|
|
w.hdr = make(http.Header)
|
|
|
|
r.Header.Set("Cookie", cookie)
|
|
|
|
r.URL = &url.URL{Path: "/"}
|
|
|
|
handlerCalled = false
|
|
|
|
handler2(&w, &r)
|
|
|
|
assert.True(t, handlerCalled)
|
|
|
|
r.Header.Del("Cookie")
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
r.Header.Del("Authorization")
|
|
|
|
|
|
|
|
// get login page with a valid cookie - we're redirected to /
|
|
|
|
handler2 = optionalAuth(handler)
|
|
|
|
w.hdr = make(http.Header)
|
|
|
|
r.Header.Set("Cookie", cookie)
|
|
|
|
r.URL = &url.URL{Path: loginURL}
|
|
|
|
handlerCalled = false
|
|
|
|
handler2(&w, &r)
|
2021-01-13 06:56:05 -07:00
|
|
|
assert.NotEmpty(t, w.hdr.Get("Location"))
|
|
|
|
assert.False(t, handlerCalled)
|
2019-10-11 02:41:01 -07:00
|
|
|
r.Header.Del("Cookie")
|
|
|
|
|
|
|
|
// get login page with an invalid cookie
|
|
|
|
handler2 = optionalAuth(handler)
|
|
|
|
w.hdr = make(http.Header)
|
|
|
|
r.Header.Set("Cookie", "bad")
|
|
|
|
r.URL = &url.URL{Path: loginURL}
|
|
|
|
handlerCalled = false
|
|
|
|
handler2(&w, &r)
|
|
|
|
assert.True(t, handlerCalled)
|
|
|
|
r.Header.Del("Cookie")
|
|
|
|
|
2020-02-13 08:42:07 -07:00
|
|
|
Context.auth.Close()
|
2019-10-11 02:41:01 -07:00
|
|
|
}
|
2021-04-06 04:31:20 -07:00
|
|
|
|
|
|
|
func TestRealIP(t *testing.T) {
|
|
|
|
const remoteAddr = "1.2.3.4:5678"
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
header http.Header
|
|
|
|
remoteAddr string
|
|
|
|
wantErrMsg string
|
|
|
|
wantIP net.IP
|
|
|
|
}{{
|
|
|
|
name: "success_no_proxy",
|
|
|
|
header: nil,
|
|
|
|
remoteAddr: remoteAddr,
|
|
|
|
wantErrMsg: "",
|
|
|
|
wantIP: net.IPv4(1, 2, 3, 4),
|
|
|
|
}, {
|
|
|
|
name: "success_proxy",
|
|
|
|
header: http.Header{
|
|
|
|
textproto.CanonicalMIMEHeaderKey("X-Real-IP"): []string{"1.2.3.5"},
|
|
|
|
},
|
|
|
|
remoteAddr: remoteAddr,
|
|
|
|
wantErrMsg: "",
|
|
|
|
wantIP: net.IPv4(1, 2, 3, 5),
|
|
|
|
}, {
|
|
|
|
name: "success_proxy_multiple",
|
|
|
|
header: http.Header{
|
|
|
|
textproto.CanonicalMIMEHeaderKey("X-Forwarded-For"): []string{
|
|
|
|
"1.2.3.6, 1.2.3.5",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
remoteAddr: remoteAddr,
|
|
|
|
wantErrMsg: "",
|
|
|
|
wantIP: net.IPv4(1, 2, 3, 6),
|
|
|
|
}, {
|
|
|
|
name: "error_no_proxy",
|
|
|
|
header: nil,
|
|
|
|
remoteAddr: "1:::2",
|
|
|
|
wantErrMsg: `getting ip from client addr: address 1:::2: ` +
|
|
|
|
`too many colons in address`,
|
|
|
|
wantIP: nil,
|
|
|
|
}}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
if tc.wantErrMsg == "" {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
} else {
|
|
|
|
require.Error(t, err)
|
|
|
|
assert.Equal(t, tc.wantErrMsg, err.Error())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|