소스 검색

fix: allow target="_blank" in dompurify

Sv443 7 달 전
부모
커밋
3fd29b4b44
1개의 변경된 파일7개의 추가작업 그리고 0개의 파일을 삭제
  1. 7 0
      src/utils/dom.ts

+ 7 - 0
src/utils/dom.ts

@@ -239,6 +239,13 @@ export function copyToClipboard(text: Stringifiable) {
 
 let ttPolicy: TTPolicy | undefined;
 
+DOMPurify.addHook("afterSanitizeAttributes", function (node) {
+  if("target" in node) {
+    node.setAttribute("target", "_blank");
+    node.setAttribute("rel", "noopener noreferrer");
+  }
+});
+
 /** Sets innerHTML directly on Firefox and Safari, while on Chromium a [Trusted Types policy](https://developer.mozilla.org/en-US/docs/Web/API/Trusted_Types_API) is used to set the HTML */
 export function setInnerHtml(element: HTMLElement, html: string) {
   if(!ttPolicy && window?.trustedTypes?.createPolicy) {