Information exposure through directory listings

File and information exposure through directory listings

Description

A directory listing displays the files in a requested folder. Go's http.FileServer may list a directory when it has no index.html. Exposing it without separate access controls can reveal directory structure and filenames. Attackers may discover configuration files, backups, logs or scripts and use them for account compromise, code analysis or further vulnerability discovery.

Potential impact

  • Directory structure, filenames and backup or configuration information may become public.
  • Hiding links is not access control. If a discovered file is readable, its contents may also be accessible.
  • Exposed source code or configuration may help attackers find other weaknesses, such as SQL injection or authentication bypasses.

Remediation

  • Point http.FileServer at a dedicated public directory under a limited URL path. Before release, check it for configuration files, .env, .git, backups, logs, and symlinks that lead outside it. Restrict who can add or change files and links there.
  • Disable directory listings on the server or proxy that actually serves the files. A readable file may still be requested directly.
  • For protected files, authorize the user before calling ServeContent or ServeFile.

Examples

The revised handler assumes that ./public contains only files intended for everyone and no links to outside content.

Before

go
package main

import (
    "log"
    "net/http"
)

// Before: expose the project root directory directly
func main() {
    // Expose the current directory (".") through FileServer
    fs := http.FileServer(http.Dir("."))

    // Accessing http://localhost:8080/ on the server
    // May reveal the directory listing and files inside
    http.Handle("/", fs)

    log.Fatal(http.ListenAndServe(":8080", nil))
}

After

go
package main

import (
    "log"
    "net/http"
    "os"
    "path/filepath"
)

// After: 
// 1) Serve only a dedicated static directory
// 2) Filter sensitive filenames before serving

// Directory containing only public static files
const staticDir = "./public"

// Illustrative filter for some sensitive names and extensions
func isForbidden(path string) bool {
    base := filepath.Base(path)
    if base == ".env" || base == "config.yaml" || base == "config.yml" {
        return true
    }
    ext := filepath.Ext(path)
    switch ext {
    case ".log", ".bak", ".old", ".zip":
        return true
    }
    return false
}

func secureStaticHandler(w http.ResponseWriter, r *http.Request) {
    // Calculate the static file path
    cleanPath := filepath.Clean(r.URL.Path)
    fullPath := filepath.Join(staticDir, cleanPath)

    if isForbidden(fullPath) {
        http.NotFound(w, r)
        return
    }

    // Check whether the file exists
    info, err := os.Stat(fullPath)
    if err != nil || info.IsDir() {
        // Reject directories instead of listing them
        http.NotFound(w, r)
        return
    }

    http.ServeFile(w, r, fullPath)
}

func main() {
    // Serve static files only under /static
    http.HandleFunc("/static/", func(w http.ResponseWriter, r *http.Request) {
        // Remove the /static prefix and call the internal handler
        r.URL.Path = r.URL.Path[len("/static"):]
        secureStaticHandler(w, r)
    })

    // Use the regular handler for other paths
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("<h1>Home</h1>"))
    })

    log.Fatal(http.ListenAndServe(":8080", nil))
}

The handler serves files under /static/ and returns 404 for directory requests. isForbidden filters only a few names and extensions. Control the directory’s contents and write permissions at deployment.

References