-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(frontend): security XSS / image tag (#4030)
Co-authored-by: Martial Maillot <[email protected]>
- Loading branch information
Showing
15 changed files
with
234 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import React from "react"; | ||
import styled from "styled-components"; | ||
import xss, { escapeAttrValue } from "xss"; | ||
|
||
import { htmlParser } from "../lib/html"; | ||
|
||
type Props = { | ||
children: string; | ||
inline?: boolean; | ||
}; | ||
|
||
/** | ||
* List of tags that are allowed in the HTML | ||
* @type {string[]} | ||
* webcomponent-tooltip is used as an overlay for the definition of the words | ||
*/ | ||
const whiteListTags = ["webcomponent-tooltip", "webcomponent-tooltip-cc"]; | ||
|
||
/** | ||
* List of attributes that are allowed for the tags scan in the HTML | ||
* @type {string[]} | ||
* class is used for modeles-de-courrier | ||
*/ | ||
const whiteListAttr = ["class", "rel", "href", "target"]; | ||
|
||
const Html = ({ children, inline = false, ...props }: Props): JSX.Element => { | ||
return ( | ||
<Div | ||
{...props} | ||
isInline={inline} | ||
dangerouslySetInnerHTML={{ | ||
__html: xss(htmlParser(children), { | ||
onIgnoreTag: function (tag, html, _options) { | ||
if (whiteListTags.some((whiteTag) => whiteTag === tag)) { | ||
return html; | ||
} | ||
}, | ||
onTagAttr: function (_tag, name, value, _isWhiteAttr) { | ||
if (whiteListAttr.some((whiteAttr) => whiteAttr === name)) { | ||
return name + '="' + escapeAttrValue(value) + '"'; | ||
} | ||
}, | ||
}), | ||
}} | ||
/> | ||
); | ||
}; | ||
|
||
export default Html; | ||
|
||
const Div = styled.div` | ||
${({ isInline }: { isInline: boolean }) => | ||
isInline && "display: inline-block;"}; | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,9 @@ | |
|
||
exports[`<Html /> should render 1`] = ` | ||
<div> | ||
<div> | ||
<div | ||
class="" | ||
> | ||
<b> | ||
Hello | ||
</b> | ||
|
Oops, something went wrong.