Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
chore: add escape to prevent XSS (#833)
  • Loading branch information
boojack committed Dec 23, 2022
1 parent c07b4a5 commit 65cc19c
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion web/src/components/SearchBar.tsx
Expand Up @@ -38,7 +38,7 @@ const SearchBar = () => {
useEffect(() => {
const text = locationStore.getState().query.text;
setQueryText(text === undefined ? "" : text);
}, [locationStore.getState().query.text]);
}, [locationStore.state.query.text]);

const handleMemoTypeItemClick = (type: MemoSpecType | undefined) => {
const { type: prevType } = locationStore.getState().query ?? {};
Expand Down
8 changes: 3 additions & 5 deletions web/src/labs/highlighter/index.ts
@@ -1,6 +1,4 @@
const escapeRegExp = (str: string): string => {
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
};
import { escape } from "lodash";

const walkthroughNodeWithKeyword = (node: HTMLElement, keyword: string) => {
if (node.nodeType === 3) {
Expand All @@ -19,8 +17,8 @@ export const highlightWithWord = (html: string, keyword?: string): string => {
if (!keyword) {
return html;
}
keyword = escapeRegExp(keyword);
keyword = escape(keyword);
const wrap = document.createElement("div");
wrap.innerHTML = html;
wrap.innerHTML = escape(html);
return walkthroughNodeWithKeyword(wrap, keyword);
};
3 changes: 2 additions & 1 deletion web/src/labs/marked/parser/Bold.ts
@@ -1,3 +1,4 @@
import { escape } from "lodash";
import { marked } from "..";
import Link from "./Link";

Expand All @@ -14,7 +15,7 @@ const renderer = (rawStr: string): string => {
return rawStr;
}

const parsedContent = marked(matchResult[1], [], [Link]);
const parsedContent = marked(escape(matchResult[1]), [], [Link]);
return `<strong>${parsedContent}</strong>`;
};

Expand Down
3 changes: 2 additions & 1 deletion web/src/labs/marked/parser/BoldEmphasis.ts
@@ -1,3 +1,4 @@
import { escape } from "lodash";
import { marked } from "..";
import Link from "./Link";

Expand All @@ -14,7 +15,7 @@ const renderer = (rawStr: string): string => {
return rawStr;
}

const parsedContent = marked(matchResult[1], [], [Link]);
const parsedContent = marked(escape(matchResult[1]), [], [Link]);
return `<strong><em>${parsedContent}</em></strong>`;
};

Expand Down
3 changes: 2 additions & 1 deletion web/src/labs/marked/parser/Emphasis.ts
@@ -1,3 +1,4 @@
import { escape } from "lodash";
import { marked } from "..";
import Link from "./Link";

Expand All @@ -14,7 +15,7 @@ const renderer = (rawStr: string): string => {
return rawStr;
}

const parsedContent = marked(matchResult[1], [], [Link]);
const parsedContent = marked(escape(matchResult[1]), [], [Link]);
return `<em>${parsedContent}</em>`;
};

Expand Down
2 changes: 1 addition & 1 deletion web/src/labs/marked/parser/Link.ts
Expand Up @@ -17,7 +17,7 @@ const renderer = (rawStr: string): string => {
if (!matchResult) {
return rawStr;
}
const parsedContent = marked(matchResult[1], [], [InlineCode, BoldEmphasis, Emphasis, Bold]);
const parsedContent = marked(escape(matchResult[1]), [], [InlineCode, BoldEmphasis, Emphasis, Bold]);
return `<a class='link' target='_blank' rel='noreferrer' href='${escape(matchResult[2])}'>${parsedContent}</a>`;
};

Expand Down

0 comments on commit 65cc19c

Please sign in to comment.