{"record":{"id":"6f2821c8e352456f","repo":"siyuan-note/siyuan","slug":"invalid-export-path","errorCode":null,"errorMessage":"invalid export path","messagePattern":"invalid export path","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/encrypted_export.go","lineNumber":186,"sourceCode":"// normalExportTempName 避免普通导出使用加密导出的第一层归属目录，保留归档内部的原始名称。\nfunc normalExportTempName(name string) string {\n\tparts := strings.SplitN(filepath.ToSlash(filepath.Clean(name)), \"/\", 2)\n\tif ast.IsNodeIDPattern(parts[0]) {\n\t\tparts[0] = \"export-\" + parts[0]\n\t}\n\treturn filepath.FromSlash(strings.Join(parts, \"/\"))\n}\n\n// AcquireExportArtifactLease 为导出产物取得覆盖整个复制过程的生命周期租约。\nfunc AcquireExportArtifactLease(exportPath string) (lease *ExportArtifactLease, err error) {\n\tif after, ok := strings.CutPrefix(exportPath, \"/export/\"); ok {\n\t\tfileName, decodeErr := url.PathUnescape(after)\n\t\tif decodeErr != nil {\n\t\t\treturn nil, decodeErr\n\t\t}\n\t\tfileName = filepath.Clean(fileName)\n\t\tif fileName == \".\" || strings.HasPrefix(fileName, \"..\") || filepath.IsAbs(fileName) {\n\t\t\treturn nil, errors.New(\"invalid export path\")\n\t\t}\n\t\tif IsManagedEncryptedExportPath(fileName) {\n\t\t\tboxID, artifact, resolved := ResolveManagedEncryptedExport(fileName)\n\t\t\tif !resolved {\n\t\t\t\treturn nil, errors.New(\"managed export is unavailable\")\n\t\t\t}\n\t\t\tif err = AcquireEncryptedBoxOperation(boxID); err != nil {\n\t\t\t\treturn nil, err\n\t\t\t}\n\t\t\tHoldBoxReadLock(boxID)\n\t\t\trelease := true\n\t\t\tdefer func() {\n\t\t\t\tif release {\n\t\t\t\t\tReleaseBoxReadLock(boxID)\n\t\t\t\t\tReleaseEncryptedBoxOperation(boxID)\n\t\t\t\t}\n\t\t\t}()\n\t\t\t_, artifact, resolved = ResolveManagedEncryptedExport(fileName)","sourceCodeStart":168,"sourceCodeEnd":204,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/encrypted_export.go#L168-L204","documentation":"AcquireExportArtifactLease validates any path under the \"/export/\" prefix before issuing a mobile export artifact lease. After URL-decoding and filepath.Clean, if the remaining path is \".\", starts with \"..\", or is absolute, it cannot name a safe relative artifact inside the export temp directory, so the lease is refused. This is a defensive check against path traversal and malformed export URLs.","triggerScenarios":"Call AcquireExportArtifactLease (directly or via materializeExportArtifact / AcquireMobileExportLease) with a path like \"/export/..\", \"/export/.\", \"/export/%2e%2e%2fetc\", \"/export//..\", or an absolute path surviving Clean such as \"/export/C:\\\\foo\" or a URL-decoded leading-slash segment.","commonSituations":"Hand-built export URLs with unescaped or double-encoded segments; clients concatenating a user-supplied filename onto \"/export/\"; stale bookmarks/plugins referencing removed or renamed export temp files; mobile clients passing a full server URL instead of just the export sub-path.","solutions":["Check the exportPath passed to AcquireExportArtifactLease: after the \"/export/\" prefix it must be a non-empty, relative, non-traversal path (no leading \"/\", no \"..\" segments)","URL-encode filename segments once with url.PathEscape when building the path; do not pass raw or double-encoded user input","Use filepath.Rel or strip the known base directory server-side instead of trusting client-provided relative paths","Log the exact rejected exportPath and confirm it is the intended artifact name, not a whole URL or an absolute temp path"],"exampleFix":"// before\nlease, err := model.AcquireExportArtifactLease(\"/export/\" + userPath)\n// after\nuserPath = url.PathEscape(filepath.ToSlash(filepath.Clean(userPath)))\nif strings.HasPrefix(userPath, \"..\") || filepath.IsAbs(userPath) {\n    return errors.New(\"refusing to request unsafe export path\")\n}\nlease, err := model.AcquireExportArtifactLease(\"/export/\" + userPath)","handlingStrategy":"validation","validationCode":"function isSafeExportPath(p) {\n  if (!p.startsWith(\"/export/\")) return false;\n  const rest = decodeURIComponent(p.slice(\"/export/\".length));\n  return rest !== \".\" && !rest.startsWith(\"..\") && !require(\"path\").isAbsolute(rest);\n}","typeGuard":"function hasSafeRelativeName(p) {\n  return typeof p === \"string\" && p.length > 0 &&\n    !p.split(\"/\").some(seg => seg === \"..\" || seg === \".\");\n}","tryCatchPattern":"try {\n  lease = await acquireExportArtifactLease(p);\n} catch (e) {\n  if (String(e).includes(\"invalid export path\")) {\n    throw new Error(`Refusing malformed export path: ${p}`); // do not retry\n  }\n  throw e;\n}","preventionTips":["Always URL-escape each path segment once with encodeURIComponent/PathEscape when building export paths","Never splice user-supplied filenames directly onto \"/export/\"","Validate with a shared helper (clean + reject '..'/absolute) on both client and server sides","Treat this error as a programming bug, not a transient failure — do not auto-retry"],"tags":["path-traversal","validation","export","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"}