Skip to content

Commit d22dffc

Browse files
Fix path traversal vulnerability in schema compiler (Alert #457) (#8803)
1 parent 8d1f356 commit d22dffc

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

pkg/parser/schema_compiler.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"errors"
77
"fmt"
88
"os"
9+
"path/filepath"
910
"sort"
1011
"strings"
1112
"sync"
@@ -222,8 +223,11 @@ func validateWithSchemaAndLocation(frontmatter map[string]any, schemaJSON, conte
222223
var frontmatterContent string
223224
var frontmatterStart = 2 // Default: frontmatter starts at line 2
224225

226+
// Sanitize the path to prevent path traversal attacks
227+
cleanPath := filepath.Clean(filePath)
228+
225229
if filePath != "" {
226-
if content, readErr := os.ReadFile(filePath); readErr == nil {
230+
if content, readErr := os.ReadFile(cleanPath); readErr == nil {
227231
lines := strings.Split(string(content), "\n")
228232

229233
// Look for frontmatter section with improved detection
@@ -267,7 +271,8 @@ func validateWithSchemaAndLocation(frontmatter map[string]any, schemaJSON, conte
267271
// Create context lines around the adjusted line number in the full file
268272
var adjustedContextLines []string
269273
if filePath != "" {
270-
if content, readErr := os.ReadFile(filePath); readErr == nil {
274+
// Use the same sanitized path
275+
if content, readErr := os.ReadFile(cleanPath); readErr == nil {
271276
allLines := strings.Split(string(content), "\n")
272277
// Create context around the adjusted line (±3 lines)
273278
// The console formatter expects context to be centered around the error line

0 commit comments

Comments
 (0)