fhir-react 2.1.1-beta.1 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fhir-react",
3
- "version": "2.1.1-beta.1",
3
+ "version": "2.1.1-beta.3",
4
4
  "description": "React component library for displaying FHIR Resources ",
5
5
  "main": "build/index.js",
6
6
  "peerDependencies": {
@@ -3,6 +3,15 @@ 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);
@@ -17,7 +26,7 @@ const Markdown = props => {
17
26
  'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
18
27
  'a', 'hr', 'table', 'thead', 'tbody', 'tr', 'th', 'td',
19
28
  ],
20
- ALLOWED_ATTR: ['href', 'title', 'target', 'rel', 'class', 'id'],
29
+ ALLOWED_ATTR: ['href', 'title', 'target', 'class', 'id'],
21
30
  });
22
31
  } catch {}
23
32
 
@@ -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 = '![tracker](https://evil.com/track.png)';
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
@@ -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
+ });