|
| 1 | +package pg_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + "github.com/testcontainers/testcontainers-go" |
| 12 | + "github.com/testcontainers/testcontainers-go/modules/postgres" |
| 13 | + "github.com/testcontainers/testcontainers-go/wait" |
| 14 | + |
| 15 | + "github.com/spandigital/cel2sql/v2/pg" |
| 16 | +) |
| 17 | + |
| 18 | +// TestConnectionStringLengthValidation tests that connection strings exceeding |
| 19 | +// the maximum length are rejected |
| 20 | +func TestConnectionStringLengthValidation(t *testing.T) { |
| 21 | + ctx := context.Background() |
| 22 | + |
| 23 | + tests := []struct { |
| 24 | + name string |
| 25 | + connStr string |
| 26 | + expectErr bool |
| 27 | + errMsg string |
| 28 | + }{ |
| 29 | + { |
| 30 | + name: "valid_short_connection_string", |
| 31 | + connStr: "postgresql://user:pass@localhost:5432/db", |
| 32 | + expectErr: false, |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "valid_connection_string_at_limit", |
| 36 | + connStr: "postgresql://user:pass@localhost:5432/db?" + strings.Repeat("a", 959), // Total = 1000 |
| 37 | + expectErr: false, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "connection_string_exceeds_limit", |
| 41 | + connStr: "postgresql://user:pass@localhost:5432/db?" + strings.Repeat("a", 960), // Total = 1001 |
| 42 | + expectErr: true, |
| 43 | + errMsg: "connection string exceeds maximum length", |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "very_long_connection_string", |
| 47 | + connStr: strings.Repeat("postgresql://user:pass@localhost:5432/db?", 100), // Way over limit |
| 48 | + expectErr: true, |
| 49 | + errMsg: "connection string exceeds maximum length", |
| 50 | + }, |
| 51 | + } |
| 52 | + |
| 53 | + for _, tt := range tests { |
| 54 | + t.Run(tt.name, func(t *testing.T) { |
| 55 | + provider, err := pg.NewTypeProviderWithConnection(ctx, tt.connStr) |
| 56 | + |
| 57 | + if tt.expectErr { |
| 58 | + require.Error(t, err) |
| 59 | + assert.Contains(t, err.Error(), tt.errMsg) |
| 60 | + assert.Nil(t, provider) |
| 61 | + } else { |
| 62 | + // Note: These will fail to connect since they're not real DBs, |
| 63 | + // but we're testing length validation which happens first |
| 64 | + if err != nil { |
| 65 | + // Should fail with connection error, not length error |
| 66 | + assert.NotContains(t, err.Error(), "exceeds maximum length") |
| 67 | + } |
| 68 | + if provider != nil { |
| 69 | + provider.Close() |
| 70 | + } |
| 71 | + } |
| 72 | + }) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// TestMalformedConnectionStringsNoCredentialExposure tests that malformed |
| 77 | +// connection strings don't expose credentials in error messages |
| 78 | +func TestMalformedConnectionStringsNoCredentialExposure(t *testing.T) { |
| 79 | + ctx := context.Background() |
| 80 | + |
| 81 | + // Test various truly malformed connection strings that will cause parsing errors |
| 82 | + testCases := []struct { |
| 83 | + name string |
| 84 | + connStr string |
| 85 | + secrets []string // Strings that should NOT appear in error |
| 86 | + }{ |
| 87 | + { |
| 88 | + name: "invalid_syntax_with_credentials", |
| 89 | + connStr: "postgresql://admin:secret@::invalid::/database", |
| 90 | + secrets: []string{"secret", "admin"}, |
| 91 | + }, |
| 92 | + { |
| 93 | + name: "malformed_uri_with_password", |
| 94 | + connStr: "postgressql://user:P@ss123!@[invalid", // Invalid URI syntax |
| 95 | + secrets: []string{"P@ss123!", "user"}, |
| 96 | + }, |
| 97 | + { |
| 98 | + name: "broken_url_encoding", |
| 99 | + connStr: "postgresql://app:pass%@host/db", // Invalid URL encoding |
| 100 | + secrets: []string{"pass%", "app"}, |
| 101 | + }, |
| 102 | + } |
| 103 | + |
| 104 | + for _, tc := range testCases { |
| 105 | + t.Run(tc.name, func(t *testing.T) { |
| 106 | + provider, err := pg.NewTypeProviderWithConnection(ctx, tc.connStr) |
| 107 | + |
| 108 | + // These should fail during parsing |
| 109 | + if err != nil { |
| 110 | + // Critical security check: error message must NOT contain any secrets |
| 111 | + errorMsg := err.Error() |
| 112 | + t.Logf("Error message (should be generic): %s", errorMsg) |
| 113 | + |
| 114 | + for _, secret := range tc.secrets { |
| 115 | + assert.NotContains(t, errorMsg, secret, |
| 116 | + "Error message MUST NOT expose credential: %s", secret) |
| 117 | + } |
| 118 | + |
| 119 | + // Verify we get generic error message (either length or connection error) |
| 120 | + assert.True(t, |
| 121 | + errorMsg == "failed to create connection pool" || |
| 122 | + strings.Contains(errorMsg, "exceeds maximum length"), |
| 123 | + "Error should be generic, got: %s", errorMsg) |
| 124 | + } |
| 125 | + |
| 126 | + if provider != nil { |
| 127 | + provider.Close() |
| 128 | + } |
| 129 | + }) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +// TestValidConnectionWithPostgreSQL17 tests that valid connection strings work |
| 134 | +// correctly with PostgreSQL 17 and don't break existing functionality |
| 135 | +func TestValidConnectionWithPostgreSQL17(t *testing.T) { |
| 136 | + ctx := context.Background() |
| 137 | + |
| 138 | + // Create a PostgreSQL 17 container |
| 139 | + container, err := postgres.Run(ctx, |
| 140 | + "postgres:17", |
| 141 | + postgres.WithDatabase("testdb"), |
| 142 | + postgres.WithUsername("testuser"), |
| 143 | + postgres.WithPassword("testpass"), |
| 144 | + testcontainers.WithWaitStrategy( |
| 145 | + wait.ForLog("database system is ready to accept connections"). |
| 146 | + WithOccurrence(2). |
| 147 | + WithStartupTimeout(time.Second*60), |
| 148 | + ), |
| 149 | + ) |
| 150 | + require.NoError(t, err) |
| 151 | + |
| 152 | + defer func() { |
| 153 | + if err := container.Terminate(ctx); err != nil { |
| 154 | + t.Errorf("failed to terminate container: %v", err) |
| 155 | + } |
| 156 | + }() |
| 157 | + |
| 158 | + // Get connection string |
| 159 | + connStr, err := container.ConnectionString(ctx, "sslmode=disable") |
| 160 | + require.NoError(t, err) |
| 161 | + t.Logf("Connection string length: %d characters", len(connStr)) |
| 162 | + |
| 163 | + // Verify connection string is within limit |
| 164 | + assert.LessOrEqual(t, len(connStr), pg.MaxConnectionStringLength, |
| 165 | + "Generated connection string should be within limit") |
| 166 | + |
| 167 | + // Test that connection succeeds with valid connection string |
| 168 | + provider, err := pg.NewTypeProviderWithConnection(ctx, connStr) |
| 169 | + require.NoError(t, err, "Valid connection string should not error") |
| 170 | + require.NotNil(t, provider, "Provider should be created") |
| 171 | + defer provider.Close() |
| 172 | + |
| 173 | + // Verify provider is functional by accessing schemas |
| 174 | + schemas := provider.GetSchemas() |
| 175 | + assert.NotNil(t, schemas) |
| 176 | +} |
| 177 | + |
| 178 | +// TestInvalidConnectionFormats tests various invalid connection string formats |
| 179 | +func TestInvalidConnectionFormats(t *testing.T) { |
| 180 | + ctx := context.Background() |
| 181 | + |
| 182 | + testCases := []struct { |
| 183 | + name string |
| 184 | + connStr string |
| 185 | + }{ |
| 186 | + { |
| 187 | + name: "invalid_scheme", |
| 188 | + connStr: "mysql://user:pass@localhost/db", |
| 189 | + }, |
| 190 | + { |
| 191 | + name: "invalid_port", |
| 192 | + connStr: "postgresql://user:pass@localhost:abc/db", |
| 193 | + }, |
| 194 | + { |
| 195 | + name: "garbage_string", |
| 196 | + connStr: "this is not a connection string", |
| 197 | + }, |
| 198 | + } |
| 199 | + |
| 200 | + for _, tc := range testCases { |
| 201 | + t.Run(tc.name, func(t *testing.T) { |
| 202 | + provider, err := pg.NewTypeProviderWithConnection(ctx, tc.connStr) |
| 203 | + |
| 204 | + // Verify error is generic if one occurs |
| 205 | + if err != nil { |
| 206 | + errorMsg := err.Error() |
| 207 | + // Should be either length error or generic connection error |
| 208 | + assert.True(t, |
| 209 | + errorMsg == "failed to create connection pool" || |
| 210 | + strings.Contains(errorMsg, "exceeds maximum length"), |
| 211 | + "Error should be generic, got: %s", errorMsg) |
| 212 | + |
| 213 | + // Verify no connection details leaked |
| 214 | + assert.NotContains(t, errorMsg, tc.connStr) |
| 215 | + } |
| 216 | + |
| 217 | + if provider != nil { |
| 218 | + provider.Close() |
| 219 | + } |
| 220 | + }) |
| 221 | + } |
| 222 | +} |
| 223 | + |
| 224 | +// TestConnectionStringSecurityProperties verifies security properties of connection handling |
| 225 | +func TestConnectionStringSecurityProperties(t *testing.T) { |
| 226 | + ctx := context.Background() |
| 227 | + |
| 228 | + t.Run("error_does_not_leak_connection_string", func(t *testing.T) { |
| 229 | + // Use a malformed connection string that will actually cause a parsing error |
| 230 | + connStr := "postgresql://UNIQUE_USER_12345:UNIQUE_PASS_67890@[::invalid/UNIQUE_DB_ABC" |
| 231 | + |
| 232 | + provider, err := pg.NewTypeProviderWithConnection(ctx, connStr) |
| 233 | + |
| 234 | + if err != nil { |
| 235 | + errorMsg := err.Error() |
| 236 | + t.Logf("Error message: %s", errorMsg) |
| 237 | + |
| 238 | + // Verify none of the unique markers appear in error |
| 239 | + assert.NotContains(t, errorMsg, "UNIQUE_USER_12345") |
| 240 | + assert.NotContains(t, errorMsg, "UNIQUE_PASS_67890") |
| 241 | + assert.NotContains(t, errorMsg, "UNIQUE_DB_ABC") |
| 242 | + |
| 243 | + // Should be our generic message |
| 244 | + assert.Equal(t, "failed to create connection pool", errorMsg) |
| 245 | + } |
| 246 | + |
| 247 | + if provider != nil { |
| 248 | + provider.Close() |
| 249 | + } |
| 250 | + }) |
| 251 | + |
| 252 | + t.Run("length_validation_happens_before_parsing", func(t *testing.T) { |
| 253 | + // Create a very long invalid connection string |
| 254 | + longConnStr := "postgresql://user:password@host/db?" + strings.Repeat("x", 2000) |
| 255 | + |
| 256 | + provider, err := pg.NewTypeProviderWithConnection(ctx, longConnStr) |
| 257 | + require.Error(t, err) |
| 258 | + require.Nil(t, provider) |
| 259 | + |
| 260 | + // Should fail with length error, not parsing error |
| 261 | + assert.Contains(t, err.Error(), "exceeds maximum length") |
| 262 | + assert.NotContains(t, err.Error(), "user") |
| 263 | + assert.NotContains(t, err.Error(), "password") |
| 264 | + }) |
| 265 | + |
| 266 | + t.Run("no_connection_details_in_error_type", func(t *testing.T) { |
| 267 | + // Use malformed string that will cause parse error |
| 268 | + connStr := "postgresql://secret_user:secret_pass@[invalid" |
| 269 | + |
| 270 | + provider, err := pg.NewTypeProviderWithConnection(ctx, connStr) |
| 271 | + |
| 272 | + if err != nil { |
| 273 | + // Error should not expose details |
| 274 | + errorMsg := err.Error() |
| 275 | + t.Logf("Error message: %s", errorMsg) |
| 276 | + |
| 277 | + assert.Equal(t, "failed to create connection pool", errorMsg) |
| 278 | + |
| 279 | + // Verify it's a plain error without wrapped details |
| 280 | + assert.NotContains(t, errorMsg, "secret_user") |
| 281 | + assert.NotContains(t, errorMsg, "secret_pass") |
| 282 | + } |
| 283 | + |
| 284 | + if provider != nil { |
| 285 | + provider.Close() |
| 286 | + } |
| 287 | + }) |
| 288 | +} |
| 289 | + |
| 290 | +// TestConnectionStringLengthConstant verifies the constant value is reasonable |
| 291 | +func TestConnectionStringLengthConstant(t *testing.T) { |
| 292 | + // Verify constant is set to expected value |
| 293 | + assert.Equal(t, 1000, pg.MaxConnectionStringLength, |
| 294 | + "MaxConnectionStringLength should be 1000 (aligns with ODBC standard of 1024)") |
| 295 | + |
| 296 | + // Verify typical connection strings fit well within limit |
| 297 | + typicalConnStr := "postgresql://username:password@hostname.example.com:5432/database_name?sslmode=require&connect_timeout=10" |
| 298 | + assert.Less(t, len(typicalConnStr), pg.MaxConnectionStringLength, |
| 299 | + "Typical connection string should fit within limit") |
| 300 | + |
| 301 | + // Verify even complex connection strings fit |
| 302 | + complexConnStr := "postgresql://my_application_user:My$ecureP@ssw0rd!@prod-db-primary.us-east-1.company.internal:5432/application_database?sslmode=verify-full&sslrootcert=/etc/ssl/certs/ca-bundle.crt&connect_timeout=30&application_name=MyApp" |
| 303 | + assert.Less(t, len(complexConnStr), pg.MaxConnectionStringLength, |
| 304 | + "Complex connection string should fit within limit") |
| 305 | +} |
0 commit comments