🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
🐛 fix(skill): keep frontmatter parsing consistent (#21252)
Bryanaace12fe7416
diff --git a/apps/server/src/routers/lambda/__tests__/integration/agentSkills.integration.test.ts b/apps/server/src/routers/lambda/__tests__/integration/agentSkills.integration.test.tsindex d90ae66a655..edc4eb1e382 100644--- a/apps/server/src/routers/lambda/__tests__/integration/agentSkills.integration.test.ts+++ b/apps/server/src/routers/lambda/__tests__/integration/agentSkills.integration.test.ts@@ -672,6 +672,35 @@ describe('Skill Router Integration Tests', () => {});describe('convertDocumentToSkill', () => {+ it('does not execute frontmatter when converting a document', async () => {+ const executed = vi.fn();+ vi.stubGlobal('__skillFrontmatterExecuted', executed);+ try {+ const caller = agentDocumentRouter.createCaller(createTestContext(userId));+ const agentId = await createTestAgent(serverDB, userId);+ const doc = await agentDocumentModel.create(+ agentId,+ 'untrusted-document',+ `---js+(() => { globalThis.__skillFrontmatterExecuted(); throw new Error('Executed frontmatter'); })()+---+Body`,+ );+ await expect(+ caller.convertDocumentToSkill({+ agentId,+ description: 'Description',+ name: 'untrusted-skill',+ sourceAgentDocumentId: doc!.id,+ title: 'Skill',+ }),+ ).rejects.toMatchObject({ code: 'BAD_REQUEST' });+ expect(executed).not.toHaveBeenCalled();+ } finally {+ vi.unstubAllGlobals();+ }+ });+it('should migrate an existing document into a managed skill in place', async () => {const caller = agentDocumentRouter.createCaller(createTestContext(userId));const agentId = await createTestAgent(serverDB, userId);diff --git a/apps/server/src/routers/lambda/agentDocument/skills.ts b/apps/server/src/routers/lambda/agentDocument/skills.tsindex 9f19c6ae0c4..c128f11c124 100644--- a/apps/server/src/routers/lambda/agentDocument/skills.ts+++ b/apps/server/src/routers/lambda/agentDocument/skills.ts@@ -7,6 +7,7 @@ import { deriveAgentDocumentFields } from '@/database/models/agentDocuments';import { router } from '@/libs/trpc/lambda';import type { AgentDocumentsService } from '@/server/services/agentDocuments';import { getUnifiedSkillNamespaceRootPath } from '@/server/services/agentDocumentVfs/mounts/skills/path';+import { safeMatterOptions } from '@/server/utils/safeMatterOptions';import { agentDocumentProcedureWrite, handleAgentDocumentVfsError } from './procedures';@@ -37,7 +38,7 @@ const updateMountedSkillSchema = z.object({*/const stripLeadingFrontmatter = (content: string): string => {try {- const parsed = matter(content);+ const parsed = matter(content, safeMatterOptions);return parsed.matter ? parsed.content.trimStart() : content;} catch {return content;diff --git a/apps/server/src/services/skill/parser.test.ts b/apps/server/src/services/skill/parser.test.tsindex 0a05e1ec575..08ce5484e75 100644--- a/apps/server/src/services/skill/parser.test.ts+++ b/apps/server/src/services/skill/parser.test.ts@@ -4,7 +4,7 @@ import { join } from 'node:path';import { unzip, zip } from 'fflate';import { sha256 } from 'js-sha256';-import { afterAll, beforeAll, describe, expect, it } from 'vitest';+import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';import { SkillManifestError, SkillParseError } from './errors';import { SkillParser } from './parser';@@ -31,6 +31,24 @@ describe('SkillParser', () => {const parser = new SkillParser();describe('parseSkillMd', () => {+ it('rejects JavaScript frontmatter without executing it', () => {+ const executed = vi.fn();+ vi.stubGlobal('__skillFrontmatterExecuted', executed);+ const content = `---js+(() => { globalThis.__skillFrontmatterExecuted(); throw new Error('Executed frontmatter'); })()+---+# Skill++Skill description.`;++ try {+ expect(() => parser.parseSkillMd(content)).toThrow(SkillParseError);+ expect(executed).not.toHaveBeenCalled();+ } finally {+ vi.unstubAllGlobals();+ }+ });+it('should parse valid SKILL.md with frontmatter', () => {const content = `---name: test-skilldiff --git a/apps/server/src/services/skill/parser.ts b/apps/server/src/services/skill/parser.tsindex fa1926dcc7c..af78318cdca 100644--- a/apps/server/src/services/skill/parser.ts+++ b/apps/server/src/services/skill/parser.ts@@ -12,6 +12,8 @@ import matter from 'gray-matter';import { sha256 } from 'js-sha256';import { marked, type Token, type Tokens } from 'marked';+import { safeMatterOptions } from '@/server/utils/safeMatterOptions';+import { SkillManifestError, SkillParseError } from './errors';export interface ParseSkillMdOptions {@@ -91,7 +93,7 @@ export class SkillParser {parseSkillMd(fileContent: string, options?: ParseSkillMdOptions): ParsedSkill {try {const source = this.stripLeadingCommentsBeforeFrontMatter(fileContent);- const { data, content } = matter(source);+ const { data, content } = matter(source, safeMatterOptions);// gray-matter yields the same empty `data` for an absent block and for an// empty / comment-only one (`---\n---`), so presence is detected on the// source: a present-but-incomplete block must stay authoritative.diff --git a/apps/server/src/services/skillManagement/frontmatter.test.ts b/apps/server/src/services/skillManagement/frontmatter.test.tsindex a7b6b7319fa..7cf36245607 100644--- a/apps/server/src/services/skillManagement/frontmatter.test.ts+++ b/apps/server/src/services/skillManagement/frontmatter.test.ts@@ -1,5 +1,5 @@import matter from 'gray-matter';-import { describe, expect, it } from 'vitest';+import { describe, expect, it, vi } from 'vitest';import {normalizeSkillIndexContent,@@ -64,6 +64,29 @@ describe('parseSkillFrontmatter', () => {});describe('normalizeSkillIndexContent', () => {+ it('does not execute a second frontmatter block while serializing the body', () => {+ const executed = vi.fn();+ vi.stubGlobal('__skillFrontmatterExecuted', executed);+ try {+ expect(() =>+ normalizeSkillIndexContent({+ bundleName: 'skill',+ content: `---+name: skill+description: Description+---+---js+(() => { globalThis.__skillFrontmatterExecuted(); throw new Error('Executed frontmatter'); })()+---+Body`,+ }),+ ).toThrow();+ expect(executed).not.toHaveBeenCalled();+ } finally {+ vi.unstubAllGlobals();+ }+ });+it('rewrites the frontmatter name from the bundle filename and keeps the body', () => {expect(normalizeSkillIndexContent({diff --git a/apps/server/src/services/skillManagement/frontmatter.ts b/apps/server/src/services/skillManagement/frontmatter.tsindex 9112af1cdf8..f7037f312c1 100644--- a/apps/server/src/services/skillManagement/frontmatter.ts+++ b/apps/server/src/services/skillManagement/frontmatter.ts@@ -1,5 +1,7 @@import matter from 'gray-matter';+import { safeMatterOptions } from '@/server/utils/safeMatterOptions';+/*** Parsed metadata from a managed skill `SKILL.md` frontmatter block.*/@@ -214,11 +216,15 @@ export const normalizeSkillIndexContent = (input: NormalizeSkillIndexContentInputhrow new Error('Skill frontmatter description is required');}- const serialized = matter.stringify(body, {- ...data,- description: normalizeFrontmatterScalar(description, 'description'),- name: bundleName,- });+ const serialized = matter.stringify(+ body,+ {+ ...data,+ description: normalizeFrontmatterScalar(description, 'description'),+ name: bundleName,+ },+ safeMatterOptions,+ );return body.endsWith('\n') ? serialized : serialized.replace(/\n$/, '');};diff --git a/apps/server/src/utils/safeMatterOptions.ts b/apps/server/src/utils/safeMatterOptions.tsnew file mode 100644index 00000000000..f6569b323b6--- /dev/null+++ b/apps/server/src/utils/safeMatterOptions.ts@@ -0,0 +1,7 @@+export const safeMatterOptions = {+ engines: {+ javascript: () => {+ throw new Error('Executable frontmatter is not supported');+ },+ },+};