CRITICAL missing tlsginbasic auth

Missing Tls in Gin with Basic Auth

Missing Tls in Gin with Basic Auth — how this specific combination creates or exposes the vulnerability

Transport Layer Security (TLS) ensures confidentiality and integrity between clients and servers. When a Gin service uses HTTP Basic Authentication without TLS, credentials are transmitted in an easily reversible format. Basic Auth encodes the username and password with Base64, which provides no encryption. An attacker performing network-level interception can decode the credentials trivially.

In a Gin application, if routes that accept Basic Auth are served over plain HTTP, credentials can be captured through various methods such as unencrypted wireless networks, compromised routers, or malicious proxies. Because the Authorization header is static per request, captured traffic can be reused (replay attacks) to impersonate a legitimate user. MiddleBrick’s unauthenticated scan checks for the presence of TLS and flags endpoints that transmit credentials without encryption as part of its Data Exposure and Encryption checks.

The combination is particularly dangerous because Basic Auth is often used in scenarios where simplicity is prioritized over security, such as internal tooling or legacy integrations. Without TLS, there is no protection against man-in-the-middle (MITM) attacks at any point in the network path. Even if the Gin server is not directly exposed to the internet, lateral movement within a network can expose these unprotected endpoints. Scanning with a tool like middleBrick helps identify endpoints that lack TLS while using Basic Auth, providing a clear remediation path before credentials are exposed in production.

Basic Auth-Specific Remediation in Gin — concrete code fixes

To secure Basic Auth in Gin, you must enforce HTTPS so that credentials are never transmitted in cleartext. Below is a minimal, secure Gin example that uses TLS and Basic Auth together. It includes a self-signed certificate for local testing and a production-ready structure for loading certificates from files.

//go
package main

import (
	"fmt"
	"net/http"

	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()

	r.GET("/secure", func(c *gin.Context) {
		user, pass, ok := c.Request.BasicAuth()
		if !ok || !validateCredentials(user, pass) {
			c.Header("WWW-Authenticate", `Basic realm="restricted", charset="UTF-8"`)
			c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "invalid credentials"})
			return
		}
		c.JSON(http.StatusOK, gin.H{"message": "authenticated"})
	})

	// Use TLS configuration with proper certificates
	server := &http.Server{
		Addr:      ":8443",
		Handler:   r,
		TLSConfig: nil, // configure as needed for production
	}

	// In production, provide full paths to certificate and key files
	// certPath := "/etc/tls/server.crt"
	// keyPath := "/etc/tls/server.key"
	// err := server.ListenAndServeTLS(certPath, keyPath)

	// For local testing with a self-signed certificate:
	// generate with: openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes
	fmt.Println("Starting secure server on https://localhost:8443")
	err := server.ListenAndServeTLS("cert.pem", "key.pem")
	if err != nil {
		panic(err)
	}
}

func validateCredentials(user, pass string) bool {
	// Replace with secure credential validation, e.g., constant-time comparison
	return user == "admin" && pass == "S3cur3P@ss!"
}

Key points in this setup:

  • Always use ListenAndServeTLS with valid certificate and key paths.
  • Set the WWW-Authenticate header when credentials are missing or invalid to guide clients properly.
  • Avoid hardcoding credentials in production; use environment variables or a secure vault and rotate them regularly.
  • Consider migrating away from Basic Auth when possible in favor of token-based mechanisms (e.g., OAuth2, JWT over HTTPS) to reduce long-term risk.

middleBrick’s OpenAPI/Swagger analysis can detect whether an endpoint defines security schemes like Basic Auth and whether the served schema references HTTPS. By correlating spec definitions with runtime behavior, the scanner highlights mismatches such as an HTTPS-only spec but HTTP test results, which indicate deployment configuration errors.

Related CWEs: encryption

CWE IDNameSeverity
CWE-319Cleartext Transmission of Sensitive Information HIGH
CWE-295Improper Certificate Validation HIGH
CWE-326Inadequate Encryption Strength HIGH
CWE-327Use of a Broken or Risky Cryptographic Algorithm HIGH
CWE-328Use of Weak Hash HIGH
CWE-330Use of Insufficiently Random Values HIGH
CWE-338Use of Cryptographically Weak PRNG MEDIUM
CWE-693Protection Mechanism Failure MEDIUM
CWE-757Selection of Less-Secure Algorithm During Negotiation HIGH
CWE-261Weak Encoding for Password HIGH

Frequently Asked Questions

Can middleBrick detect missing TLS when Basic Auth is used?
Yes. middleBrick scans unauthenticated attack surfaces and flags endpoints that transmit credentials without encryption by correlating OpenAPI/Swagger security definitions with runtime observations.
Is migrating from Basic Auth to token-based authentication recommended?
Yes. While enforcing HTTPS with Basic Auth reduces risk, moving to token-based flows (e.g., OAuth2, JWT over HTTPS) provides stronger identity management and reduces long-term exposure when used with secure storage and rotation practices.