mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-18 05:52:10 +00:00
🧨 fix: Defuse Skill and Artifact Parsing Backtracking (#15856)
* 🛡️ fix: Prevent ReDoS in skill and artifact parsing * 🧵 fix: Normalize artifact fences in linear time * 🧠 fix: Bound artifact normalization memory
This commit is contained in:
parent
7b71c3fc35
commit
fe351ee171
6 changed files with 146 additions and 9 deletions
57
packages/api/src/artifacts/update.spec.ts
Normal file
57
packages/api/src/artifacts/update.spec.ts
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
import { replaceArtifactContent } from './update';
|
||||
|
||||
const artifactText = [
|
||||
':::artifact{identifier="example" type="text/markdown" title="Example"}',
|
||||
'```md',
|
||||
'ORIGINAL',
|
||||
'```',
|
||||
':::',
|
||||
].join('\n');
|
||||
|
||||
const artifact = {
|
||||
start: 0,
|
||||
end: artifactText.length,
|
||||
source: 'text' as const,
|
||||
text: artifactText,
|
||||
};
|
||||
|
||||
describe('replaceArtifactContent', () => {
|
||||
it('normalizes blank lines before a closing code and artifact fence', () => {
|
||||
const result = replaceArtifactContent(artifactText, artifact, 'ORIGINAL', 'UPDATED\n\n');
|
||||
|
||||
expect(result).toContain('UPDATED\n```\n:::');
|
||||
});
|
||||
|
||||
it('normalizes before a code fence separated from the artifact close by blank lines', () => {
|
||||
const text = artifactText.replace('```\n:::', '```\n\n:::');
|
||||
const result = replaceArtifactContent(
|
||||
text,
|
||||
{ ...artifact, end: text.length, text },
|
||||
'ORIGINAL',
|
||||
'UPDATED\n\n',
|
||||
);
|
||||
|
||||
expect(result).toContain('UPDATED\n```\n\n:::');
|
||||
});
|
||||
|
||||
it('handles long whitespace near-misses without backtracking', () => {
|
||||
const updated = `\n\`\`\`${' \n'.repeat(100_000)}X`;
|
||||
const result = replaceArtifactContent(artifactText, artifact, 'ORIGINAL', updated);
|
||||
|
||||
expect(result).toContain(updated);
|
||||
}, 1_000);
|
||||
|
||||
it('handles long consecutive-newline near-misses without backtracking', () => {
|
||||
const updated = `\n\`\`\`${'\n'.repeat(100_000)}X`;
|
||||
const result = replaceArtifactContent(artifactText, artifact, 'ORIGINAL', updated);
|
||||
|
||||
expect(result).toContain(updated);
|
||||
}, 1_000);
|
||||
|
||||
it('normalizes a near-limit newline run without per-line allocations', () => {
|
||||
const updated = `UPDATED${'\n'.repeat(2_500_000)}`;
|
||||
const result = replaceArtifactContent(artifactText, artifact, 'ORIGINAL', updated);
|
||||
|
||||
expect(result).toContain('UPDATED\n```\n:::');
|
||||
}, 1_000);
|
||||
});
|
||||
|
|
@ -54,8 +54,7 @@ const getCloseRange = (
|
|||
}
|
||||
|
||||
const markerStart = lineStart + contentStart;
|
||||
const markerText = text.slice(markerStart);
|
||||
if (!markerText.startsWith(ARTIFACT_END) || markerText.startsWith(ARTIFACT_START)) {
|
||||
if (!text.startsWith(ARTIFACT_END, markerStart) || text.startsWith(ARTIFACT_START, markerStart)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
@ -213,8 +212,38 @@ const replaceRange = (
|
|||
return originalText.substring(0, start) + updated + separator + endText;
|
||||
};
|
||||
|
||||
const isClosingArtifactFenceAt = (text: string, start: number): boolean => {
|
||||
const marker = text[start];
|
||||
if (marker !== '`' && marker !== '~') {
|
||||
return false;
|
||||
}
|
||||
|
||||
let markerEnd = start + 1;
|
||||
while (markerEnd < text.length && text[markerEnd] === marker) {
|
||||
markerEnd++;
|
||||
}
|
||||
if (markerEnd - start < 3) {
|
||||
return false;
|
||||
}
|
||||
|
||||
let hasLineBreak = false;
|
||||
for (let i = markerEnd; i < text.length; i++) {
|
||||
if (text[i] === '\n') {
|
||||
hasLineBreak = true;
|
||||
continue;
|
||||
}
|
||||
if (/\s/.test(text[i])) {
|
||||
continue;
|
||||
}
|
||||
return hasLineBreak && text.startsWith(ARTIFACT_END, i);
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
const normalizeBeforeClosingArtifactFence = (text: string): string =>
|
||||
text.replace(/\n+(?=(?:`{3,}|~{3,})\s*\n\s*:::)/g, '\n');
|
||||
text.replace(/\n{2,}/g, (newlines, offset: number) =>
|
||||
isClosingArtifactFenceAt(text, offset + newlines.length) ? '\n' : newlines,
|
||||
);
|
||||
|
||||
export const findAllArtifacts = (message: ArtifactMessage): ArtifactBoundary[] => {
|
||||
const artifacts: ArtifactBoundary[] = [];
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import {
|
|||
AccessRoleIds,
|
||||
PrincipalType,
|
||||
PermissionBits,
|
||||
SKILL_BODY_MAX_LENGTH,
|
||||
} from 'librechat-data-provider';
|
||||
import {
|
||||
partitionIssues,
|
||||
|
|
@ -565,6 +566,31 @@ describe('skill validation helpers', () => {
|
|||
});
|
||||
|
||||
describe('Skill CRUD methods', () => {
|
||||
it('rejects an oversized body before scanning its frontmatter on create', async () => {
|
||||
const body = `---\nalways-apply: a${' '.repeat(SKILL_BODY_MAX_LENGTH)}b\n---`;
|
||||
|
||||
await expect(methods.createSkill(makeSkillInput({ body }))).rejects.toMatchObject({
|
||||
code: 'SKILL_VALIDATION_FAILED',
|
||||
issues: [expect.objectContaining({ field: 'body', code: 'TOO_LONG' })],
|
||||
});
|
||||
});
|
||||
|
||||
it('rejects an oversized body before scanning its frontmatter on update', async () => {
|
||||
const { skill } = await methods.createSkill(makeSkillInput());
|
||||
const body = `---\nalways-apply: a${' '.repeat(SKILL_BODY_MAX_LENGTH)}b\n---`;
|
||||
|
||||
await expect(
|
||||
methods.updateSkill({
|
||||
id: skill._id.toString(),
|
||||
expectedVersion: 1,
|
||||
update: { body },
|
||||
}),
|
||||
).rejects.toMatchObject({
|
||||
code: 'SKILL_VALIDATION_FAILED',
|
||||
issues: [expect.objectContaining({ field: 'body', code: 'TOO_LONG' })],
|
||||
});
|
||||
});
|
||||
|
||||
it('creates a skill with version 1 and default fileCount 0', async () => {
|
||||
const { skill, warnings } = await methods.createSkill(makeSkillInput());
|
||||
expect(skill.name).toBe('demo-skill');
|
||||
|
|
|
|||
|
|
@ -1153,15 +1153,18 @@ export function createSkillMethods(
|
|||
normalizedFrontmatter && 'frontmatter' in normalizedFrontmatter
|
||||
? normalizedFrontmatter.frontmatter
|
||||
: data.frontmatter;
|
||||
const bodyIssues = validateSkillBody(data.body);
|
||||
/* Parse body's always-apply status once — reused for validation
|
||||
(below) and derivation in `resolveAlwaysApplyFromInput`. Avoids
|
||||
parsing the same YAML frontmatter block twice per create. */
|
||||
const bodyAlwaysApply =
|
||||
data.body !== undefined ? extractAlwaysApplyFromBody(data.body) : undefined;
|
||||
bodyIssues.length === 0 && data.body !== undefined
|
||||
? extractAlwaysApplyFromBody(data.body)
|
||||
: undefined;
|
||||
const issues: ValidationIssue[] = [
|
||||
...validateSkillName(data.name),
|
||||
...validateSkillDescription(data.description),
|
||||
...validateSkillBody(data.body),
|
||||
...bodyIssues,
|
||||
...validateSkillDisplayTitle(data.displayTitle),
|
||||
...validateSkillFrontmatter(frontmatter),
|
||||
...validateAlwaysApply(data.alwaysApply),
|
||||
|
|
@ -1484,17 +1487,20 @@ export function createSkillMethods(
|
|||
? normalizedFrontmatter.frontmatter
|
||||
: update.frontmatter;
|
||||
|
||||
const bodyIssues = update.body !== undefined ? validateSkillBody(update.body) : [];
|
||||
/* Parse body's always-apply status once — reused for validation
|
||||
(precedence-aware, below) and the derivation cascade further
|
||||
down. Avoids parsing the same YAML frontmatter block twice per
|
||||
update. */
|
||||
const bodyAlwaysApply =
|
||||
update.body !== undefined ? extractAlwaysApplyFromBody(update.body) : undefined;
|
||||
bodyIssues.length === 0 && update.body !== undefined
|
||||
? extractAlwaysApplyFromBody(update.body)
|
||||
: undefined;
|
||||
const issues: ValidationIssue[] = [];
|
||||
if (update.name !== undefined) issues.push(...validateSkillName(update.name));
|
||||
if (update.description !== undefined)
|
||||
issues.push(...validateSkillDescription(update.description));
|
||||
if (update.body !== undefined) issues.push(...validateSkillBody(update.body));
|
||||
issues.push(...bodyIssues);
|
||||
if (update.displayTitle !== undefined)
|
||||
issues.push(...validateSkillDisplayTitle(update.displayTitle));
|
||||
if (update.frontmatter !== undefined) issues.push(...validateSkillFrontmatter(frontmatter));
|
||||
|
|
|
|||
15
packages/data-schemas/src/utils/yaml.spec.ts
Normal file
15
packages/data-schemas/src/utils/yaml.spec.ts
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
import { stripYamlTrailingComment } from './yaml';
|
||||
|
||||
describe('stripYamlTrailingComment', () => {
|
||||
it('strips comments separated from an unquoted scalar by whitespace', () => {
|
||||
expect(stripYamlTrailingComment('true # enabled by default')).toBe('true');
|
||||
expect(stripYamlTrailingComment('false\t# disabled')).toBe('false');
|
||||
expect(stripYamlTrailingComment('hashtag#value')).toBe('hashtag#value');
|
||||
expect(stripYamlTrailingComment(' # comment only')).toBe('');
|
||||
});
|
||||
|
||||
it('handles long comment-free whitespace runs in linear time', () => {
|
||||
const value = `a${' '.repeat(100_000)}b`;
|
||||
expect(stripYamlTrailingComment(value)).toBe(value);
|
||||
}, 1_000);
|
||||
});
|
||||
|
|
@ -10,6 +10,10 @@
|
|||
*/
|
||||
export function stripYamlTrailingComment(value: string): string {
|
||||
if (value.trimStart().startsWith('#')) return '';
|
||||
const match = value.match(/^(.*?)\s+#.*$/);
|
||||
return match ? match[1] : value;
|
||||
for (let i = 1; i < value.length; i++) {
|
||||
if (value[i] === '#' && /\s/.test(value[i - 1])) {
|
||||
return value.slice(0, i).trimEnd();
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue