Compare commits

...
1 Commits
Author SHA1 Message Date
lukaszraczylo 013dfa99fd Updates. 2025-09-30 00:36:46 +01:00
11 changed files with 387 additions and 56 deletions
+66 -21
View File
@@ -8,6 +8,7 @@ import (
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"os"
"time" "time"
"unicode/utf8" "unicode/utf8"
@@ -16,6 +17,8 @@ import (
libpack_logger "github.com/lukaszraczylo/go-simple-graphql/logging" libpack_logger "github.com/lukaszraczylo/go-simple-graphql/logging"
) )
// Size limit constants are defined in query.go
// min returns the smaller of two integers // min returns the smaller of two integers
func min(a, b int) int { func min(a, b int) int {
if a < b { if a < b {
@@ -32,23 +35,38 @@ func max(a, b int) int {
return b return b
} }
var ( // getDefaultClient creates a default HTTP client with optimized settings
// Shared HTTP transport with optimized settings // This is called dynamically to ensure environment variables are read at runtime
defaultTransport = &http.Transport{ func getDefaultClient() *http.Client {
MaxIdleConns: 100, return &http.Client{
MaxIdleConnsPerHost: 100, Transport: &http.Transport{
IdleConnTimeout: 90 * time.Second, MaxIdleConns: 100,
DisableCompression: true, // Disable automatic request compression to prevent "trailing garbage" errors MaxIdleConnsPerHost: 100,
ForceAttemptHTTP2: true, IdleConnTimeout: 90 * time.Second,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, // Skip TLS verification for test environments DisableCompression: true, // Disable automatic request compression to prevent "trailing garbage" errors
ForceAttemptHTTP2: true,
TLSClientConfig: getTLSConfig(),
},
Timeout: 30 * time.Second,
}
}
// getTLSConfig returns TLS configuration based on environment settings
// By default, TLS verification is enabled for security.
// Set GRAPHQL_INSECURE_SKIP_VERIFY=true to disable verification (NOT RECOMMENDED for production)
func getTLSConfig() *tls.Config {
skipVerify := os.Getenv("GRAPHQL_INSECURE_SKIP_VERIFY") == "true"
if skipVerify {
// Log warning when running in insecure mode
fmt.Fprintf(os.Stderr, "WARNING: TLS certificate verification is disabled. This is insecure and should not be used in production.\n")
} }
// Shared HTTP client with timeouts return &tls.Config{
defaultClient = &http.Client{ InsecureSkipVerify: skipVerify,
Transport: defaultTransport, MinVersion: tls.VersionTLS12, // Enforce minimum TLS 1.2 for security
Timeout: 30 * time.Second,
} }
) }
func (qe *QueryExecutor) executeQuery() ([]byte, error) { func (qe *QueryExecutor) executeQuery() ([]byte, error) {
// Reuse buffer from pool to avoid allocations // Reuse buffer from pool to avoid allocations
@@ -58,7 +76,10 @@ func (qe *QueryExecutor) executeQuery() ([]byte, error) {
buf.Write(qe.Query) buf.Write(qe.Query)
httpRequest, err := http.NewRequest(http.MethodPost, qe.endpoint, nil) qe.mu.RLock()
endpoint := qe.endpoint
qe.mu.RUnlock()
httpRequest, err := http.NewRequest(http.MethodPost, endpoint, nil)
if err != nil { if err != nil {
qe.Logger.Error(&libpack_logger.LogMessage{ qe.Logger.Error(&libpack_logger.LogMessage{
Message: "Can't create HTTP request", Message: "Can't create HTTP request",
@@ -82,7 +103,9 @@ func (qe *QueryExecutor) executeQuery() ([]byte, error) {
retriesMax := 1 retriesMax := 1
if qe.Retries { if qe.Retries {
qe.mu.RLock()
retriesMax = qe.retries_number retriesMax = qe.retries_number
qe.mu.RUnlock()
} }
var queryResult queryResults var queryResult queryResults
@@ -100,8 +123,8 @@ func (qe *QueryExecutor) executeQuery() ([]byte, error) {
"body_size": len(requestBody), "body_size": len(requestBody),
"contains_jsonQuery": bytes.Contains(requestBody, []byte(`"jsonQuery"`)), "contains_jsonQuery": bytes.Contains(requestBody, []byte(`"jsonQuery"`)),
"contains_base64": bytes.Contains(requestBody, []byte("eyJ")), "contains_base64": bytes.Contains(requestBody, []byte("eyJ")),
"first_100_chars": string(requestBody[:min(100, len(requestBody))]), "first_100_chars": sanitizeForLogging(string(requestBody[:min(100, len(requestBody))])),
"last_50_chars": string(requestBody[max(0, len(requestBody)-50):]), "last_50_chars": sanitizeForLogging(string(requestBody[max(0, len(requestBody)-50):])),
"is_valid_utf8": utf8.Valid(requestBody), "is_valid_utf8": utf8.Valid(requestBody),
"has_null_bytes": bytes.Contains(requestBody, []byte{0}), "has_null_bytes": bytes.Contains(requestBody, []byte{0}),
"has_control_chars": hasControlChars(requestBody), "has_control_chars": hasControlChars(requestBody),
@@ -132,9 +155,11 @@ func (qe *QueryExecutor) executeQuery() ([]byte, error) {
}) })
// Use default client if custom client is not set // Use default client if custom client is not set
qe.mu.RLock()
client := qe.client client := qe.client
qe.mu.RUnlock()
if client == nil { if client == nil {
client = defaultClient client = getDefaultClient()
} }
httpResponse, err := client.Do(httpRequest) httpResponse, err := client.Do(httpRequest)
if err != nil { if err != nil {
@@ -159,16 +184,32 @@ func (qe *QueryExecutor) executeQuery() ([]byte, error) {
}, },
}) })
// First, read the entire response body into a buffer // Check Content-Length header if available for early size validation
if contentLength := httpResponse.ContentLength; contentLength > 0 && contentLength > MaxResponseSize {
return fmt.Errorf("response size %d exceeds maximum allowed size of %d bytes", contentLength, MaxResponseSize)
}
// First, read the entire response body into a buffer with size limit
rawBuf := bufferPool.Get().(*bytes.Buffer) rawBuf := bufferPool.Get().(*bytes.Buffer)
rawBuf.Reset() rawBuf.Reset()
defer bufferPool.Put(rawBuf) defer bufferPool.Put(rawBuf)
_, err = io.Copy(rawBuf, httpResponse.Body) // Use LimitedReader to prevent reading more than MaxResponseSize
limitedReader := &io.LimitedReader{
R: httpResponse.Body,
N: MaxResponseSize + 1, // Read one extra byte to detect oversized responses
}
n, err := io.Copy(rawBuf, limitedReader)
if err != nil { if err != nil {
return fmt.Errorf("error reading HTTP response body: %w", err) return fmt.Errorf("error reading HTTP response body: %w", err)
} }
// Check if response exceeded size limit
if n > MaxResponseSize {
return fmt.Errorf("response size exceeds maximum allowed size of %d bytes", MaxResponseSize)
}
rawData := rawBuf.Bytes() rawData := rawBuf.Bytes()
qe.Logger.Debug(&libpack_logger.LogMessage{ qe.Logger.Debug(&libpack_logger.LogMessage{
Message: "Read raw response data", Message: "Read raw response data",
@@ -341,7 +382,11 @@ func (qe *QueryExecutor) executeQuery() ([]byte, error) {
}), }),
retry.Attempts(uint(retriesMax)), retry.Attempts(uint(retriesMax)),
retry.DelayType(retry.BackOffDelay), retry.DelayType(retry.BackOffDelay),
retry.Delay(time.Duration(qe.retries_delay)), retry.Delay(func() time.Duration {
qe.mu.RLock()
defer qe.mu.RUnlock()
return qe.retries_delay
}()),
retry.MaxDelay(10*time.Second), retry.MaxDelay(10*time.Second),
retry.LastErrorOnly(true), retry.LastErrorOnly(true),
) )
+37 -8
View File
@@ -2,7 +2,9 @@ package gql
import ( import (
"crypto/tls" "crypto/tls"
"fmt"
"net/http" "net/http"
"os"
"strings" "strings"
"time" "time"
@@ -10,7 +12,7 @@ import (
"golang.org/x/net/http2" "golang.org/x/net/http2"
) )
func (b *BaseClient) createHttpClient() (http_client *http.Client) { func (b *BaseClient) createHttpClient() (*http.Client, error) {
// Optimized connection pool settings for better performance // Optimized connection pool settings for better performance
httpTransport := &http.Transport{ httpTransport := &http.Transport{
MaxIdleConns: 100, // Reduced from 512 MaxIdleConns: 100, // Reduced from 512
@@ -24,7 +26,14 @@ func (b *BaseClient) createHttpClient() (http_client *http.Client) {
ReadBufferSize: 4096, // Optimize buffer size ReadBufferSize: 4096, // Optimize buffer size
} }
if strings.HasPrefix(b.endpoint, "http://") { var http_client *http.Client
// Protect read of endpoint field
b.mu.RLock()
endpoint := b.endpoint
b.mu.RUnlock()
if strings.HasPrefix(endpoint, "http://") {
http_client = &http.Client{ http_client = &http.Client{
Timeout: 30 * time.Second, // Increased for better reliability Timeout: 30 * time.Second, // Increased for better reliability
Transport: httpTransport, Transport: httpTransport,
@@ -36,10 +45,12 @@ func (b *BaseClient) createHttpClient() (http_client *http.Client) {
Message: "Using HTTP/1.1 over http", Message: "Using HTTP/1.1 over http",
Pairs: nil, Pairs: nil,
}) })
} else if strings.HasPrefix(b.endpoint, "https://") { } else if strings.HasPrefix(endpoint, "https://") {
tlsClientConfig := &tls.Config{} tlsClientConfig := getHTTPClientTLSConfig()
if strings.HasPrefix(b.endpoint, "https://") { if strings.HasPrefix(endpoint, "https://") && tlsClientConfig.InsecureSkipVerify {
tlsClientConfig.InsecureSkipVerify = true b.Logger.Warning(&libpack_logger.LogMessage{
Message: "TLS certificate verification is disabled. This is insecure and should not be used in production.",
})
} }
http2Transport := &http2.Transport{ http2Transport := &http2.Transport{
AllowHTTP: true, AllowHTTP: true,
@@ -64,7 +75,25 @@ func (b *BaseClient) createHttpClient() (http_client *http.Client) {
b.Logger.Critical(&libpack_logger.LogMessage{ b.Logger.Critical(&libpack_logger.LogMessage{
Message: "Invalid endpoint - neither http or https", Message: "Invalid endpoint - neither http or https",
}) })
return nil // Return error instead of nil to prevent nil pointer dereference
return nil, fmt.Errorf("invalid endpoint: %s - must start with http:// or https://", endpoint)
}
return http_client, nil
}
// getHTTPClientTLSConfig returns TLS configuration for HTTP client
// By default, TLS verification is enabled for security.
// Set GRAPHQL_INSECURE_SKIP_VERIFY=true to disable verification (NOT RECOMMENDED for production)
func getHTTPClientTLSConfig() *tls.Config {
skipVerify := os.Getenv("GRAPHQL_INSECURE_SKIP_VERIFY") == "true"
if skipVerify {
// Log warning to stderr when running in insecure mode
fmt.Fprintf(os.Stderr, "WARNING: TLS certificate verification is disabled. This is insecure and should not be used in production.\n")
}
return &tls.Config{
InsecureSkipVerify: skipVerify,
MinVersion: tls.VersionTLS12, // Enforce minimum TLS 1.2 for security
} }
return http_client
} }
+88 -13
View File
@@ -2,6 +2,7 @@ package gql
import ( import (
"net/http" "net/http"
"os"
"testing" "testing"
"time" "time"
@@ -13,7 +14,8 @@ func (suite *Tests) TestBaseClient_createHttpClient() {
client := NewConnection() client := NewConnection()
client.SetEndpoint("http://example.com/graphql") client.SetEndpoint("http://example.com/graphql")
httpClient := client.createHttpClient() httpClient, err := client.createHttpClient()
assert.NoError(err)
assert.NotNil(httpClient) assert.NotNil(httpClient)
assert.Equal(30*time.Second, httpClient.Timeout) assert.Equal(30*time.Second, httpClient.Timeout)
assert.NotNil(httpClient.Transport) assert.NotNil(httpClient.Transport)
@@ -33,10 +35,22 @@ func (suite *Tests) TestBaseClient_createHttpClient() {
}) })
suite.T().Run("should create HTTP/2 client for https endpoints", func(t *testing.T) { suite.T().Run("should create HTTP/2 client for https endpoints", func(t *testing.T) {
// Set environment variable for test to skip TLS verification
oldValue := os.Getenv("GRAPHQL_INSECURE_SKIP_VERIFY")
os.Setenv("GRAPHQL_INSECURE_SKIP_VERIFY", "true")
t.Cleanup(func() {
if oldValue != "" {
os.Setenv("GRAPHQL_INSECURE_SKIP_VERIFY", oldValue)
} else {
os.Unsetenv("GRAPHQL_INSECURE_SKIP_VERIFY")
}
})
client := NewConnection() client := NewConnection()
client.SetEndpoint("https://example.com/graphql") client.SetEndpoint("https://example.com/graphql")
httpClient := client.createHttpClient() httpClient, err := client.createHttpClient()
assert.NoError(err)
assert.NotNil(httpClient) assert.NotNil(httpClient)
assert.Equal(30*time.Second, httpClient.Timeout) assert.Equal(30*time.Second, httpClient.Timeout)
assert.NotNil(httpClient.Transport) assert.NotNil(httpClient.Transport)
@@ -56,7 +70,9 @@ func (suite *Tests) TestBaseClient_createHttpClient() {
client := NewConnection() client := NewConnection()
client.SetEndpoint("ftp://example.com/graphql") client.SetEndpoint("ftp://example.com/graphql")
httpClient := client.createHttpClient() httpClient, err := client.createHttpClient()
assert.Error(err)
assert.Contains(err.Error(), "invalid endpoint")
assert.Nil(httpClient) assert.Nil(httpClient)
}) })
@@ -64,21 +80,24 @@ func (suite *Tests) TestBaseClient_createHttpClient() {
client := NewConnection() client := NewConnection()
client.SetEndpoint("http://example.com/graphql") client.SetEndpoint("http://example.com/graphql")
httpClient := client.createHttpClient() httpClient, err := client.createHttpClient()
assert.NoError(err)
assert.NotNil(httpClient) assert.NotNil(httpClient)
assert.NotNil(httpClient.CheckRedirect) assert.NotNil(httpClient.CheckRedirect)
// Test redirect policy // Test redirect policy
req, _ := http.NewRequest("GET", "http://example.com", nil) req, _ := http.NewRequest("GET", "http://example.com", nil)
err := httpClient.CheckRedirect(req, []*http.Request{}) err2 := httpClient.CheckRedirect(req, []*http.Request{})
assert.Equal(http.ErrUseLastResponse, err) assert.Equal(http.ErrUseLastResponse, err2)
}) })
suite.T().Run("should handle empty endpoint", func(t *testing.T) { suite.T().Run("should handle empty endpoint", func(t *testing.T) {
client := NewConnection() client := NewConnection()
client.SetEndpoint("") client.SetEndpoint("")
httpClient := client.createHttpClient() httpClient, err := client.createHttpClient()
assert.Error(err)
assert.Contains(err.Error(), "invalid endpoint")
assert.Nil(httpClient) assert.Nil(httpClient)
}) })
@@ -86,9 +105,35 @@ func (suite *Tests) TestBaseClient_createHttpClient() {
client := NewConnection() client := NewConnection()
client.SetEndpoint("example.com/graphql") client.SetEndpoint("example.com/graphql")
httpClient := client.createHttpClient() httpClient, err := client.createHttpClient()
assert.Error(err)
assert.Contains(err.Error(), "invalid endpoint")
assert.Nil(httpClient) assert.Nil(httpClient)
}) })
suite.T().Run("should enable TLS verification by default for https endpoints", func(t *testing.T) {
// Ensure the environment variable is not set
oldValue := os.Getenv("GRAPHQL_INSECURE_SKIP_VERIFY")
os.Unsetenv("GRAPHQL_INSECURE_SKIP_VERIFY")
t.Cleanup(func() {
if oldValue != "" {
os.Setenv("GRAPHQL_INSECURE_SKIP_VERIFY", oldValue)
}
})
client := NewConnection()
client.SetEndpoint("https://example.com/graphql")
httpClient, err := client.createHttpClient()
assert.NoError(err)
assert.NotNil(httpClient)
// Check that TLS verification is enabled by default
transport, ok := httpClient.Transport.(*http2.Transport)
assert.True(ok)
assert.NotNil(transport.TLSClientConfig)
assert.False(transport.TLSClientConfig.InsecureSkipVerify) // Should be false by default
})
} }
func (suite *Tests) TestBaseClient_createHttpClient_transportSettings() { func (suite *Tests) TestBaseClient_createHttpClient_transportSettings() {
@@ -96,7 +141,9 @@ func (suite *Tests) TestBaseClient_createHttpClient_transportSettings() {
client := NewConnection() client := NewConnection()
client.SetEndpoint("http://example.com/graphql") client.SetEndpoint("http://example.com/graphql")
httpClient := client.createHttpClient() httpClient, err := client.createHttpClient()
assert.NoError(err)
assert.NotNil(httpClient)
transport := httpClient.Transport.(*http.Transport) transport := httpClient.Transport.(*http.Transport)
// Verify all transport settings // Verify all transport settings
@@ -112,10 +159,23 @@ func (suite *Tests) TestBaseClient_createHttpClient_transportSettings() {
}) })
suite.T().Run("should configure HTTP/2 transport correctly", func(t *testing.T) { suite.T().Run("should configure HTTP/2 transport correctly", func(t *testing.T) {
// Set environment variable for test to skip TLS verification
oldValue := os.Getenv("GRAPHQL_INSECURE_SKIP_VERIFY")
os.Setenv("GRAPHQL_INSECURE_SKIP_VERIFY", "true")
t.Cleanup(func() {
if oldValue != "" {
os.Setenv("GRAPHQL_INSECURE_SKIP_VERIFY", oldValue)
} else {
os.Unsetenv("GRAPHQL_INSECURE_SKIP_VERIFY")
}
})
client := NewConnection() client := NewConnection()
client.SetEndpoint("https://example.com/graphql") client.SetEndpoint("https://example.com/graphql")
httpClient := client.createHttpClient() httpClient, err := client.createHttpClient()
assert.NoError(err)
assert.NotNil(httpClient)
transport := httpClient.Transport.(*http2.Transport) transport := httpClient.Transport.(*http2.Transport)
// Verify all HTTP/2 transport settings // Verify all HTTP/2 transport settings
@@ -135,17 +195,30 @@ func (suite *Tests) TestBaseClient_createHttpClient_logging() {
// This test verifies that the function runs without error // This test verifies that the function runs without error
// The actual logging is tested indirectly through the logger tests // The actual logging is tested indirectly through the logger tests
httpClient := client.createHttpClient() httpClient, err := client.createHttpClient()
assert.NoError(err)
assert.NotNil(httpClient) assert.NotNil(httpClient)
}) })
suite.T().Run("should log HTTP/2 usage", func(t *testing.T) { suite.T().Run("should log HTTP/2 usage", func(t *testing.T) {
// Set environment variable for test to skip TLS verification
oldValue := os.Getenv("GRAPHQL_INSECURE_SKIP_VERIFY")
os.Setenv("GRAPHQL_INSECURE_SKIP_VERIFY", "true")
t.Cleanup(func() {
if oldValue != "" {
os.Setenv("GRAPHQL_INSECURE_SKIP_VERIFY", oldValue)
} else {
os.Unsetenv("GRAPHQL_INSECURE_SKIP_VERIFY")
}
})
client := NewConnection() client := NewConnection()
client.SetEndpoint("https://example.com/graphql") client.SetEndpoint("https://example.com/graphql")
// This test verifies that the function runs without error // This test verifies that the function runs without error
// The actual logging is tested indirectly through the logger tests // The actual logging is tested indirectly through the logger tests
httpClient := client.createHttpClient() httpClient, err := client.createHttpClient()
assert.NoError(err)
assert.NotNil(httpClient) assert.NotNil(httpClient)
}) })
@@ -155,7 +228,9 @@ func (suite *Tests) TestBaseClient_createHttpClient_logging() {
// This test verifies that the function runs without error // This test verifies that the function runs without error
// and returns nil for invalid endpoints (Critical() won't exit in tests) // and returns nil for invalid endpoints (Critical() won't exit in tests)
httpClient := client.createHttpClient() httpClient, err := client.createHttpClient()
assert.Error(err)
assert.Contains(err.Error(), "invalid endpoint")
assert.Nil(httpClient) assert.Nil(httpClient)
}) })
} }
+2
View File
@@ -10,6 +10,7 @@ import (
// TestQueryJSONEncoding verifies that the Query struct serializes correctly // TestQueryJSONEncoding verifies that the Query struct serializes correctly
// without including the JsonQuery field, preventing "Trailing garbage" errors // without including the JsonQuery field, preventing "Trailing garbage" errors
func TestQueryJSONEncoding(t *testing.T) { func TestQueryJSONEncoding(t *testing.T) {
t.Parallel()
assert := assertions.New(t) assert := assertions.New(t)
client := CreateTestClient() client := CreateTestClient()
@@ -47,6 +48,7 @@ func TestQueryJSONEncoding(t *testing.T) {
// TestQueryJSONEncodingPreventsTrailingGarbage ensures the fix prevents the specific error // TestQueryJSONEncodingPreventsTrailingGarbage ensures the fix prevents the specific error
func TestQueryJSONEncodingPreventsTrailingGarbage(t *testing.T) { func TestQueryJSONEncodingPreventsTrailingGarbage(t *testing.T) {
t.Parallel()
assert := assertions.New(t) assert := assertions.New(t)
client := CreateTestClient() client := CreateTestClient()
+42 -5
View File
@@ -1,6 +1,7 @@
package gql package gql
import ( import (
"fmt"
"net/http" "net/http"
"time" "time"
@@ -40,7 +41,18 @@ func NewConnection() (b *BaseClient) {
retries_number: envutil.GetInt("GRAPHQL_RETRIES_NUMBER", 3), retries_number: envutil.GetInt("GRAPHQL_RETRIES_NUMBER", 3),
minify_queries: envutil.GetBool("GRAPHQL_MINIFY_QUERIES", true), // Default: enabled for production efficiency minify_queries: envutil.GetBool("GRAPHQL_MINIFY_QUERIES", true), // Default: enabled for production efficiency
} }
b.client = b.createHttpClient() client, err := b.createHttpClient()
if err != nil {
b.Logger.Critical(&logging.LogMessage{
Message: "Failed to create HTTP client",
Pairs: map[string]interface{}{
"error": err.Error(),
},
})
// For backward compatibility, we'll still return the client but it won't work
return b
}
b.client = client
b.Logger.Debug(&logging.LogMessage{ b.Logger.Debug(&logging.LogMessage{
Message: "Created new GraphQL client connection", Message: "Created new GraphQL client connection",
Pairs: map[string]interface{}{ Pairs: map[string]interface{}{
@@ -51,21 +63,46 @@ func NewConnection() (b *BaseClient) {
} }
func (b *BaseClient) SetEndpoint(endpoint string) { func (b *BaseClient) SetEndpoint(endpoint string) {
b.mu.Lock()
defer b.mu.Unlock()
b.endpoint = endpoint b.endpoint = endpoint
} }
func (b *BaseClient) SetOutput(responseType string) { func (b *BaseClient) SetOutput(responseType string) error {
// allowed are byte, string, mapstring // Validate responseType - allowed values are "byte", "string", "mapstring"
// check if responseType is allowed allowedTypes := map[string]bool{
// TODO: implement "byte": true,
"string": true,
"mapstring": true,
}
if !allowedTypes[responseType] {
return fmt.Errorf("invalid response type: %s. Allowed values are: byte, string, mapstring", responseType)
}
b.mu.Lock()
defer b.mu.Unlock()
b.responseType = responseType b.responseType = responseType
b.Logger.Debug(&logging.LogMessage{
Message: "Response type updated",
Pairs: map[string]interface{}{
"response_type": responseType,
},
})
return nil
} }
func (b *BaseClient) SetHTTPClient(client *http.Client) { func (b *BaseClient) SetHTTPClient(client *http.Client) {
b.mu.Lock()
defer b.mu.Unlock()
b.client = client b.client = client
} }
func (b *BaseClient) SetQueryMinification(enabled bool) { func (b *BaseClient) SetQueryMinification(enabled bool) {
b.mu.Lock()
defer b.mu.Unlock()
b.minify_queries = enabled b.minify_queries = enabled
b.Logger.Debug(&logging.LogMessage{ b.Logger.Debug(&logging.LogMessage{
Message: "GraphQL query minification setting updated", Message: "GraphQL query minification setting updated",
+50 -6
View File
@@ -10,6 +10,13 @@ import (
libpack_logger "github.com/lukaszraczylo/go-simple-graphql/logging" libpack_logger "github.com/lukaszraczylo/go-simple-graphql/logging"
) )
// Size limit constants for security
const (
MaxQuerySize = 100 * 1024 // 100KB max query size
MaxVariableSize = 10 * 1024 // 10KB max variables size
MaxResponseSize = 10 * 1024 * 1024 // 10MB max response size
)
// minifyGraphQLQuery removes unnecessary whitespace from GraphQL queries while preserving string literals // minifyGraphQLQuery removes unnecessary whitespace from GraphQL queries while preserving string literals
func minifyGraphQLQuery(query string) string { func minifyGraphQLQuery(query string) string {
if query == "" { if query == "" {
@@ -238,7 +245,10 @@ func (b *BaseClient) compileQuery(queryPartials ...any) *Query {
// Apply query minification if enabled (default: true) // Apply query minification if enabled (default: true)
finalQuery := query finalQuery := query
if b.minify_queries { b.mu.RLock()
minifyEnabled := b.minify_queries
b.mu.RUnlock()
if minifyEnabled {
originalSize := len(query) originalSize := len(query)
minifiedQuery := minifyGraphQLQuery(query) minifiedQuery := minifyGraphQLQuery(query)
minifiedSize := len(minifiedQuery) minifiedSize := len(minifiedQuery)
@@ -268,9 +278,36 @@ func (b *BaseClient) compileQuery(queryPartials ...any) *Query {
} }
func (b *BaseClient) Query(query string, variables map[string]interface{}, headers map[string]interface{}) (any, error) { func (b *BaseClient) Query(query string, variables map[string]interface{}, headers map[string]interface{}) (any, error) {
// Validate query size limit
if len(query) > MaxQuerySize {
b.Logger.Error(&libpack_logger.LogMessage{
Message: "Query exceeds maximum size limit",
Pairs: map[string]interface{}{
"query_size": len(query),
"max_size": MaxQuerySize,
},
})
return nil, fmt.Errorf("query size %d exceeds maximum allowed size of %d bytes", len(query), MaxQuerySize)
}
// Process flags before compilation to avoid recompilation // Process flags before compilation to avoid recompilation
enableCache, enableRetries, cleanedVariables := processFlags(variables, headers) enableCache, enableRetries, cleanedVariables := processFlags(variables, headers)
// Validate variables size limit
if cleanedVariables != nil {
variablesJSON, err := json.Marshal(cleanedVariables)
if err == nil && len(variablesJSON) > MaxVariableSize {
b.Logger.Error(&libpack_logger.LogMessage{
Message: "Variables exceed maximum size limit",
Pairs: map[string]interface{}{
"variables_size": len(variablesJSON),
"max_size": MaxVariableSize,
},
})
return nil, fmt.Errorf("variables size %d exceeds maximum allowed size of %d bytes", len(variablesJSON), MaxVariableSize)
}
}
// Compile query once with cleaned variables // Compile query once with cleaned variables
compiledQuery := b.compileQuery(query, cleanedVariables) compiledQuery := b.compileQuery(query, cleanedVariables)
if compiledQuery == nil || compiledQuery.JsonQuery == nil { if compiledQuery == nil || compiledQuery.JsonQuery == nil {
@@ -282,11 +319,14 @@ func (b *BaseClient) Query(query string, variables map[string]interface{}, heade
} }
b.Logger.Debug(&libpack_logger.LogMessage{ b.Logger.Debug(&libpack_logger.LogMessage{
Message: "Compiled query", Message: "Compiled query",
Pairs: map[string]interface{}{"query": compiledQuery}, Pairs: map[string]interface{}{"query": sanitizeForLogging(compiledQuery.Query)},
}) })
var queryHash string var queryHash string
if (enableCache || b.cache_global) && strutil.HasPrefix(compiledQuery.Query, "query") { b.mu.RLock()
cacheGlobal := b.cache_global
b.mu.RUnlock()
if (enableCache || cacheGlobal) && strutil.HasPrefix(compiledQuery.Query, "query") {
b.Logger.Debug(&libpack_logger.LogMessage{ b.Logger.Debug(&libpack_logger.LogMessage{
Message: "Cache enabled", Message: "Cache enabled",
Pairs: nil, Pairs: nil,
@@ -295,13 +335,13 @@ func (b *BaseClient) Query(query string, variables map[string]interface{}, heade
if cachedValue := b.cacheLookup(queryHash); cachedValue != nil { if cachedValue := b.cacheLookup(queryHash); cachedValue != nil {
b.Logger.Debug(&libpack_logger.LogMessage{ b.Logger.Debug(&libpack_logger.LogMessage{
Message: "Cache hit", Message: "Cache hit",
Pairs: map[string]interface{}{"query": compiledQuery}, Pairs: map[string]interface{}{"query_hash": queryHash},
}) })
return b.decodeResponse(cachedValue) return b.decodeResponse(cachedValue)
} }
b.Logger.Debug(&libpack_logger.LogMessage{ b.Logger.Debug(&libpack_logger.LogMessage{
Message: "Cache miss", Message: "Cache miss",
Pairs: map[string]interface{}{"query": compiledQuery}, Pairs: map[string]interface{}{"query_hash": queryHash},
}) })
} }
@@ -315,7 +355,11 @@ func (b *BaseClient) Query(query string, variables map[string]interface{}, heade
} }
return "no-cache" return "no-cache"
}(), }(),
Retries: enableRetries || b.retries_enable, Retries: func() bool {
b.mu.RLock()
defer b.mu.RUnlock()
return enableRetries || b.retries_enable
}(),
} }
rv, err := q.executeQuery() rv, err := q.executeQuery()
+17
View File
@@ -8,6 +8,7 @@ import (
) )
func TestMinifyGraphQLQuery(t *testing.T) { func TestMinifyGraphQLQuery(t *testing.T) {
t.Parallel()
assert := assertions.New(t) assert := assertions.New(t)
tests := []struct { tests := []struct {
@@ -131,6 +132,7 @@ func TestMinifyGraphQLQuery(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := minifyGraphQLQuery(tt.input) result := minifyGraphQLQuery(tt.input)
assert.Equal(tt.expected, result, "Minified query should match expected output") assert.Equal(tt.expected, result, "Minified query should match expected output")
@@ -150,6 +152,7 @@ func TestMinifyGraphQLQuery(t *testing.T) {
} }
func TestMinifyGraphQLQuerySizeReduction(t *testing.T) { func TestMinifyGraphQLQuerySizeReduction(t *testing.T) {
t.Parallel()
assert := assertions.New(t) assert := assertions.New(t)
// Test with a heavily formatted query // Test with a heavily formatted query
@@ -208,9 +211,11 @@ func TestMinifyGraphQLQuerySizeReduction(t *testing.T) {
} }
func TestBaseClientQueryMinification(t *testing.T) { func TestBaseClientQueryMinification(t *testing.T) {
t.Parallel()
assert := assertions.New(t) assert := assertions.New(t)
t.Run("should_minify_queries_by_default", func(t *testing.T) { t.Run("should_minify_queries_by_default", func(t *testing.T) {
t.Parallel()
client := CreateTestClient() client := CreateTestClient()
// Verify minification is enabled by default // Verify minification is enabled by default
@@ -233,6 +238,7 @@ func TestBaseClientQueryMinification(t *testing.T) {
}) })
t.Run("should_respect_minification_disabled", func(t *testing.T) { t.Run("should_respect_minification_disabled", func(t *testing.T) {
t.Parallel()
client := CreateTestClient() client := CreateTestClient()
client.SetQueryMinification(false) client.SetQueryMinification(false)
@@ -254,6 +260,7 @@ func TestBaseClientQueryMinification(t *testing.T) {
}) })
t.Run("should_handle_string_literals_correctly", func(t *testing.T) { t.Run("should_handle_string_literals_correctly", func(t *testing.T) {
t.Parallel()
client := CreateTestClient() client := CreateTestClient()
query := `mutation test($value: String!) { query := `mutation test($value: String!) {
@@ -278,6 +285,7 @@ func TestBaseClientQueryMinification(t *testing.T) {
}) })
t.Run("should_demonstrate_size_reduction_with_production_case", func(t *testing.T) { t.Run("should_demonstrate_size_reduction_with_production_case", func(t *testing.T) {
t.Parallel()
client := CreateTestClient() client := CreateTestClient()
// Use the production case from trailing garbage test // Use the production case from trailing garbage test
@@ -324,6 +332,7 @@ func TestBaseClientQueryMinification(t *testing.T) {
} }
func TestIsAlphaNumeric(t *testing.T) { func TestIsAlphaNumeric(t *testing.T) {
t.Parallel()
assert := assertions.New(t) assert := assertions.New(t)
tests := []struct { tests := []struct {
@@ -351,6 +360,7 @@ func TestIsAlphaNumeric(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(string(tt.char), func(t *testing.T) { t.Run(string(tt.char), func(t *testing.T) {
t.Parallel()
result := isAlphaNumeric(tt.char) result := isAlphaNumeric(tt.char)
assert.Equal(tt.expected, result, "isAlphaNumeric should correctly identify character type") assert.Equal(tt.expected, result, "isAlphaNumeric should correctly identify character type")
}) })
@@ -358,9 +368,11 @@ func TestIsAlphaNumeric(t *testing.T) {
} }
func TestQueryMinificationEnvironmentVariable(t *testing.T) { func TestQueryMinificationEnvironmentVariable(t *testing.T) {
t.Parallel()
assert := assertions.New(t) assert := assertions.New(t)
t.Run("should_respect_GRAPHQL_MINIFY_QUERIES_env_var", func(t *testing.T) { t.Run("should_respect_GRAPHQL_MINIFY_QUERIES_env_var", func(t *testing.T) {
t.Parallel()
// This test would require setting environment variables // This test would require setting environment variables
// For now, we'll test the default behavior // For now, we'll test the default behavior
client := NewConnection() client := NewConnection()
@@ -371,19 +383,23 @@ func TestQueryMinificationEnvironmentVariable(t *testing.T) {
} }
func TestQueryMinificationEdgeCases(t *testing.T) { func TestQueryMinificationEdgeCases(t *testing.T) {
t.Parallel()
assert := assertions.New(t) assert := assertions.New(t)
t.Run("should_handle_empty_query", func(t *testing.T) { t.Run("should_handle_empty_query", func(t *testing.T) {
t.Parallel()
result := minifyGraphQLQuery("") result := minifyGraphQLQuery("")
assert.Equal("", result, "Empty query should remain empty") assert.Equal("", result, "Empty query should remain empty")
}) })
t.Run("should_handle_query_with_only_whitespace", func(t *testing.T) { t.Run("should_handle_query_with_only_whitespace", func(t *testing.T) {
t.Parallel()
result := minifyGraphQLQuery(" \n\t \n ") result := minifyGraphQLQuery(" \n\t \n ")
assert.Equal("", result, "Query with only whitespace should become empty") assert.Equal("", result, "Query with only whitespace should become empty")
}) })
t.Run("should_handle_nested_quotes", func(t *testing.T) { t.Run("should_handle_nested_quotes", func(t *testing.T) {
t.Parallel()
query := `mutation test { query := `mutation test {
insert_test(value: "String with \"nested quotes\" and 'mixed' quotes") { insert_test(value: "String with \"nested quotes\" and 'mixed' quotes") {
id id
@@ -395,6 +411,7 @@ func TestQueryMinificationEdgeCases(t *testing.T) {
}) })
t.Run("should_preserve_spaces_between_keywords", func(t *testing.T) { t.Run("should_preserve_spaces_between_keywords", func(t *testing.T) {
t.Parallel()
query := `query test($var: String!) { query := `query test($var: String!) {
field(where: {id: {_eq: $var}}) { field(where: {id: {_eq: $var}}) {
id id
+2 -1
View File
@@ -71,7 +71,8 @@ func (suite *RequestCompressionTestSuite) TestRequestCompression() {
client.endpoint = endpoint client.endpoint = endpoint
// Create HTTP client using the same logic as the library // Create HTTP client using the same logic as the library
httpClient := client.createHttpClient() httpClient, err := client.createHttpClient()
assert.NoError(err, "HTTP client should be created without error")
assert.NotNil(httpClient, "HTTP client should be created") assert.NotNil(httpClient, "HTTP client should be created")
// Verify transport settings // Verify transport settings
+63
View File
@@ -0,0 +1,63 @@
package gql
import (
"regexp"
"strings"
)
// Sensitive data patterns to redact from logs
var (
// Pattern for common sensitive field names in JSON/GraphQL
sensitiveFieldPattern = regexp.MustCompile(`(?i)"?(password|passwd|pwd|token|apikey|api_key|api-key|secret|authorization|auth|bearer|credentials|private_key|private-key|access_token|refresh_token|client_secret|session|cookie)"?\s*:\s*"[^"]*"`)
// Pattern for authorization headers
authHeaderPattern = regexp.MustCompile(`(?i)(authorization|x-api-key|x-auth-token|bearer)\s*:\s*[^\s,}]+`)
// Pattern for basic auth in URLs
basicAuthURLPattern = regexp.MustCompile(`(https?://)([^:]+):([^@]+)@`)
// Pattern for JWT tokens
jwtPattern = regexp.MustCompile(`eyJ[A-Za-z0-9_-]+\.eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+`)
)
const redactedText = "[REDACTED]"
// sanitizeForLogging removes sensitive data from strings before logging
// This includes passwords, tokens, API keys, authorization headers, etc.
func sanitizeForLogging(input string) string {
if input == "" {
return input
}
// Create a working copy
sanitized := input
// Redact sensitive fields in JSON/GraphQL
sanitized = sensitiveFieldPattern.ReplaceAllStringFunc(sanitized, func(match string) string {
// Preserve the field name but redact the value
parts := strings.SplitN(match, ":", 2)
if len(parts) == 2 {
fieldName := parts[0]
return fieldName + `: "` + redactedText + `"`
}
return redactedText
})
// Redact authorization headers
sanitized = authHeaderPattern.ReplaceAllStringFunc(sanitized, func(match string) string {
parts := strings.SplitN(match, ":", 2)
if len(parts) == 2 {
headerName := parts[0]
return headerName + ": " + redactedText
}
return redactedText
})
// Redact basic auth in URLs
sanitized = basicAuthURLPattern.ReplaceAllString(sanitized, "${1}"+redactedText+":"+redactedText+"@")
// Redact JWT tokens
sanitized = jwtPattern.ReplaceAllString(sanitized, redactedText)
return sanitized
}
+2
View File
@@ -2,6 +2,7 @@ package gql
import ( import (
"net/http" "net/http"
"sync"
"time" "time"
cache "github.com/lukaszraczylo/go-simple-graphql/cache" cache "github.com/lukaszraczylo/go-simple-graphql/cache"
@@ -9,6 +10,7 @@ import (
) )
type BaseClient struct { type BaseClient struct {
mu sync.RWMutex // Protects all fields for thread-safe access
cache *cache.Cache cache *cache.Cache
Logger *logging.Logger Logger *logging.Logger
client *http.Client client *http.Client
+18 -2
View File
@@ -2,6 +2,7 @@ package gql
import ( import (
"net/http/httptest" "net/http/httptest"
"os"
"testing" "testing"
assertions "github.com/stretchr/testify/assert" assertions "github.com/stretchr/testify/assert"
@@ -13,11 +14,19 @@ type Tests struct {
} }
var ( var (
assert *assertions.Assertions assert *assertions.Assertions
mockServer *httptest.Server mockServer *httptest.Server
originalInsecureSkipValue string
) )
func (suite *Tests) SetupSuite() { func (suite *Tests) SetupSuite() {
// Save original environment variable value
originalInsecureSkipValue = os.Getenv("GRAPHQL_INSECURE_SKIP_VERIFY")
// Set environment variable to skip TLS verification for test mock server
// The mock server uses self-signed certificates
os.Setenv("GRAPHQL_INSECURE_SKIP_VERIFY", "true")
// Start mock GraphQL server // Start mock GraphQL server
mockServer = StartMockServer() mockServer = StartMockServer()
} }
@@ -30,6 +39,13 @@ func (suite *Tests) TearDownSuite() {
// Cleanup shared test cache // Cleanup shared test cache
CleanupTestCache() CleanupTestCache()
// Restore original environment variable value
if originalInsecureSkipValue != "" {
os.Setenv("GRAPHQL_INSECURE_SKIP_VERIFY", originalInsecureSkipValue)
} else {
os.Unsetenv("GRAPHQL_INSECURE_SKIP_VERIFY")
}
} }
func (suite *Tests) SetupTest() { func (suite *Tests) SetupTest() {