verbesserungen
This commit is contained in:
@@ -180,6 +180,7 @@ func readFile(absPath, displayPath string) string {
|
||||
}
|
||||
|
||||
func writeFile(absPath, displayPath, content string) string {
|
||||
content = cleanContent(content)
|
||||
if err := os.MkdirAll(filepath.Dir(absPath), 0755); err != nil {
|
||||
return fmt.Sprintf("WRITE_FILE ERROR: Verzeichnis anlegen fehlgeschlagen: %v", err)
|
||||
}
|
||||
@@ -213,12 +214,22 @@ func listFiles(absPath, displayPath string) string {
|
||||
// sanitizePath stellt sicher dass der Pfad innerhalb des workDir bleibt.
|
||||
// Verhindert Directory Traversal wie ../../etc/passwd
|
||||
func sanitizePath(workDir, relPath string) (string, error) {
|
||||
// Absoluten Zielpfad berechnen
|
||||
abs := filepath.Join(workDir, relPath)
|
||||
abs = filepath.Clean(abs)
|
||||
// Wenn LLM einen absoluten Pfad schickt → relativen Teil extrahieren
|
||||
if filepath.IsAbs(relPath) {
|
||||
workDirClean := filepath.Clean(workDir)
|
||||
// Prüfen ob der absolute Pfad innerhalb des workDir liegt
|
||||
if strings.HasPrefix(relPath, workDirClean) {
|
||||
// Absoluten Pfad direkt nutzen, kein Join nötig
|
||||
return filepath.Clean(relPath), nil
|
||||
}
|
||||
// Absoluter Pfad außerhalb workDir → nur Dateiname nehmen
|
||||
relPath = filepath.Base(relPath)
|
||||
}
|
||||
|
||||
// Muss mit workDir beginnen
|
||||
// Normaler Fall: relativer Pfad
|
||||
abs := filepath.Clean(filepath.Join(workDir, relPath))
|
||||
workDirClean := filepath.Clean(workDir)
|
||||
|
||||
if !strings.HasPrefix(abs, workDirClean+string(filepath.Separator)) &&
|
||||
abs != workDirClean {
|
||||
return "", fmt.Errorf("Pfad außerhalb des Arbeitsverzeichnisses")
|
||||
@@ -226,3 +237,21 @@ func sanitizePath(workDir, relPath string) (string, error) {
|
||||
|
||||
return abs, nil
|
||||
}
|
||||
|
||||
func cleanContent(content string) string {
|
||||
// Escaped Quotes normalisieren
|
||||
content = strings.ReplaceAll(content, `\"`, `"`)
|
||||
content = strings.ReplaceAll(content, `\\n`, "\n")
|
||||
content = strings.ReplaceAll(content, `\\t`, "\t")
|
||||
|
||||
// Markdown Codeblöcke entfernen
|
||||
lines := strings.Split(content, "\n")
|
||||
var cleaned []string
|
||||
for _, line := range lines {
|
||||
if strings.HasPrefix(strings.TrimSpace(line), "```") {
|
||||
continue
|
||||
}
|
||||
cleaned = append(cleaned, line)
|
||||
}
|
||||
return strings.TrimSpace(strings.Join(cleaned, "\n"))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user