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

chore: add escape to prevent XSS #833

Merged
merged 1 commit into from
Dec 23, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion web/src/components/SearchBar.tsx
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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