Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jlarky/react-json-script

Safe(r) way to set json content of script tag
https://github.com/jlarky/react-json-script

Last synced: about 1 month ago
JSON representation

Safe(r) way to set json content of script tag

Awesome Lists containing this project

README

        

# @jlarky/react-json-script

## Why

- https://pragmaticwebsecurity.com/articles/spasecurity/json-stringify-xss
- https://twitter.com/ariesrclark/status/1784765484497617203

This causes hydration error (see why `dangerouslySetInnerHTML` in https://blog.logrocket.com/improving-react-seo-structured-data/#adding-structured-data-react-app):

```tsx
const App = () => {
const data = {"id":1};

return {JSON.stringify(data)}
}
```

This causes XSS (see https://pragmaticwebsecurity.com/articles/spasecurity/json-stringify-xss):

```tsx
const App = () => {
const data = {"content":"alert('gotcha!')","id":1};

return
}
```

## Installation

```bash
bunx jsr add @jlarky/react-json-script
# or
npx jsr add @jlarky/react-json-script
```

## Example

```jsx
import { JsonScript } from "@jlarky/react-json-script";

const data = {
"@context": "https://schema.org",
"@type": "Organization",
"url": "http://www.example.com",
"name": "Unlimited Ball Bearings Corp.",
"contactPoint": {
"@type": "ContactPoint",
"telephone": "+1-401-555-1212",
"contactType": "Customer service"
}
};

const App = () => (

);
```

## Development

To install dependencies:

```bash
bun install
```

To publish:

```bash
bun run publish
```