Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
dangerous-components
Advanced tools
Easy way to embed dangerous Inner HTML for React.
This is a spiritual sucessor of dangerous library, which was harder to use and much bigger in terms of library size.
React core team has made it especially hard to embed dangerous HTML in React.
And texts are encoded to prevent security issue such as XSS (Cross-site scripting).
So you are stuck with doing an arcane syntax, dangerouslySetInnerHTML.
function createMarkup() {
return {__html: 'First · Second'};
}
function MyComponent() {
return <div dangerouslySetInnerHTML={createMarkup()} />;
}
Check out the code snippet below (this is it).
import Dangerous from 'dangerous-components';
const title = `<h1>Some Title</h1>`;
const Header = ({title}) => <Dangerous html={title} />
const App = () => (
<>
<Header title={title} />
...
</>
)
It will render Header
accepting title
as a div
.
<div dangerouslySetInnerHTML={{__html: `<h1>Some Title</h1>`}} />
You can read the following code as "Dangerous HTML title".
<Dangerous html={title} />
If you need to render it as a head, such as h1
, then you can specify as
prop to do so.
<Dangerous html={title} as="h1" />
It reads like "render dangerous html title as h1".
If your site makes a heavy use of HTML from another data source (such as internal site or Gatsby blog sites, etc), you already might have an internal utility function to make it easy to render dangerous HTMl anyways.
Just use this tiny module without having to worry about module paths like import Dangerous from '../../../util'
If you can't trust your users' input.
There is a reason why it's intentionally hard to render insecure content in React.
Gatsby uses heavy use of dangerouslySetInnerHTML
everywhere.
If you trust your input, then use it there.
I've used it (dogfooding) for my static site as an example, which made the code much more readable.
e.g.) A logic to generate a blog post
function PostTemplate({ data }) {
const post = data.wordpressPost;
const {
link,
title,
fields: { content },
categories
} = post;
return (
<Layout>
<SEO
title={title}
canonicalURL={link}
description={title}
keywords={categories.map(_ => _.name)}
/>
- <h1 dangerouslySetInnerHTML={{ __html: title }} />
+ <Dangerous html={title} as="h1" />
<img src={post.jetpack_featured_media_url} alt="featured" />
<PostIcons node={post} css={{ marginBottom: rhythm(1 / 2) }} />
<OriginalPost canonicalURL={link} />
- <div dangerouslySetInnerHTML={{ __html: content }} />
+ <Dangerous html={content} />
</Layout>
);
}
You can see that the code is much more declarative and readable.
You can read it as "render dangerous html title as h1" and "render dangerous html content".
FAQs
Dangerously Set Inner HTML in React with ease
The npm package dangerous-components receives a total of 0 weekly downloads. As such, dangerous-components popularity was classified as not popular.
We found that dangerous-components demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.