Skip to content

🀯 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.

πŸ”’ fix(skill): prevent ReDoS in GitHub skill import basePath matching (#16548)

LiJian349bbe326eb8
diff --git a/apps/server/src/services/skill/parser.test.ts b/apps/server/src/services/skill/parser.test.ts
index ebe0d0f66bd..f4c7505403e 100644
--- a/apps/server/src/services/skill/parser.test.ts
+++ b/apps/server/src/services/skill/parser.test.ts
@@ -472,6 +472,136 @@ Root content`;
});
});
+ // Regression tests for the ReDoS vulnerability reported in lobehub/lobehub#16494.
+ // `basePath` is derived from the fully user-controlled GitHub URL path segment
+ // (https://github.com/owner/repo/tree/branch/<path>) and used to be interpolated
+ // directly into `new RegExp('^[^/]+/<basePath>/SKILL\\.md$')` inside findSkillMd.
+ // That allowed catastrophic backtracking (event-loop DoS) and SyntaxError crashes.
+ describe('findSkillMd basePath security (ReDoS regression, #16494)', () => {
+ it('should not hang on a malicious ReDoS basePath', async () => {
+ // A long run of "a" followed by a non-matching char makes the old
+ // `^[^/]+/(a+)+/SKILL\.md$` regex backtrack exponentially (~2^40 steps).
+ const evilDecoy = 'a'.repeat(40);
+ const testFiles = {
+ [`repo-main/${evilDecoy}X/data.txt`]: new TextEncoder().encode('x'),
+ };
+
+ const zipped = await createZip(testFiles);
+ const buffer = Buffer.from(zipped);
+
+ const start = performance.now();
+ // No SKILL.md exists, so it should reject with "not found" β€” the point is that
+ // it does so quickly instead of blocking the event loop for tens of seconds.
+ await expect(parser.parseZipPackage(buffer, { basePath: '(a+)+' })).rejects.toThrow(
+ 'SKILL.md not found',
+ );
+ const elapsed = performance.now() - start;
+
+ expect(elapsed).toBeLessThan(1000);
+ });
+
+ it('should not throw SyntaxError for a basePath with invalid regex syntax', async () => {
+ // `[invalid` is not a valid RegExp; the old code threw a SyntaxError (HTTP 500).
+ // It must now be treated as a literal string and fail gracefully as "not found".
+ const testFiles = {
+ 'repo-main/skills/demo/SKILL.md': new TextEncoder().encode(
+ '---\nname: demo\ndescription: demo\n---\nDemo',
+ ),
+ };
+
+ const zipped = await createZip(testFiles);
+ const buffer = Buffer.from(zipped);
+
+ await expect(parser.parseZipPackage(buffer, { basePath: '[invalid' })).rejects.toThrow(
+ 'SKILL.md not found',
+ );
+ });
+
+ it('should treat regex metacharacters in basePath as literals (no "any char" matching)', async () => {
+ // Path uses "aXb"; basePath "a.b" must NOT match it (the old regex treated "." as
+ // "any char" and would have matched). The literal directory "a.b" does not exist.
+ const testFiles = {
+ 'repo-main/aXb/SKILL.md': new TextEncoder().encode(
+ '---\nname: axb\ndescription: axb\n---\naXb',
+ ),
+ };
+
+ const zipped = await createZip(testFiles);
+ const buffer = Buffer.from(zipped);
+
+ await expect(parser.parseZipPackage(buffer, { basePath: 'a.b' })).rejects.toThrow(
+ 'SKILL.md not found',
+ );
+ });
+
+ it('should still match a directory whose literal name contains regex metacharacters', async () => {
+ // A real directory literally named "a.b" must be found (via exact-path lookup).
+ const skillMd = `---
+name: literal-dot-skill
+description: Skill in a dir literally named a.b
+---
+Literal dot content`;
+
+ const testFiles = {
+ 'repo-main/a.b/SKILL.md': new TextEncoder().encode(skillMd),
+ };
+
+ const zipped = await createZip(testFiles);
+ const buffer = Buffer.from(zipped);
+
+ const result = await parser.parseZipPackage(buffer, { basePath: 'a.b' });
+ expect(result.manifest.name).toBe('literal-dot-skill');
+ });
+
+ it('should report "not found" (not silently import the root skill) when basePath misses', async () => {
+ // User explicitly requested a subdirectory via the GitHub URL path, but no SKILL.md
+ // exists there. Even though the repo has a root-level SKILL.md, we must NOT fall
+ // through and silently import that unrelated skill β€” it must be reported as missing.
+ const testFiles = {
+ 'repo-main/SKILL.md': new TextEncoder().encode(
+ '---\nname: root\ndescription: root\n---\nRoot',
+ ),
+ 'repo-main/README.md': new TextEncoder().encode('# Repo'),
+ };
+
+ const zipped = await createZip(testFiles);
+ const buffer = Buffer.from(zipped);
+
+ // Nonexistent subpath
+ await expect(
+ parser.parseZipPackage(buffer, { basePath: 'skills/does-not-exist' }),
+ ).rejects.toThrow('SKILL.md not found');
+
+ // Invalid-regex-looking subpath must behave the same (no silent root import)
+ await expect(parser.parseZipPackage(buffer, { basePath: '[invalid' })).rejects.toThrow(
+ 'SKILL.md not found',
+ );
+ });
+
+ it('should still resolve the basePath via the fallback when the root prefix differs', async () => {
+ // When findGitHubRootPrefix picks a prefix that does not match the SKILL.md's
+ // top-level directory, the exact lookup misses and the fallback must still find
+ // `<single-segment>/<basePath>/SKILL.md`. Insert the decoy first so it becomes
+ // the detected root prefix.
+ const skillMd = `---
+name: fallback-skill
+description: Found via fallback
+---
+Fallback content`;
+
+ const testFiles = {
+ 'decoy-dir/other.txt': new TextEncoder().encode('decoy'),
+ 'repo-main/skills/demo/SKILL.md': new TextEncoder().encode(skillMd),
+ };
+
+ const zipped = await createZip(testFiles);
+ const buffer = Buffer.from(zipped);
+
+ const result = await parser.parseZipPackage(buffer, { basePath: 'skills/demo' });
+ expect(result.manifest.name).toBe('fallback-skill');
+ });
+ });
+
describe('parseZipPackage with repackSkillZip', () => {
it('should not return skillZipBuffer when repackSkillZip is false/undefined', async () => {
const skillMd = `---
diff --git a/apps/server/src/services/skill/parser.ts b/apps/server/src/services/skill/parser.ts
index 3ee6fff5243..3d93f319426 100644
--- a/apps/server/src/services/skill/parser.ts
+++ b/apps/server/src/services/skill/parser.ts
@@ -200,11 +200,23 @@ export class SkillParser {
}
}
- // Fallback: try to find SKILL.md that contains the basePath
- const basePathPattern = new RegExp(
- `^[^/]+/${basePath.replaceAll(/^\/|\/$/g, '')}/SKILL\\.md$`,
- );
- const matchWithBasePath = allPaths.find((path) => basePathPattern.test(path));
+ // Fallback: try to find SKILL.md at <root>/<basePath>/SKILL.md, where <root>
+ // is a single top-level directory segment (the GitHub "{repo}-{branch}/" prefix).
+ //
+ // NOTE: basePath is fully user-controlled (derived from the GitHub URL path). It must
+ // NOT be interpolated into a `new RegExp(...)`, otherwise crafted paths like `(a+)+`
+ // cause catastrophic backtracking (ReDoS, blocking the event loop) and `[invalid`
+ // throws a SyntaxError. Use plain string matching instead, which is equivalent to the
+ // old `^[^/]+/<basePath>/SKILL\.md$` pattern.
+ const normalizedBasePath = basePath.replaceAll(/^\/|\/$/g, '');
+ const suffix = `/${normalizedBasePath}/SKILL.md`;
+ const matchWithBasePath = allPaths.find((path) => {
+ if (!path.endsWith(suffix)) return false;
+ // The part before the suffix must be a single non-empty segment (no slashes),
+ // matching the `^[^/]+` anchor of the original pattern.
+ const prefix = path.slice(0, -suffix.length);
+ return prefix.length > 0 && !prefix.includes('/');
+ });
if (matchWithBasePath) {
return {
@@ -212,6 +224,12 @@ export class SkillParser {
skillMdPath: matchWithBasePath,
};
}
+
+ // basePath was explicitly requested (GitHub subdirectory import) but no SKILL.md
+ // exists there. Do NOT fall through to the generic root/first-level lookup below:
+ // that would silently import an unrelated root skill (e.g. "repo-main/SKILL.md")
+ // when the user asked for a specific subdirectory. Report "not found" instead.
+ return { skillMdContent: '', skillMdPath: null };
}
// Check root directory first