Skip to content

Commit

Permalink
fix(dompurify): allow target property and force the use of rel=noopener
Browse files Browse the repository at this point in the history
  • Loading branch information
tomperr committed Aug 20, 2023
1 parent 72fa348 commit 20ec99e
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 2 deletions.
9 changes: 9 additions & 0 deletions demos/classchart.html
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,15 @@ <h1>Class diagram demos</h1>
</pre>
<hr />

<pre class="mermaid">
classDiagram
class test {
+bar : foo
}
note for test "<a href='https://mermaid.js.org/' target='_blank'>mermaid</a>"
</pre>
<hr />

<script type="module">
import mermaid from './mermaid.esm.mjs';
mermaid.initialize({
Expand Down
2 changes: 1 addition & 1 deletion docs/config/setup/modules/defaultConfig.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#### Defined in

[defaultConfig.ts:268](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/defaultConfig.ts#L268)
[defaultConfig.ts:269](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/defaultConfig.ts#L269)

---

Expand Down
1 change: 1 addition & 0 deletions packages/mermaid/src/defaultConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ const config: RequiredDeep<MermaidConfig> = {
// TODO: can we make this default to `true` instead?
useMaxWidth: false,
},
dompurifyConfig: { ADD_ATTR: ['target'] },
};

const keyify = (obj: any, prefix = ''): string[] =>
Expand Down
10 changes: 9 additions & 1 deletion packages/mermaid/src/mermaidAPI.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ vi.mock('stylis', () => {
};
});
import { compile, serialize } from 'stylis';
import DOMPurify from 'dompurify';

/**
* @see https://vitest.dev/guide/mocking.html Mock part of a module
Expand Down Expand Up @@ -644,7 +645,14 @@ describe('mermaidAPI', () => {
it('allows dompurify config to be set', () => {
mermaidAPI.initialize({ dompurifyConfig: { ADD_ATTR: ['onclick'] } });

expect(mermaidAPI!.getConfig()!.dompurifyConfig!.ADD_ATTR).toEqual(['onclick']);
expect(mermaidAPI!.getConfig()!.dompurifyConfig!.ADD_ATTR).toEqual(['target', 'onclick']);
});
it('add `rel` property on link if `target` property is set', () => {
mermaidAPI.initialize();
const res = DOMPurify.sanitize('<a href="#" target="_blank">foo</a>', mermaidAPI.getConfig().dompurifyConfig!) as HTMLElement;

expect(res).toMatch(/target="_blank"/i);
expect(res).toMatch(/rel="noopener"/i);
});
});

Expand Down
6 changes: 6 additions & 0 deletions packages/mermaid/src/mermaidAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,12 @@ function initialize(options: MermaidConfig = {}) {
const config =
typeof options === 'object' ? configApi.setSiteConfig(options) : configApi.getSiteConfig();

DOMPurify.addHook('afterSanitizeAttributes', function (node) {
if ('target' in node) {
node.setAttribute('rel', 'noopener');
}
});

setLogLevel(config.logLevel);
addDiagrams();
}
Expand Down

0 comments on commit 20ec99e

Please sign in to comment.