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(frontend): security XSS / image tag #4030

Merged
merged 25 commits into from
Dec 10, 2021
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
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=""
Copy link
Contributor

Choose a reason for hiding this comment

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

c'est bizarre que ça rajoute une class vide

Copy link
Member Author

Choose a reason for hiding this comment

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

Oui, c'est vrai, bonne question

Copy link
Member Author

Choose a reason for hiding this comment

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

En vrai, c'est dû au parsing fait par le xss, qui essaye de retirer le plus de choses externes. Mais en soit, relou si ça génère des <div class="" .. dans la prod

Copy link
Member Author

Choose a reason for hiding this comment

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

Dans la prod, il me semble que ça ne génère pas cela, donc pour moi c'est ok j'ai l'impression

Copy link
Contributor

Choose a reason for hiding this comment

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

c'est juste qu'avant on l'ajoutait pas et maintenant oui

>
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