@getnote/mcp 1.5.0 → 1.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +53 -3
- package/package.json +5 -4
- package/src/index.ts +64 -5
package/dist/index.js
CHANGED
|
@@ -49,6 +49,55 @@ const node_fs_1 = require("node:fs");
|
|
|
49
49
|
const node_path_1 = require("node:path");
|
|
50
50
|
const pkg = JSON.parse((0, node_fs_1.readFileSync)((0, node_path_1.join)(__dirname, "..", "package.json"), "utf-8"));
|
|
51
51
|
const SERVER_VERSION = pkg.version;
|
|
52
|
+
// ─── Security ────────────────────────────────────────────────────────────────
|
|
53
|
+
/**
|
|
54
|
+
* 校验并解析 upload_image 的 image_path,防止路径穿越(安全漏洞 #10)。
|
|
55
|
+
*
|
|
56
|
+
* 为什么要校验:upload_image 会把 image_path 指向的本地文件读出来并上传到公网
|
|
57
|
+
* CDN。如果不校验,攻击者只要能发起 MCP 调用,就能传入任意路径
|
|
58
|
+
* (如 "/etc/passwd"、"../../.ssh/id_rsa"),把宿主机上的任意敏感文件读出来
|
|
59
|
+
* 并外泄到 CDN —— 等同于任意文件读取漏洞。
|
|
60
|
+
*
|
|
61
|
+
* 校验策略:
|
|
62
|
+
* - 拒绝 null byte:底层 syscall 会在 \0 处截断字符串,可绕过后续校验。
|
|
63
|
+
* - 拒绝绝对路径(以 / 或 \ 开头,或平台判定为绝对路径)。
|
|
64
|
+
* - 拒绝包含 ".." 的路径段:避免跳出工作目录 / 允许目录。
|
|
65
|
+
* - 若设置了 GETNOTE_MCP_ALLOWED_ROOT:用 realpathSync + resolve 解开符号链接
|
|
66
|
+
* 后,确保最终真实路径严格位于该目录内(防止软链逃逸)。
|
|
67
|
+
* - 若未设置:默认只允许相对路径(上面已拒绝绝对路径与 ".."),相对当前
|
|
68
|
+
* 工作目录读取。
|
|
69
|
+
*
|
|
70
|
+
* @returns 经校验、可安全读取的最终路径。
|
|
71
|
+
*/
|
|
72
|
+
function resolveSafeImagePath(imagePath) {
|
|
73
|
+
if (typeof imagePath !== "string" || imagePath.length === 0) {
|
|
74
|
+
throw new Error("image_path must be a non-empty string");
|
|
75
|
+
}
|
|
76
|
+
// null byte 会截断底层路径字符串,从而绕过后续的字符串校验
|
|
77
|
+
if (imagePath.includes("\0")) {
|
|
78
|
+
throw new Error("image_path must not contain null bytes");
|
|
79
|
+
}
|
|
80
|
+
// 绝对路径直接拒绝(覆盖 POSIX 的 "/"、Windows 的 "\" 与盘符)
|
|
81
|
+
if ((0, node_path_1.isAbsolute)(imagePath) || /^[/\\]/.test(imagePath)) {
|
|
82
|
+
throw new Error("image_path must be a relative path; absolute paths are not allowed");
|
|
83
|
+
}
|
|
84
|
+
// 任意 ".." 路径段都可能用来跳出允许目录
|
|
85
|
+
if (imagePath.split(/[/\\]/).includes("..")) {
|
|
86
|
+
throw new Error('image_path must not contain ".." path segments');
|
|
87
|
+
}
|
|
88
|
+
const allowedRoot = process.env.GETNOTE_MCP_ALLOWED_ROOT;
|
|
89
|
+
if (allowedRoot) {
|
|
90
|
+
// realpathSync 解开符号链接,防止用软链把路径指向允许目录之外的文件
|
|
91
|
+
const rootReal = (0, node_fs_1.realpathSync)((0, node_path_1.resolve)(allowedRoot));
|
|
92
|
+
const target = (0, node_fs_1.realpathSync)((0, node_path_1.resolve)(rootReal, imagePath));
|
|
93
|
+
if (target !== rootReal && !target.startsWith(rootReal + node_path_1.sep)) {
|
|
94
|
+
throw new Error("image_path resolves outside of GETNOTE_MCP_ALLOWED_ROOT");
|
|
95
|
+
}
|
|
96
|
+
return target;
|
|
97
|
+
}
|
|
98
|
+
// 未配置允许目录:仅允许相对路径(已校验),相对 cwd 解析
|
|
99
|
+
return (0, node_path_1.resolve)(process.cwd(), imagePath);
|
|
100
|
+
}
|
|
52
101
|
// ─── Config ──────────────────────────────────────────────────────────────────
|
|
53
102
|
function getApiKey() {
|
|
54
103
|
// 1. --api-key flag
|
|
@@ -380,7 +429,7 @@ const TOOLS = [
|
|
|
380
429
|
properties: {
|
|
381
430
|
image_path: {
|
|
382
431
|
type: "string",
|
|
383
|
-
description: "
|
|
432
|
+
description: "本地图片文件路径。出于安全考虑仅接受相对路径:不允许绝对路径(以 / 或 \\ 开头)、不允许包含 \"..\" 的路径段、不允许 null byte。默认相对当前工作目录解析;若设置了环境变量 GETNOTE_MCP_ALLOWED_ROOT,则路径必须落在该目录内(含符号链接解析后)。",
|
|
384
433
|
},
|
|
385
434
|
image_base64: {
|
|
386
435
|
type: "string",
|
|
@@ -695,8 +744,9 @@ async function handleTool(name, input, client) {
|
|
|
695
744
|
const fs = await Promise.resolve().then(() => __importStar(require("fs")));
|
|
696
745
|
let imageData;
|
|
697
746
|
if (input.image_path) {
|
|
698
|
-
// 从文件路径读取
|
|
699
|
-
|
|
747
|
+
// 从文件路径读取 —— 先做路径安全校验,防止任意文件读取(漏洞 #10)
|
|
748
|
+
const safePath = resolveSafeImagePath(input.image_path);
|
|
749
|
+
imageData = fs.readFileSync(safePath);
|
|
700
750
|
}
|
|
701
751
|
else if (input.image_base64) {
|
|
702
752
|
// 从 Base64 解码
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@getnote/mcp",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.1",
|
|
4
4
|
"description": "MCP server for 得到大脑(Get笔记) — manage notes and knowledge bases from AI assistants",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"bin": {
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"dependencies": {
|
|
25
25
|
"@modelcontextprotocol/sdk": "^1.0.0",
|
|
26
26
|
"axios": "^1.16.1",
|
|
27
|
-
"form-data": "^4.0.
|
|
27
|
+
"form-data": "^4.0.6",
|
|
28
28
|
"zod": "^3.22.0"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
@@ -36,10 +36,11 @@
|
|
|
36
36
|
},
|
|
37
37
|
"overrides": {
|
|
38
38
|
"express-rate-limit": "^8.2.2",
|
|
39
|
-
"hono": "^4.12.
|
|
39
|
+
"hono": "^4.12.21",
|
|
40
40
|
"path-to-regexp": "^8.4.1",
|
|
41
41
|
"fast-uri": "^3.1.2",
|
|
42
42
|
"ip-address": "^10.2.0",
|
|
43
|
-
"axios": "^1.16.1"
|
|
43
|
+
"axios": "^1.16.1",
|
|
44
|
+
"qs": "^6.15.2"
|
|
44
45
|
}
|
|
45
46
|
}
|
package/src/index.ts
CHANGED
|
@@ -16,14 +16,71 @@ import {
|
|
|
16
16
|
} from "@modelcontextprotocol/sdk/types.js";
|
|
17
17
|
import { z } from "zod";
|
|
18
18
|
import { GetNoteClient, GetNoteAPIError, SaveNoteReq, UpdateNoteReq } from "./client.js";
|
|
19
|
-
import { readFileSync } from "node:fs";
|
|
20
|
-
import { join } from "node:path";
|
|
19
|
+
import { readFileSync, realpathSync } from "node:fs";
|
|
20
|
+
import { join, resolve, isAbsolute, sep } from "node:path";
|
|
21
21
|
|
|
22
22
|
const pkg = JSON.parse(
|
|
23
23
|
readFileSync(join(__dirname, "..", "package.json"), "utf-8")
|
|
24
24
|
) as { version: string };
|
|
25
25
|
const SERVER_VERSION: string = pkg.version;
|
|
26
26
|
|
|
27
|
+
// ─── Security ────────────────────────────────────────────────────────────────
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* 校验并解析 upload_image 的 image_path,防止路径穿越(安全漏洞 #10)。
|
|
31
|
+
*
|
|
32
|
+
* 为什么要校验:upload_image 会把 image_path 指向的本地文件读出来并上传到公网
|
|
33
|
+
* CDN。如果不校验,攻击者只要能发起 MCP 调用,就能传入任意路径
|
|
34
|
+
* (如 "/etc/passwd"、"../../.ssh/id_rsa"),把宿主机上的任意敏感文件读出来
|
|
35
|
+
* 并外泄到 CDN —— 等同于任意文件读取漏洞。
|
|
36
|
+
*
|
|
37
|
+
* 校验策略:
|
|
38
|
+
* - 拒绝 null byte:底层 syscall 会在 \0 处截断字符串,可绕过后续校验。
|
|
39
|
+
* - 拒绝绝对路径(以 / 或 \ 开头,或平台判定为绝对路径)。
|
|
40
|
+
* - 拒绝包含 ".." 的路径段:避免跳出工作目录 / 允许目录。
|
|
41
|
+
* - 若设置了 GETNOTE_MCP_ALLOWED_ROOT:用 realpathSync + resolve 解开符号链接
|
|
42
|
+
* 后,确保最终真实路径严格位于该目录内(防止软链逃逸)。
|
|
43
|
+
* - 若未设置:默认只允许相对路径(上面已拒绝绝对路径与 ".."),相对当前
|
|
44
|
+
* 工作目录读取。
|
|
45
|
+
*
|
|
46
|
+
* @returns 经校验、可安全读取的最终路径。
|
|
47
|
+
*/
|
|
48
|
+
function resolveSafeImagePath(imagePath: string): string {
|
|
49
|
+
if (typeof imagePath !== "string" || imagePath.length === 0) {
|
|
50
|
+
throw new Error("image_path must be a non-empty string");
|
|
51
|
+
}
|
|
52
|
+
// null byte 会截断底层路径字符串,从而绕过后续的字符串校验
|
|
53
|
+
if (imagePath.includes("\0")) {
|
|
54
|
+
throw new Error("image_path must not contain null bytes");
|
|
55
|
+
}
|
|
56
|
+
// 绝对路径直接拒绝(覆盖 POSIX 的 "/"、Windows 的 "\" 与盘符)
|
|
57
|
+
if (isAbsolute(imagePath) || /^[/\\]/.test(imagePath)) {
|
|
58
|
+
throw new Error(
|
|
59
|
+
"image_path must be a relative path; absolute paths are not allowed"
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
// 任意 ".." 路径段都可能用来跳出允许目录
|
|
63
|
+
if (imagePath.split(/[/\\]/).includes("..")) {
|
|
64
|
+
throw new Error('image_path must not contain ".." path segments');
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const allowedRoot = process.env.GETNOTE_MCP_ALLOWED_ROOT;
|
|
68
|
+
if (allowedRoot) {
|
|
69
|
+
// realpathSync 解开符号链接,防止用软链把路径指向允许目录之外的文件
|
|
70
|
+
const rootReal = realpathSync(resolve(allowedRoot));
|
|
71
|
+
const target = realpathSync(resolve(rootReal, imagePath));
|
|
72
|
+
if (target !== rootReal && !target.startsWith(rootReal + sep)) {
|
|
73
|
+
throw new Error(
|
|
74
|
+
"image_path resolves outside of GETNOTE_MCP_ALLOWED_ROOT"
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
return target;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// 未配置允许目录:仅允许相对路径(已校验),相对 cwd 解析
|
|
81
|
+
return resolve(process.cwd(), imagePath);
|
|
82
|
+
}
|
|
83
|
+
|
|
27
84
|
// ─── Config ──────────────────────────────────────────────────────────────────
|
|
28
85
|
|
|
29
86
|
function getApiKey(): string {
|
|
@@ -373,7 +430,8 @@ const TOOLS: Tool[] = [
|
|
|
373
430
|
properties: {
|
|
374
431
|
image_path: {
|
|
375
432
|
type: "string",
|
|
376
|
-
description:
|
|
433
|
+
description:
|
|
434
|
+
"本地图片文件路径。出于安全考虑仅接受相对路径:不允许绝对路径(以 / 或 \\ 开头)、不允许包含 \"..\" 的路径段、不允许 null byte。默认相对当前工作目录解析;若设置了环境变量 GETNOTE_MCP_ALLOWED_ROOT,则路径必须落在该目录内(含符号链接解析后)。",
|
|
377
435
|
},
|
|
378
436
|
image_base64: {
|
|
379
437
|
type: "string",
|
|
@@ -718,8 +776,9 @@ async function handleTool(
|
|
|
718
776
|
let imageData: Buffer;
|
|
719
777
|
|
|
720
778
|
if (input.image_path) {
|
|
721
|
-
// 从文件路径读取
|
|
722
|
-
|
|
779
|
+
// 从文件路径读取 —— 先做路径安全校验,防止任意文件读取(漏洞 #10)
|
|
780
|
+
const safePath = resolveSafeImagePath(input.image_path as string);
|
|
781
|
+
imageData = fs.readFileSync(safePath);
|
|
723
782
|
} else if (input.image_base64) {
|
|
724
783
|
// 从 Base64 解码
|
|
725
784
|
imageData = Buffer.from(input.image_base64 as string, "base64");
|