Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:sparkles: Align skill name validation with kernel path rules https://github.com/siyuan-note/siyuan/issues/20190

Danielc89865d103c5
diff --git a/app/src/ai/skills/manager.ts b/app/src/ai/skills/manager.ts
index 036546571a..761169fe96 100644
--- a/app/src/ai/skills/manager.ts
+++ b/app/src/ai/skills/manager.ts
@@ -6,7 +6,7 @@ import {escapeHtml} from "../../util/escape";
import {getFileRenameTarget, getFileTree} from "../../util/fileTree";
import {getHostCapabilities} from "../../util/hostCapabilities";
import {isBrowser, isMobile} from "../../util/functions";
-import {canChangeSkillEntry, getSkillDirectory, SkillSourceState} from "./state";
+import {canChangeSkillEntry, getSkillDirectory, isValidSkillName, SkillSourceState} from "./state";
import {createSkillManagerPage} from "./page";
import type {AISkillFileData, AISkillFileEntry, AISkillFileRequestInput} from "../../types/api";
/// #if !MOBILE
@@ -377,7 +377,7 @@ ${mobile ? "" : `<div class="skill-manager__actions">${button("save", lang.save)
const input = inputDialog.element.querySelector<HTMLInputElement>("input");
const name = value.trim();
const target = getFileRenameTarget(action === "rename" ? entry.path : "", name);
- if (target === undefined) {
+ if (target === undefined || !isValidSkillName(name)) {
input.setCustomValidity(lang.agentSkillNameTip);
input.reportValidity();
return;
diff --git a/app/src/ai/skills/state.test.ts b/app/src/ai/skills/state.test.ts
index ac9f74345a..a9dd7b696a 100644
--- a/app/src/ai/skills/state.test.ts
+++ b/app/src/ai/skills/state.test.ts
@@ -1,6 +1,18 @@
import * as assert from "node:assert/strict";
import {test} from "node:test";
-import {canChangeSkillEntry, getSkillDirectory, isSkillEntryPoint, SkillSourceState} from "./state";
+import {canChangeSkillEntry, getSkillDirectory, isSkillEntryPoint, isValidSkillName, SkillSourceState} from "./state";
+
+test("skill names reject invalid path components and Windows device names", () => {
+ for (const name of ["", ".", "..", " notes", "notes ", "notes.", "a/b", "a\\b", "a:b", "a<b", "a>b",
+ "a\"b", "a|b", "a?b", "a*b", "a~b", "a\u0000b", "a\u001fb", "a\u007fb", "a\u0085b", "CON", "con.txt",
+ "PRN", "AUX", "NUL", "CONIN$", "CONOUT$", "COM0", "COM1.md", "LPT9", "COM\u00b9", "LPT\u00b2.txt",
+ "COM\u00b3", "COMLPT1", "con .txt"]) {
+ assert.equal(isValidSkillName(name), false, name);
+ }
+ for (const name of ["notes", "notes.md", ".config", "SKILL.md", "my notes", "技能", "CONSOLE", "COM10", "LPT12.txt"]) {
+ assert.equal(isValidSkillName(name), true, name);
+ }
+});
test("skill entry points and resources cannot be renamed or deleted separately", () => {
assert.equal(isSkillEntryPoint({path: "review/SKILL.md", isDir: false, editable: true}), true);
diff --git a/app/src/ai/skills/state.ts b/app/src/ai/skills/state.ts
index b94f61d051..06e698bbce 100644
--- a/app/src/ai/skills/state.ts
+++ b/app/src/ai/skills/state.ts
@@ -1,5 +1,18 @@
import type {AISkillFileEntry} from "../../types/api";
+// 技能资源名称遵循内核的跨平台路径规则。
+export const isValidSkillName = (name: string) => {
+ if (!name || name === "." || name === ".." || name !== name.trim() || name.endsWith(".") ||
+ /[\\/:<>"|?*~]/.test(name) || Array.from(name).some(char => {
+ const code = char.charCodeAt(0);
+ return code <= 0x1f || (code >= 0x7f && code <= 0x9f);
+ })) {
+ return false;
+ }
+ const device = name.split(".")[0].replace(/[ .]+$/, "").toUpperCase();
+ return !/^(CON|PRN|AUX|NUL|CONIN\$|CONOUT\$|(COM(LPT)?|LPT)[0-9\u00b9\u00b2\u00b3])$/.test(device);
+};
+
export const isSkillEntryPoint = (entry: AISkillFileEntry) =>
!entry.isDir && /^[^/]+\/SKILL\.md$/i.test(entry.path);