MCPcopy
hub / github.com/wavetermdev/waveterm / validateAndResolveFilePath

Function validateAndResolveFilePath

pkg/waveappstore/waveappstore.go:235–261  ·  view source on GitHub ↗
(appDir string, fileName string)

Source from the content-addressed store, hash-verified

233}
234
235func validateAndResolveFilePath(appDir string, fileName string) (string, error) {
236 if filepath.IsAbs(fileName) {
237 return "", fmt.Errorf("fileName must be relative, got absolute path: %s", fileName)
238 }
239
240 cleanPath := filepath.Clean(fileName)
241 if strings.HasPrefix(cleanPath, "..") || strings.Contains(cleanPath, string(filepath.Separator)+"..") {
242 return "", fmt.Errorf("path traversal not allowed: %s", fileName)
243 }
244
245 fullPath := filepath.Join(appDir, cleanPath)
246 resolvedPath, err := filepath.Abs(fullPath)
247 if err != nil {
248 return "", fmt.Errorf("failed to resolve path: %w", err)
249 }
250
251 resolvedAppDir, err := filepath.Abs(appDir)
252 if err != nil {
253 return "", fmt.Errorf("failed to resolve app directory: %w", err)
254 }
255
256 if !strings.HasPrefix(resolvedPath, resolvedAppDir+string(filepath.Separator)) && resolvedPath != resolvedAppDir {
257 return "", fmt.Errorf("path escapes app directory: %s", fileName)
258 }
259
260 return resolvedPath, nil
261}
262
263func WriteAppFile(appId string, fileName string, contents []byte) error {
264 if err := ValidateAppId(appId); err != nil {

Callers 7

WriteAppFileFunction · 0.85
ReadAppFileFunction · 0.85
DeleteAppFileFunction · 0.85
ReplaceInAppFileFunction · 0.85
ReplaceInAppFilePartialFunction · 0.85
RenameAppFileFunction · 0.85
FormatGoFileFunction · 0.85

Calls 1

CleanMethod · 0.45

Tested by

no test coverage detected