forked from rancher/dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support "target=_blank" in A links via clean-html directive (rancher#…
…9928) * Allow target=_blank on A tags through DOMPurify * Remove unused params * Fix lint issue * Prevent directives from being overwritten * Fix lint issues
- Loading branch information
Showing
5 changed files
with
77 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import DOMPurify from 'dompurify'; | ||
import { uniq } from '@shell/utils/array'; | ||
|
||
const ALLOWED_TAGS = [ | ||
'code', | ||
'li', | ||
'a', | ||
'p', | ||
'b', | ||
'br', | ||
'ul', | ||
'pre', | ||
'span', | ||
'div', | ||
'i', | ||
'em', | ||
'strong', | ||
]; | ||
|
||
// Allow 'A' tags to keep the target=_blank attribute if they have it | ||
DOMPurify.addHook('uponSanitizeAttribute', (node, data) => { | ||
if (node.tagName === 'A' && data.attrName === 'target' && data.attrValue === '_blank') { | ||
data.forceKeepAttr = true; | ||
} | ||
}); | ||
|
||
// Ensure if an 'A' tag has target=_blank that we add noopener, noreferrer and nofollow to the 'rel' attribute | ||
DOMPurify.addHook('afterSanitizeAttributes', (node) => { | ||
if (node.tagName === 'A' && node?.target === '_blank') { | ||
const rel = ['noopener', 'noreferrer', 'nofollow']; | ||
const existingRel = node.rel?.length ? node.rel.split(' ') : []; | ||
const combined = uniq([...rel, ...existingRel]); | ||
|
||
node.setAttribute('rel', combined.join(' ')); | ||
} | ||
}); | ||
|
||
export const purifyHTML = (value, options = { ALLOWED_TAGS }) => { | ||
return DOMPurify.sanitize(value, options); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* Load the directives | ||
* | ||
* These are included in a function that can be explictly called, so that we can be sure | ||
* of the execution order, rather than importing them at the top of a file. | ||
*/ | ||
export function loadDirectives() { | ||
import('./clean-html-directive'); | ||
import('./clean-tooltip-directive'); | ||
import('./directives'); | ||
} |