CVE-2019-25073 (published 2022-12-27T22:15Z)

Improper path sanitization in github.com/goadesign/goa before v3.0.9, v2.0.10, or v1.4.3 allow remote attackers to read files outside of the intended directory.

CVSS v3 base score: 7.5, HIGH (CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:N/A:N)

Weakness information:
cwe_id: CWE-22
cwe_name: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')
description: The product uses external input to construct a pathname that is intended to identify a file or directory that is located underneath a restricted parent directory, but the product does not properly neutralize special elements within the pathname that can cause the pathname to resolve to a location that is outside of the restricted directory.
extended_description: 
            Many file operations are intended to take place within a restricted directory. By using special elements such as ".." and "/" separators, attackers can escape outside of the restricted location to access files or directories that are elsewhere on the system. One of the most common special elements is the "../" sequence, which in most modern operating systems is interpreted as the parent directory of the current location. This is referred to as relative path traversal. Path traversal also covers the use of absolute pathnames such as "/usr/local/bin" to access unexpected files. This is referred to as absolute path traversal.
         
         

Fix information:
repo_url: https://github.com/goadesign/goa
commit_details: repo_url: https://github.com/goadesign/goa
author: Christie Koehler
author_date: 2019-12-04 14:34:09-08:00
msg: v1: Prevent directory path traversal in FileHandler (#2388)

* Add check for attempted path traversal.

* Re-work check for attempted path traversal.

* Fix linting errors.
repository: repo_url: https://github.com/goadesign/goa
repo_name: goadesign/goa
description: 🌟 Goa: Elevate Go API development! 🚀 Streamlined design, automatic code generation, and seamless HTTP/gRPC support. ✨
repo_language: Go
owner: goadesign
file_changes: filename: service.go
change_type: ModificationType.MODIFY
diff: @@ -372,6 +372,10 @@ func (ctrl *Controller) FileHandler(path, filename string) Handler {
 		}
 	}
 	return func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
+		// prevent path traversal
+		if attemptsPathTraversal(req.URL.Path, path) {
+			return ErrNotFound(req.URL.Path)
+		}
 		fname := filename
 		if len(wc) > 0 {
 			if m, ok := ContextRequest(ctx).Params[wc]; ok {
@@ -415,6 +419,32 @@ func (ctrl *Controller) FileHandler(path, filename string) Handler {
 	}
 }
 
+func attemptsPathTraversal(req string, path string) bool {
+	if !strings.Contains(req, "..") {
+		return false
+	}
+
+	currentPathIdx := 0
+	if idx := strings.LastIndex(path, "/*"); idx > -1 && idx < len(path)-1 {
+		req = req[idx+1:]
+	}
+	for _, runeValue := range strings.FieldsFunc(req, isSlashRune) {
+		if runeValue == ".." {
+			currentPathIdx--
+			if currentPathIdx < 0 {
+				return true
+			}
+		} else {
+			currentPathIdx++
+		}
+	}
+	return false
+}
+
+func isSlashRune(r rune) bool {
+	return os.IsPathSeparator(uint8(r))
+}
+
 var replacer = strings.NewReplacer(
 	"&", "&amp;",
 	"<", "&lt;",

programming_language: Go