Skip to content

Commit

Permalink
setting html DANGEROUSLY
Browse files Browse the repository at this point in the history
  • Loading branch information
jennspencer committed May 9, 2024
1 parent 8ac2507 commit 3ca6ecc
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 113 deletions.
69 changes: 0 additions & 69 deletions components/HTMLBlock/index.jsx

This file was deleted.

36 changes: 36 additions & 0 deletions components/HTMLBlock/index.tsx
@@ -0,0 +1,36 @@
import React, { useEffect } from 'react';
import { renderToString, renderToStaticMarkup } from 'react-dom/server';

const MATCH_SCRIPT_TAGS = /<script\b[^>]*>([\s\S]*?)<\/script *>\n?/gim;

const extractScripts = (html: string = ''): [string, () => void] => {
const scripts: string[] = [];
let match: RegExpExecArray | null;
while ((match = MATCH_SCRIPT_TAGS.exec(html)) !== null) {
scripts.push(match[1]);
}
const cleaned = html.replace(MATCH_SCRIPT_TAGS, '');
return [cleaned, () => scripts.map(js => window.eval(js))];
};

const HTMLBlock = props => {
const { children, runScripts, safeMode = false } = props;
const html = renderToString(<>{children}</>);
const [cleanedHtml, exec] = extractScripts(html);

useEffect(() => {

Check failure on line 21 in components/HTMLBlock/index.tsx

View workflow job for this annotation

GitHub Actions / Test (lts/-1, 16)

__tests__/components/HTMLBlock.test.jsx

Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons: 1. You might have mismatching versions of React and the renderer (such as React DOM) 2. You might be breaking the Rules of Hooks 3. You might have more than one copy of React in the same app See https://fb.me/react-invalid-hook-call for tips about how to debug and fix this problem. ❯ resolveDispatcher node_modules/react/cjs/react.development.js:1465:13 ❯ Proxy.useEffect node_modules/react/cjs/react.development.js:1508:20 ❯ Module.HTMLBlock [as default] components/HTMLBlock/index.tsx:21:3 ❯ __tests__/components/HTMLBlock.test.jsx:10:19

Check failure on line 21 in components/HTMLBlock/index.tsx

View workflow job for this annotation

GitHub Actions / Test (lts/-1, 17)

__tests__/components/HTMLBlock.test.jsx

Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons: 1. You might have mismatching versions of React and the renderer (such as React DOM) 2. You might be breaking the Rules of Hooks 3. You might have more than one copy of React in the same app See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem. ❯ resolveDispatcher node_modules/react/cjs/react.development.js:1476:13 ❯ Proxy.useEffect node_modules/react/cjs/react.development.js:1519:20 ❯ Module.HTMLBlock [as default] components/HTMLBlock/index.tsx:21:3 ❯ __tests__/components/HTMLBlock.test.jsx:10:19

Check failure on line 21 in components/HTMLBlock/index.tsx

View workflow job for this annotation

GitHub Actions / Test (lts/-1, 18)

__tests__/components/HTMLBlock.test.jsx

TypeError: Cannot read properties of null (reading 'useEffect') ❯ Proxy.useEffect node_modules/react/cjs/react.development.js:1634:21 ❯ Module.HTMLBlock [as default] components/HTMLBlock/index.tsx:21:3 ❯ __tests__/components/HTMLBlock.test.jsx:10:19
if (typeof window !== 'undefined' && typeof runScripts === 'boolean' && runScripts) exec();
}, [runScripts, exec]);

if (safeMode) {
return (
<pre className="html-unsafe">
<code dangerouslySetInnerHTML={{ __html: renderToStaticMarkup(cleanedHtml) }} />
</pre>
);
}

return <div className="rdmd-html" dangerouslySetInnerHTML={{ __html: html }} />;
};

export default HTMLBlock;
18 changes: 2 additions & 16 deletions components/Image/index.tsx
@@ -1,32 +1,18 @@
import * as React from 'react';

interface Props extends React.PropsWithChildren<React.HTMLAttributes<HTMLImageElement>> {
align: string;
alt: string;
border: boolean;
caption: string;
className: string;
height: string;
lazy: boolean;
src: string;
title: string;
width: string;
}

const Image = (props: Props) => {
const Image = props => {
const [lightbox, setLightbox] = React.useState(false);

const {
align = '',
alt = '',
border = false,
caption,
className = '',
height = 'auto',
lazy,
src = '',
title = '',
width = 'auto',
lazyImages: lazy = true,
} = props;

if (className === 'emoji') {
Expand Down
23 changes: 4 additions & 19 deletions docs/sanitizing-tests.md
@@ -1,22 +1,7 @@

## Sanitizing `style` tags

<style>
* {
background-color: olive;
}
</style>


## Sanitizing `style` attributes

<p style="background-color: salmon">fish content</p>


## Sanitizing html blocks

[block:html]
{
"html": "<style>* { border: 3px solid magenta; }</style>"
}
[/block]
<HTMLBlock>
<h2>Header</h2>
<p>hello there</p>
</HTMLBlock>
1 change: 1 addition & 0 deletions enums.ts
Expand Up @@ -3,4 +3,5 @@ export enum NodeTypes {
emoji = 'emoji',
i = 'i',
image = 'image',
htmlBlock = 'html-block',
}
1 change: 1 addition & 0 deletions index.tsx
Expand Up @@ -48,6 +48,7 @@ const makeUseMDXComponents = (more: RunOpts['components']) => {
...Components,
Variable,
'code-tabs': Components.CodeTabs,
'html-block': Components.HTMLBlock,
img: Components.Image,
};

Expand Down
9 changes: 0 additions & 9 deletions processor/compile/html-block.js

This file was deleted.

8 changes: 8 additions & 0 deletions processor/compile/html-block.ts
@@ -0,0 +1,8 @@
import { HTMLBlock } from '../../types';

const htmlBlock = (node: HTMLBlock) => {
const html = node.data.hProperties.html;
return `<HTMLBlock>${JSON.stringify({ html }, null, 2)}</HTMLBlock>`;
}

export default htmlBlock;
2 changes: 2 additions & 0 deletions processor/compile/index.ts
@@ -1,13 +1,15 @@
import gemoji from './gemoji';
import codeTabs from './code-tabs';
import image from './image';
import htmlBlock from './html-block';
import { NodeTypes } from '../../enums';

const compilers = {
handlers: {
[NodeTypes.emoji]: gemoji,
[NodeTypes.codeTabs]: codeTabs,
[NodeTypes.image]: image,
[NodeTypes.htmlBlock]: htmlBlock,
},
};

Expand Down
10 changes: 10 additions & 0 deletions types.d.ts
Expand Up @@ -9,6 +9,16 @@ interface CodeTabs extends Parent {
};
}

interface HTMLBlock extends Parent {
type: NodeTypes.htmlBlock;
data: Data & {
hName: 'html-block';
hProperties: {
html: string;
};
};
}

interface Gemoji extends Literal {
type: NodeTypes.emoji;
name: string;
Expand Down

0 comments on commit 3ca6ecc

Please sign in to comment.