Skip to content

Commit

Permalink
fix: shifted dompurify.addhook functions inside removescript
Browse files Browse the repository at this point in the history
  • Loading branch information
devbyharshit committed Oct 23, 2023
1 parent 3f486ac commit 7960f94
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions packages/mermaid/src/diagrams/common/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,28 @@ export const getRows = (s?: string): string[] => {
* @returns The safer text
*/
export const removeScript = (txt: string): string => {
return DOMPurify.sanitize(txt);
};
const TEMPORARY_ATTRIBUTE = 'data-temp-href-target';

const TEMPORARY_ATTRIBUTE = 'data-temp-href-target';
DOMPurify.addHook('beforeSanitizeAttributes', (node: Element) => {
if (node.tagName === 'A' && node.hasAttribute('target')) {
node.setAttribute(TEMPORARY_ATTRIBUTE, node.getAttribute('target') || '');
}

Check warning on line 33 in packages/mermaid/src/diagrams/common/common.ts

View check run for this annotation

Codecov / codecov/patch

packages/mermaid/src/diagrams/common/common.ts#L33

Added line #L33 was not covered by tests
});

DOMPurify.addHook('beforeSanitizeAttributes', (node: Element) => {
if (node.tagName === 'A' && node.hasAttribute('target')) {
node.setAttribute(TEMPORARY_ATTRIBUTE, node.getAttribute('target') || '');
}
});

DOMPurify.addHook('afterSanitizeAttributes', (node: Element) => {
if (node.tagName === 'A' && node.hasAttribute(TEMPORARY_ATTRIBUTE)) {
node.setAttribute('target', node.getAttribute(TEMPORARY_ATTRIBUTE) || '');
node.removeAttribute(TEMPORARY_ATTRIBUTE);
if (node.getAttribute('target') === '_blank') {
node.setAttribute('rel', 'noopener');
const sanitizedText = DOMPurify.sanitize(txt);

DOMPurify.addHook('afterSanitizeAttributes', (node: Element) => {
if (node.tagName === 'A' && node.hasAttribute(TEMPORARY_ATTRIBUTE)) {
node.setAttribute('target', node.getAttribute(TEMPORARY_ATTRIBUTE) || '');
node.removeAttribute(TEMPORARY_ATTRIBUTE);

Check warning on line 41 in packages/mermaid/src/diagrams/common/common.ts

View check run for this annotation

Codecov / codecov/patch

packages/mermaid/src/diagrams/common/common.ts#L41

Added line #L41 was not covered by tests
if (node.getAttribute('target') === '_blank') {
node.setAttribute('rel', 'noopener');
}
}

Check warning on line 45 in packages/mermaid/src/diagrams/common/common.ts

View check run for this annotation

Codecov / codecov/patch

packages/mermaid/src/diagrams/common/common.ts#L43-L45

Added lines #L43 - L45 were not covered by tests
}
});
});

return sanitizedText;
};

const sanitizeMore = (text: string, config: MermaidConfig) => {
if (config.flowchart?.htmlLabels !== false) {
Expand Down

0 comments on commit 7960f94

Please sign in to comment.