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 1 commit
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
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.

18 changes: 17 additions & 1 deletion packages/code-du-travail-frontend/src/common/Html.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";
import styled from "styled-components";
import xss from "xss";
import xss, { getDefaultWhiteList } from "xss";

import { htmlParser } from "../lib";

Expand All @@ -9,6 +9,11 @@ type Props = {
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"];
maxgfr marked this conversation as resolved.
Show resolved Hide resolved

const Html = ({ children, inline = false, ...props }: Props): JSX.Element => {
Expand All @@ -25,6 +30,17 @@ const Html = ({ children, inline = false, ...props }: Props): JSX.Element => {
}
}
},
// Use for modeles-de-courrier
whiteList: {
...getDefaultWhiteList(),
...{
div: ["class"],
h3: ["class"],
li: ["class"],
p: ["class"],
span: ["class"],
},
},
}),
}}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,9 @@ exports[`<Answer /> should renders 1`] = `
<div
class=""
>
<p>
<p
class="test-content"
>
Contenu au format
<strong>
html
Expand Down Expand Up @@ -2193,7 +2195,9 @@ exports[`<Answer /> should renders a breadcrumbs 1`] = `
<div
class=""
>
<p>
<p
class="test-content"
>
Contenu au format
<strong>
html
Expand Down Expand Up @@ -3250,7 +3254,9 @@ exports[`<Answer /> should renders back to results link 1`] = `
<div
class=""
>
<p>
<p
class="test-content"
>
Contenu au format
<strong>
html
Expand Down Expand Up @@ -4420,7 +4426,9 @@ exports[`<Answer /> should renders related content 1`] = `
<div
class=""
>
<p>
<p
class="test-content"
>
annualisation du temps de travail. Annualisation de l'annualisation.
</p>
</div>
Expand Down Expand Up @@ -5632,7 +5640,9 @@ exports[`<Answer /> should renders tooltip 1`] = `
<div
class=""
>
<p>
<p
class="test-content"
>
Contenu au format
<strong>
html
Expand Down Expand Up @@ -6690,7 +6700,9 @@ exports[`<Answer /> should renders tooltip for words with diacritics without bre
<div
class=""
>
<p>
<p
class="test-content"
>
Contenu au format
<strong>
indemnités
Expand Down Expand Up @@ -7748,7 +7760,9 @@ exports[`<Answer /> should renders tooltip without breaking a tag 1`] = `
<div
class=""
>
<p>
<p
class="test-content"
>
<a
href=""
>
Expand Down Expand Up @@ -8806,7 +8820,9 @@ exports[`<Answer /> should renders tooltip without breaking previous word 1`] =
<div
class=""
>
<p>
<p
class="test-content"
>
annualisation du temps de travail. Annualisation de l'annualisation.
</p>
</div>
Expand Down
3 changes: 3 additions & 0 deletions packages/code-du-travail-frontend/src/lib/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import cheerio from "cheerio";
export const htmlParser = (html: string): string => {
const $ = cheerio.load(html, null, false);

// FIXME: Remove style from docx converter
$("style").remove();

$("dl").replaceWith(() => {
const src = $(this).find("source").attr("srcset");
return src
Expand Down