Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

6256 - Allow themeVariables to have additional valid CSS colors #6261

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions packages/mermaid/src/utils/sanitizeDirective.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { sanitizeDirective } from './sanitizeDirective.js';

describe('sanitizeDirective - Theme Variable Validation', () => {
it('should allow color names', () => {
const input = { themeVariables: { mainBkg: 'green' } };
sanitizeDirective(input);
expect(input.themeVariables.mainBkg).toBe('green');
});

it('should allow hex colors', () => {
const input = { themeVariables: { mainBkg: '#000', actorBkg: '#000000' } };
sanitizeDirective(input);
expect(input.themeVariables.mainBkg).toBe('#000');
expect(input.themeVariables.actorBkg).toBe('#000000');
});

it('should allow rgb colors', () => {
const input = { themeVariables: { mainBkg: 'rgb(0, 0, 0)', actorBkg: 'rgba(0, 0, 0, 0.5)' } };
sanitizeDirective(input);
expect(input.themeVariables.mainBkg).toBe('rgb(0, 0, 0)');
expect(input.themeVariables.actorBkg).toBe('rgba(0, 0, 0, 0.5)');
});

it('should allow hsl colors', () => {
const input = {
themeVariables: { mainBkg: 'hsl(0, 0%, 0%)', actorBkg: 'hsla(0, 0%, 0%, 0.5)' },
};
sanitizeDirective(input);
expect(input.themeVariables.mainBkg).toBe('hsl(0, 0%, 0%)');
expect(input.themeVariables.actorBkg).toBe('hsla(0, 0%, 0%, 0.5)');
});

it('should allow css variables', () => {
const input = { themeVariables: { mainBkg: 'var(--my-color)' } };
sanitizeDirective(input);
expect(input.themeVariables.mainBkg).toBe('var(--my-color)');
});

it('should allow light-dark function', () => {
const input = { themeVariables: { mainBkg: 'light-dark(green, blue)' } };
sanitizeDirective(input);
expect(input.themeVariables.mainBkg).toBe('light-dark(green, blue)');
});

it('should allow lighten and darken', () => {
const input = { themeVariables: { mainBkg: 'lighten(darken(green, 10%), 10%)' } };
sanitizeDirective(input);
expect(input.themeVariables.mainBkg).toBe('lighten(darken(green, 10%), 10%)');
});

it('should allow relative color syntax', () => {
const input = {
themeVariables: {
mainBkg: 'oklch(from var(--base-color) calc(l * 1.15) c h)',
actorBkg: 'lch(from var(--base-color) calc(l + 20) c h)',
actorBorder: 'lch(from var(--base-color) calc(l - 20) c h)',
signalColor: 'rgb(from red r g b / alpha)',
},
};
sanitizeDirective(input);
expect(input.themeVariables.mainBkg).toBe('oklch(from var(--base-color) calc(l * 1.15) c h)');
expect(input.themeVariables.actorBkg).toBe('lch(from var(--base-color) calc(l + 20) c h)');
expect(input.themeVariables.actorBorder).toBe('lch(from var(--base-color) calc(l - 20) c h)');
expect(input.themeVariables.signalColor).toBe('rgb(from red r g b / alpha)');
});

it('should set invalid input to empty string', () => {
const input = { themeVariables: { mainBkg: '{}' } };
sanitizeDirective(input);
expect(input.themeVariables.mainBkg).toBe('');
});

it('should sanitize unsafe characters', () => {
const input = { themeVariables: { mainBkg: '<script>alert("XSS")</script>' } };
sanitizeDirective(input);
expect(input.themeVariables.mainBkg).toBe('');
});
});
2 changes: 1 addition & 1 deletion packages/mermaid/src/utils/sanitizeDirective.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const sanitizeDirective = (args: any): void => {
if (args.themeVariables) {
for (const k of Object.keys(args.themeVariables)) {
const val = args.themeVariables[k];
if (val?.match && !val.match(/^[\d "#%(),.;A-Za-z]+$/)) {
if (val?.match && !val.match(/^[\d "#%()*+,./;A-Za-z-]+$/)) {
args.themeVariables[k] = '';
}
}
Expand Down
Loading