-
Notifications
You must be signed in to change notification settings - Fork 900
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core/presentation): Make all DOMPurify'd links open in a new wind…
…ow (#7511)
- Loading branch information
1 parent
b382a22
commit aef69cf
Showing
2 changed files
with
34 additions
and
12 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
app/scripts/modules/core/src/presentation/domPurifyOpenLinksInNewWindow.ts
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,19 @@ | ||
import * as DOMPurify from 'dompurify'; | ||
|
||
export function domPurifyOpenLinksInNewWindow() { | ||
// Add a hook to make all DOMPurify'd links open a new window | ||
// See: https://github.com/cure53/DOMPurify/tree/master/demos#hook-to-open-all-links-in-a-new-window-link | ||
DOMPurify.addHook('afterSanitizeAttributes', function(node: any) { | ||
// set all elements owning target to target=_blank | ||
if ('target' in node) { | ||
node.setAttribute('target', '_blank'); | ||
// prevent https://www.owasp.org/index.php/Reverse_Tabnabbing | ||
node.setAttribute('rel', 'noopener noreferrer'); | ||
} | ||
// set non-HTML/MathML links to xlink:show=new | ||
if (!node.hasAttribute('target') && (node.hasAttribute('xlink:href') || node.hasAttribute('href'))) { | ||
node.setAttribute('xlink:show', 'new'); | ||
} | ||
return node; | ||
}); | ||
} |
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