fix: address performance and security findings
- Add PostgreSQL-backed backend and frontend tests to CI - Harden Android API logging and disable app backups - Update the performance report with resolved findings
This commit is contained in:
1 parent
a84a36349b
commit
937f51b4fb
35 files changed
+2566
-332
No files matched your search
@@ -0,0 +1,121 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func mustTrusted(t *testing.T, value string) []*net.IPNet {
|
||||
t.Helper()
|
||||
networks, err := parseTrustedProxies(value)
|
||||
if err != nil {
|
||||
t.Fatalf("parseTrustedProxies(%q) error = %v", value, err)
|
||||
}
|
||||
return networks
|
||||
}
|
||||
|
||||
func TestParseTrustedProxiesAcceptsAddressesAndRanges(t *testing.T) {
|
||||
networks := mustTrusted(t, " 10.0.0.5, 192.168.2.0/24 ,, fd00::1 ")
|
||||
if len(networks) != 3 {
|
||||
t.Fatalf("parsed %d networks, want 3", len(networks))
|
||||
}
|
||||
for _, ip := range []string{"10.0.0.5", "192.168.2.77", "fd00::1"} {
|
||||
if !inNetworks(net.ParseIP(ip), networks) {
|
||||
t.Fatalf("%s should be trusted", ip)
|
||||
}
|
||||
}
|
||||
for _, ip := range []string{"10.0.0.6", "192.168.3.1", "fd00::2"} {
|
||||
if inNetworks(net.ParseIP(ip), networks) {
|
||||
t.Fatalf("%s should not be trusted", ip)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseTrustedProxiesEmptyTrustsNothing(t *testing.T) {
|
||||
networks := mustTrusted(t, "")
|
||||
if len(networks) != 0 {
|
||||
t.Fatalf("empty TRUSTED_PROXIES parsed %d networks, want none", len(networks))
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseTrustedProxiesRejectsGarbage(t *testing.T) {
|
||||
for _, value := range []string{"proxy.local", "10.0.0.0/33", "10.0.0.1/", "999.1.1.1"} {
|
||||
if _, err := parseTrustedProxies(value); err == nil {
|
||||
t.Fatalf("parseTrustedProxies(%q) returned no error", value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestClientIPUsesSocketPeerUnlessPeerIsTrusted(t *testing.T) {
|
||||
trusted := mustTrusted(t, "10.0.0.1")
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
remoteAddr string
|
||||
forwarded string
|
||||
trusted []*net.IPNet
|
||||
want string
|
||||
}{
|
||||
{name: "untrusted peer ignores forwarded header", remoteAddr: "192.0.2.9:4000", forwarded: "203.0.113.7", trusted: trusted, want: "192.0.2.9"},
|
||||
{name: "no trusted proxies at all", remoteAddr: "10.0.0.1:4000", forwarded: "203.0.113.7", trusted: nil, want: "10.0.0.1"},
|
||||
{name: "trusted peer uses forwarded client", remoteAddr: "10.0.0.1:4000", forwarded: "203.0.113.7", trusted: trusted, want: "203.0.113.7"},
|
||||
{name: "trusted peer without header uses peer", remoteAddr: "10.0.0.1:4000", forwarded: "", trusted: trusted, want: "10.0.0.1"},
|
||||
{name: "chain skips trusted hops", remoteAddr: "10.0.0.1:4000", forwarded: "198.51.100.4, 203.0.113.7, 10.0.0.1", trusted: trusted, want: "203.0.113.7"},
|
||||
{name: "spoofed leftmost entry is not used", remoteAddr: "10.0.0.1:4000", forwarded: "1.2.3.4, 203.0.113.7", trusted: trusted, want: "203.0.113.7"},
|
||||
{name: "malformed entry falls back to peer", remoteAddr: "10.0.0.1:4000", forwarded: "203.0.113.7, not-an-ip", trusted: trusted, want: "10.0.0.1"},
|
||||
{name: "all hops trusted returns leftmost", remoteAddr: "10.0.0.1:4000", forwarded: "10.0.0.1, 10.0.0.1", trusted: trusted, want: "10.0.0.1"},
|
||||
{name: "IPv6 forwarded client is normalised", remoteAddr: "10.0.0.1:4000", forwarded: "2001:DB8::7", trusted: trusted, want: "2001:db8::7"},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
request := httptest.NewRequest(http.MethodPost, "/api/auth/login", nil)
|
||||
request.RemoteAddr = tc.remoteAddr
|
||||
if tc.forwarded != "" {
|
||||
request.Header.Set("X-Forwarded-For", tc.forwarded)
|
||||
}
|
||||
if got := clientIP(request, tc.trusted); got != tc.want {
|
||||
t.Fatalf("clientIP() = %q, want %q", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequestIsHTTPSHonoursTLSOrTrustedProxyOnly(t *testing.T) {
|
||||
trusted := mustTrusted(t, "10.0.0.1")
|
||||
|
||||
direct := httptest.NewRequest(http.MethodGet, "/api/lists", nil)
|
||||
direct.TLS = &tls.ConnectionState{}
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
remoteAddr string
|
||||
proto string
|
||||
tlsConn bool
|
||||
want bool
|
||||
}{
|
||||
{name: "plain request", remoteAddr: "192.0.2.9:4000", want: false},
|
||||
{name: "terminated here", remoteAddr: "192.0.2.9:4000", tlsConn: true, want: true},
|
||||
{name: "trusted proxy reports https", remoteAddr: "10.0.0.1:4000", proto: "https", want: true},
|
||||
{name: "trusted proxy reports http", remoteAddr: "10.0.0.1:4000", proto: "http", want: false},
|
||||
{name: "untrusted peer claims https", remoteAddr: "192.0.2.9:4000", proto: "https", want: false},
|
||||
{name: "last proto hop decides", remoteAddr: "10.0.0.1:4000", proto: "https, http", want: false},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
request := httptest.NewRequest(http.MethodGet, "/api/lists", nil)
|
||||
request.RemoteAddr = tc.remoteAddr
|
||||
if tc.proto != "" {
|
||||
request.Header.Set("X-Forwarded-Proto", tc.proto)
|
||||
}
|
||||
if tc.tlsConn {
|
||||
request.TLS = direct.TLS
|
||||
}
|
||||
if got := requestIsHTTPS(request, trusted); got != tc.want {
|
||||
t.Fatalf("requestIsHTTPS() = %v, want %v", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user