Skip to content

Commit 3799f42

Browse files
authored
fix: no fallback from TLS to unsecure connection (#4113)
* fix: no fallback from TLS to unsecure connection * chore: improve debug logging
1 parent b4b19cb commit 3799f42

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

internal/utils/connect.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ func GetPoolerConfig(projectRef string) *pgconn.Config {
7070
return nil
7171
}
7272
// There is a risk of MITM attack if we simply trust the hostname specified in pooler URL.
73-
if !isSupabaseDomain(poolerConfig.Host) {
74-
fmt.Fprintln(logger, "Pooler hostname does not belong to Supabase domain:", poolerConfig.Host)
73+
if !strings.HasSuffix(poolerConfig.Host, "."+CurrentProfile.ProjectHost) {
74+
fmt.Fprintln(logger, "Pooler hostname does not belong to current profile:", poolerConfig.Host)
7575
return nil
7676
}
7777
fmt.Fprintln(logger, "Using connection pooler:", Config.Db.Pooler.ConnectionString)
@@ -92,15 +92,6 @@ func ParsePoolerURL(connString string) (*pgconn.Config, error) {
9292
return poolerConfig, nil
9393
}
9494

95-
func isSupabaseDomain(host string) bool {
96-
switch GetSupabaseAPIHost() {
97-
case "https://api.supabase.green":
98-
return strings.HasSuffix(host, ".supabase.green")
99-
default:
100-
return strings.HasSuffix(host, ".supabase.com")
101-
}
102-
}
103-
10495
// Connnect to local Postgres with optimised settings. The caller is responsible for closing the connection returned.
10596
func ConnectLocalPostgres(ctx context.Context, config pgconn.Config, options ...func(*pgx.ConnConfig)) (*pgx.Conn, error) {
10697
if len(config.Host) == 0 {
@@ -121,13 +112,29 @@ func ConnectLocalPostgres(ctx context.Context, config pgconn.Config, options ...
121112
if config.ConnectTimeout == 0 {
122113
config.ConnectTimeout = 2 * time.Second
123114
}
115+
options = append(options, func(cc *pgx.ConnConfig) {
116+
cc.TLSConfig = nil
117+
})
124118
return ConnectByUrl(ctx, ToPostgresURL(config), options...)
125119
}
126120

127121
func ConnectByUrl(ctx context.Context, url string, options ...func(*pgx.ConnConfig)) (*pgx.Conn, error) {
128122
if viper.GetBool("DEBUG") {
129123
options = append(options, debug.SetupPGX)
130124
}
125+
// No fallback from TLS to unsecure connection
126+
options = append(options, func(cc *pgx.ConnConfig) {
127+
if cc.TLSConfig == nil {
128+
return
129+
}
130+
var fallbacks []*pgconn.FallbackConfig
131+
for _, fc := range cc.Fallbacks {
132+
if fc.TLSConfig != nil {
133+
fallbacks = append(fallbacks, fc)
134+
}
135+
}
136+
cc.Fallbacks = fallbacks
137+
})
131138
return pgxv5.Connect(ctx, url, options...)
132139
}
133140

internal/utils/connect_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package utils
22

33
import (
44
"context"
5+
"fmt"
56
"net"
67
"net/http"
78
"testing"
@@ -25,9 +26,9 @@ var dbConfig = pgconn.Config{
2526
Database: "postgres",
2627
}
2728

28-
const (
29-
PG13_POOLER_URL = "postgres://postgres:[YOUR-PASSWORD]@aws-0-ap-southeast-1.pooler.supabase.com:6543/postgres?options=reference%3Dzupyfdrjfhbeevcogohz"
30-
PG15_POOLER_URL = "postgres://postgres.zupyfdrjfhbeevcogohz:[YOUR-PASSWORD]@fly-0-sin.pooler.supabase.com:6543/postgres"
29+
var (
30+
PG13_POOLER_URL = fmt.Sprintf("postgres://postgres:[YOUR-PASSWORD]@aws-0-ap-southeast-1.pooler.%s:6543/postgres?options=reference%%3Dzupyfdrjfhbeevcogohz", CurrentProfile.ProjectHost)
31+
PG15_POOLER_URL = fmt.Sprintf("postgres://postgres.zupyfdrjfhbeevcogohz:[YOUR-PASSWORD]@fly-0-sin.pooler.%s:6543/postgres", CurrentProfile.ProjectHost)
3132
)
3233

3334
func TestConnectByConfig(t *testing.T) {

internal/utils/profile.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package utils
22

33
import (
44
"context"
5+
"fmt"
56
"strings"
67

78
"github.com/go-errors/errors"
@@ -45,6 +46,7 @@ func LoadProfile(ctx context.Context, fsys afero.Fs) error {
4546
prof := viper.GetString("PROFILE")
4647
for _, p := range allProfiles {
4748
if strings.EqualFold(p.Name, prof) {
49+
fmt.Fprintln(GetDebugLogger(), "Using project host:", p.ProjectHost)
4850
CurrentProfile = p
4951
return nil
5052
}

0 commit comments

Comments
 (0)