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 highlight xss bug #272

Merged
merged 2 commits into from
Jul 8, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 3 additions & 65 deletions web/src/components/search/SearchResult.vue
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ import { date } from "quasar";
import { useI18n } from "vue-i18n";

import searchService from "../../services/search";
import { getKeywords, highlightAndSpecialChars } from "../../utils/highlight";

export default defineComponent({
name: "ComponentSearchSearchList",
Expand Down Expand Up @@ -436,67 +437,6 @@ export default defineComponent({
searchTable.value.setPagination(pagination.value);
};

// eg.1: Gold => ['Gold']
// eg.2: City:Paris => ['Paris']
// eg.3: City:Paris Gold => ['Paris', 'Gold']
// eg.4: City:par* => ['par']
// eg.5: "Paris Gold" => ['Paris Gold']
const getKeywords = (queryString) => {
if (!queryString || queryString.trim().length == 0) {
return [];
}

let arr = [];
// queryString + " " is for special split regular
// split by space, but ignore double quotation marks
const groups = (queryString + " ").split(/ s*(?![^"]*"\ )/);
for (let i = 0; i < groups.length - 1; i++) {
const group = groups[i];
if (!group || group.trim().length == 0) {
continue;
}
// group + ":" is for special split regular
// split by :, but ignore "
const fieldWordArr = (group + ":").split(/:s*(?![^"]*"\:)/);
let keyword = group;
if (fieldWordArr.length > 2) {
keyword = fieldWordArr[1];
}
// delete start and end of * and "
keyword = keyword.replace(/(^\**)|(\**$)/g,"").replace(/(^"*)|("*$)/g,"");
if (keyword.trim().length > 0) {
// make sure key not empty or not space
arr.push(keyword);
}
}
return arr;
};

const highlightResultValue = (value, keywords) => {
if (!value) {
return value;
}

if (typeof value == "string") {
for (const idx in keywords) {
const keyword = keywords[idx];
const highlightText = "<span class='highlight'>" + keyword + "</span>";
value = value.replaceAll(keyword, highlightText);
}
} else if (Array.isArray(value)) {
for (let i = 0; i < value.length; i++) {
value[i] = highlightResultValue(value[i], keywords);
}
} else if (typeof value == "object") {
for (const key in value) {
value[key] = highlightResultValue(value[key], keywords);
}
} else {
// other type direct return value.
}
return value;
};

let lastIndexName = "";
const searchLoading = ref(false);
const searchData = (indexData, queryData) => {
Expand Down Expand Up @@ -530,10 +470,8 @@ export default defineComponent({
fields[keys[i]] = {};
}

if (keywords && keywords.length > 0) {
// highlight keyword
row._source = highlightResultValue(row._source, keywords);
}
// highlight keyword and htmlSpecialChars
row._source = highlightAndSpecialChars(row._source, keywords);
});
emit("updated:fields", Object.keys(fields));
}
Expand Down
64 changes: 64 additions & 0 deletions web/src/utils/highlight.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@

// eg.1: Gold => ['Gold']
// eg.2: City:Paris => ['Paris']
// eg.3: City:Paris Gold => ['Paris', 'Gold']
// eg.4: City:par* => ['par']
// eg.5: "Paris Gold" => ['Paris Gold']
import {htmlSpecialChars} from "./html";

export function getKeywords (queryString: string) {
if (!queryString || queryString.trim().length == 0) {
return [];
}

let arr = [];
// queryString + " " is for special split regular
// split by space, but ignore double quotation marks
const groups = (queryString + " ").split(/ s*(?![^"]*"\ )/);
for (let i = 0; i < groups.length - 1; i++) {
const group = groups[i];
if (!group || group.trim().length == 0) {
continue;
}
// group + ":" is for special split regular
// split by :, but ignore "
const fieldWordArr = (group + ":").split(/:s*(?![^"]*"\:)/);
let keyword = group;
if (fieldWordArr.length > 2) {
keyword = fieldWordArr[1];
}
// delete start and end of * and "
keyword = keyword.replace(/(^\**)|(\**$)/g,"").replace(/(^"*)|("*$)/g,"");
if (keyword.trim().length > 0) {
// make sure key not empty or not space
arr.push(keyword);
}
}
return arr;
};

export function highlightAndSpecialChars (value: any, keywords:[]) {
if (!value) {
return value;
}

if (typeof value == "string") {
value = htmlSpecialChars(value)
for (const idx in keywords) {
const keyword = htmlSpecialChars(keywords[idx]);
const highlightText = "<span class='highlight'>" + keyword + "</span>";
value = value.replaceAll(keyword, highlightText);
}
} else if (Array.isArray(value)) {
for (let i = 0; i < value.length; i++) {
value[i] = highlightAndSpecialChars(value[i], keywords);
}
} else if (typeof value == "object") {
for (const key in value) {
value[key] = highlightAndSpecialChars(value[key], keywords);
}
} else {
// other type direct return value.
}
return value;
};
42 changes: 42 additions & 0 deletions web/src/utils/html.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
export function htmlSpecialChars(str: string) {

if (!str || str.length == 0) {
return "";
}

let s = "";
for (let i = 0; i < str.length; i++) {
switch (str.substring(i, i + 1)) {
case "<":
s += "&lt;";
break;
case ">":
s += "&gt;";
break;
case "&":
s += "&amp;";
break;
case " ":
if (str.substring(i + 1, i + 1 + 1) == " ") {
s += " &nbsp;";
i++;
} else {
s += " ";
}
break;
case "\"":
s += "&quot;";
break;
case "'":
s += "&#39;";
break;
case "\n":
s += "<br>";
break;
default:
s += str.substring(i, i + 1);
break;
}
}
return s;
}