safe-mdx 1.3.2 → 1.3.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/README.md +14 -14
- package/dist/assets/HtmlToJsxConverter-Ds0bTjpw.js +24 -0
- package/dist/assets/_commonjsHelpers-CqkleIqs.js +1 -0
- package/dist/assets/index-B5fPOjPt.css +1 -0
- package/dist/assets/index-B7ATSoRE.js +9 -0
- package/dist/assets/index-BwZ2FTRd.js +146 -0
- package/dist/assets/index-R1UqLMGJ.js +1 -0
- package/dist/assets/index-c0qeY2gs.js +9 -0
- package/dist/assets/jsx-runtime-BhZZLbvw.js +9 -0
- package/dist/assets/jsx-runtime-NArryeSM.js +1 -0
- package/dist/assets/react-Ca6JzGpx.js +1 -0
- package/dist/assets/react-dom-BYRHYqYl.js +1 -0
- package/dist/html/attributes.d.ts +19 -0
- package/dist/html/attributes.d.ts.map +1 -0
- package/dist/html/attributes.js +289 -0
- package/dist/html/attributes.js.map +1 -0
- package/dist/html/convert-attributes.d.ts +6 -0
- package/dist/html/convert-attributes.d.ts.map +1 -0
- package/dist/html/convert-attributes.js +43 -0
- package/dist/html/convert-attributes.js.map +1 -0
- package/dist/html/domparser-browser.d.ts +4 -0
- package/dist/html/domparser-browser.d.ts.map +1 -0
- package/dist/html/domparser-browser.js +7 -0
- package/dist/html/domparser-browser.js.map +1 -0
- package/dist/html/domparser.d.ts +2 -0
- package/dist/html/domparser.d.ts.map +1 -0
- package/dist/html/domparser.js +5 -0
- package/dist/html/domparser.js.map +1 -0
- package/dist/html/html-to-mdx-ast.d.ts +23 -0
- package/dist/html/html-to-mdx-ast.d.ts.map +1 -0
- package/dist/html/html-to-mdx-ast.js +227 -0
- package/dist/html/html-to-mdx-ast.js.map +1 -0
- package/dist/html/html-to-mdx-ast.test.d.ts +2 -0
- package/dist/html/html-to-mdx-ast.test.d.ts.map +1 -0
- package/dist/html/html-to-mdx-ast.test.js +324 -0
- package/dist/html/html-to-mdx-ast.test.js.map +1 -0
- package/dist/html/remark-mdx-jsx-normalize.d.ts +10 -0
- package/dist/html/remark-mdx-jsx-normalize.d.ts.map +1 -0
- package/dist/html/remark-mdx-jsx-normalize.js +117 -0
- package/dist/html/remark-mdx-jsx-normalize.js.map +1 -0
- package/dist/html/valid-html-elements.d.ts +10 -0
- package/dist/html/valid-html-elements.d.ts.map +1 -0
- package/dist/html/valid-html-elements.js +50 -0
- package/dist/html/valid-html-elements.js.map +1 -0
- package/dist/index.html +19 -0
- package/dist/parse.d.ts +2 -0
- package/dist/parse.d.ts.map +1 -1
- package/dist/parse.js +2 -0
- package/dist/parse.js.map +1 -1
- package/dist/safe-mdx.d.ts +1 -1
- package/dist/safe-mdx.d.ts.map +1 -1
- package/dist/safe-mdx.js +23 -71
- package/dist/safe-mdx.js.map +1 -1
- package/dist/safe-mdx.test.js +161 -8
- package/dist/safe-mdx.test.js.map +1 -1
- package/package.json +27 -6
- package/src/html/README +17 -0
- package/src/html/attributes.ts +297 -0
- package/src/html/convert-attributes.ts +59 -0
- package/src/html/domparser-browser.ts +6 -0
- package/src/html/domparser.ts +5 -0
- package/src/html/html-to-mdx-ast.test.ts +365 -0
- package/src/html/html-to-mdx-ast.ts +304 -0
- package/src/html/remark-mdx-jsx-normalize.ts +128 -0
- package/src/html/valid-html-elements.ts +65 -0
- package/src/parse.ts +3 -0
- package/src/safe-mdx.test.tsx +178 -12
- package/src/safe-mdx.tsx +23 -79
- package/dist/HtmlToJsxConverter.d.ts +0 -10
- package/dist/HtmlToJsxConverter.d.ts.map +0 -1
- package/dist/HtmlToJsxConverter.js +0 -22
- package/dist/HtmlToJsxConverter.js.map +0 -1
- package/dist/plugins.d.ts +0 -12
- package/dist/plugins.d.ts.map +0 -1
- package/dist/plugins.js +0 -68
- package/dist/plugins.js.map +0 -1
- package/src/HtmlToJsxConverter.tsx +0 -37
package/README.md
CHANGED
|
@@ -13,21 +13,21 @@
|
|
|
13
13
|
- Render MDX without `eval` on the server, so you can render MDX in Cloudflare Workers and Vercel Edge
|
|
14
14
|
- Works with React Server Components
|
|
15
15
|
- Supports custom MDX components
|
|
16
|
-
-
|
|
17
|
-
-
|
|
16
|
+
- Custom `createElement`. Pass a no-op function to use safe-mdx as a validation step.
|
|
17
|
+
- Use `componentPropsSchema` to validate component props against a schema (works with Zod, Valibot, etc).
|
|
18
18
|
- ESM `https://` imports support with `allowClientEsmImports` option (disabled by default for security)
|
|
19
|
-
-
|
|
19
|
+
- Fast. 3ms to render the [full mdx document for Zod v3](https://github.com/colinhacks/zod/blob/0a49fa39348b7c72b19ddedc3b0f879bd395304b/packages/docs/content/packages/v3.mdx) (2500 lines)
|
|
20
20
|
|
|
21
21
|
## Why
|
|
22
22
|
|
|
23
|
-
The default MDX renderer uses `eval` (or `new Function(code)`) to render MDX components in the server. This is a security risk if the
|
|
23
|
+
The default MDX renderer uses `eval` (or `new Function(code)`) to render MDX components in the server. This is a security risk if the MDX code comes from untrusted sources and it's not allowed in some environments like Cloudflare Workers.
|
|
24
24
|
|
|
25
|
-
For example in
|
|
25
|
+
For example in a hypothetical platform similar to Notion, where users can write Markdown and publish it as a website, a user could be able to write MDX code that extracts secrets from the server in the SSR pass, using this library that is not possible. This is what happened with Mintlify platform in 2024.
|
|
26
26
|
|
|
27
27
|
Some use cases for this package are:
|
|
28
28
|
|
|
29
29
|
- Render MDX in Cloudflare Workers and Vercel Edge
|
|
30
|
-
- Safely render dynamically generated MDX code, like inside a ChatGPT
|
|
30
|
+
- Safely render dynamically generated MDX code, like inside a ChatGPT-style interface
|
|
31
31
|
- Render user generated MDX, like in a multi-tenant SaaS app
|
|
32
32
|
|
|
33
33
|
<br>
|
|
@@ -217,7 +217,7 @@ export function Page() {
|
|
|
217
217
|
|
|
218
218
|
## Reading the frontmatter
|
|
219
219
|
|
|
220
|
-
safe-mdx renderer ignores the frontmatter, to get its values you
|
|
220
|
+
safe-mdx renderer ignores the frontmatter, to get its values you will have to parse the MDX to mdast and read it there.
|
|
221
221
|
|
|
222
222
|
```tsx
|
|
223
223
|
import { SafeMdxRenderer } from 'safe-mdx'
|
|
@@ -253,9 +253,9 @@ export function Page() {
|
|
|
253
253
|
|
|
254
254
|
## Override code block component
|
|
255
255
|
|
|
256
|
-
It's not
|
|
256
|
+
It's not practical to override the code block component using `code` as a component override, because it will also be used for inline code blocks. It also does not have access to meta string and language.
|
|
257
257
|
|
|
258
|
-
Instead you can use `renderNode` to return some
|
|
258
|
+
Instead you can use `renderNode` to return some JSX for a specific mdast node:
|
|
259
259
|
|
|
260
260
|
```tsx
|
|
261
261
|
<SafeMdxRenderer
|
|
@@ -301,15 +301,15 @@ safe-mdx is designed to avoid server-side evaluation of untrusted MDX input.
|
|
|
301
301
|
|
|
302
302
|
However, it's important to note that safe-mdx does not provide protection against client-side vulnerabilities, such as Cross-Site Scripting (XSS) or script injection attacks. While safe-mdx itself does not perform any evaluation or rendering of user-provided content, the rendering library or components used in conjunction with safe-mdx may introduce security risks if not properly configured or sanitized.
|
|
303
303
|
|
|
304
|
-
This is
|
|
304
|
+
This is okay if you render your MDX in isolation from each tenant - for example on different subdomains - because an XSS attack cannot affect all tenants. But if instead you render the MDX from different tenants on the same domain, one tenant could steal cookies set from other customers.
|
|
305
305
|
|
|
306
306
|
## Limitations
|
|
307
307
|
|
|
308
308
|
These features are not supported yet:
|
|
309
309
|
|
|
310
|
-
-
|
|
311
|
-
-
|
|
312
|
-
- Exporting
|
|
310
|
+
- Expressions that use methods or functions, currently expressions are evaluated with [eval-estree-expression](https://github.com/jonschlinkert/eval-estree-expression) with the functions option disabled.
|
|
311
|
+
- Importing components or data from other files (unless `allowClientEsmImports` is enabled for `https://` imports).
|
|
312
|
+
- Exporting unresolved components or declaring components inline in the MDX
|
|
313
313
|
|
|
314
314
|
**Note**: JSX components in attributes are now supported! You can use React components inside attributes like `<Card icon={<Icon />}>` without relying on JavaScript evaluation.
|
|
315
315
|
|
|
@@ -317,4 +317,4 @@ To overcome the remaining limitations you can define custom logic in your compon
|
|
|
317
317
|
|
|
318
318
|
## Future Roadmap
|
|
319
319
|
|
|
320
|
-
-
|
|
320
|
+
- Add support for scope parameter to allow referencing variables in expressions and code
|