{"record":{"id":"ba18f766b1825196","repo":"siyuan-note/siyuan","slug":"history-path-s-is-not-under-history-directory","errorCode":null,"errorMessage":"history path [%s] is not under history directory","messagePattern":"history path \\[(.+?)\\] is not under history directory","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/history.go","lineNumber":613,"sourceCode":"\t}\n\tIncSync()\n\tutil.PushMsg(Conf.Language(102), 3000)\n\treturn nil\n}\n\n// validateHistoryPath 校验历史路径是否位于工作区内且属于历史目录。\n// 拒绝路径穿越攻击（..、绝对路径等）。返回规范化的绝对路径。\nfunc validateHistoryPath(historyPath string) (string, error) {\n\tp := filepath.Join(util.WorkspaceDir, historyPath)\n\tif !gulu.File.IsSubPath(util.WorkspaceDir, p) {\n\t\treturn \"\", fmt.Errorf(\"history path [%s] is not in workspace\", historyPath)\n\t}\n\tif !gulu.File.IsExist(p) {\n\t\treturn \"\", fmt.Errorf(\"history path [%s] not exist\", historyPath)\n\t}\n\trel, err := filepath.Rel(util.HistoryDir, p)\n\tif err != nil || strings.HasPrefix(rel, \"..\") {\n\t\treturn \"\", fmt.Errorf(\"history path [%s] is not under history directory\", historyPath)\n\t}\n\treturn p, nil\n}\n\n// IsEncryptedHistoryPath 判断历史路径是否明确属于加密笔记本。\nfunc IsEncryptedHistoryPath(absPath string) bool {\n\tboxID := ExtractBoxIDFromHistoryPath(absPath)\n\tif boxID == \"\" {\n\t\treturn false\n\t}\n\tif IsEncryptedBox(boxID) {\n\t\treturn true\n\t}\n\trel, err := filepath.Rel(util.HistoryDir, absPath)\n\tif err != nil {\n\t\treturn false\n\t}\n\tparts := strings.SplitN(filepath.ToSlash(rel), \"/\", 3)","sourceCodeStart":595,"sourceCodeEnd":631,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/history.go#L595-L631","documentation":"validateHistoryPath finally requires the normalized path to sit under util.HistoryDir. Even paths inside the workspace are rejected if the relative path from HistoryDir escapes it (starts with ..), preventing rollback of arbitrary workspace files through the history APIs.","triggerScenarios":"Calling a history API with a workspace-internal but non-history path, e.g. data/storage/... or data/notebooks/..., or a path like history/../data/<file>.","commonSituations":"Confusing data paths with history paths; attempting to restore a file directly from data/ via the history API; misconfigured tooling that prefixes history entries with extra directories.","solutions":["Ensure the path is rooted under the workspace's history directory (history/...)","Compute the path relative to util.HistoryDir and confirm it does not begin with '..' before calling","Use the history listing API to enumerate valid entries rather than pointing at arbitrary workspace files"],"exampleFix":"// before\nrollbackAssetsHistory(\"data/assets/foo.png\") // not under history/\n// after\nrollbackAssetsHistory(\"history/20240101000000-update/<boxID>/assets/foo.png\")","handlingStrategy":"validation","validationCode":"const rel = path.relative(historyDir, path.resolve(workspaceDir, p));\nif (rel.startsWith('..') || path.isAbsolute(rel)) throw new Error('path must be under history/');","typeGuard":null,"tryCatchPattern":"try { await rollbackAssetsHistory(p); } catch (e) { if (String(e).includes('not under history directory')) { p = path.join('history', p); retry(); } else { throw e; } }","preventionTips":["Root all history API paths at the history directory","Do not reuse the history rollback API for arbitrary workspace files","Assert path.prefix === 'history/' in callers before invoking rollback"],"tags":["history","validation","path-traversal","security"],"backgroundTag":"path-traversal-blocked","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}