logo

Database

Go Path Traversal Open File

Description

Detects potential path traversal vulnerabilities in Go code where os.OpenFile is called with user-controlled input. This could allow attackers to access arbitrary files outside the intended directory by manipulating file paths with "../" sequences or absolute paths.

Weakness:

270 - Insecure functionality - File Creation

Category: Functionality Abuse

Detection Strategy

    • Confirms the Go 'os' package is imported in the source code

    • Identifies calls to os.OpenFile function

    • Verifies that the file path parameter passed to os.OpenFile contains or is derived from user input

    • Reports a vulnerability when user-controlled data flows into the file path argument without proper sanitization

Vulnerable code example

package main

import (
    "net/http"
    "os"
)

func handleFile(w http.ResponseWriter, r *http.Request) {...

✅ Secure code example

package main

import (
    "net/http"
    "os"
    "path/filepath"
    "strings"
)...