diff --git a/libs/tiptap-editor/src/__tests__/slash-command.test.ts b/libs/tiptap-editor/src/__tests__/slash-command.test.ts new file mode 100644 index 0000000..9118c73 --- /dev/null +++ b/libs/tiptap-editor/src/__tests__/slash-command.test.ts @@ -0,0 +1,56 @@ +import { describe, it, expect } from 'vitest' +import { isValidUrl } from '../slash-command' + +/** + * isValidUrl 纯函数测试。 + * + * 只允许 http(s):// 和 data:image/ 开头,拒绝 javascript: 等危险或非图片 scheme。 + */ + +describe('isValidUrl', () => { + describe('接受', () => { + it.each([ + ['http://example.com', 'http 协议'], + ['https://example.com', 'https 协议'], + ['https://example.com/path?q=1#frag', '带路径/查询/片段'], + ['HTTP://EXAMPLE.COM', 'http 大写(大小写不敏感)'], + ['Https://Example.Com', '混合大小写'], + ['http://localhost:3000/img.png', 'localhost + 端口'], + ['data:image/png;base64,iVBORw0KGgo=', 'data URL png'], + ['data:image/jpeg;base64,/9j/4AAQ', 'data URL jpeg'], + ['data:image/svg+xml,', 'data URL svg'], + ['data:image/', 'data:image 前缀(最小匹配)'], + ])('%s (%s)', (url) => { + expect(isValidUrl(url)).toBe(true) + }) + }) + + describe('拒绝', () => { + it.each([ + ['javascript:alert(1)', 'javascript scheme(XSS 风险)'], + ['JavaScript:alert(1)', 'javascript 大写'], + ['ftp://example.com/file', 'ftp scheme'], + ['file:///etc/passwd', 'file scheme'], + ['mailto:foo@bar.com', 'mailto scheme'], + ['data:text/html,