Cookie exposure without HttpOnly

Script access to sensitive cookies without HttpOnly

Description

Without HttpOnly, JavaScript running on the page can read a session ID or authentication token through document.cookie.

Potential impact

If XSS exposes a valid session cookie, someone else could view data, change settings or make payments with the user’s permissions. An administrator’s session gives broader access.

Remediation

  • Default to HttpOnly: true and Secure: true in the shared helpers that create, renew and delete session or authentication cookies. Serve them over HTTPS.
  • Fix XSS as well. Even with HttpOnly, a script on the page can send requests authenticated by the browser.

Examples

For a working login, implement user authentication and a server-side store that links each session ID to its user. Supply a certificate and key for the HTTPS example.

Before

go
package main

import (
    "crypto/rand"
    "encoding/base64"
    "log"
    "net/http"
    "time"
)

func newSessionID() (string, error) {
    token := make([]byte, 32)
    if _, err := rand.Read(token); err != nil {
        return "", err
    }
    return base64.RawURLEncoding.EncodeToString(token), nil
}

// Before: session cookie without HttpOnly
func setSessionCookie(w http.ResponseWriter, sessionID string) {
    cookie := http.Cookie{
        Name:    "session_id",
        Value:   sessionID,
        Path:    "/",
        // HttpOnly and Secure are missing
        Expires: time.Now().Add(30 * time.Minute),
    }
    http.SetCookie(w, &cookie)
}

func handler(w http.ResponseWriter, r *http.Request) {
    sessionID, err := newSessionID()
    if err != nil {
        http.Error(w, "session creation failed", http.StatusInternalServerError)
        return
    }
    setSessionCookie(w, sessionID)
    w.Write([]byte("ok"))
}

func main() {
    http.HandleFunc("/login", handler)
    log.Fatal(http.ListenAndServe(":8080", nil))
}

After

go
package main

import (
    "crypto/rand"
    "encoding/base64"
    "log"
    "net/http"
    "time"
)

func newSessionID() (string, error) {
    token := make([]byte, 32)
    if _, err := rand.Read(token); err != nil {
        return "", err
    }
    return base64.RawURLEncoding.EncodeToString(token), nil
}

// After: set HttpOnly and Secure in a shared helper
func setSecureSessionCookie(w http.ResponseWriter, sessionID string) {
    cookie := http.Cookie{
        Name:     "session_id",
        Value:    sessionID,
        Path:     "/",
        Expires:  time.Now().Add(30 * time.Minute),
        HttpOnly: true,          // Not readable by JS
        Secure:   true,          // Send only over HTTPS
        SameSite: http.SameSiteLaxMode,
    }
    http.SetCookie(w, &cookie)
}

func handler(w http.ResponseWriter, r *http.Request) {
    sessionID, err := newSessionID()
    if err != nil {
        http.Error(w, "session creation failed", http.StatusInternalServerError)
        return
    }
    // Store the association between the authenticated user and sessionID in server-side session storage.
    setSecureSessionCookie(w, sessionID)
    w.Write([]byte("ok"))
}

func main() {
    http.HandleFunc("/login", handler)
    log.Fatal(http.ListenAndServeTLS(":8443", "server.crt", "server.key", nil))
}

Both examples generate a random session ID. The revised helper sets HttpOnly, Secure and SameSite, and ListenAndServeTLS handles HTTPS requests.

References