Skip to content

Commit

Permalink
fix(plugins): use textContent to protect against xss
Browse files Browse the repository at this point in the history
  • Loading branch information
Fdawgs committed Apr 15, 2024
1 parent 510541e commit f3328af
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/plugins/pdf-to-html/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ async function plugin(server, options) {
* Overwrite content of remaining title element with temp file id,
* as Poppler reveals directory structure in title.
*/
titles[0].innerHTML = id;
titles[0].textContent = id;

/**
* `fixUtf8` function replaces most common incorrectly converted
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/rtf-to-html/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ async function plugin(server, options) {
meta.content = "text/html; charset=utf-8";
meta.httpEquiv = "content-type";
const title = dom.window.document.createElement("title");
title.innerHTML = id;
title.textContent = id;
dom.window.document.head.prepend(meta, title);

/**
Expand Down
12 changes: 7 additions & 5 deletions src/plugins/tidy-css/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async function plugin(server) {
// Create style element inside head if none already exist
if (styles.length === 0 && (newFonts || newBackgroundColor)) {
const element = dom.window.document.createElement("style");
element.innerHTML = "div {}";
element.textContent = "div {}";
dom.window.document.head.append(element);

styles = dom.window.document.querySelectorAll("style");
Expand All @@ -49,11 +49,11 @@ async function plugin(server) {
// Combine style elements into single element
const combinedStyle = dom.window.document.createElement("style");
styles.forEach((style) => {
combinedStyle.innerHTML += style.innerHTML;
combinedStyle.textContent += style.textContent || "";
style.remove();
});

const styleObj = cssomParse(combinedStyle.innerHTML);
const styleObj = cssomParse(combinedStyle.textContent || "");

styleObj.cssRules.forEach((rule) => {
if (rule instanceof CSSStyleRule) {
Expand Down Expand Up @@ -112,10 +112,12 @@ async function plugin(server) {
* Minifies output whilst also removing HTML comment tags
* wrapping CSS, and redundant semi-colons, generated by Poppler.
*/
combinedStyle.innerHTML = cssCleaner.minify(styleObj.toString()).styles;
combinedStyle.textContent = cssCleaner.minify(
styleObj.toString()
).styles;

// Stop empty <style> element being added
if (combinedStyle.innerHTML !== "") {
if (combinedStyle.textContent !== "") {
dom.window.document.head.append(combinedStyle);
}

Expand Down
10 changes: 5 additions & 5 deletions src/plugins/tidy-css/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ describe("Tidy-CSS plugin", () => {
// Check CSS is combined into one style tag
expect(dom.window.document.querySelectorAll("style")).toHaveLength(1);
// Check font-family is set to expected value
expect(style?.innerHTML).toMatch(expected?.fonts || /./u);
expect(style?.textContent).toMatch(expected?.fonts || /./u);
// Check background-color is set to expected value
expect(style?.innerHTML).toMatch(expected?.backgroundColor || /./u);
expect(style?.textContent).toMatch(expected?.backgroundColor || /./u);
// Check page-break-inside is set to avoid
expect(style?.innerHTML).toMatch(/page-break-inside:avoid/u);
expect(style?.textContent).toMatch(/page-break-inside:avoid/u);
// Check CSS is tidied and minified
expect(style?.innerHTML).not.toMatch(/;\}|<!--|--!?>|\n|\r/u);
expect(style?.textContent).not.toMatch(/;\}|<!--|--!?>|\n|\r/u);
expect(response.statusCode).toBe(200);
});

Expand Down Expand Up @@ -146,7 +146,7 @@ describe("Tidy-CSS plugin", () => {
// Check CSS is combined into one style tag
expect(dom.window.document.querySelectorAll("style")).toHaveLength(1);
// Check CSS is tidied and minified
expect(style?.innerHTML).not.toMatch(/;\}|<!--|--!?>|\n|\r/u);
expect(style?.textContent).not.toMatch(/;\}|<!--|--!?>|\n|\r/u);
expect(response.statusCode).toBe(200);
});

Expand Down
4 changes: 2 additions & 2 deletions src/plugins/tidy-html/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ async function plugin(server) {

styles.forEach((style) => {
const styleElement = style;
const styleObj = cssomParse(styleElement.innerHTML);
const styleObj = cssomParse(styleElement.textContent || "");
const cssRulesLength = styleObj.cssRules.length;

// Iterate over CSS rules in reverse to avoid index issues
Expand All @@ -122,7 +122,7 @@ async function plugin(server) {
}
}

styleElement.innerHTML = styleObj.toString();
styleElement.textContent = styleObj.toString();
});

// Remove all elements that match the selectors
Expand Down

0 comments on commit f3328af

Please sign in to comment.