Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Sanitize HTML tooltips #2538

Merged
merged 7 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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