fhir-react 2.1.0 → 2.1.1-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -1
- package/build/index.js +4 -4
- package/package.json +1 -1
- package/src/components/datatypes/Markdown/Markdown.js +19 -1
- package/src/components/datatypes/Markdown/Markdown.test.js +29 -0
- package/src/components/resources/QuestionnaireResponse/Group.js +1 -2
- package/src/components/resources/QuestionnaireResponse/Group.test.js +47 -0
package/package.json
CHANGED
|
@@ -3,13 +3,31 @@ import PropTypes from 'prop-types';
|
|
|
3
3
|
import marked from 'marked';
|
|
4
4
|
import DOMPurify from 'dompurify';
|
|
5
5
|
|
|
6
|
+
// Prevent reverse-tabnabbing: any link with target="_blank" gets
|
|
7
|
+
// rel="noopener noreferrer" enforced after sanitization. This runs at module
|
|
8
|
+
// level instead of in Markdown component so it isn't re-registered on every render
|
|
9
|
+
DOMPurify.addHook('afterSanitizeAttributes', node => {
|
|
10
|
+
if (node.tagName === 'A' && node.getAttribute('target') === '_blank') {
|
|
11
|
+
node.setAttribute('rel', 'noopener noreferrer');
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
|
|
6
15
|
const Markdown = props => {
|
|
7
16
|
const { fhirData } = props;
|
|
8
17
|
const markdown = String(fhirData);
|
|
9
18
|
let html = null;
|
|
10
19
|
try {
|
|
11
20
|
const unsafeHTML = marked(markdown, { gfm: true });
|
|
12
|
-
html = DOMPurify.sanitize(unsafeHTML
|
|
21
|
+
html = DOMPurify.sanitize(unsafeHTML, {
|
|
22
|
+
ALLOWED_TAGS: [
|
|
23
|
+
'p', 'br', 'strong', 'em', 'b', 'i', 'u', 's',
|
|
24
|
+
'code', 'pre', 'blockquote',
|
|
25
|
+
'ul', 'ol', 'li',
|
|
26
|
+
'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
|
|
27
|
+
'a', 'hr', 'table', 'thead', 'tbody', 'tr', 'th', 'td',
|
|
28
|
+
],
|
|
29
|
+
ALLOWED_ATTR: ['href', 'title', 'target', 'class', 'id'],
|
|
30
|
+
});
|
|
13
31
|
} catch {}
|
|
14
32
|
|
|
15
33
|
const dangerouslySetInnerHTML = html ? { __html: html } : null;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import { render } from '@testing-library/react';
|
|
3
|
+
import marked from 'marked';
|
|
3
4
|
import Markdown from './Markdown';
|
|
4
5
|
import dedent from 'dedent';
|
|
5
6
|
|
|
@@ -26,6 +27,34 @@ describe('should render Markdown correctly', () => {
|
|
|
26
27
|
expect(getByTestId('markdown').innerHTML.trim()).toEqual(expectedHTML);
|
|
27
28
|
});
|
|
28
29
|
|
|
30
|
+
it('adds rel="noopener noreferrer" to target="_blank" links', () => {
|
|
31
|
+
const markdown = '<a href="https://example.com" target="_blank">Example</a>';
|
|
32
|
+
const { getByTestId } = render(<Markdown fhirData={markdown} />);
|
|
33
|
+
const anchor = getByTestId('markdown').querySelector('a[target="_blank"]');
|
|
34
|
+
expect(anchor).not.toBeNull();
|
|
35
|
+
expect(anchor.getAttribute('rel')).toEqual('noopener noreferrer');
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('does not add rel to links without target="_blank"', () => {
|
|
39
|
+
const markdown = '[Example](https://example.com)';
|
|
40
|
+
const { getByTestId } = render(<Markdown fhirData={markdown} />);
|
|
41
|
+
const anchor = getByTestId('markdown').querySelector('a');
|
|
42
|
+
expect(anchor).not.toBeNull();
|
|
43
|
+
expect(anchor.getAttribute('rel')).toBeNull();
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('strips img tags from markdown input', () => {
|
|
47
|
+
const markdown = '';
|
|
48
|
+
|
|
49
|
+
// confirm marked produces an img tag before sanitization
|
|
50
|
+
const unsanitized = marked(markdown, { gfm: true });
|
|
51
|
+
expect(unsanitized).toContain('<img');
|
|
52
|
+
|
|
53
|
+
// confirm the rendered output has it stripped
|
|
54
|
+
const { getByTestId } = render(<Markdown fhirData={markdown} />);
|
|
55
|
+
expect(getByTestId('markdown').querySelector('img')).toBeNull();
|
|
56
|
+
});
|
|
57
|
+
|
|
29
58
|
it('escaping the HTML', () => {
|
|
30
59
|
const markdown = dedent`
|
|
31
60
|
# Header
|
|
@@ -13,8 +13,7 @@ const Group = ({ data, prepareItems, isChild = false }) => {
|
|
|
13
13
|
return data.map(prepareItems).map((item, i) => {
|
|
14
14
|
const title =
|
|
15
15
|
_get(item, 'title') ||
|
|
16
|
-
_get(item, 'text')
|
|
17
|
-
_get(item, '_linkId.fhir_comments.0');
|
|
16
|
+
_get(item, 'text');
|
|
18
17
|
|
|
19
18
|
return (
|
|
20
19
|
<ul
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { render } from '@testing-library/react';
|
|
3
|
+
import Group from './Group';
|
|
4
|
+
|
|
5
|
+
const prepareItems = item => ({ ...item, isGroup: false });
|
|
6
|
+
|
|
7
|
+
describe('Group', () => {
|
|
8
|
+
it('shows MissingValue when item has no title or text', () => {
|
|
9
|
+
const data = [{ linkId: 'test-id' }];
|
|
10
|
+
const { getByTestId } = render(
|
|
11
|
+
<Group data={data} prepareItems={prepareItems} />,
|
|
12
|
+
);
|
|
13
|
+
const item = getByTestId('linkId-test-id');
|
|
14
|
+
expect(item.querySelector('.fhir-ui__MissingValue')).not.toBeNull();
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('does not expose _linkId.fhir_comments as a label', () => {
|
|
18
|
+
const data = [
|
|
19
|
+
{
|
|
20
|
+
linkId: 'test-id',
|
|
21
|
+
_linkId: { fhir_comments: ['internal metadata'] },
|
|
22
|
+
},
|
|
23
|
+
];
|
|
24
|
+
const { getByTestId } = render(
|
|
25
|
+
<Group data={data} prepareItems={prepareItems} />,
|
|
26
|
+
);
|
|
27
|
+
const item = getByTestId('linkId-test-id');
|
|
28
|
+
expect(item.textContent).not.toContain('internal metadata');
|
|
29
|
+
expect(item.querySelector('.fhir-ui__MissingValue')).not.toBeNull();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('renders title when present', () => {
|
|
33
|
+
const data = [{ linkId: 'test-id', title: 'My Title' }];
|
|
34
|
+
const { getByTestId } = render(
|
|
35
|
+
<Group data={data} prepareItems={prepareItems} />,
|
|
36
|
+
);
|
|
37
|
+
expect(getByTestId('linkId-test-id').textContent).toContain('My Title');
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('renders text when title is absent', () => {
|
|
41
|
+
const data = [{ linkId: 'test-id', text: 'My Text' }];
|
|
42
|
+
const { getByTestId } = render(
|
|
43
|
+
<Group data={data} prepareItems={prepareItems} />,
|
|
44
|
+
);
|
|
45
|
+
expect(getByTestId('linkId-test-id').textContent).toContain('My Text');
|
|
46
|
+
});
|
|
47
|
+
});
|