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

🐛 fix: sanitize innerHTML with dompurify #176

Merged
merged 1 commit into from
Mar 29, 2024
Merged
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"color": "^4.2.3",
"copy-to-clipboard": "^3.3.3",
"diff": "^5.1.0",
"dompurify": "^3.0.11",
"fast-deep-equal": "^3.1.3",
"immer": "^9.0.21",
"lodash.flatten": "^4.4.0",
Expand Down
6 changes: 4 additions & 2 deletions src/Highlight/hooks/useShiki.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as DOMPurify from 'dompurify';
import { useEffect, useState } from 'react';
import { getHighlighter, type Highlighter } from 'shiki/bundle/web';
import { themeConfig } from '../theme';
Expand Down Expand Up @@ -42,9 +43,10 @@ export const useShiki = (language, theme) => {
lang: language,
theme,
});
return result;
return DOMPurify.sanitize(result);
} else {
return `<pre><code>${content}</code></pre>`;
// 在shiki加载完成之前,直接返回原始代码,同样需要进行xss过滤
return DOMPurify.sanitize(`<pre><code>${content}</code></pre>`);
}
};

Expand Down
Loading