OpenNHP/opennhp · error

fail to get resource absolute path

Error message

fail to get resource absolute path: %w

What it means

loadResource resolves the joined resource path to an absolute form via filepath.Abs before the path-traversal check. This error is returned when filepath.Abs(fullPath) fails (wrapped as 'fail to get resource absolute path: %w'). As with the base-dir variant, this only happens when os.Getwd fails.

Solutions

  1. Fix or restart the daemon so it runs with a valid working directory
  2. Set absolute baseDir at startup and avoid per-request filepath.Abs calls
  3. Configure a stable WorkingDirectory in systemd/Docker
  4. Inspect the wrapped getwd error to confirm root cause
Defensive patterns

Strategy: validation

Validate before calling

if strings.Contains(resourceID, "..") || filepath.IsAbs(resourceID) {
	return fmt.Errorf("resourceID must be a relative path segment")
}
if !filepath.IsAbs(baseDir) {
	return fmt.Errorf("baseDir must be an absolute path")
}

Try / catch

data, err := loadResource(resourceID)
if err != nil {
	if strings.Contains(err.Error(), "fail to get resource absolute path") {
		http.Error(w, "resource store unavailable", http.StatusInternalServerError)
		return
	}
	if strings.Contains(err.Error(), "path traversal") {
		http.Error(w, "invalid resource ID", http.StatusBadRequest)
		return
	}
	http.Error(w, "resource not found", http.StatusNotFound)
	return
}

Prevention

When it happens

Trigger: GetResource calls loadResource with a resourceID; the joined fullPath goes through filepath.Abs, which fails because the process cwd is invalid (deleted/unmounted).

Common situations: Same env conditions as error 118: removed working directory, chroot with stale cwd, container workdir unmounted; typically surfaces together with error 118 during the same request.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


AI-assisted analysis of OpenNHP/opennhp@6e04ca5ff0 (2026-09-07). Data as JSON: /api/errors/88950f222b4668e1. Report an issue: GitHub.

Appendix: source

Thrown at endpoints/server/kbs/resource/resource.go:168

		"iv":            base64.RawURLEncoding.EncodeToString(iv),
		"ciphertext":    base64.RawURLEncoding.EncodeToString(encryptedContent),
		"tag":           "",
	}

	c.JSON(http.StatusOK, response)
}

func loadResource(resourceID string) ([]byte, error) {
	absBaseDir, err := filepath.Abs(baseDir)
	if err != nil {
		return nil, fmt.Errorf("fail to get base directory absolute path: %w", err)
	}

	fullPath := filepath.Join(absBaseDir, resourceID)

	absFullPath, err := filepath.Abs(fullPath)
	if err != nil {
		return nil, fmt.Errorf("fail to get resource absolute path: %w", err)
	}

	// Check if the path is within the base directory to avoid path traversal attack.
	if !strings.HasPrefix(absFullPath, absBaseDir) {
		return nil, errors.New("invalid resource ID: potential path traversal attack")
	}

	if _, statErr := os.Stat(absFullPath); statErr != nil {
		if os.IsNotExist(statErr) {
			return nil, errors.New("resource not found")
		}
		return nil, fmt.Errorf("fail to check resource: %w", statErr)
	}

	data, err := os.ReadFile(absFullPath)
	if err != nil {
		return nil, fmt.Errorf("fail to read resource: %w", err)
	}

View on GitHub (pinned to 6e04ca5ff0)