siyuan-note/siyuan · error

history path [ ] is not in workspace

Error message

history path [%s] is not in workspace

What it means

validateHistoryPath normalizes the caller-supplied history path against the workspace and rejects any path that escapes it, blocking path traversal (.., absolute paths outside the workspace). This error means the given historyPath, after joining with WorkspaceDir, is not a subpath of the workspace.

Solutions

  1. Pass a path relative to the workspace that stays under the history directory (e.g. history/<op>-<timestamp>/...)
  2. Strip or resolve .. segments and verify the path is inside util.WorkspaceDir before calling the API
  3. Use history listing APIs to obtain valid history paths instead of constructing them manually

Example fix

// before
rollbackDocHistory("/tmp/other-workspace/history/...")
// after
rollbackDocHistory("history/20240101000000-delete-<docID>/<boxID>/<docID>.sy")
Defensive patterns

Strategy: validation

Validate before calling

const abs = path.resolve(workspaceDir, p);
if (!abs.startsWith(path.resolve(workspaceDir) + path.sep)) throw new Error('path escapes workspace');

Try / catch

try { await rollbackDocHistory(p); } catch (e) { if (String(e).includes('not in workspace')) { p = path.relative(workspaceDir, p); retry(); } else { throw e; } }

Prevention

When it happens

Trigger: Calling GetDocHistoryContent, RollbackDocHistory, RollbackAssetsHistory, RollbackNotebookHistory, RollbackAttributeViewHistory, or ResolveDocVersionBoxID with an absolute path outside the workspace or a relative path containing ../ segments.

Common situations: Hard-coding history paths from another workspace or machine; building paths by string concatenation that include user input; symlinks or .. segments introduced by external tooling.

Understand the failure class

Background: Path traversal blocked: "path escapes the workspace" and "outside site root" errors when a path will not stay inside its allowed directory — this error's family across 26 libraries.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/0f9109f4f57de5ea. Report an issue: GitHub.

Appendix: source

Thrown at kernel/model/history.go:606

			return
		}
	}

	if err = filelock.CopyNewtimes(from, to); err != nil {
		logging.LogErrorf("copy file [%s] to [%s] failed: %s", from, to, err)
		return
	}
	IncSync()
	util.PushMsg(Conf.Language(102), 3000)
	return nil
}

// validateHistoryPath 校验历史路径是否位于工作区内且属于历史目录。
// 拒绝路径穿越攻击(..、绝对路径等)。返回规范化的绝对路径。
func validateHistoryPath(historyPath string) (string, error) {
	p := filepath.Join(util.WorkspaceDir, historyPath)
	if !gulu.File.IsSubPath(util.WorkspaceDir, p) {
		return "", fmt.Errorf("history path [%s] is not in workspace", historyPath)
	}
	if !gulu.File.IsExist(p) {
		return "", fmt.Errorf("history path [%s] not exist", historyPath)
	}
	rel, err := filepath.Rel(util.HistoryDir, p)
	if err != nil || strings.HasPrefix(rel, "..") {
		return "", fmt.Errorf("history path [%s] is not under history directory", historyPath)
	}
	return p, nil
}

// IsEncryptedHistoryPath 判断历史路径是否明确属于加密笔记本。
func IsEncryptedHistoryPath(absPath string) bool {
	boxID := ExtractBoxIDFromHistoryPath(absPath)
	if boxID == "" {
		return false
	}
	if IsEncryptedBox(boxID) {

View on GitHub (pinned to 9f775e8a12)