Skip to content

Commit

Permalink
fix: Sanitize HTML tooltips (#2538)
Browse files Browse the repository at this point in the history
  • Loading branch information
novakzaballa committed Aug 1, 2023
1 parent 717f175 commit f68ea54
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 38 deletions.
42 changes: 42 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"cors": "^2.8.3",
"cross-env": "5.2.0",
"css-loader": "4.3.0",
"dompurify": "^3.0.5",
"dotenv": "6.2.0",
"express": "4.17.3",
"express-handlebars": "6.0.5",
Expand Down Expand Up @@ -135,6 +136,7 @@
"@ffmpeg-installer/ffmpeg": "^1.1.0",
"@types/classnames": "^2.3.1",
"@types/color": "^3.0.3",
"@types/dompurify": "^3.0.2",
"@types/react-router": "^5.1.19",
"@types/react-router-dom": "^4.3.1",
"@types/react-select": "^2.0.3",
Expand Down
37 changes: 0 additions & 37 deletions frontend/web/components/Toolip.js

This file was deleted.

67 changes: 67 additions & 0 deletions frontend/web/components/Tooltip.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import React from 'react'
import { renderToStaticMarkup } from 'react-dom/server'

import * as DOMPurify from 'dompurify'
import Utils from 'common/utils/utils'

const ReactTooltip = require('react-tooltip')

type StyledTooltipProps = {
children: string
}

type TooltipProps = {
children: string
plainText: boolean
place?: string | undefined
title: JSX.Element // This is actually the Tooltip parent component
}

const StyledTooltip = ({ children }: StyledTooltipProps) => (
<div className='flex-row'>
<div className='icon--tooltip ion-ios-information-circle mr-1'></div>
<span>{`${children}`}</span>
</div>
)

const tooltipStyler = (plainText: boolean, children: string): string => {
const html = renderToStaticMarkup(
<StyledTooltip>{plainText ? children : '{{html}}'}</StyledTooltip>,
)
if (plainText) {
return html
}
return html.replace('{{html}}', DOMPurify.sanitize(children.toString()))
}

const Tooltip = ({
children,
place,
plainText,
title,
}: TooltipProps): JSX.Element => {
const id = Utils.GUID()

return (
<span className='question-tooltip'>
{title ? (
<span data-for={id} data-tip>
{title}
</span>
) : (
<span className='ion ion-ios-help' data-for={id} data-tip />
)}
<ReactTooltip
html
id={id}
place={place || 'top'}
type='dark'
effect='solid'
>
{tooltipStyler(plainText, children)}
</ReactTooltip>
</span>
)
}

export default Tooltip
1 change: 1 addition & 0 deletions frontend/web/components/tags/Tag.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const Tag: FC<TagType> = ({

return (
<Tooltip
plainText
title={
<div
onClick={() => onClick?.(tag as TTag)}
Expand Down
2 changes: 1 addition & 1 deletion frontend/web/project/project-components.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Input from 'components/base/forms/Input'
import InputGroup from 'components/base/forms/InputGroup'
import PanelSearch from 'components/PanelSearch'
import AccountStore from 'common/stores/account-store'
import Tooltip from 'components/Toolip'
import Tooltip from 'components/Tooltip'
import ProjectProvider from 'common/providers/ProjectProvider'
import AccountProvider from 'common/providers/AccountProvider'

Expand Down

3 comments on commit f68ea54

@vercel
Copy link

@vercel vercel bot commented on f68ea54 Aug 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on f68ea54 Aug 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

docs – ./docs

docs.bullet-train.io
docs-flagsmith.vercel.app
docs-git-main-flagsmith.vercel.app
docs.flagsmith.com

@vercel
Copy link

@vercel vercel bot commented on f68ea54 Aug 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.