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: target blank removed from anchor tag #4933

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
2a9eb7f
fix: target blank removed from anchor tag
devbyharshit Oct 9, 2023
c279a9f
fix: clean link unit test resolved
devbyharshit Oct 9, 2023
345e82a
fix: removed static target=_blank instaed value will fetched from the…
devbyharshit Oct 13, 2023
f12b192
Merge branch 'develop' into bug/4716_fix_target_blank_getting_sanitized
devbyharshit Oct 13, 2023
b232975
Merge branch 'develop' into bug/4716_fix_target_blank_getting_sanitized
nirname Oct 20, 2023
111e067
fix: added type Element to the node used in callback in the addhook f…
devbyharshit Oct 23, 2023
77e7008
Merge branch 'develop' into bug/4716_fix_target_blank_getting_sanitized
devbyharshit Oct 23, 2023
af73818
Merge branch 'bug/4716_fix_target_blank_getting_sanitized' of https:/…
devbyharshit Oct 23, 2023
3b8c48d
fix: added type Element to the node used in callback in the dompurify…
devbyharshit Oct 23, 2023
b36cdac
Merge branch 'develop' of https://github.com/mermaid-js/mermaid into …
devbyharshit Oct 23, 2023
3f486ac
Merge branch 'bug/4716_fix_target_blank_getting_sanitized' of https:/…
devbyharshit Oct 23, 2023
7960f94
fix: shifted dompurify.addhook functions inside removescript
devbyharshit Oct 23, 2023
06d2ba8
fix: added two unit tests to check for the secured anchor tag
devbyharshit Oct 25, 2023
54ab3fc
fix: added an e2e test case for classdiagram with anchor tag
devbyharshit Oct 26, 2023
3394541
Merge branch 'develop' into bug/4716_fix_target_blank_getting_sanitized
devbyharshit Oct 26, 2023
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
12 changes: 12 additions & 0 deletions cypress/integration/rendering/classDiagram.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -501,4 +501,16 @@ describe('Class diagram', () => {
B : -methods()
`);
});

it('should handle notes with anchor tag having target attribute', () => {
renderGraph(
`classDiagram
class test { }
note for test "<a href='https://mermaid.js.org/' target="_blank"><code>note about mermaid</code></a>"`
);

cy.get('svg').then((svg) => {
cy.get('a').should('have.attr', 'target', '_blank').should('have.attr', 'rel', 'noopener');
});
});
});
14 changes: 14 additions & 0 deletions packages/mermaid/src/diagrams/common/common.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ describe('when securityLevel is antiscript, all script must be removed', () => {
compareRemoveScript(`<img onerror="alert('hello');">`, `<img>`);
});

it('should detect unsecured target attribute, if value is _blank then generate a secured link', () => {
compareRemoveScript(
`<a href="https://mermaid.js.org/" target="_blank">note about mermaid</a>`,
`<a href="https://mermaid.js.org/" target="_blank" rel="noopener">note about mermaid</a>`
);
});

it('should detect unsecured target attribute from links', () => {
compareRemoveScript(
`<a href="https://mermaid.js.org/" target="_self">note about mermaid</a>`,
`<a href="https://mermaid.js.org/" target="_self">note about mermaid</a>`
);
});

it('should detect iframes', () => {
compareRemoveScript(
`<iframe src="http://abc.com/script1.js"></iframe>
Expand Down
22 changes: 21 additions & 1 deletion packages/mermaid/src/diagrams/common/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,27 @@
* @returns The safer text
*/
export const removeScript = (txt: string): string => {
return DOMPurify.sanitize(txt);
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
});

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);
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#L44-L45

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

return sanitizedText;
};

const sanitizeMore = (text: string, config: MermaidConfig) => {
Expand Down
Loading