Skip to content

Commit

Permalink
fix(frontend): security XSS / image tag (#4030)
Browse files Browse the repository at this point in the history
Co-authored-by: Martial Maillot <[email protected]>
  • Loading branch information
maxgfr and m-maillot authored Dec 10, 2021
1 parent 3d6feec commit 28c122d
Show file tree
Hide file tree
Showing 15 changed files with 234 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2346,7 +2346,9 @@ exports[`<FicheMT /> should render 1`] = `
<div
class="c54 c55"
>
<div>
<div
class=""
>
lorem ipsum intro
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1384,7 +1384,9 @@ exports[`<Term /> should render 1`] = `
Définition
</th>
<td>
<div>
<div
class=""
>
<p>
Accord collectif de travail conclu au niveau d'une entreprise.
</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2435,7 +2435,9 @@ exports[`<ModelesDeCourrier /> should render 1`] = `
<div
class="c52 c53"
>
<div>
<div
class=""
>
un description
</div>
</div>
Expand Down Expand Up @@ -2508,7 +2510,9 @@ exports[`<ModelesDeCourrier /> should render 1`] = `
<div
class="c26"
>
<div>
<div
class=""
>
<p>
youhou
</p>
Expand Down
4 changes: 3 additions & 1 deletion packages/code-du-travail-frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@socialgouv/reference-article": "^1.0.1",
"@ungap/url-search-params": "^0.2.2",
"@zeit/next-source-maps": "^0.0.3",
"cheerio": "^1.0.0-rc.10",
"date-fns": "^2.22.1",
"debounce-promise": "^3.1.2",
"final-form": "^4.20.2",
Expand Down Expand Up @@ -73,7 +74,8 @@
"styled-components": "^5.3.0",
"typescript": "^4.3.5",
"unified": "^9.2.2",
"uuid": "^8.3.2"
"uuid": "^8.3.2",
"xss": "^1.0.10"
},
"devDependencies": {
"@babel/core": "^7.14.8",
Expand Down
1 change: 1 addition & 0 deletions packages/code-du-travail-frontend/pages/_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import "katex/dist/katex.min.css";
// eslint-disable-next-line import/no-extraneous-dependencies
import "react-image-lightbox/style.css";
import "../public/static/modeles.css";

import * as Sentry from "@sentry/nextjs";
import { GlobalStyles, ThemeProvider } from "@socialgouv/cdtn-ui";
Expand Down
8 changes: 8 additions & 0 deletions packages/code-du-travail-frontend/public/static/modeles.css

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

16 changes: 0 additions & 16 deletions packages/code-du-travail-frontend/src/common/Html.js

This file was deleted.

54 changes: 54 additions & 0 deletions packages/code-du-travail-frontend/src/common/Html.tsx
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;"};
`;
Original file line number Diff line number Diff line change
Expand Up @@ -877,11 +877,15 @@ exports[`<Answer /> should renders 1`] = `
<div
class="c20 c21"
>
<div>
<div
class=""
>
intro de l'article
</div>
</div>
<div>
<div
class=""
>
<p
class="test-content"
>
Expand Down Expand Up @@ -2185,11 +2189,15 @@ exports[`<Answer /> should renders a breadcrumbs 1`] = `
<div
class="c30 c31"
>
<div>
<div
class=""
>
intro de l'article
</div>
</div>
<div>
<div
class=""
>
<p
class="test-content"
>
Expand Down Expand Up @@ -3243,11 +3251,15 @@ exports[`<Answer /> should renders back to results link 1`] = `
<div
class="c20 c21"
>
<div>
<div
class=""
>
intro de l'article
</div>
</div>
<div>
<div
class=""
>
<p
class="test-content"
>
Expand Down Expand Up @@ -4414,11 +4426,15 @@ exports[`<Answer /> should renders related content 1`] = `
<div
class="c20 c21"
>
<div>
<div
class=""
>
intro de l'article
</div>
</div>
<div>
<div
class=""
>
<p
class="test-content"
>
Expand Down Expand Up @@ -5631,11 +5647,15 @@ exports[`<Answer /> should renders tooltip 1`] = `
<div
class="c20 c21"
>
<div>
<div
class=""
>
intro de l'article
</div>
</div>
<div>
<div
class=""
>
<p
class="test-content"
>
Expand Down Expand Up @@ -6690,11 +6710,15 @@ exports[`<Answer /> should renders tooltip for words with diacritics without bre
<div
class="c20 c21"
>
<div>
<div
class=""
>
intro de l'article
</div>
</div>
<div>
<div
class=""
>
<p
class="test-content"
>
Expand Down Expand Up @@ -7749,11 +7773,15 @@ exports[`<Answer /> should renders tooltip without breaking a tag 1`] = `
<div
class="c20 c21"
>
<div>
<div
class=""
>
intro de l'article
</div>
</div>
<div>
<div
class=""
>
<p
class="test-content"
>
Expand Down Expand Up @@ -8809,11 +8837,15 @@ exports[`<Answer /> should renders tooltip without breaking previous word 1`] =
<div
class="c20 c21"
>
<div>
<div
class=""
>
intro de l'article
</div>
</div>
<div>
<div
class=""
>
<p
class="test-content"
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

exports[`<Html /> should render 1`] = `
<div>
<div>
<div
class=""
>
<b>
Hello
</b>
Expand Down
Loading

0 comments on commit 28c122d

Please sign in to comment.