Cleartext transmission of sensitive information

Cleartext transmission of sensitive information

Description

On any HTTP segment without TLS, a network observer can read or change requests and responses.

Potential impact

Exposed passwords or session cookies can allow account impersonation. Altered payment data or responses can also compromise transactions or breach applicable privacy or security requirements.

Remediation

  • If Go terminates external TLS, use http.ListenAndServeTLS. If a trusted reverse proxy terminates it, protect both the external HTTPS connection and the path to the application. Use a certificate the client trusts and configure trust separately for internal or development self-signed certificates.
  • Send sensitive requests over HTTPS from the start and send only the data needed. Set Secure on sensitive cookies and Strict-Transport-Security on HTTPS responses. A redirect cannot protect an HTTP request already sent before the browser learned the policy.

Examples

Before

go
package main

import (
    "log"
    "net/http"
)

func loginHandler(w http.ResponseWriter, r *http.Request) {
    // Simplified: real authentication must verify the password
    username := r.FormValue("username")
    password := r.FormValue("password")

    // Plain HTTP can expose username and password in transit
    log.Printf("login attempt: %s / %s", username, password)
    w.Write([]byte("ok"))
}

func main() {
    http.HandleFunc("/login", loginHandler)

    // Before: serve HTTP without TLS
    // Requests and responses travel in plaintext
    if err := http.ListenAndServe(":80", nil); err != nil {
        log.Fatal(err)
    }
}

After

go
package main

import (
    "log"
    "net/http"
    "net/url"
)

func loginHandler(w http.ResponseWriter, r *http.Request) {
    // Real services require additional authentication and validation
    username := r.FormValue("username")
    log.Printf("login attempt for username=%q", username)
    w.Write([]byte("ok"))
}

func main() {
    http.HandleFunc("/login", loginHandler)

    // Use port 80 only to redirect to HTTPS
    go func() {
        if err := http.ListenAndServe(":80", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
            target := (&url.URL{
                Scheme:   "https",
                Host:     "www.example.com", // Fixed HTTPS host from deployment configuration
                Path:     r.URL.Path,
                RawQuery: r.URL.RawQuery,
            }).String()
            http.Redirect(w, r, target, http.StatusMovedPermanently)
        })); err != nil {
            log.Fatal(err)
        }
    }()

    // After: TLS with the actual certificate and key file paths
    if err := http.ListenAndServeTLS(":443", "cert.pem", "key.pem", nil); err != nil {
        log.Fatal(err)
    }
}

Port 80 redirects to a fixed deployment host. Port 443 serves HTTPS using cert.pem and key.pem. The handler logs the username with %q and omits the password. A real login must also verify credentials.

References