Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix - faro/receiver - uncontrolled data used in path expression #3056

Closed
wants to merge 1 commit into from

Conversation

t00mas
Copy link
Contributor

@t00mas t00mas commented Mar 21, 2025

PR Description

Which issue(s) this PR fixes

Notes to the Reviewer

PR Checklist

  • CHANGELOG.md updated
  • Documentation added
  • Tests updated
  • Config converters updated

if err != nil {
return nil, err
}
return os.Stat(securedPath)

Check failure

Code scanning / CodeQL

Uncontrolled data used in path expression High

This path depends on a
user-provided value
.

Copilot Autofix AI 10 days ago

To fix the problem, we need to enhance the validation logic in the securePath function to ensure that the constructed path does not contain any path traversal sequences and is within the intended directory. Specifically, we should:

  1. Ensure that the name parameter does not contain any path separators or parent directory references.
  2. Validate that the final resolved path is within the basePath directory.
Suggested changeset 1
internal/component/faro/receiver/sourcemaps.go

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/internal/component/faro/receiver/sourcemaps.go b/internal/component/faro/receiver/sourcemaps.go
--- a/internal/component/faro/receiver/sourcemaps.go
+++ b/internal/component/faro/receiver/sourcemaps.go
@@ -81,2 +81,8 @@
 	}
+
+	// Ensure the name does not contain any path separators or parent directory references
+	if strings.Contains(name, "/") || strings.Contains(name, "\\") || strings.Contains(name, "..") {
+		return "", fmt.Errorf("invalid file name: %s", name)
+	}
+
 	cleanedPath := filepath.Clean(name)
EOF
@@ -81,2 +81,8 @@
}

// Ensure the name does not contain any path separators or parent directory references
if strings.Contains(name, "/") || strings.Contains(name, "\\") || strings.Contains(name, "..") {
return "", fmt.Errorf("invalid file name: %s", name)
}

cleanedPath := filepath.Clean(name)
Copilot is powered by AI and may make mistakes. Always verify output.
if err != nil {
return nil, err
}
return os.ReadFile(securedPath)

Check failure

Code scanning / CodeQL

Uncontrolled data used in path expression High

This path depends on a
user-provided value
.

Copilot Autofix AI 10 days ago

To fix the problem, we need to ensure that the paths derived from user input are properly sanitized and validated. Specifically, we should:

  1. Ensure that the securePath method always checks that the resolved path is within the basePath, even if the basePath is empty.
  2. Improve the cleanFilePathPart function to remove any remaining potentially dangerous characters.
  3. Add additional validation to ensure that the release string does not contain any path traversal sequences.
Suggested changeset 1
internal/component/faro/receiver/sourcemaps.go

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/internal/component/faro/receiver/sourcemaps.go b/internal/component/faro/receiver/sourcemaps.go
--- a/internal/component/faro/receiver/sourcemaps.go
+++ b/internal/component/faro/receiver/sourcemaps.go
@@ -84,3 +84,3 @@
 	if fs.basePath == "" {
-		return cleanedPath, nil
+		fs.basePath = "."
 	}
@@ -385,3 +385,7 @@
 func cleanFilePathPart(x string) string {
-	return strings.TrimLeft(strings.ReplaceAll(strings.ReplaceAll(x, "\\", ""), "/", ""), ".")
+	cleaned := strings.ReplaceAll(strings.ReplaceAll(x, "\\", ""), "/", "")
+	if strings.Contains(cleaned, "..") {
+		return ""
+	}
+	return strings.TrimLeft(cleaned, ".")
 }
EOF
@@ -84,3 +84,3 @@
if fs.basePath == "" {
return cleanedPath, nil
fs.basePath = "."
}
@@ -385,3 +385,7 @@
func cleanFilePathPart(x string) string {
return strings.TrimLeft(strings.ReplaceAll(strings.ReplaceAll(x, "\\", ""), "/", ""), ".")
cleaned := strings.ReplaceAll(strings.ReplaceAll(x, "\\", ""), "/", "")
if strings.Contains(cleaned, "..") {
return ""
}
return strings.TrimLeft(cleaned, ".")
}
Copilot is powered by AI and may make mistakes. Always verify output.
@t00mas t00mas closed this Mar 26, 2025
@t00mas t00mas deleted the t/faro/receiver/path-expression branch March 26, 2025 09:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant